-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrainer.py
39 lines (33 loc) · 1.24 KB
/
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
30
31
32
33
34
35
36
37
38
39
import cv2,os
import numpy as np
from PIL import Image
recognizer = cv2.createLBPHFaceRecognizer()
#detector= cv2.CascadeClassifier("haarcascade_frontalface_default.xml");
path='dataset'
def getImagesWithID(path):
#get the path of all the files in the folder
imagePaths=[os.path.join(path,f) for f in os.listdir(path)]
#create empth face list
faces=[]
#create empty ID list
IDs=[]
#now looping through all the image paths and loading the Ids and the images
for imagePath in imagePaths:
#loading the image and converting it to gray scale
faceImg=Image.open(imagePath).convert('L')
#Now we are converting the PIL image into numpy array
faceNp=np.array(faceImg,'uint8')
#getting the Id from the image
ID=int(os.path.split(imagePath)[-1].split(".")[1])
# extract the face from the training image sample
faces.append(faceNp)
print ID
#If a face is there then append that in the list as well as Id of it
IDs.append(ID)
cv2.imshow("training",faceNp)
cv2.waitKey(10)
return np.array(IDs),faces,
Ids,faces = getImagesWithID('dataset')
recognizer.train(faces, Ids)
recognizer.save('recognizer/trainingData.yml')
cv2.destroyAllWindows()