Download extract_frames.py from MaplesWCT/EVAThink-Dataset-Stage2: direct link, hf CLI and curl.
- Browser
- Download file 2.6 kB
-
https://huggingface.co/datasets/MaplesWCT/EVAThink-Dataset-Stage2/resolve/main/extract_frames.py
- Command line
-
hf download hf://datasets/MaplesWCT/EVAThink-Dataset-Stage2/extract_frames.py
-
curl -L -o extract_frames.py https://huggingface.co/datasets/MaplesWCT/EVAThink-Dataset-Stage2/resolve/main/extract_frames.py
2.6 kB
| #!/usr/bin/env python3 | |
| """Unpack the frame archives of this dataset and (optionally) write a parquet with absolute frame paths. | |
| python extract_frames.py # unzip frames_zip/*.zip -> ./frames/<frame_dir>/000.jpg ... 031.jpg | |
| python extract_frames.py --abs # also write train.abs.parquet (frame paths made absolute; needs pyarrow) | |
| python extract_frames.py --out /data # extract somewhere else (frames end up in /data/frames) | |
| `train.parquet` stores frame paths relative to the extraction root (`frames/<frame_dir>/NNN.jpg`). | |
| """ | |
| import argparse | |
| import glob | |
| import hashlib | |
| import json | |
| import os | |
| import zipfile | |
| HERE = os.path.dirname(os.path.abspath(__file__)) | |
| def sha256(path): | |
| h = hashlib.sha256() | |
| with open(path, "rb") as f: | |
| for blk in iter(lambda: f.read(1 << 24), b""): | |
| h.update(blk) | |
| return h.hexdigest() | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--out", default=HERE, help="extraction root (default: the dataset folder)") | |
| ap.add_argument("--abs", action="store_true", help="write train.abs.parquet with absolute frame paths") | |
| ap.add_argument("--no-verify", action="store_true", help="skip the sha256 check of the zip files") | |
| args = ap.parse_args() | |
| out = os.path.abspath(args.out) | |
| manifest = json.load(open(os.path.join(HERE, "manifest.json"))) | |
| want = {z["file"]: z for z in manifest["zips"]} | |
| zips = sorted(glob.glob(os.path.join(HERE, "frames_zip", "frames-*.zip"))) | |
| assert len(zips) == len(want), f"expected {len(want)} zip files, found {len(zips)}" | |
| for zp in zips: | |
| rel = "frames_zip/" + os.path.basename(zp) | |
| if not args.no_verify: | |
| assert sha256(zp) == want[rel]["sha256"], f"sha256 mismatch: {rel} (re-download it)" | |
| with zipfile.ZipFile(zp) as z: | |
| z.extractall(out) | |
| print("extracted", rel, flush=True) | |
| n = len(glob.glob(os.path.join(out, "frames", "*", "*.jpg"))) | |
| assert n == manifest["frame_files"], f"expected {manifest['frame_files']} frames, found {n}" | |
| print(f"ok: {n} frames in {os.path.join(out, 'frames')}") | |
| if args.abs: | |
| import pyarrow as pa | |
| import pyarrow.parquet as pq | |
| t = pq.read_table(os.path.join(HERE, "train.parquet")) | |
| rows = t.to_pylist() | |
| for r in rows: | |
| r["videos"][0]["video"] = [os.path.join(out, f) for f in r["videos"][0]["video"]] | |
| dst = os.path.join(out, "train.abs.parquet") | |
| pq.write_table(pa.Table.from_pylist(rows, schema=t.schema), dst) | |
| print("wrote", dst) | |
| if __name__ == "__main__": | |
| main() | |