【发布时间】:2018-06-04 20:22:10
【问题描述】:
各位资深程序员大家好!我在特征脸图像训练部分有一个错误。
错误是: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
【问题讨论】:
-
为什么要在
getImagesAndLabels中再次检测检测到的已调整大小的人脸图像? -
事实是我不知道。我只是从 youtube 上得到这个用于 LBPHFaceRecognizer 的代码。请帮助我前辈。