3

I very new to python. Im using face_recognizer = cv2.face.LBPHFaceRecognizer_create() and defined my predict function as

#this function recognizes the person in image passed
#and draws a rectangle around detected face with name of the 
#subject
def predict(test_img):
#make a copy of the image as we don't want to chang original image
img = test_img.copy()
#detect face from the image
face, rect = detect_face(img)

#predict the image using our face recognizer 
label= face_recognizer.predict(face)
#get name of respective label returned by face recognizer
label_text = subjects[label]

#draw a rectangle around face detected
draw_rectangle(img, rect)
#draw name of predicted person
draw_text(img, label_text, rect[0], rect[1]-5)

return img`

and i get the following error while predicting the face using predict function

---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-13-d6517b4e38bd> in <module>()
  6 
  7 #perform a prediction
----> 8 predicted_img1 = predict(test_img1)
  9 #predicted_img2 = predict(test_img2)
 10 print("Prediction complete")

<ipython-input-12-b46266ecb9d5> in predict(test_img)
  9 
 10     #predict the image using our face recognizer
---> 11     label= face_recognizer.predict(face)
 12     #get name of respective label returned by face recognizer
 13     label_text = subjects[label]

error: C:\projects\opencv-python\opencv\modules\core\src\matrix.cpp:310: 
error: (-215) s >= 0 in function cv::setSize

Many thanks in advance

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
SRK
  • 53
  • 5
  • 1
    Is it possible that `detect_face()` is giving you some indication (e.g., `face is None`) that it didn't find a face, and that you're proceeding with an invalid `rect`? – Dave W. Smith Dec 17 '17 at 19:48
  • @DaveW.Smith, I didnt get any error that you are mentioning. The one mentioned above is the only error I get. – SRK Dec 18 '17 at 08:41
  • Sometimes the underlying OpenCV C++ code coughs up some strange errors if you pass it invalid input. What does `print(repr(face))` done right before the call to `face_recognizer.predict()` show? – Dave W. Smith Dec 19 '17 at 02:41
  • @DaveW.Smith, I used the codes from [git repo](https://github.com/informramiz/opencv-face-recognition-python) and only changed `face_recognizer = cv2.face.LBPHFaceRecognizer_create()` – SRK Dec 19 '17 at 15:50
  • 1
    Take a closer look at the `detect_face()` method. Note that it will return `None, None` if it doesn't find faces. The example method you copied that calls that method doesn't check for `None`. I suspect that's where the problem originates. – Dave W. Smith Dec 19 '17 at 18:26

1 Answers1

0

I was testing the same github project and encountered the same error when adding new training images and testing image. The problem goes away when using one of the images from the training data as test image. So the problem would be there's nothing in place to deal with when no match is found as @DaveW.Smith suggests.

Harry
  • 1,147
  • 13
  • 13