forked from aharley/pips
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw.py
246 lines (212 loc) · 6.82 KB
/
draw.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
import cv2
import numpy as np
import torch
import torch.nn.functional as F
from PIL import Image
import sys
sys.path.append("../pips/")
import pips_utils.improc
def draw_thumbnails(
rgbs,
deps,
mask_locs,
trajs,
trajs_rev=None,
anchor_idx=None,
ncol=10,
keypoints_to_plot=0,
):
"""
Draw thumbnail for of each keypoint for each frame.
Inputs:
rgbs: array of rgb frames (n_timestep, 3, W, H)
deps: array of dep frames (n_timestep, 3, W, H)
mask_locs: list of mask indices (from mask R-CNN)
trajs: array of trajectories (n_timestep, n_keypoints, 2)
trajs_rev: array of reverse trajectories in order (n_timestep, n_keypoints, 2)
anchor_idx: index of anchor frame (if applicable, default = None)
ncol: number of columns in thumbnail grid
Outputs:
thumbnails: list of thumbnails for each keypoint
"""
# Resize rgbs
# rgbs = np.transpose(rgbs, [0, 2, 3, 1])
rgbs = rgbs[:, :, :, [2, 1, 0]]
# deps = np.transpose(deps, [0, 2, 3, 1])
pad = np.full(
(50, (20 + rgbs.shape[2]) * ncol, rgbs.shape[3] + 1),
(255, 255, 255, 255),
)
# Annotate frames
def ann_keypoint(k):
anns, row = [], []
for i in range(rgbs.shape[0]):
rgb = draw_masked(rgbs[i], mask_locs[i])
dep = draw_masked(deps[i], mask_locs[i])
ann_fwd = draw_keypoint(rgb, trajs[i, k], radius=15)
dep = draw_keypoint(dep, trajs[i, k], radius=15)
frame = np.concatenate([dep, ann_fwd], axis=0)
if trajs_rev is not None:
ann_rev = draw_keypoint(rgb, trajs_rev[i, k], radius=15)
frame = np.concatenate([frame, ann_rev], axis=0)
frame = cv2.copyMakeBorder(
frame,
10,
10,
10,
10,
cv2.BORDER_CONSTANT,
value=(0, 0, 255, 255) if i == anchor_idx else (255, 255, 255, 255),
)
row += [frame]
if i < rgbs.shape[0] - 1 and len(row) == ncol:
strip = np.concatenate(row, axis=1)
anns += [np.concatenate([strip, pad], axis=0)]
row = []
# Add last row
strip = np.concatenate(row, axis=1)
empty = np.full(
(strip.shape[0], pad.shape[1] - strip.shape[1], strip.shape[2]),
(255, 255, 255, 255),
)
anns += [np.concatenate([strip, empty], axis=1)]
anns = np.concatenate(anns, axis=0)
return anns
thumbnails = []
idx_list = range(trajs.shape[1]) if keypoints_to_plot is None else keypoints_to_plot
for k in idx_list:
thumbnails.append(ann_keypoint(k))
return thumbnails
def draw_keypoint(rgb, pt, radius=4, color=(255, 0, 255, 255), fill=True):
"""
Draw keypoint on frame.
Inputs:
rgb: rgb frame
pt: array (2,) specifying point center
radius: radius of point
color: color of point
Outputs:
rgb: annotated rgb frame
"""
if not np.isnan(pt).any():
pt = pt.astype(int)
rgb = cv2.circle(
rgb.copy(),
pt,
radius,
color,
-1 if fill else 1,
)
return rgb
def draw_frame_multi_pts(rgb, pts, radius=4, color=(255, 0, 255)):
"""
Draw keypoint on frame.
Inputs:
rgb: rgb frame
pts: array (num,2) specifying point center
radius: radius of point
color: color of point
Outputs:
rgb: annotated rgb frame
"""
for pt_idx in range(pts.shape[0]):
pt = pts[pt_idx]
if not np.isnan(pt).any():
rgb = cv2.circle(
rgb.copy(),
(round(pt[0]), round(pt[1])),
radius,
color,
-1,
)
return rgb
def draw_trajectory(rgb, traj, color=(255, 0, 255), arrow_size=4):
"""
Draw trajectory on frame.
Inputs:
rgb: rgb frame
traj: array (n_timestep, n_keypoints, 2) of trajectories
color: color of arrow lines
arrow_size: size of arrow tip (in px)
Outputs:
rgb: annotated rgb frame
"""
for k in range(traj.shape[1]):
for i in range(1, traj.shape[0]):
src = traj[i - 1, k]
dst = traj[i, k]
if not np.isnan(src).any():
arrow_frac = arrow_size / max(1, np.linalg.norm(dst - src, axis=0))
if not np.isnan(dst).any():
rgb = cv2.arrowedLine(
rgb.copy(),
(round(src[0]), round(src[1])),
(round(dst[0]), round(dst[1])),
color,
2,
tipLength=arrow_frac,
)
return rgb
def draw_masked(im, mask_locs):
im = cv2.cvtColor(im, cv2.COLOR_BGR2BGRA)
im[:, :, 3] = 100
im[mask_locs[0], mask_locs[1], 3] = 255
return im
def draw_pips_animation(pips, trajs, rgbs, deps, linewidth=1, pad=50):
# Add extra dimension if needed
if trajs.dim() < 4:
trajs = trajs.unsqueeze(0)
_, S, _, H, W = rgbs.shape
rgbs = F.pad(
rgbs.reshape(S, 3, H, W),
(pad, pad, pad, pad),
"constant",
0,
).reshape(1, S, 3, H + pad * 2, W + pad * 2)
deps = F.pad(
deps.reshape(1 * S, 3, H, W),
(pad, pad, pad, pad),
"constant",
0,
).reshape(1, S, 3, H + pad * 2, W + pad * 2)
trajs = trajs + pad
# visualize the input RGB
o1 = pips.sw.summ_rgbs(
"inputs/rgbs",
pips_utils.improc.preprocess_color(rgbs[0:1]).unbind(1),
only_return=True,
)
# visualize trajs overlaid on the disparity map
o1a = pips.sw.summ_traj2ds_on_rgbs(
"inputs/trajs_on_dep",
trajs[0:1],
pips_utils.improc.preprocess_color(deps[0:1]),
cmap="spring",
linewidth=linewidth,
only_return=True,
)
# visualize the trajs overlaid on the rgbs
o2 = pips.sw.summ_traj2ds_on_rgbs(
"outputs/trajs_on_rgbs",
trajs[0:1],
pips_utils.improc.preprocess_color(rgbs[0:1]),
cmap="spring",
linewidth=linewidth,
only_return=True,
)
# visualize the trajs alone
o3 = pips.sw.summ_traj2ds_on_rgbs(
"outputs/trajs_on_black",
trajs[0:1],
torch.ones_like(rgbs[0:1]) * -0.5,
cmap="spring",
linewidth=linewidth,
only_return=True,
)
# concat these for a synced wide vis
wide_cat = torch.cat([o1, o1a, o2, o3], dim=-1)
# write to disk, in case that's more convenient
wide_list = list(wide_cat.unbind(1))
wide_list = [wide[0].permute(1, 2, 0).cpu().numpy() for wide in wide_list]
wide_list = [Image.fromarray(wide) for wide in wide_list]
return wide_list