| """ |
| 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) |
| |
| 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:] |
|
|
| 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() |
|
|