我使用以下代码进行使用OpenCV python的面部识别。虽然我已经调整了图像大小,但仍然会出现错误,因为图像应该调整大小。怎么解决?
import cv2, os
import numpy as np
recognizer = cv2.createFisherFaceRecognizer() # Eigenface Recognizer
def get_images_and_labels(path):
image_paths = [os.path.join(path, f) for f in os.listdir(path)]
images = []
labels = []
for image_path in image_paths:
image_pil = cv2.imread(image_path,0)
image = np.array(image_pil, 'uint8')
indx = os.path.split(image_path)[1].split(".")[0]
images.append(image)
labels.append(int(indx))
cv2.imshow("Adding faces to traning set...", image_pil)
cv2.waitKey(50)
return images, labels
path ='G:\Resizing\Fisher Faces\data'
images, labels = get_images_and_labels(path)
cv2.destroyAllWindows()
recognizer.train(images, np.array(labels))
print "The system has trained successfully."
cascPath = "G:\Resizing\lBPH\haarcascades\haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascPath)
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=10,
minSize=(30, 30),
flags = cv2.cv.CV_HAAR_SCALE_IMAGE
)
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.imshow('frame',frame)
for (x, y, w, h) in faces:
resized = cv2.resize(gray[y: y + h, x: x + w], (200,200), interpolation = cv2.INTER_AREA)
img=cv2.equalizeHist(resized)
nbr_predicted, conf = recognizer.predict(img)
print "Index {} is Correctly Recognized with confidence {}".format(nbr_predicted, conf)
cap.release()
cv2.destroyAllWindows()
错误是
"OpenCV Error: Bad argument (Wrong input image size. Reason: Training and Test images must be of equal size! Expected an image with 1228800 elements, but got 40000.)
in cv::Fisherfaces::predict, file ..\..\..\..\opencv\modules\contrib\src\facerec.cpp, line 623
Traceback (most recent call last):
File "G:/Resizing/Fisher Faces/FisherFaces.py", line 79, in <module>
nbr_predicted, conf = recognizer.predict(img)
cv2.error: ..\..\..\..\opencv\modules\contrib\src\facerec.cpp:623: error: (-5) Wrong input image size. Reason: Training and Test images must be of equal size! Expected an image with 1228800 elements, but got 40000. in function cv::Fisherfaces::predict"