Hello to all senior programmer! I have an error on eigenfaces image training part.
The error is : OpenCV Error: Unsupported format or combination of formats (In the Eigenfaces method all input samples (training images) must be of equal size! Expected 27889 pixels, but was 27556 pixels.) in cv::face::Eigenfaces::train, file C:\\projects\\opencv-python\\opencv_contrib\\modules\\face\\src\\eigen_faces.cpp, line 68
Which mean my pictures don't be in equal size. I try cv2.rezise() when I capture picture from camera but it still doesn't work.
here is my capture code :
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()
and here is training part:
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. I adapt this code from LBPHFaceRecognizer Thank you!*3
cv2.resize()
function to make all images of the same sizeUse the below code as a reference for you training part:
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')
Keep in mind even the test images has to be of same size
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.