File size: 2,072 Bytes
541f082
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Extract all or part of WildShadow-Video into paired frame directories."""

from __future__ import annotations

import argparse
import csv
import subprocess
from concurrent.futures import ThreadPoolExecutor, as_completed
from pathlib import Path


def extract(video: Path, destination: Path) -> None:
    destination.mkdir(parents=True, exist_ok=True)
    subprocess.run([
        "ffmpeg", "-hide_banner", "-loglevel", "error", "-y", "-i", str(video),
        "-vsync", "0", "-start_number", "0", str(destination / "rgb_%06d.png"),
    ], check=True)


def extract_row(root: Path, output: Path, row: dict[str, str]) -> None:
    destination = output / row["split"] / row["dataset"] / row["clip_id"]
    extract(root / row["shadow_video"], destination / "origin")
    extract(root / row["shadow_free_video"], destination / "shadow_free")


def main() -> None:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--root", type=Path, default=Path(__file__).resolve().parents[1])
    parser.add_argument("--output", required=True, type=Path)
    parser.add_argument("--split", choices=("train", "test"))
    parser.add_argument("--dataset", choices=("3dfront", "icity", "infinigen", "hssd"))
    parser.add_argument("--workers", type=int, default=8)
    args = parser.parse_args()
    with (args.root / "manifests" / "clips.csv").open(newline="", encoding="utf-8") as handle:
        rows = list(csv.DictReader(handle))
    if args.split:
        rows = [row for row in rows if row["split"] == args.split]
    if args.dataset:
        rows = [row for row in rows if row["dataset"] == args.dataset]
    with ThreadPoolExecutor(max_workers=args.workers) as pool:
        futures = [pool.submit(extract_row, args.root, args.output, row) for row in rows]
        for index, future in enumerate(as_completed(futures), 1):
            future.result()
            if index % 50 == 0 or index == len(futures):
                print(f"Extracted {index}/{len(futures)} clips", flush=True)


if __name__ == "__main__":
    main()