In [2]:
import cv2, os
import numpy as np
from PIL import Image

# For face detection we will use the Haar Cascade provided by OpenCV.
cascadePath = "/home/mckc/Downloads/face_recognizer/haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascadePath)

# For face recognition we will the the LBPH Face Recognizer 
recognizer = cv2.createLBPHFaceRecognizer()


---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-2-afb127574956> in <module>()
----> 1 import cv2, os
      2 import numpy as np
      3 from PIL import Image
      4 
      5 # For face detection we will use the Haar Cascade provided by OpenCV.

ImportError: No module named cv2

In [3]:
# Path to the Yale Dataset
path = '/home/mckc/Downloads/face_recognizer/yalefaces'
# Call the get_images_and_labels function and get the face images and the 
# corresponding labels
images, labels = get_images_and_labels(path)
cv2.destroyAllWindows()

# Perform the tranining
recognizer.train(images, np.array(labels))

# Append the images with the extension .sad into image_paths
image_paths = [os.path.join(path, f) for f in os.listdir(path) if f.endswith('.sad')]
for image_path in image_paths:
    predict_image_pil = Image.open(image_path).convert('L')
    predict_image = np.array(predict_image_pil, 'uint8')
    faces = faceCascade.detectMultiScale(predict_image)
    for (x, y, w, h) in faces:
        nbr_predicted, conf = recognizer.predict(predict_image[y: y + h, x: x + w])
        nbr_actual = int(os.path.split(image_path)[1].split(".")[0].replace("subject", ""))
        if nbr_actual == nbr_predicted:
            print "{} is Correctly Recognized with confidence {}".format(nbr_actual, conf)
        else:
            print "{} is Incorrect Recognized as {}".format(nbr_actual, nbr_predicted)
        cv2.imshow("Recognizing Face", predict_image[y: y + h, x: x + w])
        cv2.waitKey(1000)


12 is Correctly Recognized with confidence 30.7063923459
5 is Correctly Recognized with confidence 34.3565508048
2 is Correctly Recognized with confidence 29.15139655
8 is Correctly Recognized with confidence 99.8276423452
4 is Correctly Recognized with confidence 0.0
14 is Correctly Recognized with confidence 25.5569113665
15 is Correctly Recognized with confidence 25.2105434172
13 is Correctly Recognized with confidence 32.6527994538
1 is Correctly Recognized with confidence 37.6819645242
11 is Correctly Recognized with confidence 39.0980390296
6 is Correctly Recognized with confidence 25.8154211953
9 is Correctly Recognized with confidence 46.2072675938
10 is Correctly Recognized with confidence 19.4849737279
7 is Correctly Recognized with confidence 44.3297146637
3 is Correctly Recognized with confidence 32.3938790423

In [ ]: