在编写人脸识别的时候(代码如下):
import cv2
import numpy as np
cascPath = "C:\opencv\sources\data\haarcascades\haarcascade_frontalface_alt2.xml"
faceCascade = cv2.CascadeClassifier(cascPath)
video_capture = cv2.VideoCapture(0)
while True:
ret, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=10, #Adjust accuracy
minSize=(50, 50),
flags=cv2.CASCADE_SCALE_IMAGE
)
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 255, 255), 2)
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
if cv2.waitKey(1) & 0xFF == ord('s'):
cv2.imwrite('test1.png', frame)
img = cv2.imread("test1.png")
face = img[x:x+w,y:y+h]
cv2.imshow('Face', face)
cv2.imwrite("face_detected.png", face)
images=[]
images.append(cv2.imread("klp.jpg",cv2.IMREAD_GRAYSCALE))
images.append(cv2.imread("wh.jpg",cv2.IMREAD_GRAYSCALE))
images.append(cv2.imread("wk.jpg",cv2.IMREAD_GRAYSCALE))
Labels=[0,1,2]
recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.train(images, np.array(Labels))
predict_image = cv2.imread("face_detected.png",cv2.IMREAD_GRAYSCALE)
label,confidence= recognizer.predict(predict_image)
print("Label=", label)
print("Confidence=", confidence)
video_capture.release()
cv2.destroyAllWindows()
出现了如下问题:
Exception has occurred: error
OpenCV(4.1.0) C:\projects\opencv-python\opencv\modules\core\src\matrix.cpp:235: error: (-215:Assertion failed) s >= 0 in function 'cv::setSize'
File "F:\learn_python\Models\DetectFace&Recognize.py", line 41, in <module>
recognizer.train(images, np.array(Labels))