Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import os
- import numpy as np
- import matplotlib.pyplot as plt
- from sklearn.model_selection import train_test_split
- from sklearn.datasets import fetch_lfw_people
- from sklearn.metrics import classification_report
- from sklearn.decomposition import PCA
- from sklearn.neural_network import MLPClassifier
- subjects = ["0", "1", "2"]
- # Prepare training data
- data_folder_path = 'C:/Users/MUICT/Desktop/jupyter/face/'
- dirs = os.listdir(data_folder_path)
- faces = []
- labels = []
- face_cascade = cv2.CascadeClassifier('D:/opencv/sources/data/hogcascades/haarcascade_frontalface_default.xml')
- for dir_name in dirs:
- subject_dir_path = data_folder_path + dir_name
- print(subject_dir_path)
- subject_images_names = os.listdir(subject_dir_path)
- for image_name in subject_images_names:
- image_path = subject_dir_path + "/" + image_name
- image = cv2.imread(image_path)
- gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
- facesL = face_cascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5);
- if (len(facesL) > 0):
- (x, y, w, h) = facesL[0]
- faces.append(cv2.resize(gray[y:y+w, x:x+h],(70,70)))
- labels.append(int(dir_name))
- print("Total faces: ", len(faces))
- print("Total labels: ", len(labels))
- #recognizer = cv2.face.EigenFaceRecognizer_create()
- recognizer = cv2.face.LBPHFaceRecognizer_create()
- #print(labels)
- recognizer.train(faces, np.array(labels))
- # Predict
- test = cv2.imread('C:/Users/MUICT/Desktop/jupyter/test.jpg')
- testG = cv2.cvtColor(test, cv2.COLOR_BGR2GRAY)
- testF = face_cascade.detectMultiScale(testG, scaleFactor=1.2, minNeighbors=5);
- (x, y, w, h) = testF[0]
- testFF = cv2.resize(testG[y:y+w, x:x+h],(70,70))
- label, confidence = recognizer.predict(testFF)
- print(label)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement