sph_dataset / process_frames.py
ioandanielc's picture
Add files using upload-large-folder tool
aa07075 verified
#!/usr/bin/env python3
"""Apply border recoloring to side/front frames; copy top frames as-is.
Input: final_data/sim_NNNNN/frames/{front,side,top}/frame_NNNNN.png
Output: final_data_processed/sim_NNNNN/frames/{front,side,top}/frame_NNNNN.png
"""
from __future__ import annotations
import os
import shutil
from pathlib import Path
import numpy as np
from PIL import Image
# ── constants (from prepare.py / cvae_sph2img) ───────────────────────────────
PURE_GREEN = np.array([0, 197, 0], dtype=np.uint8)
PURE_BLUE = np.array([0, 0, 189], dtype=np.uint8)
PURE_BLACK = np.array([0, 0, 0], dtype=np.uint8)
PURE_GRAY = np.array([98, 93, 90], dtype=np.uint8)
BORDER_THICKNESS_PX = 10
SIDE_BOTTOM_HEIGHT_PX = 188
SIDE_BUFFER = 3
# ── image processing ──────────────────────────────────────────────────────────
def detect_side_columns(rgb: np.ndarray) -> tuple[int, int]:
h, w, _ = rgb.shape
if h == 0 or w == 0:
return (0, 0)
probe_y = max(0, h - SIDE_BOTTOM_HEIGHT_PX)
row = rgb[probe_y, :, :3]
mask = np.all(row == PURE_BLACK, axis=1) | np.all(row == PURE_GRAY, axis=1)
left = 0
while left < w and mask[left]:
left += 1
right = 0
idx = w - 1
while idx >= 0 and mask[idx]:
right += 1
idx -= 1
return (left, right)
def fixed_border_recolor(rgb: np.ndarray, left_columns: int, right_columns: int) -> np.ndarray:
out = rgb[:, :, :3].copy()
h, w, _ = out.shape
bt = min(BORDER_THICKNESS_PX, h, w)
sbh = min(SIDE_BOTTOM_HEIGHT_PX, h)
out[:bt, :, :] = PURE_BLUE
out[h - bt:, :, :] = PURE_GREEN
if left_columns > 0:
lw = min(left_columns, w)
out[:, :lw, :] = PURE_BLUE
out[h - sbh:, :lw, :] = PURE_GREEN
if right_columns > 0:
rw = min(right_columns, w)
out[:, w - rw:, :] = PURE_BLUE
out[h - sbh:, w - rw:, :] = PURE_GREEN
return out
def process_image(src: Path, dst: Path, is_side: bool) -> None:
rgb = np.array(Image.open(src).convert("RGB"))
h, w, _ = rgb.shape
default_cols = min(BORDER_THICKNESS_PX, h, w)
if is_side:
left, right = detect_side_columns(rgb)
if left > 0: left = min(w, left + SIDE_BUFFER)
if right > 0: right = min(w, right + SIDE_BUFFER)
else:
left, right = default_cols, default_cols
out = fixed_border_recolor(rgb, left, right)
Image.fromarray(out).save(dst)
# ── main ──────────────────────────────────────────────────────────────────────
def main() -> None:
here = Path(__file__).parent
src_root = here / "final_data"
dst_root = here / "final_data_processed"
sim_dirs = sorted(src_root.iterdir())
print(f"Found {len(sim_dirs)} simulations")
for sim_dir in sim_dirs:
if not sim_dir.is_dir():
continue
frames_src = sim_dir / "frames"
if not frames_src.is_dir():
print(f" SKIP {sim_dir.name} — no frames/ directory")
continue
for view in ("front", "side", "top"):
view_src = frames_src / view
view_dst = dst_root / sim_dir.name / "frames" / view
if not view_src.is_dir():
continue
view_dst.mkdir(parents=True, exist_ok=True)
for png in sorted(view_src.glob("*.png")):
dst_path = view_dst / png.name
if view == "top":
shutil.copy2(png, dst_path)
else:
process_image(png, dst_path, is_side=(view == "side"))
print(f" {sim_dir.name} done")
print("All done.")
if __name__ == "__main__":
main()