python cv2.face缺乏参数

时间:2018-04-03 04:19:25

标签: python opencv3.0 face-recognition opencv3.1 opencv-contrib

下面的代码

IndexOutOfBoundsException

我发现在这个opencv版本中编写如下代码是不可能的

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()

目标是加载之前在example.yml中收集的数据。但是,我不知道如何做到这一点,附加的代码几乎没有完整的参数。

1 个答案:

答案 0 :(得分:0)

创建识别器对象:

recognizer = cv2.face.LBPHFaceRecognizer_create()

要加载数据,请执行以下操作:

recognizer.read(filename)