Download code/ingest.py from d9993r/CRICKET: direct link, hf CLI and curl.
- Browser
- Download file 2.6 kB
-
https://huggingface.co/datasets/d9993r/CRICKET/resolve/main/code/ingest.py
- Command line
-
hf download hf://datasets/d9993r/CRICKET/code/ingest.py
-
curl -L -o ingest.py https://huggingface.co/datasets/d9993r/CRICKET/resolve/main/code/ingest.py
2.6 kB
| """Stage 1 - register every video in datasets/ with metadata, checksum and a | |
| rights record. IDs are stable across reruns: an existing file (same sha256) | |
| keeps its source_id and session_id; new files get the next number.""" | |
| from datetime import datetime | |
| from pathlib import Path | |
| from common import clean_title, probe_video, read_json, rel, sha256_file, write_json | |
| VIDEO_EXT = {".mp4", ".mkv", ".mov", ".avi", ".webm", ".m4v"} | |
| def settings_for(cfg, folder_name, file_name): | |
| cat = dict(cfg["categories"].get(folder_name) or { | |
| "category": folder_name.lower().replace(" ", "_"), "view_description": "a cricket video", | |
| "time_scale": 1.0}) | |
| for key, over in (cfg.get("overrides") or {}).items(): | |
| if key in file_name: | |
| cat.update(over) | |
| return cat | |
| def run_ingest(cfg): | |
| datasets = cfg["_paths"]["datasets"] | |
| reg_path = cfg["_paths"]["results"] / "registry" / "sources.json" | |
| old = {s["sha256"]: s for s in (read_json(reg_path) if reg_path.exists() else [])} | |
| next_n = max([int(s["source_id"].split("_")[-1]) for s in old.values()] or [0]) + 1 | |
| files = sorted(p for p in datasets.rglob("*") if p.suffix.lower() in VIDEO_EXT and p.is_file()) | |
| if not files: | |
| raise SystemExit(f"No videos found under {rel(datasets)}/ - put your category folders there.") | |
| sources = [] | |
| for f in files: | |
| folder = f.relative_to(datasets).parts[0] if len(f.relative_to(datasets).parts) > 1 else "uncategorised" | |
| print(f" ingest {rel(f)}") | |
| sha = sha256_file(f) | |
| meta = probe_video(f) | |
| st = settings_for(cfg, folder, f.name) | |
| if sha in old: | |
| sid, sess = old[sha]["source_id"], old[sha]["session_id"] | |
| else: | |
| n = next_n; next_n += 1 | |
| sid = f"SRC_YT_{n:04d}" | |
| day = datetime.fromtimestamp(f.stat().st_mtime).strftime("%Y%m%d") | |
| sess = f"{cfg['project']['site_short']}_{day}_S{n:03d}" | |
| sources.append({ | |
| "source_id": sid, "session_id": sess, "path": rel(f), "folder": folder, | |
| "title": clean_title(f.name), "sha256": sha, **meta, | |
| "category": st["category"], "view_description": st.get("view_description"), | |
| "time_scale": float(st.get("time_scale", 1.0)), | |
| "time_scale_verified": bool(st.get("time_scale_verified", st.get("time_scale", 1.0) == 1.0)), | |
| "pose_imgsz": st.get("pose_imgsz"), | |
| "rights": {"source_id": sid, **cfg["rights_default"]}, | |
| }) | |
| write_json(reg_path, sources) | |
| print(f" {len(sources)} sources -> {rel(reg_path)}") | |
| return sources | |