Reputation: 31
I am trying to build a face recognition program using Python 2.7.13 and OpenCV 3.3.0.
However, I receive this error when it tries to detect my face:
File "C:\GitProject\face_recognition\detector.py", line 20, in
<module> Id = recognizer.predict(gray[y:y+h,x:x+w])
error: C:\projects\opencv-python\opencv_contrib\modules\face\src\lbph_faces.cpp:396:
error: (-5) This LBPH model is not computed yet. Did you call the train method? in function cv::face::LBPH::predict
Here is my code:
import cv2 ,os
import numpy as np
from PIL import Image
import pickle
recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read('trainer/training_data.yml')
cascadePath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascadePath);
cam = cv2.VideoCapture(0)
font = cv2.FONT_HERSHEY_SIMPLEX
while True:
ret, im =cam.read()
gray=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
faces=faceCascade.detectMultiScale(gray, 1.2,5)
for(x,y,w,h) in faces:
cv2.rectangle(im,(x,y),(x+w,y+h),(225,0,0),2)
Id = recognizer.predict(gray[y:y+h,x:x+w])
if(conf<50):
if(Id==1):
Id="Anirban"
elif(Id==2):
Id="Sam"
else:
Id="Unknown"
cv2.PutText(cv2.fromarray(im),str(Id), (x,y+h),font, 255)
cv2.imshow('im',im)
if cv2.waitKey(10) & 0xFF==ord('q'):
break
cam.release()
cv2.destroyAllWindows()
Upvotes: 3
Views: 8970
Reputation: 1
After all, if the error still there, try to manually specify the full path of recognizer.write().
Example:
recognizer.write('/home/pi/Desktop/.../trainer/trainer.yml')
This worked for me.
Upvotes: 0
Reputation: 47
The issue is caused when loading the pre-trained .yml file. Even though you do not receive an error when loading the file, the recognizer does not properly register the the .yml file.
By changing how the training_data.yml file is generated is the key. In your training code change the recognizer.save() to recognizer.write().
I had this issue when trying to load the training data on my Raspberry Pi.
If any clarity is needed make sure to ask.
Ids , faces = getImageList(path)
recognizer.train(faces,np.array(Ids))
recognizer.save('model.yml')
recognizer.write('model.yml')
Both methods generated the model.yml file as expected but
recognizer.read('model.yml')
only worked with write().
Upvotes: 1
Reputation: 1995
Welcome to Stack Overflow. The error you received is telling you that you need to call the train method in the parent class of LBPHFaceRecognizer
before you ask it to do any recognition. Refer to the documentation here.
Upvotes: 0
Reputation: 884
Open your trainer, change the recognizer.save to recognizer.write. Don't forget to run the trainer again. It works for me.
Upvotes: 10