"""Gradio demo for both parts of the project. 1. Stereo vision: dense disparity + metric depth (StereoSGBM) on Middlebury pairs or your own rectified pair, scored against Middlebury ground truth. 2. Multi-agent pathfinding: Space-Time A* vs Conflict-Based Search on a 3D grid. """ import random import tempfile from pathlib import Path import cv2 import gradio as gr import matplotlib matplotlib.use("Agg") import numpy as np from problem2 import run_problem2 ROOT = Path(__file__).parent / "Dataset" SCENES = { "Adirondack": ROOT / "Adirondack-perfect" / "Adirondack-perfect", "Playroom": ROOT / "Playroom-perfect" / "Playroom-perfect", } def read_calib(path): vals = {} for line in Path(path).read_text().splitlines(): if "=" in line: k, v = line.split("=", 1) vals[k.strip()] = v.strip() fx = float(vals["cam0"].strip("[]").split()[0]) return fx, float(vals["baseline"]), float(vals["doffs"]), int(vals.get("ndisp", 256)) def read_pfm(path): """Read a Middlebury .pfm (OpenCV rescales these values, so parse it directly).""" with open(path, "rb") as f: channels = 3 if f.readline().strip() == b"PF" else 1 w, h = map(int, f.readline().split()) scale = float(f.readline()) data = np.fromfile(f, ("<" if scale < 0 else ">") + "f4") shape = (h, w, channels) if channels == 3 else (h, w) return np.flipud(data.reshape(shape)).astype(np.float32) def sgbm(left, right, num_disp, block): num_disp = max(16, int(np.ceil(num_disp / 16)) * 16) matcher = cv2.StereoSGBM_create( minDisparity=0, numDisparities=num_disp, blockSize=block, P1=8 * 3 * block**2, P2=32 * 3 * block**2, disp12MaxDiff=1, uniquenessRatio=10, speckleWindowSize=100, speckleRange=32, mode=cv2.STEREO_SGBM_MODE_SGBM_3WAY, ) return matcher.compute(left, right).astype(np.float32) / 16.0 def colorize(arr, valid): v = arr[valid] lo, hi = (np.percentile(v, 2), np.percentile(v, 98)) if v.size else (0, 1) norm = np.clip((arr - lo) / max(hi - lo, 1e-6), 0, 1) img = cv2.applyColorMap((norm * 255).astype(np.uint8), cv2.COLORMAP_TURBO)[..., ::-1].copy() img[~valid] = 0 return img def run_stereo(scene, left_up, right_up, scale, block): gt = None if left_up is not None and right_up is not None: left, right = left_up, right_up fx = baseline = doffs = None ndisp = 256 else: d = SCENES[scene] left = cv2.imread(str(d / "im0.png"))[..., ::-1] right = cv2.imread(str(d / "im1.png"))[..., ::-1] fx, baseline, doffs, ndisp = read_calib(d / "calib.txt") gt = read_pfm(d / "disp0.pfm") * scale # disparities shrink with the image h, w = left.shape[:2] size = (int(w * scale), int(h * scale)) l_s = cv2.resize(left, size, interpolation=cv2.INTER_AREA) r_s = cv2.resize(right, size, interpolation=cv2.INTER_AREA) disp = sgbm(cv2.cvtColor(l_s, cv2.COLOR_RGB2GRAY), cv2.cvtColor(r_s, cv2.COLOR_RGB2GRAY), ndisp * scale, block) valid = disp > 0 notes = [f"Resolution {size[0]}x{size[1]}, valid pixels {100 * valid.mean():.1f}%"] depth_img = None if fx is not None: # Middlebury: Z = baseline * f / (d + doffs), in mm (all at full resolution) depth = np.zeros_like(disp) depth[valid] = baseline * fx / (disp[valid] / scale + doffs) / 1000.0 depth_img = colorize(-depth, valid) # near = warm notes.append(f"Median depth {np.median(depth[valid]):.2f} m") if gt is not None: gt = cv2.resize(gt, size, interpolation=cv2.INTER_NEAREST) m = valid & np.isfinite(gt) & (gt > 0) err = np.abs(disp[m] - gt[m]) notes.append( f"vs ground truth: mean abs error {err.mean():.2f}px, " f"pixels off by >2px at full resolution: {100 * (err > 2 * scale).mean():.1f}%" ) return colorize(disp, valid), depth_img, " \n".join(notes) def rand_point(n, rng): return tuple(int(v) for v in rng.integers(0, n, 3)) def run_mapf(agents, grid, weight_ratio, solver, seed): rng = np.random.default_rng(int(seed)) reqs = [(rand_point(grid, rng), rand_point(grid, rng)) for _ in range(int(agents))] out = Path(tempfile.mkdtemp()) for mode in ("fast", "cbs"): (out / mode).mkdir() # problem2 writes into these but does not create them random.seed(int(seed)) results = run_problem2(reqs, grid_size=int(grid), weight_ratio=weight_ratio, seed=int(seed), solver=solver, output_dir=str(out)) rows = [] for mode, r in (results or {}).items(): st = r.get("stats", {}) rows.append([mode, st.get("status", "ok"), round(1000 * st.get("time", 0), 1), st.get("cost", ""), r.get("makespan", "")]) imgs = sorted(str(p) for p in out.rglob("*.png")) html = next(iter(out.rglob("*.html")), None) return imgs, rows, (str(html) if html else None) with gr.Blocks(title="Stereo Vision & Multi-Agent Pathfinding") as demo: gr.Markdown("# Stereo Vision & Multi-Agent Pathfinding") with gr.Tab("Stereo depth"): gr.Markdown("Pick a Middlebury scene (scored against ground truth) or upload your own **rectified** pair.") with gr.Row(): scene = gr.Dropdown(list(SCENES), value="Adirondack", label="Middlebury scene") scale = gr.Slider(0.15, 0.5, value=0.25, step=0.05, label="Scale (lower = faster)") block = gr.Slider(3, 15, value=7, step=2, label="SGBM block size") with gr.Accordion("Or upload a rectified stereo pair", open=False): with gr.Row(): lu = gr.Image(type="numpy", label="Left") ru = gr.Image(type="numpy", label="Right") with gr.Row(): disp_out = gr.Image(label="Disparity") depth_out = gr.Image(label="Depth (Middlebury scenes only)") stats = gr.Markdown() gr.Button("Compute", variant="primary").click(run_stereo, [scene, lu, ru, scale, block], [disp_out, depth_out, stats]) with gr.Tab("Multi-agent pathfinding"): gr.Markdown("Random start/goal pairs on a weighted 3D grid; compare Space-Time A* with CBS.") with gr.Row(): agents = gr.Slider(2, 8, value=3, step=1, label="Agents") grid = gr.Slider(11, 51, value=21, step=2, label="Grid size (N^3)") wr = gr.Slider(0.0, 0.5, value=0.1, step=0.05, label="Obstacle weight ratio") solver = gr.Radio(["fast", "cbs", "compare"], value="compare", label="Solver") seed = gr.Number(value=42, precision=0, label="Seed") table = gr.Dataframe(headers=["solver", "status", "time (ms)", "flowtime", "makespan"]) gallery = gr.Gallery(label="Trajectories", columns=2, height=420) html_file = gr.File(label="Interactive 3D view (HTML)") gr.Button("Plan paths", variant="primary").click(run_mapf, [agents, grid, wr, solver, seed], [gallery, table, html_file]) if __name__ == "__main__": demo.queue(max_size=8).launch()