File size: 3,056 Bytes
906e7d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Read a HandWorldBench-Corpus clip.

    python load_clip.py DATA_DIR hwc_000001

DATA_DIR is the dataset root: the directory that holds manifest.jsonl.gz and
into which the shards were extracted (`tar -xf shards/<name>.tar -C DATA_DIR`),
so that clips/, camera/ and hands/ sit next to the manifest. Every path in the
manifest is relative to DATA_DIR.
"""
import argparse
import gzip
import json
from pathlib import Path

import numpy as np


def find_clip(root, clip_id):
    compressed = root / "manifest.jsonl.gz"
    plain = root / "manifest.jsonl"
    stream = gzip.open(compressed, "rt", encoding="utf-8") if compressed.is_file() else plain.open(encoding="utf-8")
    with stream:
        for line in stream:
            row = json.loads(line)
            if row["id"] == clip_id:
                return row
    raise KeyError(clip_id)


def asset_path(root, asset, shard=None):
    candidate = root / asset["path"]
    if not candidate.exists():
        hint = f"; extract {shard} into {root} first (tar -xf {shard} -C {root})" if shard else ""
        raise FileNotFoundError(f"{candidate} not found{hint}")
    path = candidate.resolve(strict=True)
    if not path.is_relative_to(root.resolve()):
        raise ValueError(f"Invalid asset path {asset['path']}")
    return path


def load_camera(root, row):
    asset = row["camera"]
    with np.load(asset_path(root, asset, row["shard"]), allow_pickle=False) as item:
        return dict(c2w=item["c2w"], K=item["K"], D=item["D"],
                    distortion_model=str(item["distortion_model"].item()),
                    valid=item["valid"], t_s=item["t_s"], frame_index=item["frame_index"],
                    reference=asset["reference"], intrinsics=asset["intrinsics"])


def load_hands(root, row):
    asset = row.get("hands")
    if asset is None:
        return None
    with np.load(asset_path(root, asset, row["shard"]), allow_pickle=False) as item:
        return dict(joints_2d_px=item["joints_2d_px"], joints_camera_m=item["joints_camera_m"],
                    valid=item["valid"], valid_uv=item["valid_uv"],
                    in_frame_uv=item["in_frame_uv"], t_s=item["t_s"],
                    frame_index=item["frame_index"], joints_3d=asset["joints_3d"])


def main():
    cli = argparse.ArgumentParser(description=__doc__)
    cli.add_argument("root", type=Path)
    cli.add_argument("id")
    args = cli.parse_args()
    row = find_clip(args.root, args.id)
    hands = load_hands(args.root, row)
    camera = load_camera(args.root, row)
    print(json.dumps(dict(
        id=row["id"], domain=row["domain"], frames=row["frames"], fps=row["fps"],
        video=str(asset_path(args.root, row["video"], row["shard"])),
        camera_shape=list(camera["c2w"].shape),
        distortion_model=camera["distortion_model"],
        intrinsics=camera["intrinsics"],
        hand_shape=list(hands["joints_camera_m"].shape) if hands else None,
        operation=row["caption"]["operation"],
        shard=row["shard"],
    ), indent=2))


if __name__ == "__main__":
    main()