-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetect_faces.py
575 lines (415 loc) · 18.4 KB
/
detect_faces.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
from tensorflow.keras.preprocessing.image import img_to_array, load_img
from tensorflow.keras.models import load_model
import numpy as np
import cv2
import random
import math
from absl import app, flags, logging
from absl.flags import FLAGS, argparse_flags
from models.MTCNN_models import PNet, RNet, ONet
from tools.data_handling import preprocess_image
import tensorflow as tf
import os
import tqdm
import argparse
FLAGS = flags.FLAGS
flags.DEFINE_string('pnet_weights', './models/pnet.h5',
'path to the weights of the PNet')
flags.DEFINE_string('rnet_weights', './models/rnet.h5',
'path to the weights of the RNet')
flags.DEFINE_string('onet_weights', './models/onet.h5',
'path to the weights of the ONet')
flags.DEFINE_string('image', 'Sofia.jpeg',
'image to detect faces from')
class StageStatus(object):
'''
Keeps status between MTCNN stages
'''
def __init__(self, pad_result: tuple = None, width=0, height=0):
self.width = width
self.height = height
self.dy = self.edy = self.dx = self.edx = self.y = self.ey = self.x = self.ex = self.tmpw = self.tmph = []
if pad_result is not None:
self.update(pad_result)
def update(self, pad_result: tuple):
s = self
s.dy, s.edy, s.dx, s.edx, s.y, s.ey, s.x, s.ex, s.tmpw, s.tmph = pad_result
class MTCNN(object):
'''
Allows to perform MTCNN detection of faces with probability
'''
def __init__(self, min_face_size: int = 20, steps_threshold: list = None,
scale_factor: float = 0.709):
if steps_threshold is None:
steps_threshold = [0.6, 0.7, 0.7]
self._min_face_size = min_face_size
self._steps_threshold = steps_threshold
self._scale_factor = scale_factor
self._pnet = load_model(FLAGS.pnet_weights)
self._rnet = load_model(FLAGS.rnet_weights)
self._onet = load_model(FLAGS.onet_weights)
@property
def min_face_size(self):
return self._min_face_size
@min_face_size.setter
def min_face_size(self, mfc=20):
try:
self._min_face_size = int(mfc)
except ValueError:
self._min_face_size = 20
def __compute_scale_pyramid(self, m, min_layer):
scales = []
factor_count = 0
while min_layer >= 12:
scales += [m * np.power(self._scale_factor, factor_count)]
min_layer = min_layer * self._scale_factor
factor_count += 1
return scales
@staticmethod
def __scale_image(image, scale: float):
'''
scales the image to a given scale
'''
height, width, _ = image.shape
width_scaled = int(np.ceil(width * scale))
height_scaled = int(np.ceil(height * scale))
im_data = cv2.resize(image, (width_scaled, height_scaled), interpolation=cv2.INTER_AREA)
return img_to_array(im_data) / 255
@staticmethod
def __generate_bounding_box(imap, reg, scale, threshold):
# use heatmap to generate bounding boxes
stride = 2
cellsize = 12
imap = np.transpose(imap)
dx1 = np.transpose(reg[:, :, 0])
dy1 = np.transpose(reg[:, :, 1])
dx2 = np.transpose(reg[:, :, 2])
dy2 = np.transpose(reg[:, :, 3])
y, x = np.where(imap >= threshold)
if y.shape[0] == 1:
dx1 = np.flipud(dx1)
dy1 = np.flipud(dy1)
dx2 = np.flipud(dx2)
dy2 = np.flipud(dy2)
score = imap[(y,x)]
reg = np.transpose(np.vstack([dx1[(y,x)], dy1[(y,x)], dx2[(y,x)], dy2[(y,x)]]))
if reg.size == 0:
reg = np.emtpy(shape=(0,3))
bb = np.transpose(np.vstack([y, x]))
q1 = np.fix((stride * bb + 1) / scale)
q2 = np.fix((stride * bb + cellsize - 1 + 1) / scale)
boundingbox = np.hstack([q1, q2, np.expand_dims(score, 1), reg])
return boundingbox, reg
@staticmethod
def __nms(boxes, threshold, method):
"""
Non Maximum Suppression.
:param boxes: np array with bounding boxes.
:param threshold:
:param method: NMS method to apply. Available values ('Min', 'Union')
:return:
"""
if boxes.size == 0:
return np.empty((0, 3))
x1 = boxes[:, 0]
y1 = boxes[:, 1]
x2 = boxes[:, 2]
y2 = boxes[:, 3]
s = boxes[:, 4]
area = (x2 - x1 + 1) * (y2 - y1 + 1)
sorted_s = np.argsort(s)
pick = np.zeros_like(s, dtype=np.int16)
counter = 0
while sorted_s.size > 0:
i = sorted_s[-1]
pick[counter] = i
counter += 1
idx = sorted_s[0:-1]
xx1 = np.maximum(x1[i], x1[idx])
yy1 = np.maximum(y1[i], y1[idx])
xx2 = np.minimum(x2[i], x2[idx])
yy2 = np.minimum(y2[i], y2[idx])
w = np.maximum(0.0, xx2 - xx1 + 1)
h = np.maximum(0.0, yy2 - yy1 + 1)
inter = w * h
if method is 'Min':
o = inter / np.minimum(area[i], area[idx])
else:
o = inter / (area[i] + area[idx] - inter)
sorted_s = sorted_s[np.where(o <= threshold)]
pick = pick[0:counter]
return pick
@staticmethod
def __pad(total_boxes, w, h):
# pad the bounding boxes to squares
tmpw = (total_boxes[:, 2] - total_boxes[:, 0] + 1).astype(np.int32)
tmph = (total_boxes[:, 3] - total_boxes[:, 1] + 1).astype(np.int32)
numbox = total_boxes.shape[0]
dx = np.ones((numbox), dtype=np.int32)
dy = np.ones((numbox), dtype=np.int32)
edx = tmpw.copy().astype(np.int32)
edy = tmph.copy().astype(np.int32)
x = total_boxes[:, 0].copy().astype(np.int32)
y = total_boxes[:, 1].copy().astype(np.int32)
ex = total_boxes[:, 2].copy().astype(np.int32)
ey = total_boxes[:, 3].copy().astype(np.int32)
tmp = np.where(ex > w)
edx.flat[tmp] = np.expand_dims(-ex[tmp] + w + tmpw[tmp], 1)
ex[tmp] = w
tmp = np.where(ey > h)
edy.flat[tmp] = np.expand_dims(-ey[tmp] + h + tmph[tmp], 1)
ey[tmp] = h
tmp = np.where(x < 1)
dx.flat[tmp] = np.expand_dims(2 - x[tmp], 1)
x[tmp] = 1
tmp = np.where(y < 1)
dy.flat[tmp] = np.expand_dims(2 - y[tmp], 1)
y[tmp] = 1
return dy, edy, dx, edx, y, ey, x, ex, tmpw, tmph
@staticmethod
def __rerec(bbox):
# convert bbox to square
height = bbox[:, 3] - bbox[:, 1]
width = bbox[:, 2] - bbox[:, 0]
max_side_length = np.maximum(width, height)
bbox[:, 0] = bbox[:, 0] + width * 0.5 - max_side_length * 0.5
bbox[:, 1] = bbox[:, 1] + height * 0.5 - max_side_length * 0.5
bbox[:, 2:4] = bbox[:, 0:2] + np.transpose(np.tile(max_side_length, (2, 1)))
return bbox
@staticmethod
def __bbreg(boundingbox, reg):
# calibrate bounding boxes
if reg.shape[1] == 1:
reg = np.reshape(reg, (reg.shape[2], reg.shape[3]))
w = boundingbox[:, 2] - boundingbox[:, 0] + 1
h = boundingbox[:, 3] - boundingbox[:, 1] + 1
b1 = boundingbox[:, 0] + reg[:, 0] * w
b2 = boundingbox[:, 1] + reg[:, 1] * h
b3 = boundingbox[:, 2] + reg[:, 2] * w
b4 = boundingbox[:, 3] + reg[:, 3] * h
boundingbox[:, 0:4] = np.transpose(np.vstack([b1, b2, b3, b4]))
return boundingbox
def detect_faces_pnet(self, image):
height, width, _ = image.shape
m = 12 / self._min_face_size
total_boxes = np.empty((0, 9))
min_layer = np.amin([height, width]) * m
scales = self.__compute_scale_pyramid(m, min_layer)
for scale in scales:
scaled_image = self.__scale_image(image, scale)
img = np.expand_dims(scaled_image, 0)
out = self._pnet.predict(img)
out0 = out[0]
out1 = out[1]
boxes, _ = self.__generate_bounding_box(
out1[0, :, :, 1].copy(),
out0[0, :, :, :].copy(),
scale, self._steps_threshold[0])
# inter-scale nms
pick = self.__nms(boxes.copy(), 0.5, 'Union')
if boxes.size > 0 and pick.size > 0:
boxes = boxes[pick, :]
total_boxes = np.append(total_boxes, boxes, axis= 0)
numboxes = total_boxes.shape[0]
if numboxes > 0:
pick = self.__nms(total_boxes.copy(), 0.7, 'Union')
total_boxes = total_boxes[pick, :]
regw = total_boxes[:, 2] - total_boxes[:, 0]
regh = total_boxes[:, 3] - total_boxes[:, 1]
qq1 = total_boxes[:, 0] + total_boxes[:, 5] * regw
qq2 = total_boxes[:, 1] + total_boxes[:, 6] * regh
qq3 = total_boxes[:, 2] + total_boxes[:, 7] * regw
qq4 = total_boxes[:, 3] + total_boxes[:, 8] * regh
total_boxes = np.transpose(np.vstack([qq1, qq2, qq3, qq4, total_boxes[:, 4]]))
total_boxes[:, 0:4] = np.fix(total_boxes[:, 0:4]).astype(np.int32)
logging.info('PNet: %s boxes detected' % len(total_boxes))
return total_boxes
def detect_faces_rnet(self, img):
height, width, _ = img.shape
stage_status = StageStatus(width= width, height=height)
m = 12 / self._min_face_size
min_layer = np.amin([height, width]) * m
scales = self.__compute_scale_pyramid(m, min_layer)
total_boxes, stage_status = self.__stage1(img, scales, stage_status)
num_boxes = total_boxes.shape[0]
if num_boxes == 0:
return total_boxes
# second stage
tempimg = np.zeros(shape=(24, 24, 3, num_boxes))
for k in range(0, num_boxes):
tmp = np.zeros((int(stage_status.tmph[k]), int(stage_status.tmpw[k]), 3))
tmp[stage_status.dy[k] - 1:stage_status.edy[k], stage_status.dx[k] - 1:stage_status.edx[k], :] = \
img[stage_status.y[k] - 1:stage_status.ey[k], stage_status.x[k] - 1:stage_status.ex[k], :]
if tmp.shape[0] > 0 and tmp.shape[1] > 0 or tmp.shape[0] == 0 and tmp.shape[1] == 0:
tempimg[:, :, :, k] = cv2.resize(tmp, (24, 24), interpolation=cv2.INTER_AREA)
else:
return np.empty(shape=(0,))
tempimg /= 255
tempimg = np.transpose(tempimg, (3, 0, 1, 2))
out = self._rnet.predict(tempimg)
out0 = np.transpose(out[0])
out1 = np.transpose(out[1])
score = out1[1, :]
ipass = np.where(score > self._steps_threshold[1])
total_boxes = np.hstack([total_boxes[ipass[0], 0:4].copy(), np.expand_dims(score[ipass].copy(), 1)])
mv = out0[:, ipass[0]]
if total_boxes.shape[0] > 0:
pick = self.__nms(total_boxes, 0.7, 'Union')
total_boxes = total_boxes[pick, :]
total_boxes = self.__bbreg(total_boxes.copy(), np.transpose(mv[:, pick]))
logging.info('RNet: %s boxes detected' % len(total_boxes))
return total_boxes
def detect_faces(self, img) -> list:
'''
Detects bounding boxes from the specified image
'''
height, width, _ = img.shape
stage_status = StageStatus(width= width, height=height)
m = 12 / self._min_face_size
min_layer = np.amin([height, width]) * m
scales = self.__compute_scale_pyramid(m, min_layer)
stages = [self.__stage1, self.__stage2, self.__stage3]
result = [scales, stage_status]
for stage in stages:
result = stage(img, result[0], result[1])
total_boxes = result
print(total_boxes.shape)
bounding_boxes = []
for bounding_box in total_boxes:
x = max(0, int(bounding_box[0]))
y = max(0, int(bounding_box[1]))
width = int(bounding_box[2] - x)
height = int(bounding_box[3] - y)
bounding_boxes.append({
'box' : [x, y, width, height],
'confidence' : bounding_box[-1]
})
logging.info('%s bounding boxes found' % len(bounding_boxes))
return bounding_boxes, total_boxes
def __stage1(self, image, scales: list, stage_status: StageStatus):
'''
First stage of MTCNN
'''
logging.info('First stage')
total_boxes = np.empty((0,9))
status = stage_status
for scale in scales:
scaled_image = self.__scale_image(image, scale)
img = np.expand_dims(scaled_image, 0)
out = self._pnet.predict(img)
out0 = out[0]
out1 = out[1]
boxes, _ = self.__generate_bounding_box(
out1[0, :, :, 1].copy(),
out0[0, :, :, :].copy(),
scale, self._steps_threshold[0])
# inter-scale nms
pick = self.__nms(boxes.copy(), 0.5, 'Union')
if boxes.size > 0 and pick.size > 0:
boxes = boxes[pick, :]
total_boxes = np.append(total_boxes, boxes, axis= 0)
numboxes = total_boxes.shape[0]
if numboxes > 0:
pick = self.__nms(total_boxes.copy(), 0.7, 'Union')
total_boxes = total_boxes[pick, :]
regw = total_boxes[:, 2] - total_boxes[:, 0]
regh = total_boxes[:, 3] - total_boxes[:, 1]
qq1 = total_boxes[:, 0] + total_boxes[:, 5] * regw
qq2 = total_boxes[:, 1] + total_boxes[:, 6] * regh
qq3 = total_boxes[:, 2] + total_boxes[:, 7] * regw
qq4 = total_boxes[:, 3] + total_boxes[:, 8] * regh
total_boxes = np.transpose(np.vstack([qq1, qq2, qq3, qq4, total_boxes[:, 4]]))
total_boxes = self.__rerec(total_boxes.copy())
total_boxes[:, 0:4] = np.fix(total_boxes[:, 0:4]).astype(np.int32)
status = StageStatus(self.__pad(total_boxes.copy(), stage_status.width, stage_status.height),
width=stage_status.width, height=stage_status.height)
logging.info('Stage 1: %s boxes detected' % len(total_boxes))
return total_boxes, status
def __stage2(self, img, total_boxes, stage_status: StageStatus):
'''
Seconds stage of MTCNN
'''
num_boxes = total_boxes.shape[0]
if num_boxes == 0:
return total_boxes, stage_status
# second stage
tempimg = np.zeros(shape=(24, 24, 3, num_boxes))
for k in range(0, num_boxes):
tmp = np.zeros((int(stage_status.tmph[k]), int(stage_status.tmpw[k]), 3))
tmp[stage_status.dy[k] - 1:stage_status.edy[k], stage_status.dx[k] - 1:stage_status.edx[k], :] = \
img[stage_status.y[k] - 1:stage_status.ey[k], stage_status.x[k] - 1:stage_status.ex[k], :]
if tmp.shape[0] > 0 and tmp.shape[1] > 0 or tmp.shape[0] == 0 and tmp.shape[1] == 0:
tempimg[:, :, :, k] = cv2.resize(tmp, (24, 24), interpolation=cv2.INTER_AREA)
else:
return np.empty(shape=(0,)), stage_status
tempimg /= 255
tempimg = np.transpose(tempimg, (3, 0, 1, 2))
out = self._rnet.predict(tempimg)
out0 = np.transpose(out[0])
out1 = np.transpose(out[1])
score = out1[1, :]
ipass = np.where(score > self._steps_threshold[1])
total_boxes = np.hstack([total_boxes[ipass[0], 0:4].copy(), np.expand_dims(score[ipass].copy(), 1)])
mv = out0[:, ipass[0]]
if total_boxes.shape[0] > 0:
pick = self.__nms(total_boxes, 0.7, 'Union')
total_boxes = total_boxes[pick, :]
total_boxes = self.__bbreg(total_boxes.copy(), np.transpose(mv[:, pick]))
total_boxes = self.__rerec(total_boxes.copy())
logging.info('Stage 2: %s boxes detected' % len(total_boxes))
return total_boxes, stage_status
def __stage3(self, img, total_boxes, stage_status: StageStatus):
'''
Third stage of MTCNN
'''
num_boxes = total_boxes.shape[0]
if num_boxes == 0:
return total_boxes, np.empty(shape=(0,))
total_boxes = np.fix(total_boxes).astype(np.int32)
status = StageStatus(self.__pad(total_boxes.copy(), stage_status.width, stage_status.height),
width=stage_status.width, height=stage_status.height)
tempimg = np.zeros((48, 48, 3, num_boxes))
for k in range(0, num_boxes):
tmp = np.zeros((int(status.tmph[k]), int(status.tmpw[k]), 3))
tmp[status.dy[k] - 1:status.edy[k], status.dx[k] - 1:status.edx[k], :] = \
img[status.y[k] - 1:status.ey[k], status.x[k] - 1:status.ex[k], :]
if tmp.shape[0] > 0 and tmp.shape[1] > 0 or tmp.shape[0] == 0 and tmp.shape[1] == 0:
tempimg[:, :, :, k] = cv2.resize(tmp, (48, 48), interpolation=cv2.INTER_AREA)
else:
return np.empty(shape=(0,)), np.empty(shape=(0,))
tempimg /= 255
tempimg = np.transpose(tempimg, (3, 0, 1, 2))
out = self._onet.predict(tempimg)
out0 = np.transpose(out[0])
out1 = np.transpose(out[1])
score = out1[1, :]
ipass = np.where(score > self._steps_threshold[2])
total_boxes = np.hstack([total_boxes[ipass[0], 0:4].copy(), np.expand_dims(score[ipass].copy(), 1)])
mv = out0[:, ipass[0]]
if total_boxes.shape[0] > 0:
total_boxes = self.__bbreg(total_boxes.copy(), np.transpose(mv))
pick = self.__nms(total_boxes.copy(), 0.7, 'Min')
total_boxes = total_boxes[pick, :]
logging.info('Stage 3: %s boxes detected' % len(total_boxes))
return total_boxes
def main(args):
# check image
img = cv2.imread(FLAGS.image)
if not os.path.exists(FLAGS.image) or img is None or not hasattr(img, 'shape'):
logging.fatal('Image does not exists')
detector = MTCNN()
bounding_boxes, _ = detector.detect_faces(img)
faces = [f for f in bounding_boxes if f['confidence'] > 0.9]
logging.info('Found %s bounding boxes of which %s with over 95 confidences' %
(str(len(bounding_boxes)), str(len(faces))))
for face in faces:
bb = face['box']
cv2.rectangle(image,
(bb[0], bb[1]),(bb[0] + bb[2], bb[1] + bb[3]),
(0,155,255), 2)
cv2.imwrite('Sofia-sevi-net.jpg', img)
if __name__ == '__main__':
try:
app.run(main)
except SystemExit:
pass