使用python进行facerecognition错误

时间:2018-06-11 09:25:47

标签: python

我使用以下代码在使用OpenCV python的fisherfaces中进行人脸识别。虽然我已经调整了图像大小,但仍然会出现错误,因为图像应该调整大小。怎么解决?

错误:recognizer.train(faces,np.array(labels)) 错误:( - 210)在Fisherfaces方法中,所有输入样本(训练图像)必须大小相同!预计7056像素,但是6400像素。在功能训练中

这是我的代码

import cv2
import os
import numpy as np

subjects = ["", "Shiva", "lavanya"]

def detect_face(img):

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

face_cascade = cv2.CascadeClassifier('/home/veena/Downloads/opencv-3.2.0/data/haarcascades/haarcascade_frontalface_default.xml')



faces = face_cascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5);



if (len(faces) == 0):
    return None, None


(x, y, w, h) = faces[0]

return gray[y:y+w, x:x+h], faces[0]

def prepare_training_data(data_folder_path):


dirs = os.listdir(data_folder_path)


faces = []

labels = []


for dir_name in dirs:


    if not dir_name.startswith("s"):
        continue;


    label = int(dir_name.replace("s", ""))


    subject_dir_path = data_folder_path + "/" + dir_name


    subject_images_names = os.listdir(subject_dir_path)


    for image_name in subject_images_names:

        if image_name.startswith("."):
            continue;


        image_path = subject_dir_path + "/" + image_name


        image = cv2.imread(image_path)


        cv2.imshow("Training on image...", cv2.resize(image, (400, 500)))
        cv2.waitKey(100)


        face, rect = detect_face(image)


        if face is not None:


            faces.append(face)

            labels.append(label)

cv2.destroyAllWindows()
cv2.waitKey(1)
cv2.destroyAllWindows()

return faces, labels



  print("Preparing data...")
  faces, labels = prepare_training_data("training-data")
  print("Data prepared")

  print("Total faces: ", len(faces))
  print("Total labels: ", len(labels))

  recognizer = cv2.createFisherFaceRecognizer()

  recognizer.train(faces, np.array(labels))


  def draw_rectangle(img, rect):
  (x, y, w, h) = rect
  cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)

  def draw_text(img, text, x, y):
  cv2.putText(img, text, (x, y), cv2.FONT_HERSHEY_PLAIN, 1.5, (0, 255, 0), 2)

  def predict(test_img):

  img = test_img.copy()

  face, rect = detect_face(img)


  label, confidence = recognizer.predict(face)

label_text = subjects[label]


draw_rectangle(img, rect)

draw_text(img, label_text, rect[0], rect[1]-5)

return img

 print("Predicting images...")

 #test_img0 = cv2.imread("test-data/test0.jpg")
 test_img1 = cv2.imread("test-data/test1.jpg")
 test_img2 = cv2.imread("test-data/test2.jpg")

 #predicted_img0 = predict(test_img0)
 predicted_img1 = predict(test_img1)
 predicted_img2 = predict(test_img2)
 print("Prediction complete")

 #cv2.imshow(subjects[0], cv2.resize(predicted_img0, (400, 500)))
 cv2.imshow(subjects[1], cv2.resize(predicted_img1, (400, 500)))
 cv2.imshow(subjects[2], cv2.resize(predicted_img2, (400, 500)))
 cv2.waitKey(0)
 cv2.destroyAllWindows()
 cv2.waitKey(1)
 cv2.destroyAllWindows()

0 个答案:

没有答案