File size: 1,837 Bytes
ae419ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()