Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import cv2
- import os
- def face_detection(img):
- gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
- face_cascade_Haar=cv2.CascadeClassifier('C:\Users\nic 005\Downloads\opencv\sources\data\haarcascades\haarcascade_frontalface_default.xml')
- faces = face_cascade_Haar.detectMultiScale(gray,scaleFactor=1.3,minNeighbors=5)
- if(len(faces)==0):
- return None, None
- (x,y,w,h)=faces[0]
- return gray[y:y+w, x:x+h], faces[0]
- def prep_training_data(path):
- dirs=os.listdir(path)
- faces=[]
- labels=[]
- k=0
- for dir_names in dirs:
- label=k+1
- subject_dir_path= path + "/" + dir_names
- subject_images_names= os.listdir(subject_dir_path)
- for image_name in subject_images_names:
- if image_name.startswith("."):
- continue
- image=cv2.imread(subject_dir_path+ "/" + image_name)
- face, rect = face_detection(image)
- faces.append(face)
- labels.append(label)
- k=k+1
- return faces,labels
- def draw_rectangle(img, rect):
- (x, y, w, h) = rect
- cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
- def draw_text(img, text, x, y):
- cv2.putText(img, text, (x, y), cv2.FONT_HERSHEY_PLAIN, 1.5, (0, 255, 0), 2)
- def predict(test_img):
- img=test_img.copy()
- face, rect=face_detection(img)
- label=face_recognizer.predict(face)
- label_name=subjects[labels[0]]
- draw_rectangle(test_img,rect)
- draw_text(test_img, label_name, rect[0], rect[1]-5)
- return test_img
- faces, labels = prep_training_data('C:\Users\nic 005\Desktop\face_recognition\db\training')
- face_recognizer= cv2.face.LBPHFaceRecognizer_create()
- face_recognizer.train(faces, np.array(labels))
- test_img=cv2.imread('C:\Users\nic 005\Desktop\face_recognition\testing\anonym.20.jpg')
- predicted_img=predict(test_img)
- cv2.imshow("img",predicted_img)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
Add Comment
Please, Sign In to add comment