This repository was archived by the owner on Feb 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrunner.py
344 lines (265 loc) · 9.28 KB
/
runner.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#!/usr/bin/env python3
import os
import cv2
import numpy as np
from matplotlib import pyplot as plt
def get_path_list(root_path):
'''
To get a list of path directories from root path
Parameters
----------
root_path : str
Location of root directory
Returns
-------
list
List containing the names of each person
'''
return sorted(os.listdir(root_path))
def get_class_id(root_path, train_names):
'''
To get a list of train images and a list of image classes id
Parameters
----------
root_path : str
Location of images root directory
train_names : list
List containing the names of the train sub-directories
Returns
-------
list
List containing all image in the train directories
list
List containing all image classes id
'''
image_list = []
image_classes_list = []
for idx, train_name in enumerate(train_names):
train_dir = os.path.join(root_path, train_name)
for image_name in get_path_list(train_dir):
image_file = os.path.join(train_dir, image_name)
image = cv2.imread(image_file)
image_list.append(image)
image_classes_list.append(idx)
return image_list, image_classes_list
def detect_train_faces_and_filter(image_list, image_classes_list):
'''
To detect a face from given image list and filter it if the face on
the given image is less than one
Parameters
----------
image_list : list
List containing all loaded images
image_classes_list : list
List containing all image classes id
Returns
-------
list
List containing all filtered and cropped face images in grayscale
list
List containing all filtered image classes id
'''
train_face_grays = []
filtered_classes_list = []
face_cascade = cv2.CascadeClassifier("assets/haarcascade_frontalface_default.xml")
for image, class_id in zip(image_list, image_classes_list):
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(
image,
scaleFactor=1.2,
minNeighbors=5,
)
for face in faces:
x,y,w,h = face
train_face_grays.append(image[y:y+h, x:x+w])
filtered_classes_list.append(class_id)
return train_face_grays, filtered_classes_list
def detect_test_faces_and_filter(image_list):
'''
To detect a face from given image list and filter it if the face on
the given image is less than one
Parameters
----------
image_list : list
List containing all loaded images
Returns
-------
list
List containing all filtered and cropped face images in grayscale
list
List containing all filtered faces location saved in rectangle
'''
test_faces_gray = []
test_faces_rects = []
face_cascade = cv2.CascadeClassifier("assets/haarcascade_frontalface_default.xml")
for image, class_id in zip(image_list, image_classes_list):
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(
image,
scaleFactor=1.2,
minNeighbors=5,
)
for face in faces:
x,y,w,h = face
test_faces_gray.append(image[y:y+h, x:x+w])
test_faces_rects.append(face)
return test_faces_gray, test_faces_rects
def train(train_face_grays, image_classes_list):
'''
To create and train face recognizer object
Parameters
----------
train_face_grays : list
List containing all filtered and cropped face images in grayscale
image_classes_list : list
List containing all filtered image classes id
Returns
-------
object
Recognizer object after being trained with cropped face images
'''
recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.train(train_face_grays, np.array(image_classes_list))
return recognizer
def get_test_images_data(test_root_path):
'''
To load a list of test images from given path list
Parameters
----------
test_root_path : str
Location of images root directory
Returns
-------
list
List containing all image in the test directories
'''
image_list = []
for image_name in get_path_list(test_root_path):
image_file = os.path.join(test_root_path, image_name)
image = cv2.imread(image_file)
image_list.append(image)
return image_list
def predict(recognizer, test_faces_gray):
'''
To predict the test image with the recognizer
Parameters
----------
recognizer : object
Recognizer object after being trained with cropped face images
test_faces_gray : list
List containing all filtered and cropped face images in grayscale
Returns
-------
list
List containing all prediction results from given test faces
'''
result = []
for image in test_faces_gray:
class_id, confidence = recognizer.predict(image)
result.append(class_id)
return result
def draw_prediction_results(predict_results, test_image_list, test_faces_rects, train_names, size):
'''
To draw prediction results on the given test images and resize the image
Parameters
----------
predict_results : list
List containing all prediction results from given test faces
test_image_list : list
List containing all loaded test images
test_faces_rects : list
List containing all filtered faces location saved in rectangle
train_names : list
List containing the names of the train sub-directories
size : number
Final size of each test image
Returns
-------
list
List containing all test images after being drawn with
final result
'''
n = len(predict_results)
for idx in range(n):
x,y,w,h = test_faces_rects[idx]
cv2.rectangle(
test_image_list[idx],
(x, y),
(x + w, y + h),
(255, 0, 0),
2,
)
test_image_list[idx] = cv2.resize(test_image_list[idx], (size, size))
cv2.putText(
test_image_list[idx],
train_names[predict_results[idx]],
(15, 20),
cv2.FONT_HERSHEY_PLAIN,
1,
(0, 0, 255),
lineType=1
)
return test_image_list
def combine_and_show_result(image_list, size):
'''
To show the final image that already combine into one image
Parameters
----------
image_list : nparray
Array containing image data
size : number
Final size of each test image
'''
n = len(predict_results)
for idx in range(n):
image_list[idx] = cv2.resize(test_image_list[idx], (size, size))
result = cv2.hconcat(image_list)
cv2.imshow("Result", result)
cv2.waitKey(0)
cv2.destroyAllWindows()
'''
You may modify the code below if it's marked between
-------------------
Modifiable
-------------------
and
-------------------
End of modifiable
-------------------
'''
if __name__ == "__main__":
'''
Please modify train_root_path value according to the location of
your data train root directory
-------------------
Modifiable
-------------------
'''
train_root_path = "./dataset/train/"
'''
-------------------
End of modifiable
-------------------
'''
train_names = get_path_list(train_root_path)
train_image_list, image_classes_list = get_class_id(train_root_path, train_names)
train_face_grays, filtered_classes_list = detect_train_faces_and_filter(train_image_list, image_classes_list)
recognizer = train(train_face_grays, filtered_classes_list)
'''
Please modify train_root_path value according to the location of
your data train root directory
-------------------
Modifiable
-------------------
'''
test_root_path = "./dataset/test/"
'''
-------------------
End of modifiable
-------------------
'''
test_image_list = get_test_images_data(test_root_path)
test_faces_gray, test_faces_rects = detect_test_faces_and_filter(test_image_list)
predict_results = predict(recognizer, test_faces_gray)
predicted_test_image_list = draw_prediction_results(predict_results, test_image_list, test_faces_rects, train_names, 200)
combine_and_show_result(predicted_test_image_list, 200)