-
Notifications
You must be signed in to change notification settings - Fork 25
/
main.py
364 lines (313 loc) · 14.2 KB
/
main.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
import numpy as np
import cv2
import argparse
from itertools import product
from _testcapi import FLT_MIN
class YuNet:
def __init__(self, modelPath, inputSize=[320, 320], confThreshold=0.6, nmsThreshold=0.3, topK=5000, keepTopK=750):
self._model = cv2.dnn.readNet(modelPath)
self._inputNames = ''
self._outputNames = ['loc', 'conf', 'iou']
self._inputSize = inputSize # [w, h]
self._confThreshold = confThreshold
self._nmsThreshold = nmsThreshold
self._topK = topK
self._keepTopK = keepTopK
self._min_sizes = [[10, 16, 24], [32, 48], [64, 96], [128, 192, 256]]
self._steps = [8, 16, 32, 64]
self._variance = [0.1, 0.2]
# Generate priors
self._priorGen()
@property
def name(self):
return self.__class__.__name__
def setBackend(self, backend):
self._model.setPreferableBackend(backend)
def setTarget(self, target):
self._model.setPreferableTarget(target)
def setInputSize(self, input_size):
self._inputSize = input_size # [w, h]
# Regenerate priors
self._priorGen()
def infer(self, image):
assert image.shape[0] == self._inputSize[1], '{} (height of input image) != {} (preset height)'.format(image.shape[0], self._inputSize[1])
assert image.shape[1] == self._inputSize[0], '{} (width of input image) != {} (preset width)'.format(image.shape[1], self._inputSize[0])
# Preprocess
inputBlob = cv2.dnn.blobFromImage(image)
# Forward
self._model.setInput(inputBlob, self._inputNames)
outputBlob = self._model.forward(self._outputNames)
# Postprocess
results = self._postprocess(outputBlob)
return results
def _postprocess(self, outputBlob):
# Decode
dets = self._decode(outputBlob)
# NMS
keepIdx = cv2.dnn.NMSBoxes(
bboxes=dets[:, 0:4].tolist(),
scores=dets[:, -1].tolist(),
score_threshold=self._confThreshold,
nms_threshold=self._nmsThreshold,
top_k=self._topK
) # box_num x class_num
if len(keepIdx) > 0:
dets = dets[keepIdx]
# dets = np.squeeze(dets, axis=1)
return dets[:self._keepTopK]
else:
return np.empty(shape=(0, 15))
def _priorGen(self):
w, h = self._inputSize
feature_map_2th = [int(int((h + 1) / 2) / 2),
int(int((w + 1) / 2) / 2)]
feature_map_3th = [int(feature_map_2th[0] / 2),
int(feature_map_2th[1] / 2)]
feature_map_4th = [int(feature_map_3th[0] / 2),
int(feature_map_3th[1] / 2)]
feature_map_5th = [int(feature_map_4th[0] / 2),
int(feature_map_4th[1] / 2)]
feature_map_6th = [int(feature_map_5th[0] / 2),
int(feature_map_5th[1] / 2)]
feature_maps = [feature_map_3th, feature_map_4th,
feature_map_5th, feature_map_6th]
priors = []
for k, f in enumerate(feature_maps):
min_sizes = self._min_sizes[k]
for i, j in product(range(f[0]), range(f[1])): # i->h, j->w
for min_size in min_sizes:
s_kx = min_size / w
s_ky = min_size / h
cx = (j + 0.5) * self._steps[k] / w
cy = (i + 0.5) * self._steps[k] / h
priors.append([cx, cy, s_kx, s_ky])
self.priors = np.array(priors, dtype=np.float32)
def _decode(self, outputBlob):
loc, conf, iou = outputBlob
# get score
cls_scores = conf[:, 1]
iou_scores = iou[:, 0]
# clamp
_idx = np.where(iou_scores < 0.)
iou_scores[_idx] = 0.
_idx = np.where(iou_scores > 1.)
iou_scores[_idx] = 1.
scores = np.sqrt(cls_scores * iou_scores)
scores = scores[:, np.newaxis]
scale = np.array(self._inputSize)
# get bboxes
bboxes = np.hstack((
(self.priors[:, 0:2] + loc[:, 0:2] * self._variance[0] * self.priors[:, 2:4]) * scale,
(self.priors[:, 2:4] * np.exp(loc[:, 2:4] * self._variance)) * scale
))
# (x_c, y_c, w, h) -> (x1, y1, w, h)
bboxes[:, 0:2] -= bboxes[:, 2:4] / 2
# get landmarks
landmarks = np.hstack((
(self.priors[:, 0:2] + loc[:, 4: 6] * self._variance[0] * self.priors[:, 2:4]) * scale,
(self.priors[:, 0:2] + loc[:, 6: 8] * self._variance[0] * self.priors[:, 2:4]) * scale,
(self.priors[:, 0:2] + loc[:, 8:10] * self._variance[0] * self.priors[:, 2:4]) * scale,
(self.priors[:, 0:2] + loc[:, 10:12] * self._variance[0] * self.priors[:, 2:4]) * scale,
(self.priors[:, 0:2] + loc[:, 12:14] * self._variance[0] * self.priors[:, 2:4]) * scale
))
dets = np.hstack((bboxes, landmarks, scores))
return dets
def visualize(image, results, box_color=(0, 255, 0), text_color=(0, 0, 255), fps=None):
output = image.copy()
landmark_color = [
(255, 0, 0), # right eye
( 0, 0, 255), # left eye
( 0, 255, 0), # nose tip
(255, 0, 255), # right mouth corner
( 0, 255, 255) # left mouth corner
]
if fps is not None:
cv2.putText(output, 'FPS: {:.2f}'.format(fps), (0, 15), cv.FONT_HERSHEY_SIMPLEX, 0.5, text_color)
for det in results:
bbox = det[0:4].astype(np.int32)
cv2.rectangle(output, (bbox[0], bbox[1]), (bbox[0]+bbox[2], bbox[1]+bbox[3]), box_color, 2)
conf = det[-1]
cv2.putText(output, '{:.4f}'.format(conf), (bbox[0], bbox[1]+12), cv2.FONT_HERSHEY_DUPLEX, 0.5, text_color)
landmarks = det[4:14].astype(np.int32).reshape((5,2))
for idx, landmark in enumerate(landmarks):
cv2.circle(output, landmark, 2, landmark_color[idx], 2)
return output
class SFace:
def __init__(self, modelPath):
self._model = cv2.dnn.readNet(modelPath)
self._input_size = [112, 112]
self._dst = np.array([
[38.2946, 51.6963],
[73.5318, 51.5014],
[56.0252, 71.7366],
[41.5493, 92.3655],
[70.7299, 92.2041]
], dtype=np.float32)
self._dst_mean = np.array([56.0262, 71.9008], dtype=np.float32)
@property
def name(self):
return self.__class__.__name__
def setBackend(self, backend_id):
self._model.setPreferableBackend(backend_id)
def setTarget(self, target_id):
self._model.setPreferableTarget(target_id)
def _preprocess(self, image, bbox):
aligned_image = self._alignCrop(image, bbox)
return cv2.dnn.blobFromImage(aligned_image)
def infer(self, image, bbox):
# Preprocess
# inputBlob = self._preprocess(image, bbox)
inputBlob = cv2.dnn.blobFromImage(self._alignCrop(image, bbox))
# Forward
self._model.setInput(inputBlob)
outputBlob = self._model.forward()
# Postprocess
results = outputBlob / cv2.norm(outputBlob)
return results
def match(self, image1, face1, image2, face2, dis_type=0):
feature1 = self.infer(image1, face1)
feature2 = self.infer(image2, face2)
if dis_type == 0: # COSINE
return np.sum(feature1 * feature2)
elif dis_type == 1: # NORM_L2
return cv2.norm(feature1, feature2)
else:
raise NotImplementedError()
def _alignCrop(self, image, face):
# Retrieve landmarks
if face.shape[-1] == (4 + 5 * 2):
landmarks = face[4:].reshape(5, 2)
else:
raise NotImplementedError()
warp_mat = self._getSimilarityTransformMatrix(landmarks)
aligned_image = cv2.warpAffine(image, warp_mat, self._input_size, flags=cv2.INTER_LINEAR)
return aligned_image
def _getSimilarityTransformMatrix(self, src):
# compute the mean of src and dst
src_mean = np.array([np.mean(src[:, 0]), np.mean(src[:, 1])], dtype=np.float32)
dst_mean = np.array([56.0262, 71.9008], dtype=np.float32)
# subtract the means from src and dst
src_demean = src.copy()
src_demean[:, 0] = src_demean[:, 0] - src_mean[0]
src_demean[:, 1] = src_demean[:, 1] - src_mean[1]
dst_demean = self._dst.copy()
dst_demean[:, 0] = dst_demean[:, 0] - dst_mean[0]
dst_demean[:, 1] = dst_demean[:, 1] - dst_mean[1]
A = np.array([[0., 0.], [0., 0.]], dtype=np.float64)
for i in range(5):
A[0][0] += dst_demean[i][0] * src_demean[i][0]
A[0][1] += dst_demean[i][0] * src_demean[i][1]
A[1][0] += dst_demean[i][1] * src_demean[i][0]
A[1][1] += dst_demean[i][1] * src_demean[i][1]
A = A / 5
d = np.array([1.0, 1.0], dtype=np.float64)
if A[0][0] * A[1][1] - A[0][1] * A[1][0] < 0:
d[1] = -1
T = np.array([
[1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 1.0]
], dtype=np.float64)
s, u, vt = cv2.SVDecomp(A)
smax = s[0][0] if s[0][0] > s[1][0] else s[1][0]
tol = smax * 2 * FLT_MIN
rank = int(0)
if s[0][0] > tol:
rank += 1
if s[1][0] > tol:
rank += 1
det_u = u[0][0] * u[1][1] - u[0][1] * u[1][0]
det_vt = vt[0][0] * vt[1][1] - vt[0][1] * vt[1][0]
if rank == 1:
if det_u * det_vt > 0:
uvt = np.matmul(u, vt)
T[0][0] = uvt[0][0]
T[0][1] = uvt[0][1]
T[1][0] = uvt[1][0]
T[1][1] = uvt[1][1]
else:
temp = d[1]
d[1] = -1
D = np.array([[d[0], 0.0], [0.0, d[1]]], dtype=np.float64)
Dvt = np.matmul(D, vt)
uDvt = np.matmul(u, Dvt)
T[0][0] = uDvt[0][0]
T[0][1] = uDvt[0][1]
T[1][0] = uDvt[1][0]
T[1][1] = uDvt[1][1]
d[1] = temp
else:
D = np.array([[d[0], 0.0], [0.0, d[1]]], dtype=np.float64)
Dvt = np.matmul(D, vt)
uDvt = np.matmul(u, Dvt)
T[0][0] = uDvt[0][0]
T[0][1] = uDvt[0][1]
T[1][0] = uDvt[1][0]
T[1][1] = uDvt[1][1]
var1 = 0.0
var2 = 0.0
for i in range(5):
var1 += src_demean[i][0] * src_demean[i][0]
var2 += src_demean[i][1] * src_demean[i][1]
var1 /= 5
var2 /= 5
scale = 1.0 / (var1 + var2) * (s[0][0] * d[0] + s[1][0] * d[1])
TS = [
T[0][0] * src_mean[0] + T[0][1] * src_mean[1],
T[1][0] * src_mean[0] + T[1][1] * src_mean[1]
]
T[0][2] = dst_mean[0] - scale * TS[0]
T[1][2] = dst_mean[1] - scale * TS[1]
T[0][0] *= scale
T[0][1] *= scale
T[1][0] *= scale
T[1][1] *= scale
return np.array([
[T[0][0], T[0][1], T[0][2]],
[T[1][0], T[1][1], T[1][2]]
], dtype=np.float64)
if __name__=='__main__':
parser = argparse.ArgumentParser(description='A Fast and Accurate CNN-based Face Detector (https://github.com/ShiqiYu/libfacedetection).')
parser.add_argument('--imgpath', type=str, default='selfie.jpg', help='Path to the input image')
parser.add_argument('--detect_modelpath', type=str, default='weights/face_detection_yunet.onnx', help='Path to the face detect model.')
parser.add_argument('--conf_threshold', type=float, default=0.9,
help='Filter out faces of confidence < conf_threshold.')
parser.add_argument('--nms_threshold', type=float, default=0.3,
help='Suppress bounding boxes of iou >= nms_threshold.')
parser.add_argument('--top_k', type=int, default=5000, help='Keep top_k bounding boxes before NMS.')
parser.add_argument('--keep_top_k', type=int, default=750, help='Keep keep_top_k bounding boxes after NMS.')
parser.add_argument('--rec_modelpath', type=str, default='weights/face_detection_yunet.onnx',
help='Path to the face detect model.')
parser.add_argument('--img1path', type=str, default='telangpu.png', help='Path to the input image1 to match')
parser.add_argument('--img2path', type=str, default='telangpu2.png', help='Path to the input image2 to match')
parser.add_argument('--dis_type', type=int, choices=[0, 1], default=0, help='Distance type. 0: cosine, 1: norm_l1.')
args = parser.parse_args()
detector = YuNet(args.detect_modelpath, confThreshold=args.conf_threshold, nmsThreshold=args.nms_threshold, topK=args.top_k, keepTopK=args.keep_top_k)
srcimg = cv2.imread(args.imgpath)
detector.setInputSize([srcimg.shape[1], srcimg.shape[0]])
results = detector.infer(srcimg)
srcimg = visualize(srcimg, results)
winName = 'Deep learning object detection in OpenCV'
cv2.namedWindow(winName, 0)
cv2.imshow(winName, srcimg)
cv2.waitKey(0)
cv2.destroyAllWindows()
recognizer = SFace(args.rec_modelpath)
img1 = cv2.imread(args.img1path)
img2 = cv2.imread(args.img2path)
detector.setInputSize([img1.shape[1], img1.shape[0]])
face1 = detector.infer(img1)
assert face1.shape[0] > 0, 'Cannot find a face in {}'.format(args.input1)
detector.setInputSize([img2.shape[1], img2.shape[0]])
face2 = detector.infer(img2)
assert face2.shape[0] > 0, 'Cannot find a face in {}'.format(args.input2)
distance = recognizer.match(img1, face1[0][:-1], img2, face2[0][:-1], args.dis_type)
if args.dis_type == 0:
dis_type = 'Cosine'
threshold = 0.363
result = 'same identity' if distance >= threshold else 'different identity'
else:
dis_type = 'Norm-L2'
threshold = 1.128
result = 'same identity' if distance <= threshold else 'different identity'
print('Using {} distance, threshold {}: {}.'.format(dis_type, threshold, result))