Download services/lighting/intrinsic.py from modventures/floor-visualizer-gpu: direct link, hf CLI and curl.
- Browser
- Download file 6.96 kB
-
https://huggingface.co/spaces/modventures/floor-visualizer-gpu/resolve/main/services/lighting/intrinsic.py
- Command line
-
hf download hf://spaces/modventures/floor-visualizer-gpu/services/lighting/intrinsic.py
-
curl -L -o intrinsic.py https://huggingface.co/spaces/modventures/floor-visualizer-gpu/resolve/main/services/lighting/intrinsic.py
6.96 kB
| """Intrinsic-decomposition shading extraction and lighting map construction.""" | |
| from __future__ import annotations | |
| import numpy as np | |
| from config.lighting import ( | |
| SHADE_MAP_MAX, | |
| SHADE_MAP_MIN, | |
| ) | |
| from config.runtime import ENABLE_INTRINSIC_SHADING | |
| from state import intrinsic_models, device, gpu_inference | |
| from enums import IntrinsicAlignment | |
| from utils import to_numpy | |
| from services.lighting.luminance import ( | |
| broad_luminance_relative, | |
| decode_relative_map, | |
| encode_relative_map, | |
| masked_gaussian_blur, | |
| normalize_relative_field, | |
| ) | |
| from services.lighting.shading import ( | |
| build_luminance_lighting_maps, | |
| preserve_observed_lighting, | |
| smooth_relative_lighting, | |
| suppress_replaced_material_lighting, | |
| ) | |
| # --------------------------------------------------------------------------- | |
| # Intrinsic shading extraction | |
| # --------------------------------------------------------------------------- | |
| def extract_intrinsic_shading(results: dict) -> np.ndarray | None: | |
| shading = None | |
| if "gry_shd" in results: | |
| shading = results["gry_shd"] | |
| elif "dif_shd" in results: | |
| shading = results["dif_shd"] | |
| else: | |
| for key, value in results.items(): | |
| if "shd" in key or "shading" in key: | |
| shading = value | |
| break | |
| if shading is None: | |
| return None | |
| shading = to_numpy(shading) | |
| if shading is None: | |
| return None | |
| if shading.ndim == 3: | |
| if shading.shape[2] == 1: | |
| shading = shading[:, :, 0] | |
| else: | |
| shading = ( | |
| shading[:, :, 0] * 0.299 | |
| + shading[:, :, 1] * 0.587 | |
| + shading[:, :, 2] * 0.114 | |
| ) | |
| if shading.ndim != 2: | |
| return None | |
| return shading.astype(np.float32) | |
| # --------------------------------------------------------------------------- | |
| # Normalization and alignment | |
| # --------------------------------------------------------------------------- | |
| def normalize_relative_shading( | |
| shading: np.ndarray, | |
| surface_mask: np.ndarray, | |
| ) -> np.ndarray | None: | |
| import cv2 | |
| h, w = surface_mask.shape[:2] | |
| if shading.shape[:2] != (h, w): | |
| shading = cv2.resize(shading, (w, h), interpolation=cv2.INTER_LINEAR) | |
| shading = shading.astype(np.float32) | |
| floor_vals = shading[surface_mask > 0] | |
| floor_vals = floor_vals[np.isfinite(floor_vals) & (floor_vals > 1e-6)] | |
| if floor_vals.size == 0: | |
| return None | |
| median_val = float(np.median(floor_vals)) | |
| if median_val < 1e-6: | |
| return None | |
| relative = shading / median_val | |
| relative[~np.isfinite(relative)] = 1.0 | |
| relative[surface_mask == 0] = 1.0 | |
| return relative.astype(np.float32) | |
| def align_intrinsic_shading_to_luminance( | |
| relative_shading: np.ndarray, | |
| img_np: np.ndarray, | |
| surface_mask: np.ndarray, | |
| ) -> tuple[np.ndarray, str]: | |
| """Correct intrinsic maps that encode lightness with the opposite polarity.""" | |
| observed = broad_luminance_relative(img_np, surface_mask) | |
| if observed is None: | |
| return relative_shading, IntrinsicAlignment.UNCHECKED | |
| sigma = max(12.0, min(surface_mask.shape[:2]) / 32.0) | |
| intrinsic_broad = masked_gaussian_blur(relative_shading, surface_mask, sigma) | |
| intrinsic_broad = normalize_relative_field(intrinsic_broad, surface_mask) | |
| if intrinsic_broad is None: | |
| return relative_shading, IntrinsicAlignment.UNCHECKED | |
| valid = ( | |
| (surface_mask > 0) | |
| & np.isfinite(observed) | |
| & np.isfinite(intrinsic_broad) | |
| & (observed > 1e-4) | |
| & (intrinsic_broad > 1e-4) | |
| ) | |
| if int(valid.sum()) < 256: | |
| return relative_shading, IntrinsicAlignment.UNCHECKED | |
| observed_log = np.log(observed[valid]) | |
| intrinsic_log = np.log(intrinsic_broad[valid]) | |
| if float(np.std(observed_log)) < 1e-4 or float(np.std(intrinsic_log)) < 1e-4: | |
| return relative_shading, IntrinsicAlignment.UNCHECKED | |
| correlation = float(np.corrcoef(observed_log, intrinsic_log)[0, 1]) | |
| if correlation < -0.12: | |
| corrected = normalize_relative_field(1.0 / np.maximum(relative_shading, 1e-4), surface_mask) | |
| if corrected is not None: | |
| return corrected, IntrinsicAlignment.INVERTED_CORRECTED | |
| return relative_shading, IntrinsicAlignment.ALIGNED if correlation >= 0.10 else IntrinsicAlignment.LOW_CONFIDENCE | |
| # --------------------------------------------------------------------------- | |
| # Intrinsic lighting maps | |
| # --------------------------------------------------------------------------- | |
| def build_intrinsic_lighting_maps( | |
| img_np: np.ndarray, | |
| surface_mask: np.ndarray, | |
| replaced_material_mask: np.ndarray | None = None, | |
| ) -> dict[str, np.ndarray | None] | None: | |
| if not surface_mask.any() or intrinsic_models is None: | |
| return None | |
| try: | |
| img_float = img_np.astype(np.float32) / 255.0 | |
| from intrinsic.pipeline import run_pipeline | |
| with gpu_inference(): | |
| results = run_pipeline(intrinsic_models, img_float, device=str(device)) | |
| shading = extract_intrinsic_shading(results) | |
| if shading is None: | |
| return None | |
| relative = normalize_relative_shading(shading, surface_mask) | |
| if relative is None: | |
| return None | |
| relative, alignment = align_intrinsic_shading_to_luminance(relative, img_np, surface_mask) | |
| smoothed = smooth_relative_lighting(relative, surface_mask) | |
| if smoothed is None: | |
| return None | |
| luminance_maps = build_luminance_lighting_maps( | |
| img_np, | |
| surface_mask, | |
| replaced_material_mask, | |
| ) | |
| if luminance_maps and luminance_maps.get("shadeMap") is not None: | |
| observed = decode_relative_map( | |
| luminance_maps["shadeMap"], | |
| SHADE_MAP_MIN, | |
| SHADE_MAP_MAX, | |
| ) | |
| smoothed = preserve_observed_lighting(smoothed, observed, surface_mask) | |
| alignment = f"{alignment}+observed-lighting" | |
| smoothed = suppress_replaced_material_lighting( | |
| smoothed, | |
| surface_mask, | |
| replaced_material_mask, | |
| ) | |
| print(f"[LIGHTING] intrinsic shading alignment={alignment}", flush=True) | |
| from services.lighting.showroom import build_showroom_shade_map_from_relative | |
| from services.lighting.reflection import build_reflection_map | |
| return { | |
| "shadeMap": encode_relative_map(smoothed, SHADE_MAP_MIN, SHADE_MAP_MAX), | |
| "showroomShadeMap": build_showroom_shade_map_from_relative(smoothed, surface_mask), | |
| "reflectionMap": build_reflection_map( | |
| img_np, | |
| surface_mask, | |
| smoothed, | |
| replaced_material_mask, | |
| ), | |
| } | |
| except Exception as exc: | |
| print(f"Intrinsic shading decomposition failed: {exc}. Falling back to default luminance shading.", flush=True) | |
| return None | |