我正在尝试使用Python 2.7.13和OpenCV 3.3.0构建一个人脸识别程序.
但是,当我试图检测到我的脸时,我收到此错误:
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
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
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): …Run Code Online (Sandbox Code Playgroud)