Download lab_core.py from pedroivospatial/pis-spatial-intelligence-lab: direct link, hf CLI and curl.
- Browser
- Download file 7.06 kB
-
https://huggingface.co/spaces/pedroivospatial/pis-spatial-intelligence-lab/resolve/main/lab_core.py
- Command line
-
hf download hf://spaces/pedroivospatial/pis-spatial-intelligence-lab/lab_core.py
-
curl -L -o lab_core.py https://huggingface.co/spaces/pedroivospatial/pis-spatial-intelligence-lab/resolve/main/lab_core.py
7.06 kB
| """Deterministic, client-data-free visual fixtures and diagnostics for the PIS lab.""" | |
| from __future__ import annotations | |
| import numpy as np | |
| from PIL import Image, ImageDraw | |
| SAMPLE_LABELS = { | |
| "Synthetic training venue": "training_room", | |
| "Synthetic retail room": "retail_room", | |
| "Synthetic reception studio": "reception_room", | |
| } | |
| def _base_room(width: int, height: int, palette: dict[str, tuple[int, int, int]]) -> Image.Image: | |
| image = Image.new("RGB", (width, height), palette["back"]) | |
| draw = ImageDraw.Draw(image) | |
| vx, vy = width // 2, int(height * 0.39) | |
| draw.polygon([(0, 0), (width, 0), (int(width * 0.72), vy), (int(width * 0.28), vy)], fill=palette["ceiling"]) | |
| draw.polygon([(0, height), (width, height), (int(width * 0.72), vy), (int(width * 0.28), vy)], fill=palette["floor"]) | |
| draw.polygon([(0, 0), (int(width * 0.28), vy), (int(width * 0.28), int(height * 0.77)), (0, height)], fill=palette["left"]) | |
| draw.polygon([(width, 0), (int(width * 0.72), vy), (int(width * 0.72), int(height * 0.77)), (width, height)], fill=palette["right"]) | |
| draw.rectangle([(int(width * 0.28), vy), (int(width * 0.72), int(height * 0.77))], fill=palette["back"]) | |
| for y in (0.82, 0.9, 0.97): | |
| py = int(height * y) | |
| draw.line([(0, py), (width, py)], fill=palette["grid"], width=2) | |
| for x in (0.12, 0.26, 0.74, 0.88): | |
| px = int(width * x) | |
| draw.line([(px, height), (vx, vy)], fill=palette["grid"], width=2) | |
| draw.rectangle([(int(width * 0.46), int(height * 0.06)), (int(width * 0.54), int(height * 0.09))], fill=(238, 248, 255)) | |
| return image | |
| def _training_room(width: int, height: int) -> Image.Image: | |
| palette = { | |
| "ceiling": (20, 29, 42), "floor": (27, 52, 69), "left": (45, 58, 72), | |
| "right": (38, 50, 64), "back": (63, 76, 89), "grid": (73, 96, 109), | |
| } | |
| image = _base_room(width, height, palette) | |
| draw = ImageDraw.Draw(image) | |
| draw.polygon([(170, 470), (790, 470), (675, 325), (285, 325)], fill=(20, 86, 139), outline=(92, 189, 255)) | |
| draw.polygon([(205, 438), (755, 438), (663, 343), (297, 343)], outline=(222, 241, 255), width=4) | |
| for x in (340, 480, 620): | |
| draw.ellipse([(x - 22, 195), (x + 22, 240)], fill=(25, 28, 33), outline=(240, 180, 41), width=4) | |
| draw.rectangle([(x - 12, 230), (x + 12, 335)], fill=(43, 47, 54), outline=(240, 180, 41), width=3) | |
| draw.line([(x, 195), (x, 135)], fill=(190, 196, 204), width=3) | |
| draw.rectangle([(308, 258), (652, 274)], fill=(18, 25, 33)) | |
| return image | |
| def _retail_room(width: int, height: int) -> Image.Image: | |
| palette = { | |
| "ceiling": (236, 232, 221), "floor": (167, 144, 119), "left": (209, 198, 179), | |
| "right": (196, 186, 171), "back": (226, 218, 202), "grid": (148, 125, 104), | |
| } | |
| image = _base_room(width, height, palette) | |
| draw = ImageDraw.Draw(image) | |
| for x0, x1 in ((55, 218), (742, 905)): | |
| draw.rectangle([(x0, 125), (x1, 440)], fill=(89, 65, 46), outline=(43, 31, 23), width=4) | |
| for y in (205, 285, 365): | |
| draw.rectangle([(x0 + 8, y), (x1 - 8, y + 8)], fill=(53, 38, 28)) | |
| for row, y in enumerate((165, 245, 325, 405)): | |
| for col in range(3): | |
| px = x0 + 20 + col * 45 | |
| color = ((48 + row * 28) % 200, (112 + col * 35) % 210, (165 - row * 20) % 200) | |
| draw.rectangle([(px, y - 25), (px + 28, y)], fill=color) | |
| draw.polygon([(350, 485), (610, 485), (570, 380), (390, 380)], fill=(218, 206, 188), outline=(89, 65, 46), width=5) | |
| return image | |
| def _reception_room(width: int, height: int) -> Image.Image: | |
| palette = { | |
| "ceiling": (229, 237, 243), "floor": (106, 117, 126), "left": (183, 201, 214), | |
| "right": (168, 190, 206), "back": (208, 222, 231), "grid": (85, 99, 108), | |
| } | |
| image = _base_room(width, height, palette) | |
| draw = ImageDraw.Draw(image) | |
| draw.rectangle([(410, 250), (550, 375)], fill=(44, 63, 77), outline=(238, 248, 255), width=4) | |
| draw.ellipse([(458, 285), (502, 329)], outline=(56, 189, 248), width=5) | |
| draw.polygon([(285, 470), (690, 470), (630, 355), (350, 355)], fill=(36, 52, 65), outline=(56, 189, 248), width=5) | |
| draw.polygon([(315, 432), (660, 432), (620, 375), (360, 375)], fill=(232, 241, 246)) | |
| for x in (165, 785): | |
| draw.rectangle([(x - 32, 310), (x + 32, 440)], fill=(58, 80, 63), outline=(31, 53, 37), width=3) | |
| draw.ellipse([(x - 55, 250), (x + 55, 345)], fill=(67, 123, 79)) | |
| return image | |
| def generate_scene(scene_key: str, width: int = 960, height: int = 600) -> Image.Image: | |
| generators = { | |
| "training_room": _training_room, | |
| "retail_room": _retail_room, | |
| "reception_room": _reception_room, | |
| } | |
| if scene_key not in generators: | |
| raise ValueError(f"Unknown synthetic scene: {scene_key}") | |
| native = generators[scene_key](960, 600) | |
| if (width, height) == native.size: | |
| return native | |
| return native.resize((width, height), Image.Resampling.LANCZOS) | |
| def _normalize_depth(depth_image: Image.Image) -> np.ndarray: | |
| values = np.asarray(depth_image.convert("F"), dtype=np.float32) | |
| low, high = np.percentile(values, [1.0, 99.0]) | |
| if not np.isfinite(low) or not np.isfinite(high) or high <= low: | |
| return np.zeros_like(values, dtype=np.float32) | |
| return np.clip((values - low) / (high - low), 0.0, 1.0) | |
| def colorize_depth(depth_image: Image.Image) -> Image.Image: | |
| """Apply a compact navy-cyan-amber palette without external plotting libraries.""" | |
| normalized = _normalize_depth(depth_image) | |
| stops = np.array([[7, 17, 31], [11, 92, 143], [56, 189, 248], [251, 191, 36]], dtype=np.float32) | |
| position = normalized * (len(stops) - 1) | |
| left = np.floor(position).astype(np.int32) | |
| right = np.clip(left + 1, 0, len(stops) - 1) | |
| weight = (position - left)[..., None] | |
| rgb = stops[left] * (1.0 - weight) + stops[right] * weight | |
| return Image.fromarray(np.clip(rgb, 0, 255).astype(np.uint8), mode="RGB") | |
| def analyze_depth_signal(depth_image: Image.Image, sample_label: str, model_id: str) -> str: | |
| normalized = _normalize_depth(depth_image) | |
| spread = float(np.std(normalized)) | |
| grad_y, grad_x = np.gradient(normalized) | |
| edge_energy = float(np.mean(np.hypot(grad_x, grad_y))) | |
| near_far_balance = float(np.mean(normalized > 0.5)) | |
| signal_score = int(np.clip((spread * 240.0) + (edge_energy * 900.0), 0, 100)) | |
| return f"""### Experimental depth signal | |
| | Check | Result | | |
| |---|---:| | |
| | Scene | {sample_label} | | |
| | Normalized depth spread | {spread:.3f} | | |
| | Depth-edge energy | {edge_energy:.4f} | | |
| | Near/far balance | {near_far_balance:.1%} | | |
| | Demonstration signal score | {signal_score}/100 | | |
| **Interpretation:** this confirms that the model produces a differentiated relative-depth | |
| field for the synthetic scene. The score is an internal visualization aid—not accuracy, | |
| capture quality, metric distance, survey evidence, or a commercial guarantee. | |
| Model: `{model_id}` · Input: deterministic synthetic image · Stored client data: none | |
| """ | |