使用python

时间:2017-03-30 11:12:09

标签: python opencv

当我尝试这段代码时

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

出现此错误

  

错误:( - 5)输入图像尺寸错误。原因:训练和测试图像必须大小相同!预期图像有77760个元素,但得到21316.在函数cv :: face :: Eigenfaces :: predict

1 个答案:

答案 0 :(得分:0)

你介意解释一下你的问题吗?

我想你想了解这个错误的含义。这意味着您需要具有相同的图像大小才能进行训练和测试图像。