| |
| """ |
| eval/preprocess_gt_videos.py |
| |
| Apply the same spatial preprocessing used by SpaceTimePilot to the GT videos |
| in camxtime_evaluation_gt, so they match the network output format exactly. |
| |
| Pipeline (mirrors spacetimepilot/dataset/utils.py): |
| 1. Load up to 81 frames at stride=1 |
| 2. crop_and_resize: aspect-ratio preserving scale so image covers 832×480 |
| 3. CenterCrop to exactly 832×480 |
| 4. Pad with last frame if shorter than 81 frames |
| 5. Write as 30fps H264 MP4 |
| |
| For 1080×1080 source: scale to 832×832, then crop 176px top/bottom → 832×480. |
| |
| Usage (run from repo root): |
| python eval/preprocess_gt_videos.py \\ |
| --input camxtime_evaluation_gt \\ |
| --output camxtime_evaluation_gt_preprocessed |
| """ |
|
|
| import argparse |
| import multiprocessing |
| import shutil |
| from concurrent.futures import ProcessPoolExecutor, as_completed |
| from pathlib import Path |
|
|
| import imageio.v2 as imageio |
| import numpy as np |
| from PIL import Image |
| from tqdm import tqdm |
|
|
| TARGET_W = 832 |
| TARGET_H = 480 |
| NUM_FRAMES = 81 |
| FPS = 30 |
|
|
|
|
| def crop_and_resize(img: Image.Image) -> Image.Image: |
| w, h = img.size |
| scale = max(TARGET_W / w, TARGET_H / h) |
| return img.resize((round(w * scale), round(h * scale)), Image.BILINEAR) |
|
|
|
|
| def center_crop(img: Image.Image) -> Image.Image: |
| w, h = img.size |
| return img.crop(((w - TARGET_W) // 2, (h - TARGET_H) // 2, |
| (w - TARGET_W) // 2 + TARGET_W, (h - TARGET_H) // 2 + TARGET_H)) |
|
|
|
|
| def preprocess_frame(arr: np.ndarray) -> np.ndarray: |
| img = Image.fromarray(arr).convert("RGB") |
| return np.array(center_crop(crop_and_resize(img))) |
|
|
|
|
| def process_video(src: Path, dst: Path) -> None: |
| reader = imageio.get_reader(str(src)) |
| total = reader.count_frames() |
| frames = [preprocess_frame(reader.get_data(i)) for i in range(min(NUM_FRAMES, total))] |
| reader.close() |
| while len(frames) < NUM_FRAMES: |
| frames.append(frames[-1].copy()) |
| writer = imageio.get_writer(str(dst), fps=FPS, codec="libx264", quality=8) |
| for f in frames: |
| writer.append_data(f) |
| writer.close() |
|
|
|
|
| def process_scene(args): |
| scene_dir, out_dir = Path(args[0]), Path(args[1]) |
| out_scene = out_dir / scene_dir.name |
| out_scene.mkdir(parents=True, exist_ok=True) |
| n_built = n_skipped = 0 |
| for vid in sorted(scene_dir.glob("*.mp4")): |
| out_vid = out_scene / vid.name |
| if out_vid.exists(): |
| n_skipped += 1 |
| else: |
| process_video(vid, out_vid) |
| n_built += 1 |
| for ext in (".json", ".txt"): |
| src = vid.with_suffix(ext) |
| if src.exists(): |
| dst = out_scene / src.name |
| if not dst.exists(): |
| shutil.copy2(src, dst) |
| cam_json = scene_dir / "camera_data.json" |
| if cam_json.exists() and not (out_scene / "camera_data.json").exists(): |
| shutil.copy2(cam_json, out_scene / "camera_data.json") |
| return scene_dir.name, n_built, n_skipped |
|
|
|
|
| def main(): |
| parser = argparse.ArgumentParser( |
| description="Preprocess GT videos to 832×480 / 81 frames to match network output." |
| ) |
| parser.add_argument("--input", required=True, |
| help="camxtime_evaluation_gt root") |
| parser.add_argument("--output", required=True, |
| help="Output root (camxtime_evaluation_gt_preprocessed)") |
| parser.add_argument("--scenes", nargs="+") |
| parser.add_argument("--workers", type=int, |
| default=min(32, multiprocessing.cpu_count())) |
| args = parser.parse_args() |
|
|
| input_dir = Path(args.input) |
| output_dir = Path(args.output) |
| output_dir.mkdir(parents=True, exist_ok=True) |
| scenes = ([input_dir / s for s in args.scenes] if args.scenes |
| else sorted(p for p in input_dir.iterdir() if p.is_dir())) |
|
|
| print(f"CPUs: {multiprocessing.cpu_count()} | workers: {args.workers} " |
| f"| target: {TARGET_W}×{TARGET_H}, {NUM_FRAMES}f @ {FPS}fps " |
| f"| scenes: {len(scenes)}\n") |
|
|
| tasks = [(s, output_dir) for s in scenes] |
| n_done = n_errors = 0 |
|
|
| with ProcessPoolExecutor(max_workers=args.workers) as pool: |
| futures = {pool.submit(process_scene, t): Path(t[0]).name for t in tasks} |
| with tqdm(total=len(futures), desc="Overall", unit="scene", |
| dynamic_ncols=True) as pbar: |
| for fut in as_completed(futures): |
| name = futures[fut] |
| try: |
| _, built, skipped = fut.result() |
| n_done += 1 |
| tqdm.write(f" OK {name:<12s} {built} preprocessed, {skipped} skipped") |
| except Exception as exc: |
| n_errors += 1 |
| tqdm.write(f" ERR {name:<12s} {exc}") |
| pbar.set_postfix(done=n_done, err=n_errors) |
| pbar.update(1) |
|
|
| print(f"\nFinished — {n_done} scenes done, {n_errors} errors") |
| print(f"Output: {output_dir}") |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|