| |
| """Download the Hugging Face complement for the current EPIC-KITCHENS tree. |
| |
| Run from the dataset root: |
| |
| python3 download_hf_complement.py |
| |
| This script: |
| 1. Lists all video files on the Hugging Face dataset mirror. |
| 2. Compares them against the local `P*/videos/*.MP4` files. |
| 3. Writes the HF-only complement manifest to `annotations/hf_complement_paths.txt`. |
| 4. Downloads only those missing files into the current directory layout. |
| """ |
|
|
| import argparse |
| import json |
| import os |
| from pathlib import Path |
| import re |
| import sys |
| import urllib.request |
|
|
|
|
| HF_DATASET_ID = "a1raman/epic_kitchens_100" |
| HF_TREE_URL = ( |
| "https://huggingface.co/api/datasets/" |
| f"{HF_DATASET_ID}/tree/main?recursive=true&expand=false" |
| ) |
| VIDEO_PATH_RE = re.compile(r"^P\d{2}/videos/P\d{2}_\d+\.MP4$") |
|
|
|
|
| def parse_args() -> argparse.Namespace: |
| parser = argparse.ArgumentParser( |
| description="Download the HF-only complement into the current dataset tree." |
| ) |
| parser.add_argument( |
| "--root", |
| type=Path, |
| default=Path("."), |
| help="Dataset root directory. Default: current directory.", |
| ) |
| parser.add_argument( |
| "--manifest", |
| type=Path, |
| default=Path("annotations/hf_complement_paths.txt"), |
| help="Relative path of the generated manifest file.", |
| ) |
| parser.add_argument( |
| "--repo-id", |
| default=HF_DATASET_ID, |
| help=f"Hugging Face dataset repo id. Default: {HF_DATASET_ID}", |
| ) |
| parser.add_argument( |
| "--revision", |
| default="main", |
| help="Repo revision to download from. Default: main.", |
| ) |
| parser.add_argument( |
| "--max-workers", |
| type=int, |
| default=8, |
| help="Concurrent download workers for snapshot_download. Default: 8.", |
| ) |
| parser.add_argument( |
| "--manifest-only", |
| action="store_true", |
| help="Only write the complement manifest and skip downloading.", |
| ) |
| parser.add_argument( |
| "--no-hf-transfer", |
| action="store_true", |
| help="Do not enable HF_HUB_ENABLE_HF_TRANSFER=1.", |
| ) |
| return parser.parse_args() |
|
|
|
|
| def fetch_remote_video_paths(): |
| with urllib.request.urlopen(HF_TREE_URL, timeout=120) as response: |
| data = json.load(response) |
| return sorted( |
| item["path"] |
| for item in data |
| if item.get("type") == "file" and VIDEO_PATH_RE.match(item.get("path", "")) |
| ) |
|
|
|
|
| def collect_local_video_paths(root: Path): |
| return {path.as_posix() for path in root.glob("P*/videos/*.MP4")} |
|
|
|
|
| def main() -> int: |
| args = parse_args() |
| root = args.root.resolve() |
| manifest_path = (root / args.manifest).resolve() |
| manifest_path.parent.mkdir(parents=True, exist_ok=True) |
|
|
| remote_paths = fetch_remote_video_paths() |
| local_paths = collect_local_video_paths(root) |
| complement_paths = [path for path in remote_paths if path not in local_paths] |
|
|
| manifest_path.write_text("\n".join(complement_paths) + ("\n" if complement_paths else "")) |
| print(f"Wrote {len(complement_paths)} paths to {manifest_path}") |
|
|
| if args.manifest_only: |
| return 0 |
|
|
| if not complement_paths: |
| print("No HF-only complement files to download.") |
| return 0 |
|
|
| if not args.no_hf_transfer: |
| |
| os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1" |
|
|
| try: |
| from huggingface_hub import snapshot_download |
| except ImportError: |
| print( |
| "Missing dependency: install with " |
| '`python3 -m pip install -U "huggingface_hub[hf_transfer]"`', |
| file=sys.stderr, |
| ) |
| return 2 |
|
|
| snapshot_download( |
| repo_id=args.repo_id, |
| repo_type="dataset", |
| revision=args.revision, |
| local_dir=str(root), |
| allow_patterns=complement_paths, |
| max_workers=args.max_workers, |
| ) |
| print(f"Downloaded {len(complement_paths)} files into {root}") |
| return 0 |
|
|
|
|
| if __name__ == "__main__": |
| raise SystemExit(main()) |
|
|