-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtraining_dataset.py
36 lines (32 loc) · 1.39 KB
/
training_dataset.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
import os,cv2
import numpy as np
from PIL import Image
recognizer = cv2.face.LBPHFaceRecognizer_create()
detector = cv2.CascadeClassifier('cascades/haarcascade_frontalface_default.xml')
def getImagesAndLabels(path):
#get the path of all the files in the folder
imagePaths = [os.path.join(path,f) for f in os.listdir(path)]
#create empty face list
faceSamples = []
#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
pilImage=Image.open(imagePath).convert('L')
#Now we are converting the PIL image into numpy array
imageNp=np.array(pilImage,'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=detector.detectMultiScale(imageNp)
#If a face is there then append that in the list as well as Id of it
for (x,y,w,h) in faces:
faceSamples.append(imageNp[y:y+h,x:x+w])
Ids.append(Id)
return faceSamples,Ids
faces,Ids = getImagesAndLabels('dataSet/')
s = recognizer.train(faces, np.array(Ids))
recognizer.write('trainer/trainer.yml')
if os.path.exists(os.getcwd()+'/trainer/trainer.yml'):
os.system("python message_gui.py")