简体   繁体   中英

EigenFace in opencv using python

when i try this code

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

this error appear

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

Whould you mind explaining your question ?

I guess you would like to understand the meaning of this error. It means you need to have the same images size for training and test images.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM