-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.py
214 lines (173 loc) · 4.79 KB
/
main.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
import copy
import time
import argparse
import cv2
from Detector.detector import ObjectDetector
from Tracker.tracker import 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(
'--detector',
choices=[
'yolox',
'efficientdet',
'ssd',
'centernet',
'nanodet',
'mediapipe_face',
'mediapipe_hand',
'light_person_detector',
],
default='yolox',
)
parser.add_argument(
'--tracker',
choices=[
'motpy',
'bytetrack',
'mc_bytetrack',
'norfair',
'mc_norfair',
'person_reid',
'youtureid',
'sface',
'vehicle_reid',
],
default='motpy',
)
parser.add_argument("--target_id", type=str, default=None)
parser.add_argument('--use_gpu', action='store_true')
args = parser.parse_args()
return args
def main():
args = get_args()
cap_device = args.device
if args.movie is not None:
cap_device = args.movie
detector_name = args.detector
tracker_name = args.tracker
target_id = args.target_id
if target_id is not None:
target_id = [int(i) for i in target_id.split(',')]
use_gpu = args.use_gpu
# VideoCapture初期化
cap = cv2.VideoCapture(cap_device)
cap_fps = cap.get(cv2.CAP_PROP_FPS)
# Object Detection
detector = ObjectDetector(
detector_name,
target_id,
use_gpu=use_gpu,
)
detector.print_info()
# Multi Object Tracking
tracker = MultiObjectTracker(
tracker_name,
cap_fps,
use_gpu=use_gpu,
)
tracker.print_info()
# トラッキング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
d_bboxes, d_scores, d_class_ids = detector(frame)
# Multi Object Tracking
track_ids, t_bboxes, t_scores, t_class_ids = tracker(
frame,
d_bboxes,
d_scores,
d_class_ids,
)
# トラッキングIDと連番の紐付け
for track_id in track_ids:
if track_id not in track_id_dict:
new_id = len(track_id_dict)
track_id_dict[track_id] = new_id
elapsed_time = time.time() - start_time
# 描画
debug_image = draw_debug_info(
debug_image,
elapsed_time,
track_ids,
t_bboxes,
t_scores,
t_class_ids,
track_id_dict,
)
key = cv2.waitKey(1)
if key == 27: # ESC
break
cv2.imshow('MOT Tracking by Detection Pipeline Sample', debug_image)
cap.release()
cv2.destroyAllWindows()
def get_id_color(index):
temp_index = abs(int(index + 1)) * 3
color = ((37 * temp_index) % 255, (17 * temp_index) % 255,
(29 * temp_index) % 255)
return color
def draw_debug_info(
debug_image,
elapsed_time,
track_ids,
bboxes,
scores,
class_ids,
track_id_dict,
):
for id, bbox, score, class_id in zip(track_ids, bboxes, scores, class_ids):
x1, y1, x2, y2 = int(bbox[0]), int(bbox[1]), int(bbox[2]), int(bbox[3])
color = get_id_color(track_id_dict[id])
# バウンディングボックス
debug_image = cv2.rectangle(
debug_image,
(x1, y1),
(x2, y2),
color,
thickness=2,
)
# トラックID、スコア
score = '%.2f' % score
text = 'TID:%s(%s)' % (str(int(track_id_dict[id])), str(score))
debug_image = cv2.putText(
debug_image,
text,
(x1, y1 - 22),
cv2.FONT_HERSHEY_SIMPLEX,
0.5,
color,
thickness=2,
)
# クラスID
text = 'CID:%s' % (str(int(class_id)))
debug_image = cv2.putText(
debug_image,
text,
(x1, y1 - 8),
cv2.FONT_HERSHEY_SIMPLEX,
0.5,
color,
thickness=2,
)
# 経過時間(キャプチャ、物体検出、トラッキング)
cv2.putText(
debug_image,
"Elapsed Time : " + '{:.1f}'.format(elapsed_time * 1000) + "ms",
(10, 30),
cv2.FONT_HERSHEY_SIMPLEX,
0.7,
(0, 255, 0),
1,
cv2.LINE_AA,
)
return debug_image
if __name__ == '__main__':
main()