-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_classifier.py
40 lines (27 loc) · 960 Bytes
/
create_classifier.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 numpy as np
from PIL import Image
import os, cv2
# Method to train custom classifier to recognize face
def train_classifer(name):
# Read all the images in custom data-set
path = os.path.join(os.getcwd()+"/data/"+name+"/")
faces = []
ids = []
labels = []
pictures = {}
# Store images in a numpy format and ids of the user on the same index in imageNp and id lists
for root,dirs,files in os.walk(path):
pictures = files
for pic in pictures :
imgpath = path+pic
img = Image.open(imgpath).convert('L')
imageNp = np.array(img, 'uint8')
id = int(pic.split(name)[0])
#names[name].append(id)
faces.append(imageNp)
ids.append(id)
ids = np.array(ids)
#Train and save classifier
clf =cv2.face.LBPHFaceRecognizer.create()
clf.train(faces, ids)
clf.write("./data/classifiers/"+name+"_classifier.xml")