forked from axinc-ai/ailia-models
-
Notifications
You must be signed in to change notification settings - Fork 0
/
insightface.py
342 lines (272 loc) · 9.97 KB
/
insightface.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
import sys
import os
import time
import glob
from collections import namedtuple
import numpy as np
from numpy.linalg import norm
import cv2
import ailia
# import original modules
sys.path.append('../../util')
from utils import get_base_parser, update_parser # noqa: E402
from model_utils import check_and_download_models # noqa: E402
from detector_utils import load_image # noqa: E402
import webcamera_utils # noqa: E402
from insightface_utils import PriorBox, decode, decode_landm, nms, \
face_align_norm_crop, draw_detection # noqa: E402
# ======================
# Parameters
# ======================
WEIGHT_DET_PATH = 'retinaface_resnet.onnx'
MODEL_DET_PATH = 'retinaface_resnet.onnx.prototxt'
WEIGHT_REC_R100_PATH = 'arcface_r100_v1.onnx'
MODEL_REC_R100_PATH = 'arcface_r100_v1.onnx.prototxt'
WEIGHT_REC_R50_PATH = 'arcface_r50_v1.onnx'
MODEL_REC_R50_PATH = 'arcface_r50_v1.onnx.prototxt'
WEIGHT_REC_R34_PATH = 'arcface_r34_v1.onnx'
MODEL_REC_R34_PATH = 'arcface_r34_v1.onnx.prototxt'
WEIGHT_REC_MF_PATH = 'arcface_mobilefacenet.onnx'
MODEL_REC_MF_PATH = 'arcface_mobilefacenet.onnx.prototxt'
WEIGHT_GA_PATH = 'genderage_v1.onnx'
MODEL_GA_PATH = 'genderage_v1.onnx.prototxt'
REMOTE_PATH = \
'https://storage.googleapis.com/ailia-models/insightface/'
IMAGE_PATH = 'demo.jpg'
SAVE_IMAGE_PATH = 'output.png'
IMAGE_SIZE = 512
Face = namedtuple('Face', [
'category', 'prob', 'cosin_metric',
'landmark', 'x', 'y', 'w', 'h',
'embedding', 'gender', 'age'
])
# ======================
# Arguemnt Parser Config
# ======================
parser = get_base_parser('InsightFace model', IMAGE_PATH, SAVE_IMAGE_PATH)
parser.add_argument(
'--det_thresh', type=float, default=0.02,
help='det_thresh'
)
parser.add_argument(
'--nms_thresh', type=float, default=0.4,
help='nms_thresh'
)
parser.add_argument(
'--ident_thresh', type=float, default=0.25572845,
help='ident_thresh'
)
parser.add_argument(
'--top_k', type=int, default=5000,
help='top_k'
)
parser.add_argument(
'-r', '--rec_model', type=str, default='resnet100',
choices=('resnet100', 'resnet50', 'resnet34', 'mobileface'),
help='recognition model'
)
args = update_parser(parser)
# ======================
# Secondaty Functions
# ======================
def preprocess(img):
img = np.float32(img)
img -= (104, 117, 123)
img = img.transpose(2, 0, 1)
img = np.expand_dims(img, axis=0)
return img
def post_processing(im_height, im_width, loc, conf, landms):
cfg_re50 = {
'min_sizes': [[16, 32], [64, 128], [256, 512]],
'steps': [8, 16, 32],
'variance': [0.1, 0.2],
'clip': False,
}
priorbox = PriorBox(cfg_re50, image_size=(im_height, im_width))
priors = priorbox.forward()
boxes = decode(loc[0], priors, cfg_re50['variance'])
scale = np.array([im_width, im_height, im_width, im_height])
boxes = boxes * scale
scores = conf[0][:, 1]
landms = decode_landm(landms[0], priors, cfg_re50['variance'])
scale1 = np.array([
im_width, im_height, im_width, im_height,
im_width, im_height, im_width, im_height,
im_width, im_height
])
landms = landms * scale1
inds = np.where(scores > args.det_thresh)[0]
boxes = boxes[inds]
landms = landms[inds]
scores = scores[inds]
# keep top-K before NMS
order = scores.argsort()[::-1][:args.top_k]
boxes = boxes[order]
landms = landms[order]
scores = scores[order]
dets = np.hstack(
(boxes, scores[:, np.newaxis])
).astype(np.float32, copy=False)
keep = nms(dets, args.nms_thresh)
dets = dets[keep, :]
landms = landms[keep]
return dets, landms
def face_identification(faces, ident_feats):
ident_faces = []
for i in range(len(faces)):
face = faces[i]
emb = face.embedding
metrics = ident_feats.dot(emb)
category = np.argmax(metrics)
face = face._replace(cosin_metric=metrics[category])
if args.ident_thresh <= face.cosin_metric:
face = face._replace(category=category)
ident_faces.append(face)
return ident_faces
def load_identities(rec_model):
names = []
feats = []
for path in glob.glob("identities/*.PNG"):
name = ".".join(
path.replace(os.sep, '/').split('/')[-1].split('.')[:-1]
)
names.append(name)
img = load_image(path)
img = cv2.cvtColor(img, cv2.COLOR_BGRA2RGB)
img = np.transpose(img, (2, 0, 1))
img = np.expand_dims(img, axis=0)
img = img.astype(np.float32)
output = rec_model.predict({'data': img})[0]
embedding = output[0]
embedding_norm = norm(embedding)
normed_embedding = embedding / embedding_norm
feats.append(normed_embedding)
feats = np.vstack(feats)
return names, feats
# ======================
# Main functions
# ======================
def predict(img, det_model, rec_model, ga_model):
# initial preprocesses
im_height, im_width, _ = img.shape
_img = preprocess(img)
# feedforward
output = det_model.predict({'img': _img})
loc, conf, landms = output
bboxes, landmarks = post_processing(im_height, im_width, loc, conf, landms)
faces = []
for i in range(bboxes.shape[0]):
bbox = bboxes[i, 0:4]
prob = bboxes[i, 4]
landmark = landmarks[i].reshape((5, 2))
_img = face_align_norm_crop(img, landmark=landmark)
_img = cv2.cvtColor(_img, cv2.COLOR_BGR2RGB)
_img = np.transpose(_img, (2, 0, 1))
_img = np.expand_dims(_img, axis=0)
_img = _img.astype(np.float32)
output = rec_model.predict({'data': _img})[0]
embedding = output[0]
embedding_norm = norm(embedding)
normed_embedding = embedding / embedding_norm
output = ga_model.predict({'data': _img})[0]
g = output[0, 0:2]
gender = np.argmax(g)
a = output[0, 2:202].reshape((100, 2))
a = np.argmax(a, axis=1)
age = int(sum(a))
face = Face(
category=None,
prob=prob,
cosin_metric=1,
landmark=landmark,
x=bbox[0] / im_width,
y=bbox[1] / im_height,
w=(bbox[2] - bbox[0]) / im_width,
h=(bbox[3] - bbox[1]) / im_height,
embedding=normed_embedding,
gender=gender,
age=age
)
faces.append(face)
return faces
def recognize_from_image(filename, det_model, rec_model, ga_model):
# prepare input data
img = load_image(filename)
print(f'input image shape: {img.shape}')
# load identities
ident_names, ident_feats = load_identities(rec_model)
img = cv2.cvtColor(img, cv2.COLOR_BGRA2BGR)
# inference
print('Start inference...')
if args.benchmark:
print('BENCHMARK mode')
for i in range(5):
start = int(round(time.time() * 1000))
faces = predict(img, det_model, rec_model, ga_model)
end = int(round(time.time() * 1000))
print(f'\tailia processing time {end - start} ms')
else:
faces = predict(img, det_model, rec_model, ga_model)
faces = face_identification(faces, ident_feats)
# plot result
res_img = draw_detection(img, faces, ident_names)
# plot result
cv2.imwrite(args.savepath, res_img)
print('Script finished successfully.')
def recognize_from_video(video, det_model, rec_model, ga_model):
capture = webcamera_utils.get_capture(video)
# create video writer if savepath is specified as video format
if args.savepath != SAVE_IMAGE_PATH:
f_h = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
f_w = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH))
writer = webcamera_utils.get_writer(args.savepath, f_h, f_w)
else:
writer = None
# load identities
ident_names, ident_feats = load_identities(rec_model)
while (True):
ret, frame = capture.read()
if (cv2.waitKey(1) & 0xFF == ord('q')) or not ret:
break
faces = predict(frame, det_model, rec_model, ga_model)
faces = face_identification(faces, ident_feats)
# plot result
res_img = draw_detection(frame, faces, ident_names)
# show
cv2.imshow('frame', res_img)
# save results
if writer is not None:
writer.write(res_img)
capture.release()
cv2.destroyAllWindows()
if writer is not None:
writer.release()
print('Script finished successfully.')
def main():
rec_model = {
'resnet100': (WEIGHT_REC_R100_PATH, MODEL_REC_R100_PATH),
'resnet50': (WEIGHT_REC_R50_PATH, MODEL_REC_R50_PATH),
'resnet34': (WEIGHT_REC_R34_PATH, MODEL_REC_R34_PATH),
'mobileface': (WEIGHT_REC_MF_PATH, MODEL_REC_MF_PATH),
}
WEIGHT_REC_PATH, MODEL_REC_PATH = rec_model[args.rec_model]
# model files check and download
print("=== DET model ===")
check_and_download_models(WEIGHT_DET_PATH, MODEL_DET_PATH, REMOTE_PATH)
print("=== REC model ===")
check_and_download_models(WEIGHT_REC_PATH, MODEL_REC_PATH, REMOTE_PATH)
print("=== GA model ===")
check_and_download_models(WEIGHT_GA_PATH, MODEL_GA_PATH, REMOTE_PATH)
# load model
env_id = ailia.get_gpu_environment_id()
print(f'env_id: {env_id}')
# initialize
det_model = ailia.Net(MODEL_DET_PATH, WEIGHT_DET_PATH, env_id=env_id)
rec_model = ailia.Net(MODEL_REC_PATH, WEIGHT_REC_PATH, env_id=env_id)
ga_model = ailia.Net(MODEL_GA_PATH, WEIGHT_GA_PATH, env_id=env_id)
if args.video is not None:
recognize_from_video(args.video, det_model, rec_model, ga_model)
else:
recognize_from_image(args.input, det_model, rec_model, ga_model)
if __name__ == '__main__':
main()