forked from yjh0410/RT-ODLab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
track.py
428 lines (365 loc) · 16.5 KB
/
track.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
import os
import cv2
import time
import imageio
import argparse
import numpy as np
import torch
from dataset.build import build_transform
from utils.vis_tools import plot_tracking
from utils.misc import load_weight
from utils.box_ops import rescale_bboxes
from config import build_model_config, build_trans_config
from models.detectors import build_model
from models.trackers import build_tracker
os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"
IMAGE_EXT = [".jpg", ".jpeg", ".webp", ".bmp", ".png"]
def parse_args():
parser = argparse.ArgumentParser(description='Tracking Task')
# basic
parser.add_argument('-size', '--img_size', default=640, type=int,
help='the max size of input image')
parser.add_argument('--cuda', action='store_true', default=False,
help='use cuda.')
# data
parser.add_argument('--mode', type=str, default='image',
help='image, video or camera')
parser.add_argument('--path_to_img', type=str, default='dataset/demo/images/',
help='Dir to load images')
parser.add_argument('--path_to_vid', type=str, default='dataset/demo/videos/',
help='Dir to load a video')
parser.add_argument('--path_to_save', default='det_results/', type=str,
help='Dir to save results')
parser.add_argument('--fps', type=int, default=30,
help='frame rate')
parser.add_argument('--show', action='store_true', default=False,
help='show results.')
parser.add_argument('--save', action='store_true', default=False,
help='save results.')
parser.add_argument('--gif', action='store_true', default=False,
help='generate gif.')
# tracker
parser.add_argument('-tk', '--tracker', default='byte_tracker', type=str,
help='build FreeTrack')
parser.add_argument("--track_thresh", type=float, default=0.4,
help="tracking confidence threshold")
parser.add_argument("--track_buffer", type=int, default=30,
help="the frames for keep lost tracks")
parser.add_argument("--match_thresh", type=float, default=0.8,
help="matching threshold for tracking")
parser.add_argument("--aspect_ratio_thresh", type=float, default=1.6,
help="threshold for filtering out boxes of which \
aspect ratio are above the given value.")
parser.add_argument('--min_box_area', type=float, default=10,
help='filter out tiny boxes')
parser.add_argument("--mot20", default=False, action="store_true",
help="test mot20.")
# detector
parser.add_argument('-dt', '--model', default='yolov1', type=str,
help='build YOLO')
parser.add_argument('-ns', '--num_classes', type=int, default=80,
help='number of object classes.')
parser.add_argument('--weight', default=None,
type=str, help='Trained state_dict file path to open')
parser.add_argument('-ct', '--conf_thresh', default=0.3, type=float,
help='confidence threshold')
parser.add_argument('-nt', '--nms_thresh', default=0.5, type=float,
help='NMS threshold')
parser.add_argument('--topk', default=100, type=int,
help='topk candidates for testing')
parser.add_argument('-fcb', '--fuse_conv_bn', action='store_true', default=False,
help='fuse Conv & BN')
return parser.parse_args()
def get_image_list(path):
image_names = []
for maindir, subdir, file_name_list in os.walk(path):
for filename in file_name_list:
apath = os.path.join(maindir, filename)
ext = os.path.splitext(apath)[1]
if ext in IMAGE_EXT:
image_names.append(apath)
return image_names
def run(args,
tracker,
detector,
device,
transform):
save_path = os.path.join(args.path_to_save, 'tracking', args.mode)
os.makedirs(save_path, exist_ok=True)
# ------------------------- Camera ----------------------------
if args.mode == 'camera':
print('use camera !!!')
# Launch camera
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
frame_id = 0
results = []
# For saving
fourcc = cv2.VideoWriter_fourcc(*'XVID')
save_size = (640, 480)
cur_time = time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time()))
save_video_name = os.path.join(save_path, cur_time+'.avi')
fps = 15.0
out = cv2.VideoWriter(save_video_name, fourcc, fps, save_size)
print(save_video_name)
image_list = []
# start tracking
while True:
ret, frame = cap.read()
if ret:
if cv2.waitKey(1) == ord('q'):
break
# ------------------------- Detection ---------------------------
# preprocess
x, _, deltas = transform(frame)
x = x.unsqueeze(0).to(device) / 255.
orig_h, orig_w, _ = frame.shape
# detect
t0 = time.time()
bboxes, scores, labels = detector(x)
print("=============== Frame-{} ================".format(frame_id))
print("detect time: {:.1f} ms".format((time.time() - t0)*1000))
# rescale bboxes
origin_img_size = [orig_h, orig_w]
cur_img_size = [*x.shape[-2:]]
bboxes = rescale_bboxes(bboxes, origin_img_size, cur_img_size, deltas)
# track
t2 = time.time()
if len(bboxes) > 0:
online_targets = tracker.update(scores, bboxes, labels)
online_xywhs = []
online_ids = []
online_scores = []
for t in online_targets:
xywh = t.xywh
tid = t.track_id
vertical = xywh[2] / xywh[3] > args.aspect_ratio_thresh
if xywh[2] * xywh[3] > args.min_box_area and not vertical:
online_xywhs.append(xywh)
online_ids.append(tid)
online_scores.append(t.score)
results.append(
f"{frame_id},{tid},{xywh[0]:.2f},{xywh[1]:.2f},{xywh[2]:.2f},{xywh[3]:.2f},{t.score:.2f},-1,-1,-1\n"
)
print("tracking time: {:.1f} ms".format((time.time() - t2)*1000))
# plot tracking results
online_im = plot_tracking(
frame, online_xywhs, online_ids, frame_id=frame_id + 1, fps=1. / (time.time() - t0)
)
else:
online_im = frame
frame_resized = cv2.resize(online_im, save_size)
out.write(frame_resized)
if args.gif:
gif_resized = cv2.resize(online_im, (640, 480))
gif_resized_rgb = gif_resized[..., (2, 1, 0)]
image_list.append(gif_resized_rgb)
# show results
if args.show:
cv2.imshow('tracking', online_im)
ch = cv2.waitKey(1)
if ch == 27 or ch == ord("q") or ch == ord("Q"):
break
else:
break
frame_id += 1
cap.release()
out.release()
cv2.destroyAllWindows()
# generate GIF
if args.gif:
save_gif_path = os.path.join(save_path, 'gif_files')
os.makedirs(save_gif_path, exist_ok=True)
save_gif_name = os.path.join(save_gif_path, '{}.gif'.format(cur_time))
print('generating GIF ...')
imageio.mimsave(save_gif_name, image_list, fps=fps)
print('GIF done: {}'.format(save_gif_name))
# ------------------------- Video ---------------------------
elif args.mode == 'video':
# read a video
video = cv2.VideoCapture(args.path_to_vid)
fps = video.get(cv2.CAP_PROP_FPS)
# For saving
fourcc = cv2.VideoWriter_fourcc(*'XVID')
save_size = (640, 480)
cur_time = time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time()))
save_video_name = os.path.join(save_path, cur_time+'.avi')
out = cv2.VideoWriter(save_video_name, fourcc, fps, save_size)
print(save_video_name)
image_list = []
# start tracking
frame_id = 0
results = []
while(True):
ret, frame = video.read()
if ret:
# ------------------------- Detection ---------------------------
# preprocess
x, _, deltas = transform(frame)
x = x.unsqueeze(0).to(device) / 255.
orig_h, orig_w, _ = frame.shape
# detect
t0 = time.time()
bboxes, scores, labels = detector(x)
print("=============== Frame-{} ================".format(frame_id))
print("detect time: {:.1f} ms".format((time.time() - t0)*1000))
# rescale bboxes
origin_img_size = [orig_h, orig_w]
cur_img_size = [*x.shape[-2:]]
bboxes = rescale_bboxes(bboxes, origin_img_size, cur_img_size, deltas)
# track
t2 = time.time()
if len(bboxes) > 0:
online_targets = tracker.update(scores, bboxes, labels)
online_xywhs = []
online_ids = []
online_scores = []
for t in online_targets:
xywh = t.xywh
tid = t.track_id
vertical = xywh[2] / xywh[3] > args.aspect_ratio_thresh
if xywh[2] * xywh[3] > args.min_box_area and not vertical:
online_xywhs.append(xywh)
online_ids.append(tid)
online_scores.append(t.score)
results.append(
f"{frame_id},{tid},{xywh[0]:.2f},{xywh[1]:.2f},{xywh[2]:.2f},{xywh[3]:.2f},{t.score:.2f},-1,-1,-1\n"
)
print("tracking time: {:.1f} ms".format((time.time() - t2)*1000))
# plot tracking results
online_im = plot_tracking(
frame, online_xywhs, online_ids, frame_id=frame_id + 1, fps=1. / (time.time() - t0)
)
else:
online_im = frame
frame_resized = cv2.resize(online_im, save_size)
out.write(frame_resized)
if args.gif:
gif_resized = cv2.resize(online_im, (640, 480))
gif_resized_rgb = gif_resized[..., (2, 1, 0)]
image_list.append(gif_resized_rgb)
# show results
if args.show:
cv2.imshow('tracking', online_im)
ch = cv2.waitKey(1)
if ch == 27 or ch == ord("q") or ch == ord("Q"):
break
else:
break
frame_id += 1
video.release()
out.release()
cv2.destroyAllWindows()
# generate GIF
if args.gif:
save_gif_path = os.path.join(save_path, 'gif_files')
os.makedirs(save_gif_path, exist_ok=True)
save_gif_name = os.path.join(save_gif_path, '{}.gif'.format(cur_time))
print('generating GIF ...')
imageio.mimsave(save_gif_name, image_list, fps=fps)
print('GIF done: {}'.format(save_gif_name))
# ------------------------- Image ----------------------------
elif args.mode == 'image':
files = get_image_list(args.path_to_img)
files.sort()
# For saving
fourcc = cv2.VideoWriter_fourcc(*'XVID')
save_size = (640, 480)
cur_time = time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time()))
save_video_name = os.path.join(save_path, cur_time+'.avi')
out = cv2.VideoWriter(save_video_name, fourcc, fps, save_size)
print(save_video_name)
image_list = []
# start tracking
frame_id = 0
results = []
for frame_id, img_path in enumerate(files, 1):
image = cv2.imread(os.path.join(img_path))
# preprocess
x, _, deltas = transform(image)
x = x.unsqueeze(0).to(device) / 255.
orig_h, orig_w, _ = image.shape
# detect
t0 = time.time()
bboxes, scores, labels = detector(x)
print("=============== Frame-{} ================".format(frame_id))
print("detect time: {:.1f} ms".format((time.time() - t0)*1000))
# rescale bboxes
origin_img_size = [orig_h, orig_w]
cur_img_size = [*x.shape[-2:]]
bboxes = rescale_bboxes(bboxes, origin_img_size, cur_img_size, deltas)
# track
t2 = time.time()
if len(bboxes) > 0:
online_targets = tracker.update(scores, bboxes, labels)
online_xywhs = []
online_ids = []
online_scores = []
for t in online_targets:
xywh = t.xywh
tid = t.track_id
vertical = xywh[2] / xywh[3] > args.aspect_ratio_thresh
if xywh[2] * xywh[3] > args.min_box_area and not vertical:
online_xywhs.append(xywh)
online_ids.append(tid)
online_scores.append(t.score)
results.append(
f"{frame_id},{tid},{xywh[0]:.2f},{xywh[1]:.2f},{xywh[2]:.2f},{xywh[3]:.2f},{t.score:.2f},-1,-1,-1\n"
)
print("tracking time: {:.1f} ms".format((time.time() - t2)*1000))
# plot tracking results
online_im = plot_tracking(
image, online_xywhs, online_ids, frame_id=frame_id + 1, fps=1. / (time.time() - t0)
)
else:
online_im = frame
frame_resized = cv2.resize(online_im, save_size)
out.write(frame_resized)
if args.gif:
gif_resized = cv2.resize(online_im, (640, 480))
gif_resized_rgb = gif_resized[..., (2, 1, 0)]
image_list.append(gif_resized_rgb)
# show results
if args.show:
cv2.imshow('tracking', online_im)
ch = cv2.waitKey(1)
if ch == 27 or ch == ord("q") or ch == ord("Q"):
break
frame_id += 1
cv2.destroyAllWindows()
out.release()
cv2.destroyAllWindows()
# generate GIF
if args.gif:
save_gif_path = os.path.join(save_path, 'gif_files')
os.makedirs(save_gif_path, exist_ok=True)
save_gif_name = os.path.join(save_gif_path, '{}.gif'.format(cur_time))
print('generating GIF ...')
imageio.mimsave(save_gif_name, image_list, fps=fps)
print('GIF done: {}'.format(save_gif_name))
if __name__ == '__main__':
args = parse_args()
# cuda
if args.cuda:
print('use cuda')
device = torch.device("cuda")
else:
device = torch.device("cpu")
np.random.seed(0)
# config
model_cfg = build_model_config(args)
trans_cfg = build_trans_config(model_cfg['trans_type'])
# transform
transform = build_transform(args.img_size, trans_cfg, is_train=False)
# ---------------------- General Object Detector ----------------------
detector = build_model(args, model_cfg, device, args.num_classes, False)
## load trained weight
detector = load_weight(detector, args.weight, args.fuse_conv_bn)
detector.to(device).eval()
# ---------------------- General Object Tracker ----------------------
tracker = build_tracker(args)
# run
run(args=args,
tracker=tracker,
detector=detector,
device=device,
transform=transform)