-
Notifications
You must be signed in to change notification settings - Fork 0
/
face_trainer.py
29 lines (29 loc) · 968 Bytes
/
face_trainer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import os
import numpy as np
import cv2
from PIL import Image
# For face recognition we will the the LBPH Face Recognizer
recognizer = cv2.face.LBPHFaceRecognizer_create();
path="facesData"
def getImagesWithID(path):
imagePaths = [os.path.join(path, f) for f in os.listdir(path)]
# print image_path
#getImagesWithID(path)
faces = []
IDs = []
for imagePath in imagePaths:
# Read the image and convert to grayscale
facesImg = Image.open(imagePath).convert('L')
faceNP = np.array(facesImg, 'uint8')
# Get the label of the image
ID= int(os.path.split(imagePath)[-1].split(".")[1])
# Detect the face in the image
faces.append(faceNP)
IDs.append(ID)
cv2.imshow("Adding faces for traning",faceNP)
cv2.waitKey(10)
return np.array(IDs), faces
Ids,faces = getImagesWithID(path)
recognizer.train(faces,Ids)
recognizer.write("trainingdata.yml")
cv2.destroyAllWindows()