File size: 4,032 Bytes
10d7092 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | #!/usr/bin/env python3
"""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:
# This variable must be set before importing huggingface_hub.
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())
|