Datasets:
Download scripts/materialize_images.py from Bus-CY7/SpatialMirage-Stage2-TrainData: direct link, hf CLI and curl.
- Browser
- Download file 1.46 kB
-
https://huggingface.co/datasets/Bus-CY7/SpatialMirage-Stage2-TrainData/resolve/main/scripts/materialize_images.py
- Command line
-
hf download hf://datasets/Bus-CY7/SpatialMirage-Stage2-TrainData/scripts/materialize_images.py
-
curl -L -o materialize_images.py https://huggingface.co/datasets/Bus-CY7/SpatialMirage-Stage2-TrainData/resolve/main/scripts/materialize_images.py
1.46 kB
| #!/usr/bin/env python3 | |
| import argparse | |
| import json | |
| import tarfile | |
| from pathlib import Path | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("--repo-root", type=Path, default=Path(__file__).resolve().parents[1]) | |
| parser.add_argument("--annotation", type=Path, required=True) | |
| parser.add_argument("--output-root", type=Path, required=True) | |
| args = parser.parse_args() | |
| rows_by_tar = {} | |
| with args.annotation.open("r", encoding="utf-8") as handle: | |
| for line in handle: | |
| if not line.strip(): | |
| continue | |
| row = json.loads(line) | |
| rows_by_tar.setdefault(row["metadata"]["tar"], []).append(row) | |
| written = 0 | |
| for relative_tar, rows in sorted(rows_by_tar.items()): | |
| with tarfile.open(args.repo_root / relative_tar) as archive: | |
| for row in rows: | |
| for output_key, member_key in (("source_views", "source_member"), ("target_view", "target_member")): | |
| relative_output = row[output_key][0] if output_key == "source_views" else row[output_key] | |
| output = args.output_root / relative_output | |
| output.parent.mkdir(parents=True, exist_ok=True) | |
| extracted = archive.extractfile(row["metadata"][member_key]) | |
| if extracted is None: | |
| raise FileNotFoundError(row["metadata"][member_key]) | |
| output.write_bytes(extracted.read()) | |
| written += 1 | |
| print(f"materialized {written} images under {args.output_root}") | |