lundi 27 juin 2016

Face recognition using Fisher Faces OpenCV Python

I used the following code for face face recognition using OpenCV python. Although I had resized the images I still get an error as images should be resized. How to solve it ?

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:ResizingFisher Facesdata'

 images, labels = get_images_and_labels(path)
 cv2.destroyAllWindows()

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

print "The system has trained successfully."




cascPath = "G:ResizinglBPHhaarcascadeshaarcascade_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()

The error is

"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 ........opencvmodulescontribsrcfacerec.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: ........opencvmodulescontribsrcfacerec.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"

Aucun commentaire:

Enregistrer un commentaire