-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
105 lines (82 loc) · 3.16 KB
/
train.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import os
from os.path import join, isdir
from PIL import Image
import cv2
import numpy as np
import detect
import config
def getLabels(a_dir):
return [name for name in os.listdir(a_dir) if isdir(join(a_dir, name))]
def supportedImg(name):
return name.lower().endswith('.png') or name.lower().endswith('.jpg')
def combineFaces(faces, w=100, h=100, numPerRow=5):
small_img = []
row_img = []
count = 0
for img in faces:
small_img.append(cv2.resize(img, (w, h)))
count += 1
if count % numPerRow == 0:
count = 0
row_img.append(np.concatenate(small_img, axis=1))
small_img = []
if len(small_img) > 0:
for x in range (0, numPerRow-len(small_img)):
small_img.append(np.zeros((h,w), np.uint8))
row_img.append(np.concatenate(small_img, axis=1))
return np.concatenate(row_img, axis=0)
def extractFaces(a_dir, folder, levelFace=False):
faceCascade = cv2.CascadeClassifier('cascades/haarcascade_frontalface_default.xml')
eyeCascade = cv2.CascadeClassifier('cascades/haarcascade_eye.xml')
the_path = join(a_dir, folder)
result = []
for img in [f for f in os.listdir(the_path) if supportedImg(f)]:
img_path = join(the_path, img)
image, faces = detect.detectFaces(cv2.imread(img_path), faceCascade, eyeCascade, True)
if len(faces) == 0:
print("No face found in " + img_path)
for ((x, y, w, h), eyedim) in faces:
if not levelFace:
result.append(image[y:y+h, x:x+w])
else:
result.append(detect.levelFace(image, ((x, y, w, h), eyedim)))
#result.append(image[y:y+h, x:x+w])
return result
def trainRecognizer(db_folder, trainSize=config.DEFAULT_FACE_SIZE, showFaces=False, forceTrain=False):
recognizer = cv2.face.LBPHFaceRecognizer_create()
#recognizer = cv2.face.FisherFaceRecognizer_create()
#recognizer = cv2.face.EigenFaceRecognizer_create()
if (not forceTrain) and loadRecognizer(recognizer):
return recognizer
folders = getLabels(db_folder)
images = []
labels = []
label_count = 0
label_map = {}
for folder in folders:
faces = extractFaces(db_folder, folder, True)
images.extend([cv2.resize(face, trainSize) for face in faces])
labels.extend([label_count] * len(faces))
label_map[label_count] = folder
label_count += 1
if showFaces:
cv2.namedWindow("faces", 1)
cv2.imshow("faces", combineFaces(faces))
cv2.waitKey(0)
if showFaces:
cv2.destroyWindow("faces")
recognizer.train(images, np.array(labels))
for key in label_map:
recognizer.setLabelInfo(key, label_map[key])
saveRecognizer(recognizer)
return recognizer
def saveRecognizer(recognizer, filename=config.RECOGNIZER_OUTPUT_FILE):
recognizer.save(filename)
def loadRecognizer(recognizer, filename=config.RECOGNIZER_OUTPUT_FILE):
try:
recognizer.read(filename)
return True
except (cv2.error):
return False
if __name__ == '__main__':
recognizer = trainRecognizer('train', showFaces=False, forceTrain=True)