簡體   English   中英

使用Python在OpenCV中使用EigenFace

[英]EigenFace in opencv using 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()

出現此錯誤

錯誤:(-5)輸入圖像尺寸錯誤。 原因:訓練和測試圖像必須大小相等! 期望具有77760個元素的圖像,但在函數cv :: face :: Eigenfaces :: predict中得到21316。

您介意解釋您的問題嗎?

我想您想了解此錯誤的含義。 這意味着您需要具有相同的圖像大小來進行訓練和測試圖像。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM