| """Batch optical flow from 25-frame rgb_align/ (24 flow vis frames).""" |
|
|
| import json |
| import sys |
| import time |
| from pathlib import Path |
|
|
| import cv2 |
| import imageio |
| import numpy as np |
| import torch |
|
|
| sys.path.insert(0, "/project/llmsvgen/sunkai/robomaster_3d/CoAF") |
| from tools.flow_dataset.io import save_vis_png |
| from tools.flow_dataset.visualize import flow_hwc_to_colorwheel_bgr |
| from tools.unimatch_flow.model import build_unimatch_estimator |
|
|
| DATASET_ROOT = Path("/project/llmsvgen/sunkai/robomaster_3d/Casual_CoAF/coaf_dataset_24_25") |
| RAW_ROOT = DATASET_ROOT / "raw" |
| OUTPUT_ROOT = DATASET_ROOT / "modalities" / "flow" |
| RGB_ALIGN_FRAMES = 25 |
| GLOBAL_MAX_FLOW = 20.0 |
| WIDTH = 512 |
| HEIGHT = 512 |
| FPS = 8 |
|
|
|
|
| def read_rgb_align_frames(rgb_dir: Path, target_size: int): |
| frames = [] |
| for i in range(1, RGB_ALIGN_FRAMES + 1): |
| path = rgb_dir / f"frame_{i:04d}.png" |
| if not path.exists(): |
| break |
| img = cv2.imread(str(path)) |
| img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) |
| if img.shape[0] != target_size or img.shape[1] != target_size: |
| img = cv2.resize(img, (target_size, target_size), interpolation=cv2.INTER_LANCZOS4) |
| frames.append(img) |
| return frames |
|
|
|
|
| def main(): |
| device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
| estimator = build_unimatch_estimator( |
| device, |
| preset_name="gmflow_s2_reg6_mixdata", |
| height=HEIGHT, |
| width=WIDTH, |
| ) |
|
|
| episodes = sorted(RAW_ROOT.glob("episode_*")) |
| start_time = time.time() |
| processed = 0 |
| failed = [] |
|
|
| for ep_dir in episodes: |
| ep_name = ep_dir.name |
| out_dir = OUTPUT_ROOT / ep_name |
| if (out_dir / "preview.mp4").exists(): |
| processed += 1 |
| continue |
|
|
| rgb_dir = ep_dir / "rgb_align" |
| try: |
| frames = read_rgb_align_frames(rgb_dir, WIDTH) |
| if len(frames) != RGB_ALIGN_FRAMES: |
| raise ValueError(f"Expected {RGB_ALIGN_FRAMES} frames, got {len(frames)}") |
|
|
| raw_dir = out_dir / "raw" |
| vis_dir = out_dir / "vis" |
| raw_dir.mkdir(parents=True, exist_ok=True) |
| vis_dir.mkdir(parents=True, exist_ok=True) |
|
|
| |
| flow_frames_vis = [] |
| for i in range(RGB_ALIGN_FRAMES): |
| if i == 0: |
| flow = np.zeros((HEIGHT, WIDTH, 2), dtype=np.float32) |
| else: |
| flow = estimator.predict_hwc(frames[i - 1], frames[i]) |
| np.save(str(raw_dir / f"flow_{i+1:04d}.npy"), flow) |
| vis = flow_hwc_to_colorwheel_bgr(flow, GLOBAL_MAX_FLOW) |
| cv2.imwrite(str(vis_dir / f"frame_{i+1:04d}.png"), vis) |
| flow_frames_vis.append(cv2.cvtColor(vis, cv2.COLOR_BGR2RGB)) |
|
|
| imageio.mimsave( |
| str(out_dir / "preview.mp4"), |
| flow_frames_vis, |
| fps=FPS, |
| codec="libx264", |
| macro_block_size=1, |
| ) |
| meta = { |
| "num_input_frames": RGB_ALIGN_FRAMES, |
| "num_flow_frames": len(flow_frames_vis), |
| "aligned_with": "reason_indices", |
| } |
| (out_dir / "meta.json").write_text(json.dumps(meta, indent=2) + "\n") |
| processed += 1 |
| except Exception as e: |
| failed.append({"episode": ep_name, "error": str(e)}) |
|
|
| print(f"\nDone! {processed}/{len(episodes)}, {len(failed)} failed") |
| if failed: |
| (OUTPUT_ROOT / "flow_failures.json").write_text(json.dumps(failed, indent=2) + "\n") |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|