daily pastebin goal
62%
SHARE
TWEET

Untitled

a guest Feb 21st, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.
  6. cascadePath = "haarcascade_frontalface_default.xml"
  7. faceCascade = cv2.CascadeClassifier(cascadePath)
  8.  
  9. # For face recognition we will the the LBPH Face Recognizer
  10. recognizer = cv2.createLBPHFaceRecognizer()
  11.  
  12. def get_images_and_labels(path):
  13.     # Append all the absolute image paths in a list image_paths
  14.     # We will not read the image with the .sad extension in the training set
  15.     # Rather, we will use them to test our accuracy of the training
  16.     image_paths = [os.path.join(path, f) for f in os.listdir(path) if not f.endswith('.sad')]
  17.     # images will contains face images
  18.     images = []
  19.     # labels will contains the label that is assigned to the image
  20.     labels = []
  21.     for image_path in image_paths:
  22.         # Read the image and convert to grayscale
  23.         image_pil = Image.open(image_path).convert('L')
  24.         # Convert the image format into numpy array
  25.         image = np.array(image_pil, 'uint8')
  26.         # Get the label of the image
  27.         nbr = int(os.path.split(image_path)[1].split(".")[0].replace("subject", ""))
  28.         # Detect the face in the image
  29.         faces = faceCascade.detectMultiScale(image)
  30.         # If face is detected, append the face to images and the label to labels
  31.         for (x, y, w, h) in faces:
  32.             images.append(image[y: y + h, x: x + w])
  33.             labels.append(nbr)
  34.             cv2.imshow("Adding faces to traning set...", image[y: y + h, x: x + w])
  35.             cv2.waitKey(50)
  36.     # return the images list and labels list
  37.     return images, labels
  38.  
  39. # Path to the Yale Dataset
  40. path = './yalefaces'
  41. # Call the get_images_and_labels function and get the face images and the
  42. # corresponding labels
  43. images, labels = get_images_and_labels(path)
  44. cv2.destroyAllWindows()
  45.  
  46. # Perform the tranining
  47. recognizer.train(images, np.array(labels))
  48.  
  49. # Append the images with the extension .sad into image_paths
  50. image_paths = [os.path.join(path, f) for f in os.listdir(path) if f.endswith('.sad')]
  51. for image_path in image_paths:
  52.     predict_image_pil = Image.open(image_path).convert('L')
  53.     predict_image = np.array(predict_image_pil, 'uint8')
  54.     faces = faceCascade.detectMultiScale(predict_image)
  55.     for (x, y, w, h) in faces:
  56.         nbr_predicted, conf = recognizer.predict(predict_image[y: y + h, x: x + w])
  57.         nbr_actual = int(os.path.split(image_path)[1].split(".")[0].replace("subject", ""))
  58.         if nbr_actual == nbr_predicted:
  59.             print "{} is Correctly Recognized with confidence {}".format(nbr_actual, conf)
  60.         else:
  61.             print "{} is Incorrect Recognized as {}".format(nbr_actual, nbr_predicted)
  62.         cv2.imshow("Recognizing Face", predict_image[y: y + h, x: x + w])
  63.         cv2.waitKey(1000)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top