我正在编写一个面部识别程序,当我尝试训练我的识别器时,我不断收到这个错误
TypeError: Expected cv::UMat for argument 'labels'
我的代码是
def detect_face(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5);
if (len(faces)==0):
return None, None
(x, y, w, h) = faces[0]
return gray[y:y+w, x:x+h], faces[0]
def prepare_training_data():
faces = []
labels = []
for img in photo_name_list: #a collection of file locations as strings
image = cv2.imread(img)
face, rect = detect_face(image)
if face is not None:
faces.append(face)
labels.append("me")
return faces, labels
def test_photos():
face_recognizer = cv2.face.LBPHFaceRecognizer_create()
faces, labels = prepare_training_data()
face_recognizer.train(faces, np.ndarray(labels))
标签是从 prepare_training_data 返回的图像列表中每张照片的标签列表,我将其转换为 numpy 数组,因为我读到这是 train() 需要的。