使用python实现opencv中的特征脸

2024-04-19 13:10:48 发布

您现在位置:Python中文网/ 问答频道 /正文

当我尝试这个代码时

import cv2, os
import numpy as np
from PIL import Image
#from trainner import *
path="dataset2"
cascadePath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascadePath)
recognizer =cv2.face.createEigenFaceRecognizer()

#recognizer = cv2.face.createLBPHFaceRecognizer()
threshold = 105
recognizer.load("recognizer/trainningData.yml")
id=0


image_paths = [os.path.join(path, f) for f in os.listdir(path)]
for image_path in image_paths:

    predict_image_pil = Image.open(image_path).convert('L')
    predict_image = np.array(predict_image_pil, 'uint8')



    faces = faceCascade.detectMultiScale(predict_image)
    for (x, y, w, h) in faces:
        resized_image = cv2.resize(predict_image[y:y+h,x:x+w], (200, 200)) 

        cv2.rectangle(predict_image,(x,y),(x+w,y+h),(0,255,0),2)
        id,conf = recognizer.predict(predict_image[y:y+h,x:x+w])


        print(id)
        print (conf)
        if(id==1):
            id="john"
        elif(id==2):
            id="brad"
        elif(id==3):
            id="scr"
        elif(id==4):
            id="natalie portman"
        elif(id==5):
            id="jennifer lawrence"
        elif(id==6):
            id="van diesel"
        elif(id==7):
            id="jennifer aniston"
        elif(id==8):
            id="leonardo dicaprio"    
        else :
            id="unknown"
        print (id )   
        cv2.imshow("Recognizing Face", predict_image[y: y + h, x: x + w])
        cv2.waitKey(1000)
cv2.destroyAllWindows()

出现此错误

error: (-5) Wrong input image size. Reason: Training and Test images must be of equal size! Expected an image with 77760 elements, but got 21316. in function cv::face::Eigenfaces::predict


Tags: pathinfromimageimportidforos
1条回答
网友
1楼 · 发布于 2024-04-19 13:10:48

你能解释一下你的问题吗?在

我想你应该明白这个错误的含义。这意味着您需要有相同的图像大小用于训练和测试图像。在

相关问题 更多 >