-
Notifications
You must be signed in to change notification settings - Fork 357
Expand file tree
/
Copy pathvis.py
More file actions
214 lines (165 loc) · 6.02 KB
/
vis.py
File metadata and controls
214 lines (165 loc) · 6.02 KB
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
import os
import cv2
import numpy as np
from matplotlib import cm
import matplotlib.pyplot as plt
def read_video_from_path(path):
try:
cap = cv2.VideoCapture(path)
except Exception as e:
print("Error opening video file: ", e)
return None
frames = []
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
frames.append(frame) # フレームをリストに追加
cap.release()
return np.stack(frames)
class Visualizer:
def __init__(
self,
pad_value: int = 0,
linewidth: int = 2,
show_first_frame: int = 10,
tracks_leave_trace: int = 0, # -1 for infinite
):
self.color_map = cm.get_cmap("gist_rainbow")
self.show_first_frame = show_first_frame
self.tracks_leave_trace = tracks_leave_trace
self.pad_value = pad_value
self.linewidth = linewidth
def visualize(
self,
video,
tracks,
visibility=None,
filename: str = "video",
query_frame=0,
opacity: float = 1.0,
):
video = pad(video,self.pad_value,255)
color_alpha = int(opacity * 255)
tracks = tracks + self.pad_value
res_video = self.draw_tracks_on_video(
video=video,
tracks=tracks,
visibility=visibility,
query_frame=query_frame,
color_alpha=color_alpha,
)
self.save_video(res_video, filename=filename)
return res_video
def save_video(self, video, filename):
wide_list = [video[:,i,:,:,:] for i in range(video.shape[1])]
wide_list = [np.transpose(wide[0],(1, 2, 0)) for wide in wide_list]
# Prepare the video file path
save_path = filename
# Create a writer object
height, width, channels = wide_list[0].shape
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # MP4コーデック
out = cv2.VideoWriter(save_path, fourcc, 30, (width, height))
# Write frames to the video file
for frame in wide_list[2:-1]:
out.write(frame)
out.release()
print(f"Video saved to {save_path}")
def draw_tracks_on_video(
self,
video,
tracks,
visibility = None,
query_frame=0,
color_alpha: int = 255,
):
B, T, C, H, W = video.shape
_, _, N, D = tracks.shape
segm_mask = None
assert D == 2
assert C == 3
video = np.transpose(video[0],(0, 2, 3, 1)).astype(np.uint8) # S, H, W, C
tracks = tracks[0].astype(np.int64)
res_video = []
# process input video
for rgb in video:
res_video.append(rgb.copy())
vector_colors = np.zeros((T, N, 3))
#if segm_mask is None:
y_min, y_max = (
tracks[query_frame, :, 1].min(),
tracks[query_frame, :, 1].max(),
)
norm = plt.Normalize(y_min, y_max)
for n in range(N):
query_frame_ = query_frame
color = self.color_map(norm(tracks[query_frame_, n, 1]))
color = np.array(color[:3])[None] * 255
vector_colors[:, n] = np.repeat(color, T, axis=0)
# draw tracks
if self.tracks_leave_trace != 0:
for t in range(query_frame + 1, T):
first_ind = (
max(0, t - self.tracks_leave_trace)
if self.tracks_leave_trace >= 0
else 0
)
curr_tracks = tracks[first_ind : t + 1]
curr_colors = vector_colors[first_ind : t + 1]
res_video[t] = self._draw_pred_tracks(
res_video[t],
curr_tracks,
curr_colors,
)
# draw points
for t in range(T):
img = np.uint8(res_video[t])
for i in range(N):
coord = (tracks[t, i, 0], tracks[t, i, 1])
visibile = True
if visibility is not None:
visibile = visibility[0, t, i]
if coord[0] != 0 and coord[1] != 0:
img = draw_circle(
img,
coord=coord,
radius=int(self.linewidth * 2),
color=vector_colors[t, i].astype(int),
visible=visibile,
color_alpha=color_alpha,
)
res_video[t] = np.array(img)
# construct the final rgb sequence
if self.show_first_frame > 0:
res_video = [res_video[0]] * self.show_first_frame + res_video[1:]
return np.transpose(np.stack(res_video),(0, 3, 1, 2))[np.newaxis, ...].astype(np.uint8)
def draw_ellipse(image, left_up_point, right_down_point, color, visible=True):
center = (
(left_up_point[0] + right_down_point[0]) // 2,
(left_up_point[1] + right_down_point[1]) // 2,
)
axes = (
abs(right_down_point[0] - left_up_point[0]) // 2,
abs(right_down_point[1] - left_up_point[1]) // 2,
)
thickness = -1 if visible else 2
color = tuple(map(int, color))
cv2.ellipse(image, center, axes, 0, 0, 360, color, thickness)
return image
def draw_circle(rgb, coord, radius, color=(255, 0, 0), visible=True, color_alpha=None):
# Create a draw object
# Calculate the bounding box of the circle
left_up_point = (coord[0] - radius, coord[1] - radius)
right_down_point = (coord[0] + radius, coord[1] + radius)
# Draw the circle
color = tuple(list(color) + [color_alpha if color_alpha is not None else 255])
rgb = draw_ellipse(rgb,left_up_point,right_down_point,color,True)
return rgb
def pad(video, pad_value, constant_value=255):
padding = ((0, 0),
(0, 0),
(0, 0),
(pad_value, pad_value),
(pad_value, pad_value))
padded_video = np.pad(video, pad_width=padding, mode='constant', constant_values=constant_value)
return padded_video