特征脸训练图像像素大小错误

时间:2017-12-22 06:56:21

标签: python opencv

您好,所有高级程序员!我在特征脸图像训练部分有错误。

错误是:OpenCV错误:不支持的格式或格式组合(在Eigenfaces方法中,所有输入样本(训练图像)的大小必须相同!预期为27889像素,但为27556像素。)在cv :: face中: :Eigenfaces :: train,文件C:\ projects \ opencv-python \ opencv_contrib \ modules \ face \ src \ eigen_faces.cpp,第68行

这意味着我的照片尺寸不同。当我从相机捕捉图片时,我尝试cv2.rezise(),但它仍然无法正常工作。

这是我的捕获代码:

import cv2
cam = cv2.VideoCapture(0)
detector = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

Id = input('enter your id: ')
sampleNum = 0

while(True):
    ret, img = cam.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = detector.detectMultiScale(gray, 1.3, 5)

    for (x,y,w,h) in faces:
        cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)

        sampleNum = sampleNum+1

        cv2.imwrite("dataSet/user."+Id+'.'+str(sampleNum)+".jpg",cv2.resize
        (gray[y:y+h,x:x+w],(70,70)))

        cv2.imshow('frame',img)

    if cv2.waitKey(100) & 0xFF == ord('q'):#waitKey is for delay in video capture
        break
    elif sampleNum >= 50:#how many picture capture?
        break

cam.release()
cv2.destroyAllWindows()

这是培训部分:

import cv2,os
import numpy as np


recognizer = cv2.face.EigenFaceRecognizer_create()
detector= cv2.CascadeClassifier("haarcascade_frontalface_default.xml")


def getImagesAndLabels(path):

    imagePaths=[os.path.join(path,f) for f in os.listdir(path)]
    faceSamples=[]
    Ids=[]

    for imagePath in imagePaths:

        pilImage = Image.open(imagePath).convert('L')

        imageNp = np.array(pilImage,'uint8')

        Id = int(os.path.split(imagePath)[-1].split(".")[1])

        faces = detector.detectMultiScale(imageNp)

        for (x,y,w,h) in faces:
            faceSamples.append(imageNp[y:y+h,x:x+w])
            Ids.append(Id)

     return faceSamples,Ids

faces,Ids = getImagesAndLabels('dataSet')
recognizer.train(faces, np.array(Ids))
recognizer.write('trainner/trainnerEi.yml')

PS。我从LBPHFaceRecognizer调整了这段代码 谢谢!* 3

1 个答案:

答案 0 :(得分:1)

  • 好的,所以EigenFaces只有在像素空间中所有图像的尺寸相同时才有效
  • 这意味着如果培训中使用的一张图片大小为28x28,那么培训和测试中的每一张图片都必须大小为28x28
  • 如果图像大小不同,那么opencv会抛出该错误
  • 错误只是说其中一张图像在像素空间中的尺寸为27889,而其他图像的尺寸为27556像素空间。
  • 我建议您使用cv2.resize()功能制作相同尺寸的所有图像
  • 使用以下代码作为培训部分的参考:

    import cv2,os
    import numpy as np
    
    
    recognizer = cv2.face.EigenFaceRecognizer_create()
    detector= cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
    
    
    def getImagesAndLabels(path):
        width_d, height_d = 280, 280  # Declare your own width and height
        imagePaths=[os.path.join(path,f) for f in os.listdir(path)]
        faceSamples=[]
        Ids=[]
    
        for imagePath in imagePaths:
    
            pilImage = Image.open(imagePath).convert('L')
    
            imageNp = np.array(pilImage,'uint8')
    
            Id = int(os.path.split(imagePath)[-1].split(".")[1])
    
            faces = detector.detectMultiScale(imageNp)
    
            for (x,y,w,h) in faces:
                ########################################                       
                # The line to be changed by cv2.resize()
                ########################################
                faceSamples.append(cv2.resize(imageNp[y:y+h,x:x+w], (width_d, height_d))
                Ids.append(Id)
    
        return faceSamples,Ids
    
    faces,Ids = getImagesAndLabels('dataSet')
    recognizer.train(faces, np.array(Ids))
    recognizer.write('trainner/trainnerEi.yml')
    
  • 请记住测试图像必须大小相同

相关问题