-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsample_tflite.py
179 lines (146 loc) · 4.61 KB
/
sample_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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import copy
import time
import argparse
import cv2 as cv
from yunet.yunet_tflite import YuNetTFLite
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument("--device", type=int, default=0)
parser.add_argument("--movie", type=str, default=None)
parser.add_argument("--width", help='cap width', type=int, default=960)
parser.add_argument("--height", help='cap height', type=int, default=540)
parser.add_argument(
"--model",
type=str,
default='model/model_float16_quant.tflite',
)
parser.add_argument(
'--input_shape',
type=str,
default="160,120",
help="Specify an input shape for inference.",
)
parser.add_argument(
'--score_th',
type=float,
default=0.6,
help='Conf confidence',
)
parser.add_argument(
'--nms_th',
type=float,
default=0.3,
help='NMS IoU threshold',
)
parser.add_argument(
'--topk',
type=int,
default=5000,
)
parser.add_argument(
'--keep_topk',
type=int,
default=750,
)
args = parser.parse_args()
return args
def main():
# 引数解析 #################################################################
args = get_args()
cap_device = args.device
cap_width = args.width
cap_height = args.height
if args.movie is not None:
cap_device = args.movie
model_path = args.model
input_shape = tuple(map(int, args.input_shape.split(',')))
score_th = args.score_th
nms_th = args.nms_th
topk = args.topk
keep_topk = args.keep_topk
# カメラ準備 ###############################################################
cap = cv.VideoCapture(cap_device)
cap.set(cv.CAP_PROP_FRAME_WIDTH, cap_width)
cap.set(cv.CAP_PROP_FRAME_HEIGHT, cap_height)
# モデルロード #############################################################
yunet = YuNetTFLite(
model_path=model_path,
input_shape=input_shape,
conf_th=score_th,
nms_th=nms_th,
topk=topk,
keep_topk=keep_topk,
)
while True:
start_time = time.time()
# カメラキャプチャ ################################################
ret, frame = cap.read()
if not ret:
break
debug_image = copy.deepcopy(frame)
# 推論実施 ########################################################
bboxes, landmarks, scores = yunet.inference(frame)
elapsed_time = time.time() - start_time
# デバッグ描画
debug_image = draw_debug(
debug_image,
elapsed_time,
score_th,
input_shape,
bboxes,
landmarks,
scores,
)
# キー処理(ESC:終了) ##############################################
key = cv.waitKey(1)
if key == 27: # ESC
break
# 画面反映 #########################################################
cv.imshow('YuNet TFLite Sample', debug_image)
cap.release()
cv.destroyAllWindows()
def draw_debug(
image,
elapsed_time,
score_th,
input_shape,
bboxes,
landmarks,
scores,
):
image_width, image_height = image.shape[1], image.shape[0]
debug_image = copy.deepcopy(image)
for bbox, landmark, score in zip(bboxes, landmarks, scores):
if score_th > score:
continue
# 顔バウンディングボックス
x1 = int(image_width * (bbox[0] / input_shape[0]))
y1 = int(image_height * (bbox[1] / input_shape[1]))
x2 = int(image_width * (bbox[2] / input_shape[0])) + x1
y2 = int(image_height * (bbox[3] / input_shape[1])) + y1
cv.rectangle(debug_image, (x1, y1), (x2, y2), (0, 255, 0), 2)
# スコア
cv.putText(debug_image, '{:.4f}'.format(score), (x1, y1 + 12),
cv.FONT_HERSHEY_DUPLEX, 0.5, (0, 255, 0))
# 顔キーポイント
for _, landmark_point in enumerate(landmark):
x = int(image_width * (landmark_point[0] / input_shape[0]))
y = int(image_height * (landmark_point[1] / input_shape[1]))
cv.circle(debug_image, (x, y), 2, (0, 255, 0), 2)
# 推論時間
text = 'Elapsed time:' + '%.0f' % (elapsed_time * 1000)
text = text + 'ms'
debug_image = cv.putText(
debug_image,
text,
(10, 30),
cv.FONT_HERSHEY_SIMPLEX,
0.7,
(0, 255, 0),
thickness=2,
)
return debug_image
if __name__ == '__main__':
main()