-
Notifications
You must be signed in to change notification settings - Fork 13
/
2_run_hamer_on_vrs.py
237 lines (210 loc) · 7.75 KB
/
2_run_hamer_on_vrs.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
"""Script to run HaMeR on VRS data and save outputs to a pickle file."""
import pickle
import shutil
from pathlib import Path
import cv2
import imageio.v3 as iio
import numpy as np
import tyro
from egoallo.hand_detection_structs import (
SavedHamerOutputs,
SingleHandHamerOutputWrtCamera,
)
from hamer_helper import HamerHelper
from projectaria_tools.core import calibration
from projectaria_tools.core.data_provider import (
VrsDataProvider,
create_vrs_data_provider,
)
from tqdm.auto import tqdm
from egoallo.inference_utils import InferenceTrajectoryPaths
def main(traj_root: Path, overwrite: bool = False) -> None:
"""Run HaMeR for on trajectory. We'll save outputs to
`traj_root/hamer_outputs.pkl` and `traj_root/hamer_outputs_render".
Arguments:
traj_root: The root directory of the trajectory. We assume that there's
a VRS file in this directory.
overwrite: If True, overwrite any existing HaMeR outputs.
"""
paths = InferenceTrajectoryPaths.find(traj_root)
vrs_path = paths.vrs_file
assert vrs_path.exists()
pickle_out = traj_root / "hamer_outputs.pkl"
hamer_render_out = traj_root / "hamer_outputs_render" # This is just for debugging.
run_hamer_and_save(vrs_path, pickle_out, hamer_render_out, overwrite)
def run_hamer_and_save(
vrs_path: Path, pickle_out: Path, hamer_render_out: Path, overwrite: bool
) -> None:
if not overwrite:
assert not pickle_out.exists()
assert not hamer_render_out.exists()
else:
pickle_out.unlink(missing_ok=True)
shutil.rmtree(hamer_render_out, ignore_errors=True)
hamer_render_out.mkdir(exist_ok=True)
hamer_helper = HamerHelper()
# VRS data provider setup.
provider = create_vrs_data_provider(str(vrs_path.absolute()))
assert isinstance(provider, VrsDataProvider)
rgb_stream_id = provider.get_stream_id_from_label("camera-rgb")
assert rgb_stream_id is not None
num_images = provider.get_num_data(rgb_stream_id)
print(f"Found {num_images=}")
# Get calibrations.
device_calib = provider.get_device_calibration()
assert device_calib is not None
camera_calib = device_calib.get_camera_calib("camera-rgb")
assert camera_calib is not None
pinhole = calibration.get_linear_camera_calibration(1408, 1408, 450)
# Compute camera extrinsics!
sophus_T_device_camera = device_calib.get_transform_device_sensor("camera-rgb")
sophus_T_cpf_camera = device_calib.get_transform_cpf_sensor("camera-rgb")
assert sophus_T_device_camera is not None
assert sophus_T_cpf_camera is not None
T_device_cam = np.concatenate(
[
sophus_T_device_camera.rotation().to_quat().squeeze(axis=0),
sophus_T_device_camera.translation().squeeze(axis=0),
]
)
T_cpf_cam = np.concatenate(
[
sophus_T_cpf_camera.rotation().to_quat().squeeze(axis=0),
sophus_T_cpf_camera.translation().squeeze(axis=0),
]
)
assert T_device_cam.shape == T_cpf_cam.shape == (7,)
# Dict from capture timestamp in nanoseconds to fields we care about.
detections_left_wrt_cam: dict[int, SingleHandHamerOutputWrtCamera | None] = {}
detections_right_wrt_cam: dict[int, SingleHandHamerOutputWrtCamera | None] = {}
pbar = tqdm(range(num_images))
for i in pbar:
image_data, image_data_record = provider.get_image_data_by_index(
rgb_stream_id, i
)
undistorted_image = calibration.distort_by_calibration(
image_data.to_numpy_array(), pinhole, camera_calib
)
hamer_out_left, hamer_out_right = hamer_helper.look_for_hands(
undistorted_image,
focal_length=450,
)
timestamp_ns = image_data_record.capture_timestamp_ns
if hamer_out_left is None:
detections_left_wrt_cam[timestamp_ns] = None
else:
detections_left_wrt_cam[timestamp_ns] = {
"verts": hamer_out_left["verts"],
"keypoints_3d": hamer_out_left["keypoints_3d"],
"mano_hand_pose": hamer_out_left["mano_hand_pose"],
"mano_hand_betas": hamer_out_left["mano_hand_betas"],
"mano_hand_global_orient": hamer_out_left["mano_hand_global_orient"],
}
if hamer_out_right is None:
detections_right_wrt_cam[timestamp_ns] = None
else:
detections_right_wrt_cam[timestamp_ns] = {
"verts": hamer_out_right["verts"],
"keypoints_3d": hamer_out_right["keypoints_3d"],
"mano_hand_pose": hamer_out_right["mano_hand_pose"],
"mano_hand_betas": hamer_out_right["mano_hand_betas"],
"mano_hand_global_orient": hamer_out_right["mano_hand_global_orient"],
}
composited = undistorted_image
composited = hamer_helper.composite_detections(
composited,
hamer_out_left,
border_color=(255, 100, 100),
focal_length=450,
)
composited = hamer_helper.composite_detections(
composited,
hamer_out_right,
border_color=(100, 100, 255),
focal_length=450,
)
composited = put_text(
composited,
"L detections: "
+ (
"0" if hamer_out_left is None else str(hamer_out_left["verts"].shape[0])
),
0,
color=(255, 100, 100),
font_scale=10.0 / 2880.0 * undistorted_image.shape[0],
)
composited = put_text(
composited,
"R detections: "
+ (
"0"
if hamer_out_right is None
else str(hamer_out_right["verts"].shape[0])
),
1,
color=(100, 100, 255),
font_scale=10.0 / 2880.0 * undistorted_image.shape[0],
)
composited = put_text(
composited,
f"ns={timestamp_ns}",
2,
color=(255, 255, 255),
font_scale=10.0 / 2880.0 * undistorted_image.shape[0],
)
print(f"Saving image {i:06d} to {hamer_render_out / f'{i:06d}.jpeg'}")
iio.imwrite(
str(hamer_render_out / f"{i:06d}.jpeg"),
np.concatenate(
[
# Darken input image, just for contrast...
(undistorted_image * 0.6).astype(np.uint8),
composited,
],
axis=1,
),
quality=90,
)
outputs = SavedHamerOutputs(
mano_faces_right=hamer_helper.get_mano_faces("right"),
mano_faces_left=hamer_helper.get_mano_faces("left"),
detections_right_wrt_cam=detections_right_wrt_cam,
detections_left_wrt_cam=detections_left_wrt_cam,
T_device_cam=T_device_cam,
T_cpf_cam=T_cpf_cam,
)
with open(pickle_out, "wb") as f:
pickle.dump(outputs, f)
def put_text(
image: np.ndarray,
text: str,
line_number: int,
color: tuple[int, int, int],
font_scale: float,
) -> np.ndarray:
"""Put some text on the top-left corner of an image."""
image = image.copy()
font = cv2.FONT_HERSHEY_PLAIN
cv2.putText(
image,
text=text,
org=(2, 1 + int(15 * font_scale * (line_number + 1))),
fontFace=font,
fontScale=font_scale,
color=(0, 0, 0),
thickness=max(int(font_scale), 1),
lineType=cv2.LINE_AA,
)
cv2.putText(
image,
text=text,
org=(2, 1 + int(15 * font_scale * (line_number + 1))),
fontFace=font,
fontScale=font_scale,
color=color,
thickness=max(int(font_scale), 1),
lineType=cv2.LINE_AA,
)
return image
if __name__ == "__main__":
tyro.cli(main)