-
Notifications
You must be signed in to change notification settings - Fork 0
/
visual_mot.py
157 lines (144 loc) · 4.08 KB
/
visual_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
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
import sys
import os
import logging
import colorsys
from pathlib import Path
from subprocess import call
import tqdm
import hydra
import omegaconf
import numpy as np
import cv2
logger = logging.getLogger(__name__)
def generate_colors():
"""
Generate random colors.
To get visually distinct colors, generate them in HSV space then
convert to RGB.
"""
N = 30
brightness = 0.7
hsv = [(i / N, 1, brightness) for i in range(N)]
colors = list(map(lambda c: colorsys.hsv_to_rgb(*c), hsv))
perm = [
15,
13,
25,
12,
19,
8,
22,
24,
29,
17,
28,
20,
2,
27,
11,
26,
21,
4,
3,
18,
9,
5,
14,
1,
16,
0,
23,
7,
6,
10,
]
colors = [colors[idx] for idx in perm]
return colors
def visualize_sequences(
tracks, img_folder, output_folder, padding_num=6, create_video=True
):
colors = generate_colors()
max_frames_seq = tracks[:, 0].max()
previous_tid = {}
for t in tqdm.trange(1, max_frames_seq + 1):
filename_t = str(img_folder / f"{t:0{padding_num}d}")
if os.path.exists(filename_t + ".png"):
filename_t = filename_t + ".png"
elif os.path.exists(filename_t + ".jpg"):
filename_t = filename_t + ".jpg"
else:
logger.warning(
"Image file not found for " + filename_t + ".png/.jpg, continuing..."
)
continue
img = cv2.imread(filename_t)
framedata = tracks[tracks[:, 0] == t]
img_filled = img.copy()
for obj in framedata:
color = np.array(colors[obj[1] % len(colors)]) * 255
cv2.rectangle(
img,
obj[2:4],
obj[2:4] + obj[4:6],
color,
thickness=1,
lineType=cv2.LINE_AA,
)
if len(obj) >= 11:
gt_tid = obj[10]
if gt_tid in previous_tid and obj[1]!=previous_tid[gt_tid]:
cv2.rectangle(
img_filled,
obj[2:4],
obj[2:4] + obj[4:6],
color,
thickness=-1,
lineType=cv2.LINE_AA,
)
previous_tid[gt_tid] = obj[1]
img = cv2.addWeighted(img, 0.8, img_filled, 0.2, 0)
cv2.imwrite(str(output_folder / f"{t:0{padding_num}d}.jpg"), img)
if create_video:
os.chdir(output_folder)
call(
[
"ffmpeg",
"-framerate",
"10",
"-y",
"-i",
f"%0{padding_num}d.jpg",
"-c:v",
"libx264",
"-profile:v",
"high",
"-crf",
"20",
"-pix_fmt",
"yuv420p",
"-vf",
"pad='width=ceil(iw/2)*2:height=ceil(ih/2)*2'",
"output.mp4",
]
)
@hydra.main(config_path="configs", config_name="visual_mot")
def main(cfg: omegaconf.dictconfig.DictConfig) -> None:
logger.info(f"Configuration Parameters:\n {omegaconf.OmegaConf.to_yaml(cfg)}")
for key in cfg.seq_name:
tracks_file = hydra.utils.to_absolute_path(
cfg.tracks_pattern.format(seq_name=key)
)
tracks = np.loadtxt(tracks_file, delimiter=",").astype(int)
if tracks.shape[1] > 6:
tracks = tracks[(tracks[:, 7] <= 7)]
img_folder = Path(
hydra.utils.to_absolute_path(cfg.img_pattern.format(seq_name=key))
)
output_folder = Path(
hydra.utils.to_absolute_path(cfg.output_pattern.format(seq_name=key))
)
assert img_folder.exists()
output_folder.mkdir(parents=True, exist_ok=True)
visualize_sequences(tracks, img_folder, output_folder, cfg.padding_num)
if __name__ == "__main__":
main()