0

Code runs but instead of posting my name during the detector.py it says unknown. Anyone have any solutions?

import cv2
import numpy as np


recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read('trainer/trainer.yml')
cascadePath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascadePath);
font = cv2.FONT_HERSHEY_SIMPLEX
cam = cv2.VideoCapture(0)


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-20,y-20), (x+w+20,y+h+20), (0,255,0), 4)
   Id = recognizer.predict(gray[y:y+h,x:x+w])
    if(Id == 1):
        Id = "Alec"
    elif(Id == 2):
         Id = "Chase"
    else:
        Id = "Unknown"
    cv2.rectangle(im, (x-22,y-90), (x+w+22, y-22), (0,255,0), -1)
    cv2.putText(im, str(Id), (x,y-40), font, 2, (255,255,255), 3)
if cv2.waitKey(10) & 0xFF == ord('q'):
    break


cam.release()
cv2.destroyAllWindows()

It is not showing any types of error. I have commented out the If(id==X) code just to see what it would print on the screen. The program printed (1, 30-40). So I'm guessing the 1 is my ID. I have the DataSet and trainer program if I need to provide it.

2 Answers 2

1

recognizer.predict returns both the Id and confidence score.

    Id, conf = recognizer.predict(gray[y:y+h,x:x+w])
    if(conf<50):
        if(Id==1):
            Id="asciime"
        elif(Id==2):
            Id="Something"
    else:
        Id="Unknown"

OpenCV's Python API documentation is very poor. I often use the C++ reference. In this case the predict method is

void cv::face::FaceRecognizer::predict(InputArray src, int& label, double& confidence)  const

See https://docs.opencv.org/3.2.0/dd/d65/classcv_1_1face_1_1FaceRecognizer.html#ab0d593e53ebd9a0f350c989fcac7f251 .

Sign up to request clarification or add additional context in comments.

3 Comments

Can you link to some documentation so the OP has a resource to understand the outputs of the predict() method?
I add link and some explaination in the answer
Great! Nice answer.
0

Confidence is standard set to 50. However the Id has 2 values in it. the int(ID) and double(Conf). https://docs.opencv.org/3.0.0/dd/d65/classcv_1_1face_1_1FaceRecognizer.html#aede3fa2ec7a4ee35e67bc998df23883b getting the first value with Id[1] would work

for(x,y,w,h) in faces:
  cv2.rectangle(im, (x-20,y-20), (x+w+20,y+h+20), (0,255,0), 4)
  Id = recognizer.predict(gray[y:y+h,x:x+w])
    if(Id[1] == 1):
       Id = "Alec"
    elif(Id[1] == 2):
       Id = "Chase"
    else:
       Id = "Unknown"
  cv2.rectangle(im, (x-22,y-90), (x+w+22, y-22), (0,255,0), -1)
  cv2.putText(im, str(Id), (x,y-40), font, 2, (255,255,255), 3)
if cv2.waitKey(10) & 0xFF == ord('q'):
   break

or

for(x,y,w,h) in faces:
   cv2.rectangle(im, (x-20,y-20), (x+w+20,y+h+20), (0,255,0), 4)
   Id,conf = recognizer.predict(gray[y:y+h,x:x+w])
     if(Id == 1):
        Id = "Alec"
     elif(Id == 2):
        Id = "Chase"
     else:
        Id = "Unknown"
  cv2.rectangle(im, (x-22,y-90), (x+w+22, y-22), (0,255,0), -1)
  cv2.putText(im, str(Id), (x,y-40), font, 2, (255,255,255), 3)
if cv2.waitKey(10) & 0xFF == ord('q'):
    break

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.