forked from open-mmlab/mmtracking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo_mot.py
94 lines (84 loc) · 2.96 KB
/
demo_mot.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
import os
import os.path as osp
import tempfile
from argparse import ArgumentParser
import mmcv
from mmtrack.apis import inference_mot, init_model
def main():
parser = ArgumentParser()
parser.add_argument('config', help='config file')
parser.add_argument('--input', help='input video file or folder')
parser.add_argument(
'--output', help='output video file (mp4 format) or folder')
parser.add_argument('--checkpoint', help='checkpoint file')
parser.add_argument(
'--device', default='cuda:0', help='device used for inference')
parser.add_argument(
'--show',
action='store_true',
help='whether show the results on the fly')
parser.add_argument(
'--backend',
choices=['cv2', 'plt'],
default='cv2',
help='the backend to visualize the results')
parser.add_argument('--fps', help='FPS of the output video')
args = parser.parse_args()
assert args.output or args.show
# load images
if osp.isdir(args.input):
imgs = sorted(os.listdir(args.input))
IN_VIDEO = False
else:
imgs = mmcv.VideoReader(args.input)
IN_VIDEO = True
# define output
if args.output is not None:
if args.output.endswith('.mp4'):
OUT_VIDEO = True
out_dir = tempfile.TemporaryDirectory()
out_path = out_dir.name
_out = args.output.rsplit('/', 1)
if len(_out) > 1:
os.makedirs(_out[0], exist_ok=True)
else:
OUT_VIDEO = False
out_path = args.output
os.makedirs(out_path, exist_ok=True)
fps = args.fps
if args.show or OUT_VIDEO:
if fps is None and IN_VIDEO:
fps = imgs.fps
if not fps:
raise ValueError('Please set the FPS for the output video.')
fps = int(fps)
# build the model from a config file and a checkpoint file
model = init_model(args.config, args.checkpoint, device=args.device)
prog_bar = mmcv.ProgressBar(len(imgs))
# test and show/save the images
for i, img in enumerate(imgs):
if isinstance(img, str):
img = osp.join(args.input, img)
result = inference_mot(model, img, frame_id=i)
result = result['track_results']
if args.output is not None:
if IN_VIDEO or OUT_VIDEO:
out_file = osp.join(out_path, f'{i:06d}.jpg')
else:
out_file = osp.join(out_path, img.rsplit('/', 1)[-1])
else:
out_file = None
model.show_result(
img,
result,
show=args.show,
wait_time=int(1000. / fps) if fps else 0,
out_file=out_file,
backend=args.backend)
prog_bar.update()
if OUT_VIDEO:
print(f'making the output video at {args.output} with a FPS of {fps}')
mmcv.frames2video(out_path, args.output, fps=fps)
out_dir.cleanup()
if __name__ == '__main__':
main()