Add Human Hand joint visualization example
Browse files- README.md +32 -0
- scripts/visualize_human_hand.py +275 -0
README.md
CHANGED
|
@@ -72,6 +72,38 @@ provided in each episode's `controls/action_57d_spec.json`. The source wrist
|
|
| 72 |
basis and the released aligned basis are therefore intentionally not
|
| 73 |
interchangeable.
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
## License and Citation
|
| 76 |
|
| 77 |
All data in this repository are licensed under [**CC BY-NC-SA 4.0**](https://creativecommons.org/licenses/by-nc-sa/4.0/). If you find our benchmark useful in your research, please consider citing our work.
|
|
|
|
| 72 |
basis and the released aligned basis are therefore intentionally not
|
| 73 |
interchangeable.
|
| 74 |
|
| 75 |
+
## Human Hand joint visualization
|
| 76 |
+
|
| 77 |
+
[`scripts/visualize_human_hand.py`](scripts/visualize_human_hand.py) provides a
|
| 78 |
+
standalone example for projecting the released 3D hand joints onto the
|
| 79 |
+
rectified `top_head` frames of a public Human Hand validation episode. It reads
|
| 80 |
+
the episode's released `hand_camera_poses.npz` and `calibration.json`, applies
|
| 81 |
+
the tracked-camera, physical-fisheye, and virtual-camera transformations, and
|
| 82 |
+
renders the left- and right-hand 21-joint skeletons next to the original
|
| 83 |
+
frames.
|
| 84 |
+
|
| 85 |
+
Install the two runtime dependencies with:
|
| 86 |
+
|
| 87 |
+
```bash
|
| 88 |
+
python -m pip install numpy opencv-python
|
| 89 |
+
```
|
| 90 |
+
|
| 91 |
+
After extracting one of the public Human Hand validation archives, run:
|
| 92 |
+
|
| 93 |
+
```bash
|
| 94 |
+
python scripts/visualize_human_hand.py \
|
| 95 |
+
--npz inputs/human_hand/<task>/<episode>/controls/hand_camera_poses.npz \
|
| 96 |
+
--calibration inputs/human_hand/<task>/<episode>/calibration.json \
|
| 97 |
+
--frames-dir <extracted-episode>/video/top_head \
|
| 98 |
+
--output hand_joint_projection.mp4 \
|
| 99 |
+
--fps 15
|
| 100 |
+
```
|
| 101 |
+
|
| 102 |
+
The script requires the image sequence and NPZ arrays to have identical frame
|
| 103 |
+
counts and respects the released hand- and camera-validity masks. It contains
|
| 104 |
+
no hard-coded episode list or camera parameters; all geometry is read from the
|
| 105 |
+
selected public validation episode.
|
| 106 |
+
|
| 107 |
## License and Citation
|
| 108 |
|
| 109 |
All data in this repository are licensed under [**CC BY-NC-SA 4.0**](https://creativecommons.org/licenses/by-nc-sa/4.0/). If you find our benchmark useful in your research, please consider citing our work.
|
scripts/visualize_human_hand.py
ADDED
|
@@ -0,0 +1,275 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Project released Human Hand validation joints onto released top_head frames.
|
| 3 |
+
|
| 4 |
+
The script reads one local public validation episode. It contains no episode
|
| 5 |
+
inventory, server path, credential, private calibration constant, or other
|
| 6 |
+
non-public metadata.
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
from __future__ import annotations
|
| 10 |
+
|
| 11 |
+
import argparse
|
| 12 |
+
import json
|
| 13 |
+
from pathlib import Path
|
| 14 |
+
|
| 15 |
+
import cv2
|
| 16 |
+
import numpy as np
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
HAND_EDGES = (
|
| 20 |
+
(0, 1), (1, 2), (2, 3), (3, 4),
|
| 21 |
+
(0, 5), (5, 6), (6, 7), (7, 8),
|
| 22 |
+
(0, 9), (9, 10), (10, 11), (11, 12),
|
| 23 |
+
(0, 13), (13, 14), (14, 15), (15, 16),
|
| 24 |
+
(0, 17), (17, 18), (18, 19), (19, 20),
|
| 25 |
+
(5, 9), (9, 13), (13, 17),
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
REQUIRED_NPZ_KEYS = (
|
| 29 |
+
"left_positions_camera",
|
| 30 |
+
"right_positions_camera",
|
| 31 |
+
"left_valid",
|
| 32 |
+
"right_valid",
|
| 33 |
+
"camera_valid",
|
| 34 |
+
"camera_position_world",
|
| 35 |
+
"camera_quaternion_world_xyzw",
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def quaternion_xyzw_to_matrix(quaternion: np.ndarray) -> np.ndarray:
|
| 40 |
+
"""Convert an XYZW quaternion to a 3-by-3 rotation matrix."""
|
| 41 |
+
q = np.asarray(quaternion, dtype=np.float64)
|
| 42 |
+
if q.shape != (4,):
|
| 43 |
+
raise ValueError(f"expected quaternion shape (4,), got {q.shape}")
|
| 44 |
+
norm = np.linalg.norm(q)
|
| 45 |
+
if not np.isfinite(norm) or norm < 1e-8:
|
| 46 |
+
raise ValueError("invalid quaternion")
|
| 47 |
+
x, y, z, w = q / norm
|
| 48 |
+
return np.asarray(
|
| 49 |
+
[
|
| 50 |
+
[1 - 2 * (y * y + z * z), 2 * (x * y - z * w), 2 * (x * z + y * w)],
|
| 51 |
+
[2 * (x * y + z * w), 1 - 2 * (x * x + z * z), 2 * (y * z - x * w)],
|
| 52 |
+
[2 * (x * z - y * w), 2 * (y * z + x * w), 1 - 2 * (x * x + y * y)],
|
| 53 |
+
],
|
| 54 |
+
dtype=np.float64,
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def xyz(record: dict) -> np.ndarray:
|
| 59 |
+
return np.asarray([record["x"], record["y"], record["z"]], dtype=np.float64)
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def xyzw(record: dict) -> np.ndarray:
|
| 63 |
+
return np.asarray(
|
| 64 |
+
[record["x"], record["y"], record["z"], record["w"]],
|
| 65 |
+
dtype=np.float64,
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def project_joints(
|
| 70 |
+
points_stored: np.ndarray,
|
| 71 |
+
camera_position_world: np.ndarray,
|
| 72 |
+
camera_quaternion_world_xyzw: np.ndarray,
|
| 73 |
+
top_head: dict,
|
| 74 |
+
) -> tuple[np.ndarray, np.ndarray]:
|
| 75 |
+
"""Apply the released camera transforms and virtual pinhole projection."""
|
| 76 |
+
points_stored = np.asarray(points_stored, dtype=np.float64)
|
| 77 |
+
if points_stored.shape != (21, 3):
|
| 78 |
+
raise ValueError(f"expected 21x3 hand joints, got {points_stored.shape}")
|
| 79 |
+
|
| 80 |
+
# Stored/world-referenced points -> tracked camera reference frame L.
|
| 81 |
+
rotation_world_from_left = quaternion_xyzw_to_matrix(
|
| 82 |
+
camera_quaternion_world_xyzw
|
| 83 |
+
)
|
| 84 |
+
points_left = (
|
| 85 |
+
rotation_world_from_left.T
|
| 86 |
+
@ (points_stored - np.asarray(camera_position_world, dtype=np.float64)).T
|
| 87 |
+
).T
|
| 88 |
+
|
| 89 |
+
# Tracked camera reference frame L -> physical fisheye optical frame F.
|
| 90 |
+
extrinsic = top_head["extrinsics"]["extrinsic_head_fisheye_T_left"]
|
| 91 |
+
rotation_left_from_fisheye = quaternion_xyzw_to_matrix(
|
| 92 |
+
xyzw(extrinsic["rotation"])
|
| 93 |
+
)
|
| 94 |
+
translation_left_from_fisheye = xyz(extrinsic["translation"])
|
| 95 |
+
points_fisheye = (
|
| 96 |
+
rotation_left_from_fisheye.T
|
| 97 |
+
@ (points_left - translation_left_from_fisheye).T
|
| 98 |
+
).T
|
| 99 |
+
|
| 100 |
+
# Physical fisheye F -> rectified virtual top_head camera V.
|
| 101 |
+
rotation_virtual_from_fisheye = np.asarray(
|
| 102 |
+
top_head["rectification"]["source_to_virtual_rotation"],
|
| 103 |
+
dtype=np.float64,
|
| 104 |
+
)
|
| 105 |
+
if rotation_virtual_from_fisheye.shape != (3, 3):
|
| 106 |
+
raise ValueError("source_to_virtual_rotation must be a 3x3 matrix")
|
| 107 |
+
points_virtual = (rotation_virtual_from_fisheye @ points_fisheye.T).T
|
| 108 |
+
|
| 109 |
+
# Virtual pinhole camera -> image pixels; points behind it are invalid.
|
| 110 |
+
intrinsics = top_head["intrinsics"]
|
| 111 |
+
depth_valid = points_virtual[:, 2] > 1e-6
|
| 112 |
+
pixels = np.full((21, 2), np.nan, dtype=np.float64)
|
| 113 |
+
pixels[depth_valid, 0] = (
|
| 114 |
+
float(intrinsics["Fx"])
|
| 115 |
+
* points_virtual[depth_valid, 0]
|
| 116 |
+
/ points_virtual[depth_valid, 2]
|
| 117 |
+
+ float(intrinsics["Cx"])
|
| 118 |
+
)
|
| 119 |
+
pixels[depth_valid, 1] = (
|
| 120 |
+
float(intrinsics["Fy"])
|
| 121 |
+
* points_virtual[depth_valid, 1]
|
| 122 |
+
/ points_virtual[depth_valid, 2]
|
| 123 |
+
+ float(intrinsics["Cy"])
|
| 124 |
+
)
|
| 125 |
+
return pixels, depth_valid
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
def draw_hand(
|
| 129 |
+
image: np.ndarray,
|
| 130 |
+
pixels: np.ndarray,
|
| 131 |
+
depth_valid: np.ndarray,
|
| 132 |
+
color: tuple[int, int, int],
|
| 133 |
+
label: str,
|
| 134 |
+
) -> None:
|
| 135 |
+
"""Draw a 21-joint hand skeleton inside the image bounds."""
|
| 136 |
+
height, width = image.shape[:2]
|
| 137 |
+
visible = (
|
| 138 |
+
depth_valid
|
| 139 |
+
& np.isfinite(pixels).all(axis=1)
|
| 140 |
+
& (pixels[:, 0] >= 0)
|
| 141 |
+
& (pixels[:, 0] < width)
|
| 142 |
+
& (pixels[:, 1] >= 0)
|
| 143 |
+
& (pixels[:, 1] < height)
|
| 144 |
+
)
|
| 145 |
+
rounded = np.rint(pixels).astype(np.int32, casting="unsafe", copy=False)
|
| 146 |
+
for start, end in HAND_EDGES:
|
| 147 |
+
if visible[start] and visible[end]:
|
| 148 |
+
cv2.line(
|
| 149 |
+
image, tuple(rounded[start]), tuple(rounded[end]), color, 3,
|
| 150 |
+
cv2.LINE_AA,
|
| 151 |
+
)
|
| 152 |
+
for index, point in enumerate(rounded):
|
| 153 |
+
if visible[index]:
|
| 154 |
+
radius = 7 if index == 0 else 4
|
| 155 |
+
cv2.circle(image, tuple(point), radius, (255, 255, 255), -1, cv2.LINE_AA)
|
| 156 |
+
cv2.circle(image, tuple(point), radius, color, 2, cv2.LINE_AA)
|
| 157 |
+
if visible[0]:
|
| 158 |
+
anchor = tuple(rounded[0] + np.asarray([8, -8]))
|
| 159 |
+
cv2.putText(
|
| 160 |
+
image, label, anchor, cv2.FONT_HERSHEY_SIMPLEX, 0.7, color, 2,
|
| 161 |
+
cv2.LINE_AA,
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
def list_frames(frames_dir: Path) -> list[Path]:
|
| 166 |
+
frames = sorted(frames_dir.glob("frame_*.jpg"))
|
| 167 |
+
if not frames:
|
| 168 |
+
frames = sorted(frames_dir.glob("frame_*.png"))
|
| 169 |
+
if not frames:
|
| 170 |
+
raise FileNotFoundError(f"no frame_*.jpg or frame_*.png in {frames_dir}")
|
| 171 |
+
return frames
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
def parse_args() -> argparse.Namespace:
|
| 175 |
+
parser = argparse.ArgumentParser(
|
| 176 |
+
description="Visualize one released Human Hand validation episode."
|
| 177 |
+
)
|
| 178 |
+
parser.add_argument("--npz", type=Path, required=True)
|
| 179 |
+
parser.add_argument("--calibration", type=Path, required=True)
|
| 180 |
+
parser.add_argument("--frames-dir", type=Path, required=True)
|
| 181 |
+
parser.add_argument("--output", type=Path, required=True)
|
| 182 |
+
parser.add_argument("--fps", type=float, default=15.0)
|
| 183 |
+
return parser.parse_args()
|
| 184 |
+
|
| 185 |
+
|
| 186 |
+
def main() -> None:
|
| 187 |
+
args = parse_args()
|
| 188 |
+
if args.fps <= 0:
|
| 189 |
+
raise ValueError("fps must be positive")
|
| 190 |
+
|
| 191 |
+
calibration = json.loads(args.calibration.read_text(encoding="utf-8"))
|
| 192 |
+
top_head = calibration["cameras"]["top_head"]
|
| 193 |
+
frames = list_frames(args.frames_dir)
|
| 194 |
+
with np.load(args.npz, allow_pickle=False) as archive:
|
| 195 |
+
missing = [key for key in REQUIRED_NPZ_KEYS if key not in archive]
|
| 196 |
+
if missing:
|
| 197 |
+
raise KeyError(f"NPZ is missing required arrays: {', '.join(missing)}")
|
| 198 |
+
data = {key: archive[key] for key in REQUIRED_NPZ_KEYS}
|
| 199 |
+
|
| 200 |
+
frame_count = len(frames)
|
| 201 |
+
mismatched = {
|
| 202 |
+
key: len(value)
|
| 203 |
+
for key, value in data.items()
|
| 204 |
+
if len(value) != frame_count
|
| 205 |
+
}
|
| 206 |
+
if mismatched:
|
| 207 |
+
raise ValueError(
|
| 208 |
+
f"frame count is {frame_count}, but NPZ lengths differ: {mismatched}"
|
| 209 |
+
)
|
| 210 |
+
|
| 211 |
+
sample = cv2.imread(str(frames[0]), cv2.IMREAD_COLOR)
|
| 212 |
+
if sample is None:
|
| 213 |
+
raise FileNotFoundError(frames[0])
|
| 214 |
+
height, width = sample.shape[:2]
|
| 215 |
+
header_height = 54
|
| 216 |
+
args.output.parent.mkdir(parents=True, exist_ok=True)
|
| 217 |
+
writer = cv2.VideoWriter(
|
| 218 |
+
str(args.output),
|
| 219 |
+
cv2.VideoWriter_fourcc(*"mp4v"),
|
| 220 |
+
args.fps,
|
| 221 |
+
(width * 2, height + header_height),
|
| 222 |
+
)
|
| 223 |
+
if not writer.isOpened():
|
| 224 |
+
raise RuntimeError(f"cannot open output video: {args.output}")
|
| 225 |
+
|
| 226 |
+
try:
|
| 227 |
+
for frame_index, frame_path in enumerate(frames):
|
| 228 |
+
raw = cv2.imread(str(frame_path), cv2.IMREAD_COLOR)
|
| 229 |
+
if raw is None:
|
| 230 |
+
raise FileNotFoundError(frame_path)
|
| 231 |
+
if raw.shape[:2] != (height, width):
|
| 232 |
+
raise ValueError(f"inconsistent frame size: {frame_path}")
|
| 233 |
+
overlay = raw.copy()
|
| 234 |
+
|
| 235 |
+
camera_valid = bool(data["camera_valid"][frame_index])
|
| 236 |
+
hands = (
|
| 237 |
+
(data["left_positions_camera"][frame_index],
|
| 238 |
+
bool(data["left_valid"][frame_index]), (0, 220, 0), "L"),
|
| 239 |
+
(data["right_positions_camera"][frame_index],
|
| 240 |
+
bool(data["right_valid"][frame_index]), (0, 80, 255), "R"),
|
| 241 |
+
)
|
| 242 |
+
for joints, hand_valid, color, label in hands:
|
| 243 |
+
if camera_valid and hand_valid:
|
| 244 |
+
pixels, depth_valid = project_joints(
|
| 245 |
+
joints,
|
| 246 |
+
data["camera_position_world"][frame_index],
|
| 247 |
+
data["camera_quaternion_world_xyzw"][frame_index],
|
| 248 |
+
top_head,
|
| 249 |
+
)
|
| 250 |
+
draw_hand(overlay, pixels, depth_valid, color, label)
|
| 251 |
+
|
| 252 |
+
canvas = np.zeros(
|
| 253 |
+
(height + header_height, width * 2, 3), dtype=np.uint8
|
| 254 |
+
)
|
| 255 |
+
canvas[header_height:] = np.hstack([raw, overlay])
|
| 256 |
+
cv2.putText(
|
| 257 |
+
canvas, "Released top_head frame", (12, 34),
|
| 258 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.66, (245, 245, 245), 2,
|
| 259 |
+
cv2.LINE_AA,
|
| 260 |
+
)
|
| 261 |
+
cv2.putText(
|
| 262 |
+
canvas,
|
| 263 |
+
f"Released joint projection | frame {frame_index:05d}",
|
| 264 |
+
(width + 12, 34), cv2.FONT_HERSHEY_SIMPLEX, 0.66,
|
| 265 |
+
(245, 245, 245), 2, cv2.LINE_AA,
|
| 266 |
+
)
|
| 267 |
+
writer.write(canvas)
|
| 268 |
+
finally:
|
| 269 |
+
writer.release()
|
| 270 |
+
|
| 271 |
+
print(f"Wrote {frame_count} frames to {args.output}")
|
| 272 |
+
|
| 273 |
+
|
| 274 |
+
if __name__ == "__main__":
|
| 275 |
+
main()
|