SHARE
TWEET

Untitled

a guest Feb 28th, 2020 99 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import cv2
  2. import os
  3. import numpy as np
  4.  
  5. import matplotlib.pyplot as plt
  6.  
  7. from sklearn.model_selection import train_test_split
  8. from sklearn.datasets import fetch_lfw_people
  9. from sklearn.metrics import classification_report
  10. from sklearn.decomposition import PCA
  11. from sklearn.neural_network import MLPClassifier
  12.  
  13. subjects = ["0", "1", "2"]
  14.  
  15. # Prepare training data
  16. data_folder_path = 'C:/Users/MUICT/Desktop/jupyter/face/'
  17. dirs = os.listdir(data_folder_path)
  18.  
  19. faces = []
  20. labels = []
  21.  
  22. face_cascade = cv2.CascadeClassifier('D:/opencv/sources/data/hogcascades/haarcascade_frontalface_default.xml')
  23.  
  24. for dir_name in dirs:
  25.     subject_dir_path = data_folder_path + dir_name
  26.     print(subject_dir_path)
  27.     subject_images_names = os.listdir(subject_dir_path)
  28.    
  29.     for image_name in subject_images_names:
  30.         image_path = subject_dir_path + "/" + image_name
  31.         image = cv2.imread(image_path)
  32.         gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  33.         facesL = face_cascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5);
  34.         if (len(facesL) > 0):
  35.             (x, y, w, h) = facesL[0]
  36.             faces.append(cv2.resize(gray[y:y+w, x:x+h],(70,70)))
  37.             labels.append(int(dir_name))
  38.  
  39. print("Total faces: ", len(faces))
  40. print("Total labels: ", len(labels))
  41.  
  42. #recognizer = cv2.face.EigenFaceRecognizer_create()
  43. recognizer = cv2.face.LBPHFaceRecognizer_create()
  44.  
  45. #print(labels)
  46. recognizer.train(faces, np.array(labels))
  47.  
  48. # Predict
  49. test = cv2.imread('C:/Users/MUICT/Desktop/jupyter/test.jpg')
  50. testG = cv2.cvtColor(test, cv2.COLOR_BGR2GRAY)
  51. testF = face_cascade.detectMultiScale(testG, scaleFactor=1.2, minNeighbors=5);
  52. (x, y, w, h) = testF[0]
  53. testFF = cv2.resize(testG[y:y+w, x:x+h],(70,70))
  54. label, confidence = recognizer.predict(testFF)
  55. print(label)
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