-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample.py
307 lines (264 loc) · 7.64 KB
/
sample.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import copy
import time
import argparse
import cv2
from yolox.yolox_onnx import YoloxONNX
from motpy import Detection, MultiObjectTracker
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)
# YOLOX parameters
parser.add_argument(
"--yolox_model",
type=str,
default='model/yolox_nano.onnx',
)
parser.add_argument(
'--input_shape',
type=str,
default="416,416",
help="Specify an input shape for inference.",
)
parser.add_argument(
'--score_th',
type=float,
default=0.3,
help='Class confidence',
)
parser.add_argument(
'--nms_th',
type=float,
default=0.45,
help='NMS IoU threshold',
)
parser.add_argument(
'--nms_score_th',
type=float,
default=0.1,
help='NMS Score threshold',
)
parser.add_argument(
"--with_p6",
action="store_true",
help="Whether your model uses p6 in FPN/PAN.",
)
# motpy parameters
parser.add_argument(
"--max_staleness",
type=int,
default=5,
)
parser.add_argument(
"--order_pos",
type=int,
default=1,
)
parser.add_argument(
"--dim_pos",
type=int,
default=2,
)
parser.add_argument(
"--order_size",
type=int,
default=0,
)
parser.add_argument(
"--dim_size",
type=int,
default=2,
)
parser.add_argument(
"--q_var_pos",
type=float,
default=5000.0,
)
parser.add_argument(
"--r_var_pos",
type=float,
default=0.1,
)
parser.add_argument(
"--tracker_min_iou",
type=float,
default=0.25,
)
parser.add_argument(
"--multi_match_min_iou",
type=float,
default=0.93,
)
parser.add_argument(
"--min_steps_alive",
type=int,
default=3,
)
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
# YOLOX parameters
model_path = args.yolox_model
input_shape = tuple(map(int, args.input_shape.split(',')))
score_th = args.score_th
nms_th = args.nms_th
nms_score_th = args.nms_score_th
with_p6 = args.with_p6
# motpy parameters
max_staleness = args.max_staleness
order_pos = args.order_pos
dim_pos = args.dim_pos
order_size = args.order_size
dim_size = args.dim_size
q_var_pos = args.q_var_pos
r_var_pos = args.r_var_pos
tracker_min_iou = args.tracker_min_iou
multi_match_min_iou = args.multi_match_min_iou
min_steps_alive = args.min_steps_alive
# カメラ準備 ###############################################################
cap = cv2.VideoCapture(cap_device)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, cap_width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, cap_height)
cap_fps = cap.get(cv2.CAP_PROP_FPS)
# モデルロード #############################################################
# Object Detection
yolox = YoloxONNX(
model_path=model_path,
input_shape=input_shape,
class_score_th=score_th,
nms_th=nms_th,
nms_score_th=nms_score_th,
with_p6=with_p6,
providers=['CPUExecutionProvider'],
)
# Multi Object Tracking
tracker = MultiObjectTracker(
dt=1 / cap_fps,
tracker_kwargs={'max_staleness': max_staleness},
model_spec={
'order_pos': order_pos,
'dim_pos': dim_pos,
'order_size': order_size,
'dim_size': dim_size,
'q_var_pos': q_var_pos,
'r_var_pos': r_var_pos
},
matching_fn_kwargs={
'min_iou': tracker_min_iou,
'multi_match_min_iou': multi_match_min_iou
},
)
# COCOクラスリスト読み込み
with open('coco_classes.txt', 'rt') as f:
coco_classes = f.read().rstrip('\n').split('\n')
# トラッキングID保持用変数
track_id_dict = {}
while True:
start_time = time.time()
# カメラキャプチャ ################################################
ret, frame = cap.read()
if not ret:
break
debug_image = copy.deepcopy(frame)
# 推論実施 ########################################################
# Object Detection
bboxes, scores, class_ids = yolox.inference(frame)
detections = [
Detection(box=b, score=s, class_id=l)
for b, s, l in zip(bboxes, scores, class_ids)
]
# Multi Object Tracking
_ = tracker.step(detections=detections)
track_results = tracker.active_tracks(min_steps_alive=min_steps_alive)
# トラッキングIDと連番の紐付け
for track_result in track_results:
if track_result.id not in track_id_dict:
new_id = len(track_id_dict)
track_id_dict[track_result.id] = new_id
elapsed_time = time.time() - start_time
# デバッグ描画
debug_image = draw_debug(
debug_image,
elapsed_time,
score_th,
track_results,
track_id_dict,
coco_classes,
)
# キー処理(ESC:終了) ##############################################
key = cv2.waitKey(1)
if key == 27: # ESC
break
# 画面反映 #########################################################
cv2.imshow('YOLOX motpy Sample', debug_image)
cap.release()
cv2.destroyAllWindows()
def get_id_color(index):
temp_index = abs(int(index)) * 3
color = ((37 * temp_index) % 255, (17 * temp_index) % 255,
(29 * temp_index) % 255)
return color
def draw_debug(
image,
elapsed_time,
score_th,
track_results,
track_id_dict,
coco_classes,
):
debug_image = copy.deepcopy(image)
for track_result in track_results:
tracker_id = track_id_dict[track_result.id]
bbox = track_result.box
class_id = int(track_result.class_id)
score = track_result.score
x1, y1, x2, y2 = int(bbox[0]), int(bbox[1]), int(bbox[2]), int(bbox[3])
if score_th > score:
continue
color = get_id_color(tracker_id)
# バウンディングボックス
debug_image = cv2.rectangle(
debug_image,
(x1, y1),
(x2, y2),
color,
thickness=2,
)
# クラスID、スコア
score = '%.2f' % score
text = '%s:%s' % (str(coco_classes[int(class_id)]), score)
debug_image = cv2.putText(
debug_image,
text,
(x1, y1 - 10),
cv2.FONT_HERSHEY_SIMPLEX,
0.7,
color,
thickness=2,
)
# 推論時間
text = 'Elapsed time:' + '%.0f' % (elapsed_time * 1000)
text = text + 'ms'
debug_image = cv2.putText(
debug_image,
text,
(10, 30),
cv2.FONT_HERSHEY_SIMPLEX,
0.7,
(0, 255, 0),
thickness=2,
)
return debug_image
if __name__ == '__main__':
main()