File size: 3,471 Bytes
71ee09b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Build Parquet files for HuggingFace dataset viewer.

Reads Lightning Pose CSVs and corresponding PNG frames, then writes
Parquet files with embedded images to data/ for the HF viewer.

Usage (from repo root):
    python scripts/build_parquet.py
"""

import csv
import io
from pathlib import Path

import datasets
from datasets import Dataset, Features, Image, Value, Sequence

REPO_ROOT = Path(__file__).parent.parent
DATA_OUT = REPO_ROOT / "data"

VIEWS = ["Cam-A", "Cam-B", "Cam-C", "Cam-D", "Cam-E", "Cam-F"]

KEYPOINTS = [
    "L1A", "L1B", "L1C", "L1D", "L1E",
    "L2A", "L2B", "L2C", "L2D", "L2E",
    "L3A", "L3B", "L3C", "L3D", "L3E",
    "R1A", "R1B", "R1C", "R1D", "R1E",
    "R2A", "R2B", "R2C", "R2D", "R2E",
    "R3A", "R3B", "R3C", "R3D", "R3E",
]

FEATURES = Features(
    {
        "image": Image(),
        "session": Value("string"),
        "view": Value("string"),
        "split": Value("string"),
        "frame": Value("string"),
        **{f"{kp}_x": Value("float32") for kp in KEYPOINTS},
        **{f"{kp}_y": Value("float32") for kp in KEYPOINTS},
    }
)


def parse_csv(csv_path: Path, view: str, split: str) -> list[dict]:
    rows = []
    with open(csv_path) as f:
        reader = csv.reader(f)
        # Skip 3-row header: scorer, bodyparts, coords
        next(reader)
        next(reader)
        next(reader)
        for row in reader:
            img_rel_path = row[0]
            img_path = REPO_ROOT / img_rel_path
            if not img_path.exists():
                print(f"  WARNING: missing {img_path}, skipping")
                continue

            coords = row[1:]  # 60 values: x0,y0,x1,y1,...

            record: dict = {
                "image": {"path": None, "bytes": img_path.read_bytes()},
                "session": "_".join(Path(img_rel_path).parent.name.split("_")[:-1]),
                "view": view,
                "split": split,
                "frame": Path(img_rel_path).name,
            }

            for i, kp in enumerate(KEYPOINTS):
                x_str = coords[i * 2]
                y_str = coords[i * 2 + 1]
                record[f"{kp}_x"] = float(x_str) if x_str else float("nan")
                record[f"{kp}_y"] = float(y_str) if y_str else float("nan")

            rows.append(record)
    return rows


def build_split(csv_suffix: str, split_name: str) -> list[dict]:
    all_rows = []
    for view in VIEWS:
        csv_path = REPO_ROOT / f"CollectedData_{view}{csv_suffix}.csv"
        if not csv_path.exists():
            print(f"Skipping missing {csv_path}")
            continue
        print(f"  Reading {csv_path.name} ...")
        rows = parse_csv(csv_path, view, split_name)
        print(f"    {len(rows)} rows")
        all_rows.extend(rows)
    return all_rows


def main():
    DATA_OUT.mkdir(exist_ok=True)

    print("Building InD split ...")
    ind_rows = build_split("", "ind")
    ind_ds = Dataset.from_list(ind_rows, features=FEATURES)
    out = DATA_OUT / "ind-train-00000-of-00001.parquet"
    ind_ds.to_parquet(str(out))
    print(f"Wrote {out} ({out.stat().st_size / 1e6:.1f} MB, {len(ind_rows)} rows)")

    print("Building OOD split ...")
    ood_rows = build_split("_new", "ood")
    ood_ds = Dataset.from_list(ood_rows, features=FEATURES)
    out = DATA_OUT / "ood-test-00000-of-00001.parquet"
    ood_ds.to_parquet(str(out))
    print(f"Wrote {out} ({out.stat().st_size / 1e6:.1f} MB, {len(ood_rows)} rows)")


if __name__ == "__main__":
    main()