-
Notifications
You must be signed in to change notification settings - Fork 9
/
visualize.py
252 lines (205 loc) · 7.48 KB
/
visualize.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
"""
Copyright (C) 2023 ETH Zurich, Manuel Kaufmann
Script to visualize an EMDB sequence. Make sure to set the path of `EMDB_ROOT` and `SMPLX_MODELS` below.
Usage:
python visualize.py P8 68_outdoor_handstand
"""
import argparse
import glob
import os
import pickle as pkl
import cv2
import numpy as np
from aitviewer.configuration import CONFIG as C
from aitviewer.models.smpl import SMPLLayer
from aitviewer.renderables.billboard import Billboard
from aitviewer.renderables.lines import LinesTrail
from aitviewer.renderables.smpl import SMPLSequence
from aitviewer.scene.camera import OpenCVCamera
from aitviewer.viewer import Viewer
from configuration import (
EMDB_ROOT,
SMPL_SIDE_COLOR,
SMPL_SIDE_INDEX,
SMPL_SKELETON,
SMPLX_MODELS,
)
def draw_kp2d(kp2d, bboxes=None):
"""Draw 2D keypoints and bounding boxes on the image with OpenCV."""
def _draw_kp2d(img, current_frame_id):
current_kp2d = kp2d[current_frame_id].copy()
scale = img.shape[0] / 1000
# Draw lines.
for index in range(SMPL_SKELETON.shape[0]):
i, j = SMPL_SKELETON[index]
# color = SIDE_COLOR[max(SIDE_INDEX[i], SIDE_INDEX[j])]
cv2.line(
img,
tuple(current_kp2d[i, :2].astype(np.int32)),
tuple(current_kp2d[j, :2].astype(np.int32)),
(0, 0, 0),
int(scale * 3),
)
# Draw points.
for jth in range(0, kp2d.shape[1]):
color = SMPL_SIDE_COLOR[SMPL_SIDE_INDEX[jth]]
radius = scale * 5
out_color = (0, 0, 0)
in_color = color
img = cv2.circle(
img,
tuple(current_kp2d[jth, :2].astype(np.int32)),
int(radius * 1.4),
out_color,
-1,
)
img = cv2.circle(
img,
tuple(current_kp2d[jth, :2].astype(np.int32)),
int(radius),
in_color,
-1,
)
# Draw bounding box if available.
if bboxes is not None:
bbox = bboxes[current_frame_id]
x_min, y_min, x_max, y_max = (
int(bbox[0]),
int(bbox[1]),
int(bbox[2]),
int(bbox[3]),
)
cv2.rectangle(img, (x_min, y_min), (x_max, y_max), (255, 0, 0), 2)
return img
return _draw_kp2d
def draw_nothing(kp2d, bboxes=None):
"""Dummy function."""
def _draw_nothing(img, current_frame_id):
return img
return _draw_nothing
def get_camera_position(Rt):
"""Get the orientation and position of the camera in world space."""
pos = -np.transpose(Rt[:, :3, :3], axes=(0, 2, 1)) @ Rt[:, :3, 3:]
return pos.squeeze(-1)
def get_sequence_root(args):
"""Parse the path of the sequence to be visualized."""
sequence_id = "{:0>2d}".format(int(args.sequence))
candidates = glob.glob(os.path.join(EMDB_ROOT, args.subject, sequence_id + "*"))
if len(candidates) == 0:
raise ValueError(f"Could not find sequence {args.sequence} for subject {args.subject}.")
elif len(candidates) > 1:
raise ValueError(f"Sequence ID {args.sequence}* for subject {args.subject} is ambiguous.")
return candidates[0]
def main(args):
# Access EMDB data.
sequence_root = get_sequence_root(args)
data_file = glob.glob(os.path.join(sequence_root, "*_data.pkl"))[0]
with open(data_file, "rb") as f:
data = pkl.load(f)
# Set up SMPL layer.
gender = data["gender"]
smpl_layer = SMPLLayer(model_type="smpl", gender=gender)
# Create SMPL sequence.
smpl_seq = SMPLSequence(
data["smpl"]["poses_body"],
smpl_layer=smpl_layer,
poses_root=data["smpl"]["poses_root"],
betas=data["smpl"]["betas"].reshape((1, -1)),
trans=data["smpl"]["trans"],
name="EMDB Fit",
)
# Load 2D information.
kp2d = data["kp2d"]
bboxes = data["bboxes"]["bboxes"]
drawing_function = draw_kp2d if args.draw_2d else draw_nothing
# Load images.
image_dir = os.path.join(sequence_root, "images")
image_files = sorted(glob.glob(os.path.join(image_dir, "*.jpg")))
# Load camera information.
intrinsics = data["camera"]["intrinsics"]
extrinsics = data["camera"]["extrinsics"]
cols, rows = data["camera"]["width"], data["camera"]["height"]
# Create the viewer.
viewer_size = None
if args.view_from_camera:
target_height = 1080
width = int(target_height * cols / rows)
viewer_size = (width, target_height)
# If we view it from the camera drawing the 3D trajectories might be disturbing, suppress it.
args.draw_trajectories = False
viewer = Viewer(size=viewer_size)
# Prepare the camera.
intrinsics = np.repeat(intrinsics[np.newaxis, :, :], len(extrinsics), axis=0)
cameras = OpenCVCamera(intrinsics, extrinsics[:, :3], cols, rows, viewer=viewer, name="Camera")
# Display the images on a billboard.
raw_images_bb = Billboard.from_camera_and_distance(
cameras,
10.0,
cols,
rows,
image_files,
image_process_fn=drawing_function(kp2d, bboxes),
name="Image",
)
# Add everything to the scene.
viewer.scene.add(smpl_seq, cameras, raw_images_bb)
if args.draw_trajectories:
# Add a path trail for the SMPL root trajectory.
smpl_path = LinesTrail(
smpl_seq.joints[:, 0],
r_base=0.003,
color=(0, 0, 1, 1),
cast_shadow=False,
name="SMPL Trajectory",
)
# Add a path trail for the camera trajectory.
# A fixed path (i.e. not a trail), could also be enabled in the GUI on the camera node by clicking "Show path".
cam_pos = get_camera_position(extrinsics)
camera_path = LinesTrail(
cam_pos,
r_base=0.003,
color=(0.5, 0.5, 0.5, 1),
cast_shadow=False,
name="Camera Trajectory",
)
viewer.scene.add(smpl_path, camera_path)
# Remaining viewer setup.
if args.view_from_camera:
# We view the scene through the camera.
viewer.set_temp_camera(cameras)
# Hide all the GUI controls, they can be re-enabled by pressing `ESC`.
viewer.render_gui = False
else:
# We center the scene on the first frame of the SMPL sequence.
viewer.center_view_on_node(smpl_seq)
viewer.scene.origin.enabled = False
viewer.scene.floor.enabled = False
viewer.playback_fps = 30.0
viewer.run()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("subject", type=str, help="The subject ID, P0 - P9.")
parser.add_argument(
"sequence",
type=str,
help="The sequence ID. This can be any unambiguous prefix of the sequence's name, i.e. for the "
"sequence '66_outdoor_rom' it could be '66' or any longer prefix including the full name.",
)
parser.add_argument(
"--view_from_camera",
action="store_true",
help="View it from the camera's perspective.",
)
parser.add_argument(
"--draw_2d",
action="store_true",
help="Draw 2D keypoints and bounding boxes on the image.",
)
parser.add_argument(
"--draw_trajectories",
action="store_true",
help="Render SMPL and camera trajectories.",
)
args = parser.parse_args()
C.update_conf({"smplx_models": SMPLX_MODELS})
main(args)