Datasets:
Upload parquet_to_raw.py with huggingface_hub
Browse files- parquet_to_raw.py +85 -0
parquet_to_raw.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Convert a parquet HF VMD-D dataset back to raw image files.
|
| 2 |
+
|
| 3 |
+
Restores the original structure:
|
| 4 |
+
{out}/{split}/{clip_id}/JPEGImages/{frame}.jpg
|
| 5 |
+
{out}/{split}/{clip_id}/SegmentationClassPNG/{frame}.png
|
| 6 |
+
|
| 7 |
+
Usage
|
| 8 |
+
-----
|
| 9 |
+
# From HuggingFace Hub:
|
| 10 |
+
python parquet_to_raw.py --repo garrying/VMD-D
|
| 11 |
+
|
| 12 |
+
# From a local save_to_disk() directory:
|
| 13 |
+
python parquet_to_raw.py --local ./vmd_disk
|
| 14 |
+
|
| 15 |
+
# Restore only the test split:
|
| 16 |
+
python parquet_to_raw.py --repo garrying/VMD-D --splits test
|
| 17 |
+
|
| 18 |
+
# Change output directory:
|
| 19 |
+
python parquet_to_raw.py --repo garrying/VMD-D --out VMD_raw
|
| 20 |
+
"""
|
| 21 |
+
import argparse
|
| 22 |
+
from pathlib import Path
|
| 23 |
+
from datasets import load_dataset, load_from_disk
|
| 24 |
+
|
| 25 |
+
SPLITS = ["train", "test"]
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def save_split(ds, split: str, out_dir: Path):
|
| 29 |
+
split_dir = out_dir / split
|
| 30 |
+
count = 0
|
| 31 |
+
|
| 32 |
+
for i, sample in enumerate(ds):
|
| 33 |
+
image_id = sample.get("image_id") or f"{i:06d}"
|
| 34 |
+
# image_id format: "{clip_id}/{frame_stem}", e.g. "056_12/0001"
|
| 35 |
+
parts = image_id.rsplit("/", 1)
|
| 36 |
+
if len(parts) == 2:
|
| 37 |
+
clip_id, frame_stem = parts
|
| 38 |
+
else:
|
| 39 |
+
clip_id, frame_stem = "unknown", parts[0]
|
| 40 |
+
|
| 41 |
+
img_dir = split_dir / clip_id / "JPEGImages"
|
| 42 |
+
mask_dir = split_dir / clip_id / "SegmentationClassPNG"
|
| 43 |
+
img_dir.mkdir(parents=True, exist_ok=True)
|
| 44 |
+
mask_dir.mkdir(parents=True, exist_ok=True)
|
| 45 |
+
|
| 46 |
+
sample["image"].save(img_dir / f"{frame_stem}.jpg")
|
| 47 |
+
sample["mask"].save(mask_dir / f"{frame_stem}.png")
|
| 48 |
+
count += 1
|
| 49 |
+
|
| 50 |
+
if count % 200 == 0:
|
| 51 |
+
print(f" {count}/{len(ds)}")
|
| 52 |
+
|
| 53 |
+
print(f" saved {count} frames -> {split_dir}")
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def main():
|
| 57 |
+
parser = argparse.ArgumentParser(
|
| 58 |
+
description="Convert parquet VMD-D back to raw image files."
|
| 59 |
+
)
|
| 60 |
+
src = parser.add_mutually_exclusive_group(required=True)
|
| 61 |
+
src.add_argument("--repo", help="HuggingFace repo ID (e.g. garrying/VMD-D)")
|
| 62 |
+
src.add_argument("--local", help="Path to a save_to_disk() directory")
|
| 63 |
+
parser.add_argument("--out", default="VMD",
|
| 64 |
+
help="Output root directory (default: VMD)")
|
| 65 |
+
parser.add_argument("--splits", nargs="+", choices=SPLITS, default=SPLITS,
|
| 66 |
+
help="Which splits to restore (default: all)")
|
| 67 |
+
args = parser.parse_args()
|
| 68 |
+
|
| 69 |
+
out_dir = Path(args.out)
|
| 70 |
+
out_dir.mkdir(parents=True, exist_ok=True)
|
| 71 |
+
|
| 72 |
+
for split in args.splits:
|
| 73 |
+
print(f"\nLoading {split}...")
|
| 74 |
+
if args.repo:
|
| 75 |
+
ds = load_dataset(args.repo, split=split)
|
| 76 |
+
else:
|
| 77 |
+
ds = load_from_disk(str(Path(args.local) / split))
|
| 78 |
+
print(f" {len(ds)} frames — saving images...")
|
| 79 |
+
save_split(ds, split, out_dir)
|
| 80 |
+
|
| 81 |
+
print(f"\nDone! Raw files in: {out_dir.resolve()}")
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
if __name__ == "__main__":
|
| 85 |
+
main()
|