from __future__ import annotations import argparse import re import urllib.request import zipfile from pathlib import Path from tqdm import tqdm from common import ROOT URFD_URL = "https://fenix.ur.edu.pl/~mkepski/ds/uf.html" BASE_URL = "https://fenix.ur.edu.pl/~mkepski/ds/data/" def download(url: str, dst: Path) -> None: dst.parent.mkdir(parents=True, exist_ok=True) part = dst.with_suffix(dst.suffix + ".part") if part.exists(): part.unlink() with urllib.request.urlopen(url) as response, open(part, "wb") as f: total = int(response.headers.get("Content-Length") or 0) with tqdm(total=total, unit="B", unit_scale=True, desc=dst.name) as bar: while True: chunk = response.read(1024 * 1024) if not chunk: break f.write(chunk) bar.update(len(chunk)) part.replace(dst) def main() -> None: ap = argparse.ArgumentParser() ap.add_argument("--camera", choices=["cam0", "cam1"], default="cam0") ap.add_argument("--keep-zips", action="store_true") args = ap.parse_args() html = urllib.request.urlopen(URFD_URL).read().decode("utf-8", errors="ignore") pattern = rf"(fall-\d+-{args.camera}-rgb\.zip|adl-\d+-{args.camera}-rgb\.zip)" names = sorted(set(re.findall(pattern, html))) if args.camera == "cam1": names = [n for n in names if n.startswith("fall-")] if not names: raise SystemExit("No URFD RGB zip links found") for name in names: label_dir = "fall" if name.startswith("fall-") else "nonfall" seq = name.removesuffix(".zip") zip_path = ROOT / "data/raw/URFD_zips" / name out_dir = ROOT / "data/raw/URFD" / label_dir / seq if not out_dir.exists(): if zip_path.exists() and not zipfile.is_zipfile(zip_path): zip_path.unlink() if not zip_path.exists(): download(BASE_URL + name, zip_path) out_dir.mkdir(parents=True, exist_ok=True) with zipfile.ZipFile(zip_path) as zf: zf.extractall(out_dir) if zip_path.exists() and not args.keep_zips: zip_path.unlink() print(f"Prepared URFD RGB image sequences under {ROOT / 'data/raw/URFD'}") if __name__ == "__main__": main()