-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo_face_crop_onnx_tflite.py
1094 lines (981 loc) · 36.6 KB
/
demo_face_crop_onnx_tflite.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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
"""
runtime: https://github.com/microsoft/onnxruntime
pip install onnxruntime or pip install onnxruntime-gpu
pip install opencv-contrib-python==4.9.0.80
"""
from __future__ import annotations
import os
import re
import sys
import copy
import cv2
import requests
import subprocess
import numpy as np
from enum import Enum
from dataclasses import dataclass
from argparse import ArgumentParser
from typing import Tuple, Optional, List, Dict
import importlib.util
from abc import ABC, abstractmethod
from glob import glob
class Color(Enum):
BLACK = '\033[30m'
RED = '\033[31m'
GREEN = '\033[32m'
YELLOW = '\033[33m'
BLUE = '\033[34m'
MAGENTA = '\033[35m'
CYAN = '\033[36m'
WHITE = '\033[37m'
COLOR_DEFAULT = '\033[39m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
INVISIBLE = '\033[08m'
REVERSE = '\033[07m'
BG_BLACK = '\033[40m'
BG_RED = '\033[41m'
BG_GREEN = '\033[42m'
BG_YELLOW = '\033[43m'
BG_BLUE = '\033[44m'
BG_MAGENTA = '\033[45m'
BG_CYAN = '\033[46m'
BG_WHITE = '\033[47m'
BG_DEFAULT = '\033[49m'
RESET = '\033[0m'
def __str__(self):
return self.value
def __call__(self, s):
return str(self) + str(s) + str(Color.RESET)
@dataclass(frozen=False)
class Box():
classid: int
score: float
looked_score: float
x1: int
y1: int
x2: int
y2: int
landmarks: np.ndarray
class AbstractModel(ABC):
"""AbstractModel
Base class of the model.
"""
_runtime: str = 'onnx'
_model_path: str = ''
_input_shapes: List[List[int]] = []
_input_names: List[str] = []
_output_shapes: List[List[int]] = []
_output_names: List[str] = []
_mean: np.ndarray = np.array([0.000, 0.000, 0.000], dtype=np.float32)
_std: np.ndarray = np.array([1.000, 1.000, 1.000], dtype=np.float32)
# onnx/tflite
_interpreter = None
_inference_model = None
_providers = None
_swap: Tuple = (2, 0, 1)
_h_index: int = 2
_w_index: int = 3
_norm_shape: List = [1,3,1,1]
_class_score_th: float
# onnx
_onnx_dtypes_to_np_dtypes = {
"tensor(float)": np.float32,
"tensor(uint8)": np.uint8,
"tensor(int8)": np.int8,
"tensor(int64)": np.int64,
"tensor(int32)": np.int32,
}
# tflite
_input_details = None
_output_details = None
@abstractmethod
def __init__(
self,
*,
runtime: Optional[str] = 'onnx',
model_path: Optional[str] = '',
providers: Optional[List] = [
(
'TensorrtExecutionProvider', {
'trt_engine_cache_enable': True,
'trt_engine_cache_path': '.',
'trt_fp16_enable': True,
}
),
'CUDAExecutionProvider',
'CPUExecutionProvider',
],
mean: Optional[np.ndarray] = np.array([0.000, 0.000, 0.000], dtype=np.float32),
std: Optional[np.ndarray] = np.array([1.000, 1.000, 1.000], dtype=np.float32),
class_score_th: float = 0.35,
):
self._runtime = runtime
self._model_path = model_path
self._providers = providers
# Model loading
if self._runtime == 'onnx':
import onnxruntime # type: ignore
session_option = onnxruntime.SessionOptions()
session_option.log_severity_level = 3
self._interpreter = \
onnxruntime.InferenceSession(
model_path,
sess_options=session_option,
providers=providers,
)
self._providers = self._interpreter.get_providers()
self._input_shapes = [
input.shape for input in self._interpreter.get_inputs()
]
self._input_names = [
input.name for input in self._interpreter.get_inputs()
]
self._input_dtypes = [
self._onnx_dtypes_to_np_dtypes[input.type] for input in self._interpreter.get_inputs()
]
self._output_shapes = [
output.shape for output in self._interpreter.get_outputs()
]
self._output_names = [
output.name for output in self._interpreter.get_outputs()
]
self._model = self._interpreter.run
self._swap = (2, 0, 1)
self._h_index = 2
self._w_index = 3
self._norm_shape = [1,3,1,1]
elif self._runtime in ['tflite_runtime', 'tensorflow']:
if self._runtime == 'tflite_runtime':
from tflite_runtime.interpreter import Interpreter # type: ignore
self._interpreter = Interpreter(model_path=model_path)
elif self._runtime == 'tensorflow':
import tensorflow as tf # type: ignore
self._interpreter = tf.lite.Interpreter(model_path=model_path)
self._input_details = self._interpreter.get_input_details()
self._output_details = self._interpreter.get_output_details()
self._input_shapes = [
input.get('shape', None) for input in self._input_details
]
self._input_names = [
input.get('name', None) for input in self._input_details
]
self._input_dtypes = [
input.get('dtype', None) for input in self._input_details
]
self._output_shapes = [
output.get('shape', None) for output in self._output_details
]
self._output_names = [
output.get('name', None) for output in self._output_details
]
self._model = self._interpreter.get_signature_runner()
self._swap = (0, 1, 2)
self._h_index = 1
self._w_index = 2
self._norm_shape = [1,1,1,3]
self._mean = mean.reshape(self._norm_shape)
self._std = std.reshape(self._norm_shape)
self._class_score_th = class_score_th
@abstractmethod
def __call__(
self,
*,
input_datas: List[np.ndarray],
) -> List[np.ndarray]:
datas = {
f'{input_name}': input_data \
for input_name, input_data in zip(self._input_names, input_datas)
}
if self._runtime == 'onnx':
outputs = [
output for output in \
self._model(
output_names=self._output_names,
input_feed=datas,
)
]
return outputs
elif self._runtime in ['tflite_runtime', 'tensorflow']:
outputs = [
output for output in \
self._model(
**datas
).values()
]
return outputs
@abstractmethod
def _preprocess(
self,
*,
image: np.ndarray,
swap: Optional[Tuple[int,int,int]] = (2, 0, 1),
) -> np.ndarray:
raise NotImplementedError()
class YOLOX(AbstractModel):
def __init__(
self,
*,
runtime: Optional[str] = 'onnx',
model_path: Optional[str] = 'yolox_n_body_head_hand_post_0461_0.4428_1x3x480x640.onnx',
class_score_th: Optional[float] = 0.35,
providers: Optional[List] = None,
):
"""YOLOX
Parameters
----------
runtime: Optional[str]
Runtime for YOLOX. Default: onnx
model_path: Optional[str]
ONNX/TFLite file path for YOLOX
class_score_th: Optional[float]
Score threshold. Default: 0.35
providers: Optional[List]
Providers for ONNXRuntime.
"""
super().__init__(
runtime=runtime,
model_path=model_path,
class_score_th=class_score_th,
providers=providers,
)
def __call__(
self,
image: np.ndarray,
) -> List[Box]:
"""YOLOX
Parameters
----------
image: np.ndarray
Entire image
Returns
-------
boxes: np.ndarray
Predicted boxes: [N, x1, y1, x2, y2]
scores: np.ndarray
Predicted box scores: [N, score]
"""
temp_image = copy.deepcopy(image)
# PreProcess
resized_image = \
self._preprocess(
temp_image,
)
# Inference
inferece_image = np.asarray([resized_image], dtype=self._input_dtypes[0])
outputs = super().__call__(input_datas=[inferece_image])
boxes = outputs[0]
# PostProcess
result_boxes = \
self._postprocess(
image=temp_image,
boxes=boxes,
)
return result_boxes
def _preprocess(
self,
image: np.ndarray,
) -> np.ndarray:
"""_preprocess
Parameters
----------
image: np.ndarray
Entire image
swap: tuple
HWC to CHW: (2,0,1)
CHW to HWC: (1,2,0)
HWC to HWC: (0,1,2)
CHW to CHW: (0,1,2)
Returns
-------
resized_image: np.ndarray
Resized and normalized image.
"""
# Resize + Transpose
resized_image = cv2.resize(
image,
(
int(self._input_shapes[0][self._w_index]),
int(self._input_shapes[0][self._h_index]),
)
)
resized_image = resized_image.transpose(self._swap)
return resized_image
def _postprocess(
self,
image: np.ndarray,
boxes: np.ndarray,
) -> List[Box]:
"""_postprocess
Parameters
----------
image: np.ndarray
Entire image.
boxes: np.ndarray
float32[N, 7]
Returns
-------
result_boxes: List[Box]
Predicted boxes: [classid, score, x1, y1, x2, y2]
"""
"""
Detector is
N -> Number of boxes detected
batchno -> always 0: BatchNo.0
batchno_classid_score_x1y1x2y2: float32[N,7]
"""
image_height = image.shape[0]
image_width = image.shape[1]
result_boxes: List[Box] = []
if len(boxes) > 0:
scores = boxes[:, 2:3]
keep_idxs = scores[:, 0] > self._class_score_th
scores_keep = scores[keep_idxs, :]
boxes_keep = boxes[keep_idxs, :]
if len(boxes_keep) > 0:
for box, score in zip(boxes_keep, scores_keep):
x_min = int(max(0, box[3]) * image_width / self._input_shapes[0][self._w_index])
y_min = int(max(0, box[4]) * image_height / self._input_shapes[0][self._h_index])
x_max = int(min(box[5], self._input_shapes[0][self._w_index]) * image_width / self._input_shapes[0][self._w_index])
y_max = int(min(box[6], self._input_shapes[0][self._h_index]) * image_height / self._input_shapes[0][self._h_index])
result_boxes.append(
Box(
classid=int(box[1]),
score=float(score),
looked_score=0.0,
x1=x_min,
y1=y_min,
x2=x_max,
y2=y_max,
landmarks=None,
)
)
return result_boxes
class RetinaFace(AbstractModel):
def __init__(
self,
*,
runtime: Optional[str] = 'onnx',
model_path: Optional[str] = 'retinaface_mbn025_with_postprocess_480x640_max20_th0.70.onnx',
class_score_th: Optional[float] = 0.15,
providers: Optional[List] = None,
):
"""RetinaFace
Parameters
----------
runtime: Optional[str]
Runtime for YOLOX. Default: onnx
model_path: Optional[str]
ONNX/TFLite file path for YOLOX
class_score_th: Optional[float]
Score threshold. Default: 0.35
providers: Optional[List]
Providers for ONNXRuntime.
"""
super().__init__(
runtime=runtime,
model_path=model_path,
class_score_th=class_score_th,
mean=np.asarray([104, 117, 123], dtype=np.float32),
providers=providers,
)
def __call__(
self,
image: np.ndarray,
boxes: List[Box],
) -> List[Box]:
"""
Parameters
----------
image: np.ndarray
Entire image
boxes: List[Box]
Head boxes
Returns
-------
batchno_classid_score_x1y1x2y2_landms: np.ndarray
[N, [batchno, classid, score, x1, y1, x2, y2, landms0, ..., landms9]]
"""
temp_image = copy.deepcopy(image)
temp_boxes = copy.deepcopy(boxes)
# PreProcess
inferece_images = \
self._preprocess(
image=temp_image,
boxes=temp_boxes,
)
# Inference
outputs = super().__call__(input_datas=[inferece_images])
batchno_classid_score_x1y1x2y2_landms = outputs[0]
# PostProcess
face_boxes = \
self._postprocess(
face_boxes=batchno_classid_score_x1y1x2y2_landms,
head_boxes=temp_boxes,
)
return face_boxes
def _preprocess(
self,
image: np.ndarray,
boxes: List[Box],
swap: Optional[Tuple[int,int,int,int]] = (0, 3, 1, 2),
) -> np.ndarray:
"""_preprocess
Parameters
----------
image: np.ndarray
Entire image
swap: tuple
Returns
-------
resized_image: np.ndarray
Resized and normalized image.
"""
cropped_boxes = [image[box.y1:box.y2, box.x1:box.x2, :] for box in boxes]
# Normalization + BGR->RGB
resized_image_list: List[np.ndarray] = []
for cropped_box in cropped_boxes:
h, w, c = cropped_box.shape
if h > 0 and w > 0:
resized_image = cv2.resize(
cropped_box,
(
int(self._input_shapes[0][self._w_index]),
int(self._input_shapes[0][self._h_index]),
)
)
resized_image = resized_image[..., ::-1] # BGR->RGB
resized_image_list.append(resized_image)
resized_images = np.asarray(resized_image_list, dtype=self._input_dtypes[0])
resized_images = resized_images.transpose(swap)
resized_images = (resized_images - self._mean)
return resized_images
def _postprocess(
self,
face_boxes: np.ndarray,
head_boxes: List[Box],
) -> List[Box]:
"""_postprocess
Parameters
----------
boxes: np.ndarray
float32[N, 7]
Returns
-------
result_boxes: List[Box]
Predicted boxes: [classid, score, x1, y1, x2, y2]
"""
"""
Detector is
N -> Number of boxes detected
batchno -> always 0: BatchNo.0
batchno_classid_score_x1y1x2y2: float32[N,7]
"""
result_boxes: List[Box] = []
if len(face_boxes) > 0:
scores = face_boxes[:, 2:3]
keep_idxs = scores[:, 0] > self._class_score_th
scores_keep = scores[keep_idxs, :]
boxes_keep = face_boxes[keep_idxs, :]
if len(boxes_keep) > 0:
for box, score in zip(boxes_keep, scores_keep):
batchno = int(box[0])
head_w = abs(head_boxes[batchno].x2 - head_boxes[batchno].x1)
head_h = abs(head_boxes[batchno].y2 - head_boxes[batchno].y1)
x_min = int(max(0, box[3]) * head_w / self._input_shapes[0][self._w_index]) + head_boxes[batchno].x1
y_min = int(max(0, box[4]) * head_h / self._input_shapes[0][self._h_index]) + head_boxes[batchno].y1
x_max = int(min(box[5], self._input_shapes[0][self._w_index]) * head_w / self._input_shapes[0][self._w_index]) + head_boxes[batchno].x1
y_max = int(min(box[6], self._input_shapes[0][self._h_index]) * head_h / self._input_shapes[0][self._h_index]) + head_boxes[batchno].y1
landmarks: np.ndarray = box[7:]
landmarks = landmarks.reshape(-1, 2).astype(np.int32)
landmarks[:, 0] = landmarks[:, 0] * head_w / self._input_shapes[0][self._w_index] + head_boxes[batchno].x1
landmarks[:, 1] = landmarks[:, 1] * head_h / self._input_shapes[0][self._h_index] + head_boxes[batchno].y1
result_boxes.append(
Box(
classid=int(box[1]),
score=float(score),
looked_score=0.0,
x1=x_min,
y1=y_min,
x2=x_max,
y2=y_max,
landmarks=landmarks,
)
)
return result_boxes
class DBFace(AbstractModel):
def __init__(
self,
*,
runtime: Optional[str] = 'onnx',
model_path: Optional[str] = 'dbface_Nx3x192x192_post_max100_th030.onnx',
class_score_th: Optional[float] = 0.15,
providers: Optional[List] = None,
):
"""DBFace
Parameters
----------
runtime: Optional[str]
Runtime for DBFace. Default: onnx
model_path: Optional[str]
ONNX/TFLite file path for DBFace
class_score_th: Optional[float]
Score threshold. Default: 0.15
providers: Optional[List]
Providers for ONNXRuntime.
"""
super().__init__(
runtime=runtime,
model_path=model_path,
class_score_th=class_score_th,
mean=np.asarray([0.408, 0.447, 0.470], dtype=np.float32),
std=np.asarray([0.289, 0.274, 0.278], dtype=np.float32),
providers=providers,
)
def __call__(
self,
image: np.ndarray,
boxes: List[Box],
) -> List[Box]:
"""
Parameters
----------
image: np.ndarray
Entire image
boxes: List[Box]
Head boxes
Returns
-------
batchno_classid_score_x1y1x2y2_landms: np.ndarray
[N, [batchno, classid, score, x1, y1, x2, y2, landms0, ..., landms9]]
"""
temp_image = copy.deepcopy(image)
temp_boxes = copy.deepcopy(boxes)
# PreProcess
inferece_images = \
self._preprocess(
image=temp_image,
boxes=temp_boxes,
)
# Inference
outputs = super().__call__(input_datas=[inferece_images])
batchno_classid_score_x1y1x2y2_landms = outputs[0]
# PostProcess
face_boxes = \
self._postprocess(
face_boxes=batchno_classid_score_x1y1x2y2_landms,
head_boxes=temp_boxes,
)
return face_boxes
def _preprocess(
self,
image: np.ndarray,
boxes: List[Box],
swap: Optional[Tuple[int,int,int,int]] = (0, 3, 1, 2),
) -> np.ndarray:
"""_preprocess
Parameters
----------
image: np.ndarray
Entire image
swap: tuple
Returns
-------
resized_image: np.ndarray
Resized and normalized image.
"""
cropped_boxes = [image[box.y1:box.y2, box.x1:box.x2, :] for box in boxes]
# Normalization + BGR->RGB
resized_image_list: List[np.ndarray] = []
for cropped_box in cropped_boxes:
h, w, c = cropped_box.shape
if h > 0 and w > 0:
resized_image = cv2.resize(
cropped_box,
(
int(self._input_shapes[0][self._w_index]),
int(self._input_shapes[0][self._h_index]),
)
)
# resized_image = resized_image[..., ::-1] # BGR->RGB
resized_image_list.append(resized_image)
resized_images = np.asarray(resized_image_list, dtype=self._input_dtypes[0])
resized_images = resized_images.transpose(swap)
resized_images = (resized_images - self._mean)
return resized_images
def _postprocess(
self,
face_boxes: np.ndarray,
head_boxes: List[Box],
) -> List[Box]:
"""_postprocess
Parameters
----------
boxes: np.ndarray
float32[N, 7]
Returns
-------
result_boxes: List[Box]
Predicted boxes: [classid, score, x1, y1, x2, y2]
"""
"""
Detector is
N -> Number of boxes detected
batchno -> always 0: BatchNo.0
batchno_classid_score_x1y1x2y2: float32[N,7]
"""
result_boxes: List[Box] = []
if len(face_boxes) > 0:
scores = face_boxes[:, 2:3]
keep_idxs = scores[:, 0] > self._class_score_th
scores_keep = scores[keep_idxs, :]
boxes_keep = face_boxes[keep_idxs, :]
if len(boxes_keep) > 0:
for box, score in zip(boxes_keep, scores_keep):
batchno = int(box[0])
head_w = abs(head_boxes[batchno].x2 - head_boxes[batchno].x1)
head_h = abs(head_boxes[batchno].y2 - head_boxes[batchno].y1)
x_min = int(max(0, box[3]) * head_w / self._input_shapes[0][self._w_index]) + head_boxes[batchno].x1
y_min = int(max(0, box[4]) * head_h / self._input_shapes[0][self._h_index]) + head_boxes[batchno].y1
x_max = int(min(box[5], self._input_shapes[0][self._w_index]) * head_w / self._input_shapes[0][self._w_index]) + head_boxes[batchno].x1
y_max = int(min(box[6], self._input_shapes[0][self._h_index]) * head_h / self._input_shapes[0][self._h_index]) + head_boxes[batchno].y1
landmarks: np.ndarray = box[7:]
landmarks = landmarks.reshape(-1, 2).astype(np.int32)
landmarks[:, 0] = landmarks[:, 0] * head_w / self._input_shapes[0][self._w_index] + head_boxes[batchno].x1
landmarks[:, 1] = landmarks[:, 1] * head_h / self._input_shapes[0][self._h_index] + head_boxes[batchno].y1
result_boxes.append(
Box(
classid=int(box[1]),
score=float(score),
looked_score=0.0,
x1=x_min,
y1=y_min,
x2=x_max,
y2=y_max,
landmarks=landmarks,
)
)
return result_boxes
def is_parsable_to_int(s):
try:
int(s)
return True
except ValueError:
return False
def is_package_installed(package_name: str):
"""Checks if the specified package is installed.
Parameters
----------
package_name: str
Name of the package to be checked.
Returns
-------
result: bool
True if the package is installed, false otherwise.
"""
return importlib.util.find_spec(package_name) is not None
def download_file(url, folder, filename):
"""
Download a file from a URL and save it to a specified folder.
If the folder does not exist, it is created.
:param url: URL of the file to download.
:param folder: Folder where the file will be saved.
:param filename: Filename to save the file.
"""
# Create the folder if it does not exist
if not os.path.exists(folder):
os.makedirs(folder)
# Full path for the file
file_path = os.path.join(folder, filename)
# Download the file
print(f"{Color.GREEN('Downloading...')} {url} to {file_path}")
response = requests.get(url, stream=True)
if response.status_code == 200:
with open(file_path, 'wb') as file:
for chunk in response.iter_content(chunk_size=8192):
file.write(chunk)
print(f"{Color.GREEN('Download completed:')} {file_path}")
else:
print(f"Failed to download. Status code: {response.status_code}")
def get_nvidia_gpu_model() -> List[str]:
try:
# Run nvidia-smi command
output = subprocess.check_output(["nvidia-smi", "-L"], text=True)
# Extract GPU model numbers using regular expressions
models = re.findall(r'GPU \d+: (.*?)(?= \(UUID)', output)
return models
except Exception as e:
print(f"Error: {e}")
return []
def draw_dashed_line(
image: np.ndarray,
pt1: Tuple[int, int],
pt2: Tuple[int, int],
color: Tuple[int, int, int],
thickness: int = 1,
dash_length: int = 10,
):
"""Function to draw a dashed line"""
dist = ((pt1[0] - pt2[0]) ** 2 + (pt1[1] - pt2[1]) ** 2) ** 0.5
dashes = int(dist / dash_length)
for i in range(dashes):
start = [int(pt1[0] + (pt2[0] - pt1[0]) * i / dashes), int(pt1[1] + (pt2[1] - pt1[1]) * i / dashes)]
end = [int(pt1[0] + (pt2[0] - pt1[0]) * (i + 0.5) / dashes), int(pt1[1] + (pt2[1] - pt1[1]) * (i + 0.5) / dashes)]
cv2.line(image, tuple(start), tuple(end), color, thickness)
def draw_dashed_rectangle(
image: np.ndarray,
top_left: Tuple[int, int],
bottom_right: Tuple[int, int],
color: Tuple[int, int, int],
thickness: int = 1,
dash_length: int = 10
):
"""Function to draw a dashed rectangle"""
tl_tr = (bottom_right[0], top_left[1])
bl_br = (top_left[0], bottom_right[1])
draw_dashed_line(image, top_left, tl_tr, color, thickness, dash_length)
draw_dashed_line(image, tl_tr, bottom_right, color, thickness, dash_length)
draw_dashed_line(image, bottom_right, bl_br, color, thickness, dash_length)
draw_dashed_line(image, bl_br, top_left, color, thickness, dash_length)
def calculate_reduced_area(
*,
y1: int,
y2: int,
x1: int,
x2: int,
scale: float=0.80,
) -> Tuple[int, int, int, int]:
"""
Calculate the coordinates of a reduced area centered around the same center point
as the original area, but with a scale factor (default is 80%).
Parameters:
y1, y2, x1, x2 (int): Coordinates of the original area.
scale (float): Scale factor for the size reduction (default is 0.8 for 80%).
Returns:
tuple: Coordinates of the reduced area (y1', y2', x1', x2').
"""
# Calculate the center of the original area
center_y = (y1 + y2) / 2
center_x = (x1 + x2) / 2
# Calculate the height and width of the original area
height = y2 - y1
width = x2 - x1
# Calculate the height and width of the new, reduced area
new_height = height * scale
new_width = width * scale
# Calculate the coordinates of the new area
y1_prime = int(center_y - new_height / 2 * 0.7)
y2_prime = int(center_y + new_height / 2)
x1_prime = int(center_x - new_width / 2 * 0.8)
x2_prime = int(center_x + new_width / 2 * 0.8)
return y1_prime, y2_prime, x1_prime, x2_prime
def main():
parser = ArgumentParser()
parser.add_argument(
'-hdm',
'--head_detection_model',
type=str,
default='yolox_m_body_head_hand_post_0299_0.5263_1x3x480x640.onnx',
choices=[
'yolox_n_body_head_hand_post_0461_0.4428_1x3x480x640.onnx',
'yolox_t_body_head_hand_post_0299_0.4522_1x3x480x640.onnx',
'yolox_s_body_head_hand_post_0299_0.4983_1x3x480x640.onnx',
'yolox_m_body_head_hand_post_0299_0.5263_1x3x480x640.onnx',
'yolox_l_body_head_hand_0299_0.5420_post_1x3x480x640.onnx',
'yolox_x_body_head_hand_0102_0.5533_post_1x3x480x640.onnx',
],
help='ONNX/TFLite file path for YOLOX.',
)
parser.add_argument(
'-fdm',
'--face_detection_model',
type=str,
default='retinaface_resnet50_with_postprocess_Nx3x96x96_max001_th015.onnx',
choices=[
'retinaface_mbn025_with_postprocess_Nx3x64x64_max001_th0.15.onnx',
'retinaface_mbn025_with_postprocess_Nx3x96x96_max001_th0.15.onnx',
'retinaface_mbn025_with_postprocess_Nx3x128x128_max001_th0.15.onnx',
'retinaface_mbn025_with_postprocess_Nx3x160x160_max001_th0.15.onnx',
'retinaface_mbn025_with_postprocess_Nx3x192x192_max001_th0.15.onnx',
'retinaface_mbn025_with_postprocess_Nx3x224x224_max001_th0.15.onnx',
'retinaface_mbn025_with_postprocess_Nx3x256x256_max001_th0.15.onnx',
'retinaface_resnet50_with_postprocess_Nx3x64x64_max001_th015.onnx',
'retinaface_resnet50_with_postprocess_Nx3x96x96_max001_th015.onnx',
'retinaface_resnet50_with_postprocess_Nx3x128x128_max001_th015.onnx',
'retinaface_resnet50_with_postprocess_Nx3x160x160_max001_th015.onnx',
'retinaface_resnet50_with_postprocess_Nx3x192x192_max001_th015.onnx',
'retinaface_resnet50_with_postprocess_Nx3x224x224_max001_th015.onnx',
'retinaface_resnet50_with_postprocess_Nx3x256x256_max001_th015.onnx',
],
help='ONNX/TFLite file path for RetinaFace.',
)
parser.add_argument(
'-v',
'--video',
type=str,
default="0",
help='Video file path or camera index.',
)
parser.add_argument(
'-ep',
'--execution_provider',
type=str,
choices=['cpu', 'cuda', 'tensorrt'],
default='cuda',
help='Execution provider for ONNXRuntime.',
)
parser.add_argument(
'-dvw',
'--disable_video_writer',
action='store_true',
help=\
'Disable video writer. '+
'Eliminates the file I/O load associated with automatic recording to MP4. '+
'Devices that use a MicroSD card or similar for main storage can speed up overall processing.',
)
args = parser.parse_args()
# runtime check
head_detection_model_file: str = args.head_detection_model
head_detection_model_ext: str = os.path.splitext(head_detection_model_file)[1][1:].lower()
face_detection_model_file: str = args.face_detection_model
face_detection_model_ext: str = os.path.splitext(face_detection_model_file)[1][1:].lower()
if head_detection_model_ext != face_detection_model_ext:
print(Color.RED('ERROR: head_detection_model and face_detection_model must be files with the same extension.'))
sys.exit(0)
runtime: str = None
if head_detection_model_ext == 'onnx':
if not is_package_installed('onnxruntime'):
print(Color.RED('ERROR: onnxruntime is not installed. pip install onnxruntime or pip install onnxruntime-gpu'))
sys.exit(0)
runtime = 'onnx'
elif head_detection_model_ext == 'tflite':
if is_package_installed('tflite_runtime'):
runtime = 'tflite_runtime'
elif is_package_installed('tensorflow'):
runtime = 'tensorflow'
else:
print(Color.RED('ERROR: tflite_runtime or tensorflow is not installed.'))
print(Color.RED('ERROR: https://github.com/PINTO0309/TensorflowLite-bin'))
print(Color.RED('ERROR: https://github.com/tensorflow/tensorflow'))
sys.exit(0)
WEIGHT_FOLDER_PATH = '.'
# Download head detection onnx
weight_file = os.path.basename(head_detection_model_file)
if not os.path.isfile(os.path.join(WEIGHT_FOLDER_PATH, weight_file)):
url = f"https://github.com/PINTO0309/NITEC-ONNX-TensorRT/releases/download/onnx/{weight_file}"
download_file(url=url, folder=WEIGHT_FOLDER_PATH, filename=weight_file)
# Download face detection onnx
weight_file = os.path.basename(face_detection_model_file)
if not os.path.isfile(os.path.join(WEIGHT_FOLDER_PATH, weight_file)):
url = f"https://github.com/PINTO0309/NITEC-ONNX-TensorRT/releases/download/onnx/{weight_file}"
download_file(url=url, folder=WEIGHT_FOLDER_PATH, filename=weight_file)
execution_provider: str = args.execution_provider
providers: List[Tuple[str, Dict] | str] = None
if execution_provider == 'cpu':
providers = [
'CPUExecutionProvider',
]
elif execution_provider == 'cuda':
providers = [
'CUDAExecutionProvider',
'CPUExecutionProvider',
]
elif execution_provider == 'tensorrt':
providers = [
(
'TensorrtExecutionProvider', {
'trt_engine_cache_enable': True,
'trt_engine_cache_path': '.',
'trt_fp16_enable': True,
}
),
'CUDAExecutionProvider',
'CPUExecutionProvider',
]
# Model initialization
head_detection_model = \
YOLOX(
runtime=runtime,
model_path=head_detection_model_file,
providers=providers,
)