1

the code attached below

import cv2
import numpy as np

recognizer = cv2.face
recognizer.loadTrainingData('trainer/example.yml')
#recognizer.read()
cascadePath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascadePath);


cam = cv2.VideoCapture(1)
font = cv2.FONT_HERSHEY_SIMPLEX
while True:
    ret, im =cam.read()
    gray=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
    faces=faceCascade.detectMultiScale(gray, 1.3,5)
    for(x,y,w,h) in faces:
        cv2.rectangle(im,(x,y),(x+w,y+h),(225,0,0),2)
        Id, conf = recognizer.predict(gray[y:y+h,x:x+w])
        cv2.PutText(cv2.cv.fromarray(im),str(Id), (x,y+h),font, 255)
    cv2.imshow('face',im) 
    if cv2.waitKey(10) == ord('q'):
        break
cam.release()
cv2.destroyAllWindows()

I find out it is impossible to write code like below in this opencv version

recognizer = cv2.createLBPHFaceRecognizer() 
recognizer.load('trainer/example.yml')

The goal is to load the data collected before in example.yml. However I'm not sure how to do that and the attached code has few incomplete parameters.

Ryan Liao
  • 11
  • 1

1 Answers1

0

To create the recognizer object:

recognizer = cv2.face.LBPHFaceRecognizer_create()

To load the data do:

recognizer.read(filename)
Mehul Jain
  • 468
  • 4
  • 12
  • i did the same when creating the data, it is not for loading the data. – Ryan Liao Apr 03 '18 at 17:56
  • I think you can do recognizer.read(filename) to read the model. See here: https://docs.opencv.org/trunk/dd/d65/classcv_1_1face_1_1FaceRecognizer.html – Mehul Jain Apr 03 '18 at 18:01