fall / scripts /download_mcfd.py
minhy112's picture
Upload project files excluding raw data folder
ae419ed verified
Raw
History Blame Contribute Delete
1.84 kB
from __future__ import annotations
import argparse
import urllib.request
import zipfile
from pathlib import Path
from tqdm import tqdm
from common import ROOT
BASE_URL = "https://www.iro.umontreal.ca/~labimage/Dataset/chute-zip"
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("--keep-zips", action="store_true")
args = ap.parse_args()
for idx in range(1, 25):
name = f"chute{idx:02d}.zip"
label_dir = "fall" if idx <= 22 else "nonfall"
zip_path = ROOT / "data/raw/MCFD_zips" / name
out_dir = ROOT / "data/raw/MCFD" / label_dir / f"chute{idx:02d}"
if out_dir.exists() and any(out_dir.iterdir()):
continue
if zip_path.exists() and not zipfile.is_zipfile(zip_path):
zip_path.unlink()
if not zip_path.exists():
download(f"{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 MCFD scenarios under {ROOT / 'data/raw/MCFD'}")
if __name__ == "__main__":
main()