forked from axinc-ai/ailia-models
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathax_facial_features.py
321 lines (263 loc) · 9.61 KB
/
ax_facial_features.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
import argparse
import colorsys
import sys
import time
import ailia
import cv2
import numpy as np
import ax_facial_features_utils as fut
sys.path.append('../../util')
# logger
from logging import getLogger # noqa: E402
from image_utils import imread, load_image # noqa: E402
from math_utils import softmax
from model_utils import check_and_download_models # noqa: E402
from arg_utils import get_base_parser, get_savepath, update_parser # noqa: E402
from webcamera_utils import get_capture, get_writer # noqa: E402
logger = getLogger(__name__)
# ======================
# Parameters 1
# ======================
IMAGE_PATH = 'man-with-beard.jpg'
SAVE_IMAGE_PATH = 'output.png'
EYELIDS_CLASSES = ['double', 'single']
EYELASHES_CLASSES = ['dense', 'moderate', 'sparse']
FACIAL_HAIR_CLASSES = ['moustache', 'beard', 'mouth_side_hair']
# ======================
# Argument Parser Config
# ======================
parser:argparse.ArgumentParser = get_base_parser(
'ax Facial Features',
IMAGE_PATH,
SAVE_IMAGE_PATH,
)
parser.add_argument(
'-m', '--mode', nargs='+', default=['eyelids', 'eyelashes', 'facial_hair']
)
args = update_parser(parser)
# ======================
# Parameters 2
# ======================
MODELS = {
'blazeface': {},
'facemesh': {},
'ax_eyelids': {},
'ax_eyelashes': {},
'ax_facial_hair': {},
}
for k, v in MODELS.items():
stem = f'{k}.opt'
if k[:3] == 'ax_':
stem += '.obf'
v['remote_path'] = f'https://storage.googleapis.com/ailia-models/ax_facial_features/'
if 'weight_path' not in v:
v['weight_path'] = f'{stem}.onnx'
if 'model_path' not in v:
v['model_path'] = f'{stem}.onnx.prototxt'
if 'remote_path' not in v:
v['remote_path'] = f'https://storage.googleapis.com/ailia-models/{k}/'
# ======================
# Utils
# ======================
def print_results(results):
"""Print all facial features classification results"""
for res in results:
tmp = ['top_left', 'top_right', 'bottom_left', 'bottom_right']
logger.info('ROI')
logger.info('==============================================================')
for i, c in enumerate(tmp):
logger.info(f'{c} = ({res["roi"][i][0]:.2f}, {res["roi"][i][1]:.2f})'
f' (x, y)')
logger.info('')
if 'eyelids' in res:
logger.info('Eyelids')
fut.print_results(res['eyelids'], EYELIDS_CLASSES, logger)
if 'eyelashes' in res:
logger.info('Eyelashes')
fut.print_results(res['eyelashes'], EYELASHES_CLASSES, logger)
if 'facial_hair' in res:
logger.info('Facial hair')
fut.print_results(res['facial_hair'], FACIAL_HAIR_CLASSES, logger,
multilabel=True)
def plot_results(img, results, horizontal_flip=False):
"""Plot the facial features classification results on the image"""
img_w = img.shape[1]
l = [
['eyelids', EYELIDS_CLASSES, False],
['eyelashes', EYELASHES_CLASSES, False],
['facial_hair', FACIAL_HAIR_CLASSES, True],
]
n = 5
hsv_tuples = np.asarray([
(1 * x / n, 1., 1.) for x in range(n)])
colors = np.apply_along_axis(
lambda x: colorsys.hsv_to_rgb(*x), 1, hsv_tuples)
colors = (colors * 255).astype(np.uint8) # 0-255 BGR
rng = np.random.default_rng(0)
rng.shuffle(colors)
if horizontal_flip:
img_draw = np.ascontiguousarray(img[:, ::-1])
else:
img_draw = img.copy()
for res in results:
roi = np.stack(res['roi'])
if horizontal_flip:
roi[:, 0] = img_w - roi[:, 0] # Flip coordinates
# Flip top left with top right,...
tmp = roi[::2].copy()
roi[::2] = roi[1::2]
roi[1::2] = tmp
fut.draw_roi(img_draw, roi)
x, y = tuple(int(ee) for ee in (roi[0] + 2))
x = max(2, x)
y = max(2, y)
for e, c in zip(l, colors):
scores = res[e[0]]
labels = e[1]
ids_order = fut.filter_sort_results(scores, labels, multilabel=e[2])
color = tuple(int(ee) for ee in c)
text = ''
for idx in ids_order:
text += f'{labels[idx]} ({scores[idx]*100:4.2f}%); '
text = text[:-2]
# txt_color = (0, 0, 0) if np.mean(color) > 127.5 else (255, 255, 255)
txt_color = (0, 0, 0)
txt_bbox_thick = 1
font = cv2.FONT_HERSHEY_SIMPLEX
font_scale = img_w / 1450
txt_size = cv2.getTextSize(text, font, font_scale, txt_bbox_thick)[0]
w = txt_size[0] + 4
h = txt_size[1] + 8
text_position = (x + 4, y + h//2 + 4)
cv2.rectangle(img_draw, (x, y), (x + w, y + h), color, thickness=-1)
cv2.putText(img_draw, text, text_position, font, font_scale, txt_color, 1)
y = y + h + 2
return img_draw
def init():
"""Initialize all ailia models"""
# net initialize
models = {}
for k, v in MODELS.items():
models[k] = ailia.Net(v['model_path'], v['weight_path'],
env_id=args.env_id)
return models
def predict(models, raw_img):
"""Compute facial features classification predictions"""
res = []
# Face detection
input_data, scale, padding = fut.face_detector_preprocess(raw_img)
preds = models['blazeface'].predict([input_data])
detections = fut.face_detector_postprocess(preds)
# Face landmark estimation
if detections[0].size != 0:
face_imgs, face_affs, _, _ = fut.face_lm_preprocess(
raw_img[:, :, ::-1], detections, scale, padding
)
models['facemesh'].set_input_shape(face_imgs.shape)
preds = models['facemesh'].predict([face_imgs])
landmarks, _, _ = fut.face_lm_postprocess(preds, face_affs)
for i in range(len(landmarks)):
res_i = {}
lm = landmarks[i:i+1]
res_i['roi'] = fut.get_roi(lm)
if 'eyelids' in args.mode or 'eyelashes' in args.mode:
in_data = fut.facial_features_preprocess(raw_img, lm, 'eyes')
if 'eyelids' in args.mode:
preds = models['ax_eyelids'].predict(in_data)[0]
scores = softmax(preds)
res_i['eyelids'] = scores
if 'eyelashes' in args.mode:
preds = models['ax_eyelashes'].predict(in_data)[0]
scores = softmax(preds)
res_i['eyelashes'] = scores
if 'facial_hair' in args.mode:
in_data = fut.facial_features_preprocess(raw_img, lm, 'face')
preds = models['ax_facial_hair'].predict(in_data)[0]
res_i['facial_hair'] = preds
res.append(res_i)
return res
# ======================
# Main functions
# ======================
def recognize_from_image():
models = init()
if args.benchmark:
logger.info('BENCHMARK mode')
n = 5
timings = [None] * n
for i in range(n):
start = time.perf_counter()
for img_path in args.input:
img = imread(img_path)
res = predict(models, img)
print_results(res)
duration = time.perf_counter() - start
logger.info(f'\tailia processing time {round(duration * 1e3):.0f} ms')
timings[i] = duration
logger.info(
f'\tmean ailia processing time {round(np.mean(timings) * 1e3):.0f} '
f'ms ({n} run(s))'
)
else:
for img_path in args.input:
img = imread(img_path)
res = predict(models, img)
print_results(res)
# plot_results(img, res, horizontal_flip=True)
logger.info('Script finished successfully.')
def recognize_from_video():
models = init()
capture = get_capture(args.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 = get_writer(args.savepath, f_h, f_w)
else:
writer = None
frame_shown = False
while(True):
ret, frame = capture.read()
if (cv2.waitKey(1) & 0xFF == ord('q')) or not ret:
break
if frame_shown and cv2.getWindowProperty('frame', cv2.WND_PROP_VISIBLE) == 0:
break
# inference
res = predict(models, frame)
if len(res) > 0:
frame_draw = plot_results(frame, res)
else:
frame_draw = frame.copy()
if args.video == '0': # Flip horizontally if camera
if len(res) > 0:
visual_img = plot_results(frame, res, horizontal_flip=True)
else:
visual_img = np.ascontiguousarray(frame[:, ::-1])
else:
visual_img = frame_draw
cv2.imshow('frame', visual_img)
frame_shown = True
# save results
if writer is not None:
writer.write(frame_draw)
capture.release()
if writer is not None:
writer.release()
cv2.destroyAllWindows()
if writer is not None:
writer.release()
logger.info('Script finished successfully.')
def main():
# model files check and download
for model in MODELS.values():
check_and_download_models(
model['weight_path'], model['model_path'], model['remote_path']
)
if args.video is not None:
# video mode
recognize_from_video()
else:
# image mode
recognize_from_image()
if __name__ == '__main__':
main()