| """ |
| ARCTIC Hand Data Reader |
| Only reads hand (MANO) parameters from raw_seqs. |
| No body model forward pass, no object data, no splits needed. |
| |
| Usage: |
| python scripts_data/hand_reader.py |
| python scripts_data/hand_reader.py --mano_p ./unpack/arctic_data/data/raw_seqs/s01/box_grab_01.mano.npy |
| python scripts_data/hand_reader.py --mano_p ./unpack/arctic_data/data/raw_seqs/s01/box_grab_01.mano.npy --frame 10 |
| """ |
|
|
| import argparse |
| import json |
| import os.path as op |
| import sys |
| from glob import glob |
|
|
| import numpy as np |
| import torch |
|
|
| sys.path = ["."] + sys.path |
|
|
| DATA_ROOT = "./data" |
|
|
| |
| |
| VIEW_NAMES = ["ego", "allo_1", "allo_2", "allo_3", "allo_4", |
| "allo_5", "allo_6", "allo_7", "allo_8"] |
|
|
|
|
| def load_hand_seq(mano_p): |
| """ |
| Load one sequence. Mirrors construct_loader() in |
| src/arctic/preprocess_dataset.py but keeps only hand fields. |
| |
| Returns a dict with: |
| right / left -> MANO params (tensors) |
| ego_cam -> per-frame ego camera (world2ego, K, dist8) |
| static_cams -> 8 fixed allo cameras from misc.json (world2cam, K) |
| meta -> sid, seq_name, num_frames, gender |
| """ |
| |
| data = np.load(mano_p, allow_pickle=True).item() |
| num_frames = len(data["right"]["rot"]) |
|
|
| def _load_hand(side): |
| return { |
| "rot": torch.FloatTensor(data[side]["rot"]), |
| "pose": torch.FloatTensor(data[side]["pose"]), |
| "trans": torch.FloatTensor(data[side]["trans"]), |
| "shape": torch.FloatTensor(data[side]["shape"]).repeat(num_frames, 1), |
| "fitting_err": data[side]["fitting_err"], |
| } |
|
|
| right = _load_hand("right") |
| left = _load_hand("left") |
|
|
| |
| assert len(right["fitting_err"]) > 50, f"Too few frames: {mano_p}" |
|
|
| |
| ego_p = mano_p.replace("mano.npy", "egocam.dist.npy") |
| egocam = np.load(ego_p, allow_pickle=True).item() |
|
|
| R_ego = torch.FloatTensor(egocam["R_k_cam_np"]) |
| T_ego = torch.FloatTensor(egocam["T_k_cam_np"]) |
| K_ego = torch.FloatTensor(egocam["intrinsics"]) |
| dist8 = torch.FloatTensor(egocam["dist8"]) |
|
|
| |
| world2ego = torch.zeros((num_frames, 4, 4)) |
| world2ego[:, :3, :3] = R_ego |
| world2ego[:, :3, 3] = T_ego[:, :, 0] |
| world2ego[:, 3, 3] = 1.0 |
|
|
| |
| sid = mano_p.split("/")[-2] |
| misc_p = op.join(DATA_ROOT, "misc.json") |
| with open(misc_p) as f: |
| misc = json.load(f) |
|
|
| sub = misc[sid] |
| world2cam = torch.FloatTensor(np.array(sub["world2cam"])) |
| allo_K = torch.FloatTensor(np.array(sub["intris_mat"])) |
| image_size = np.array(sub["image_size"]) |
|
|
| seq_name = mano_p.split("/")[-1].replace(".mano.npy", "") |
|
|
| return { |
| "right": right, |
| "left": left, |
| "ego_cam": { |
| "world2ego": world2ego, |
| "K": K_ego, |
| "dist8": dist8, |
| }, |
| "static_cams": { |
| "world2cam": world2cam, |
| "K": allo_K, |
| "image_size": image_size, |
| }, |
| "meta": { |
| "sid": sid, |
| "seq_name": seq_name, |
| "num_frames": num_frames, |
| "gender": sub["gender"], |
| }, |
| } |
|
|
|
|
| def get_frame(seq_data, idx): |
| """Return data for a single frame (0-indexed).""" |
| n = seq_data["meta"]["num_frames"] |
| assert 0 <= idx < n, f"Frame {idx} out of range (0-{n-1})" |
|
|
| def _frame_hand(h): |
| return {k: (v[idx] if isinstance(v, torch.Tensor) else v[idx]) |
| for k, v in h.items()} |
|
|
| frame = { |
| "right": _frame_hand(seq_data["right"]), |
| "left": _frame_hand(seq_data["left"]), |
| "world2ego": seq_data["ego_cam"]["world2ego"][idx], |
| "K_ego": seq_data["ego_cam"]["K"], |
| "dist8": seq_data["ego_cam"]["dist8"], |
| "static_cams": seq_data["static_cams"], |
| } |
|
|
| |
| img_dir = op.join(DATA_ROOT, "cropped_images", |
| seq_data["meta"]["sid"], seq_data["meta"]["seq_name"]) |
| if op.exists(img_dir): |
| fname = f"{idx + 1:05d}.jpg" |
| frame["images"] = { |
| name: p for name, view_id in zip(VIEW_NAMES, range(9)) |
| if op.exists(p := op.join(img_dir, str(view_id), fname)) |
| } |
|
|
| return frame |
|
|
|
|
| |
| |
| |
|
|
| def print_summary(seq_data): |
| m = seq_data["meta"] |
| print(f"\n=== Sequence: {m['sid']}/{m['seq_name']} ===") |
| print(f"Frames : {m['num_frames']}") |
| print(f"Subject: {m['sid']} gender={m['gender']}") |
| print(f"\nRight hand shape (10,): {seq_data['right']['shape'][0].numpy()}") |
| print(f"Left hand shape (10,): {seq_data['left']['shape'][0].numpy()}") |
| print(f"\nEgo cam K (3x3):\n{seq_data['ego_cam']['K'].numpy()}") |
| print(f"Ego cam dist8: {seq_data['ego_cam']['dist8'].numpy()}") |
| print(f"\nAllo cams: {seq_data['static_cams']['world2cam'].shape[0]}") |
| print(f"Image sizes (w x h):\n{seq_data['static_cams']['image_size']}") |
|
|
|
|
| def print_frame(frame, idx): |
| print(f"\n--- Frame {idx} ---") |
| for side in ["right", "left"]: |
| h = frame[side] |
| print(f"\n[{side} hand]") |
| print(f" rot (3,) : {h['rot'].numpy()}") |
| print(f" trans (3,) : {h['trans'].numpy()}") |
| print(f" pose (45,) first 6: {h['pose'].numpy()[:6]}") |
| print(f" shape (10,) : {h['shape'].numpy()}") |
| print(f" fitting_err : {h['fitting_err']:.4f}") |
| print(f"\n[Ego world2ego row 0-1]:\n{frame['world2ego'].numpy()[:2]}") |
| if "images" in frame: |
| print(f"\n[Available images]") |
| for view, path in frame["images"].items(): |
| print(f" {view:8s}: {path}") |
|
|
|
|
| def construct_args(): |
| parser = argparse.ArgumentParser() |
| parser.add_argument("--mano_p", type=str, default=None) |
| parser.add_argument("--frame", type=int, default=0) |
| return parser.parse_args() |
|
|
|
|
| def main(): |
| args = construct_args() |
|
|
| if args.mano_p is not None: |
| mano_p = args.mano_p |
| else: |
| candidates = glob(op.join(DATA_ROOT, "raw_seqs", "*", "*.mano.npy")) |
| assert candidates, "No .mano.npy files found. Unzip raw_seqs.zip first." |
| mano_p = sorted(candidates)[0] |
|
|
| print(f"Loading: {mano_p}") |
| seq_data = load_hand_seq(mano_p) |
|
|
| print_summary(seq_data) |
| frame = get_frame(seq_data, args.frame) |
| print_frame(frame, args.frame) |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|