Spaces:
Paused
Paused
github-actions[bot] commited on
Commit ·
3e7c41b
1
Parent(s): 9e34180
Automated Deploy: 3437071a0a0f6ade4d49833e2f830224e1dab23c
Browse files
services/lighting/intrinsic.py
CHANGED
|
@@ -21,6 +21,7 @@ from services.lighting.luminance import (
|
|
| 21 |
)
|
| 22 |
from services.lighting.shading import (
|
| 23 |
build_luminance_lighting_maps,
|
|
|
|
| 24 |
preserve_observed_lighting,
|
| 25 |
smooth_relative_lighting,
|
| 26 |
suppress_replaced_material_lighting,
|
|
@@ -191,6 +192,7 @@ def build_intrinsic_lighting_maps(
|
|
| 191 |
smoothed,
|
| 192 |
replaced_material_mask,
|
| 193 |
),
|
|
|
|
| 194 |
}
|
| 195 |
except Exception as exc:
|
| 196 |
print(f"Intrinsic shading decomposition failed: {exc}. Falling back to default luminance shading.", flush=True)
|
|
|
|
| 21 |
)
|
| 22 |
from services.lighting.shading import (
|
| 23 |
build_luminance_lighting_maps,
|
| 24 |
+
compute_color_tint,
|
| 25 |
preserve_observed_lighting,
|
| 26 |
smooth_relative_lighting,
|
| 27 |
suppress_replaced_material_lighting,
|
|
|
|
| 192 |
smoothed,
|
| 193 |
replaced_material_mask,
|
| 194 |
),
|
| 195 |
+
"colorTint": compute_color_tint(img_np, surface_mask),
|
| 196 |
}
|
| 197 |
except Exception as exc:
|
| 198 |
print(f"Intrinsic shading decomposition failed: {exc}. Falling back to default luminance shading.", flush=True)
|
services/lighting/shading.py
CHANGED
|
@@ -23,10 +23,15 @@ from services.lighting.luminance import (
|
|
| 23 |
# ---------------------------------------------------------------------------
|
| 24 |
|
| 25 |
def smooth_relative_lighting(relative_shading: np.ndarray, surface_mask: np.ndarray) -> np.ndarray | None:
|
| 26 |
-
"""Keep room and cast-shadow illumination while rejecting old material detail.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
min_side = min(surface_mask.shape[:2])
|
| 28 |
-
broad_sigma = max(
|
| 29 |
-
local_sigma = max(2.
|
| 30 |
bounded = np.clip(relative_shading.astype(np.float32), 0.04, 4.0)
|
| 31 |
broad = masked_gaussian_blur(bounded, surface_mask, broad_sigma)
|
| 32 |
local = masked_gaussian_blur(bounded, surface_mask, local_sigma)
|
|
@@ -36,12 +41,12 @@ def smooth_relative_lighting(relative_shading: np.ndarray, surface_mask: np.ndar
|
|
| 36 |
|
| 37 |
local_contrast = local / np.maximum(broad, 1e-4)
|
| 38 |
local_log = np.log(np.maximum(local_contrast, 1e-4))
|
| 39 |
-
local_log = np.clip(local_log, np.log(0.
|
| 40 |
-
combined = broad_relative * np.exp(local_log * 0.
|
| 41 |
normalized = normalize_relative_field(combined, surface_mask)
|
| 42 |
if normalized is None:
|
| 43 |
return None
|
| 44 |
-
normalized = np.clip(normalized, SHADE_MAP_MIN, 1.
|
| 45 |
normalized[surface_mask == 0] = 1.0
|
| 46 |
return normalized
|
| 47 |
|
|
@@ -139,9 +144,62 @@ def build_luminance_lighting_maps(
|
|
| 139 |
None,
|
| 140 |
replaced_material_mask,
|
| 141 |
),
|
|
|
|
| 142 |
}
|
| 143 |
|
| 144 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
# ---------------------------------------------------------------------------
|
| 146 |
# Statistics
|
| 147 |
# ---------------------------------------------------------------------------
|
|
|
|
| 23 |
# ---------------------------------------------------------------------------
|
| 24 |
|
| 25 |
def smooth_relative_lighting(relative_shading: np.ndarray, surface_mask: np.ndarray) -> np.ndarray | None:
|
| 26 |
+
"""Keep room and cast-shadow illumination while rejecting old material detail.
|
| 27 |
+
|
| 28 |
+
Uses a reduced broad-blur sigma and a higher local-detail blend (85 %)
|
| 29 |
+
compared to the original (70 %) so that contact shadows and sharp light
|
| 30 |
+
boundaries are preserved rather than smeared into vague gradients.
|
| 31 |
+
"""
|
| 32 |
min_side = min(surface_mask.shape[:2])
|
| 33 |
+
broad_sigma = max(8.0, min_side / 52.0)
|
| 34 |
+
local_sigma = max(2.0, min_side / 180.0)
|
| 35 |
bounded = np.clip(relative_shading.astype(np.float32), 0.04, 4.0)
|
| 36 |
broad = masked_gaussian_blur(bounded, surface_mask, broad_sigma)
|
| 37 |
local = masked_gaussian_blur(bounded, surface_mask, local_sigma)
|
|
|
|
| 41 |
|
| 42 |
local_contrast = local / np.maximum(broad, 1e-4)
|
| 43 |
local_log = np.log(np.maximum(local_contrast, 1e-4))
|
| 44 |
+
local_log = np.clip(local_log, np.log(0.20), np.log(1.55))
|
| 45 |
+
combined = broad_relative * np.exp(local_log * 0.85)
|
| 46 |
normalized = normalize_relative_field(combined, surface_mask)
|
| 47 |
if normalized is None:
|
| 48 |
return None
|
| 49 |
+
normalized = np.clip(normalized, SHADE_MAP_MIN, 1.70)
|
| 50 |
normalized[surface_mask == 0] = 1.0
|
| 51 |
return normalized
|
| 52 |
|
|
|
|
| 144 |
None,
|
| 145 |
replaced_material_mask,
|
| 146 |
),
|
| 147 |
+
"colorTint": compute_color_tint(img_np, mask),
|
| 148 |
}
|
| 149 |
|
| 150 |
|
| 151 |
+
# ---------------------------------------------------------------------------
|
| 152 |
+
# Color temperature tint
|
| 153 |
+
# ---------------------------------------------------------------------------
|
| 154 |
+
|
| 155 |
+
def compute_color_tint(
|
| 156 |
+
img_np: np.ndarray,
|
| 157 |
+
surface_mask: np.ndarray,
|
| 158 |
+
) -> list[float] | None:
|
| 159 |
+
"""Extract the scene's color cast over the floor region as an RGB tint.
|
| 160 |
+
|
| 161 |
+
The tint is the ratio of each channel's median to the overall luminance
|
| 162 |
+
median, capturing warm/cool lighting without material albedo. Applied as
|
| 163 |
+
a per-pixel multiplicative tint in the frontend shader so the replacement
|
| 164 |
+
tile inherits the room's color temperature.
|
| 165 |
+
"""
|
| 166 |
+
if not surface_mask.any():
|
| 167 |
+
return None
|
| 168 |
+
|
| 169 |
+
mask = surface_mask > 0
|
| 170 |
+
linear = np.clip(img_np.astype(np.float32) / 255.0, 0.0, 1.0)
|
| 171 |
+
linear = np.where(
|
| 172 |
+
linear <= 0.04045,
|
| 173 |
+
linear / 12.92,
|
| 174 |
+
np.power((linear + 0.055) / 1.055, 2.4),
|
| 175 |
+
)
|
| 176 |
+
|
| 177 |
+
luminance = (
|
| 178 |
+
linear[:, :, 0] * 0.2126
|
| 179 |
+
+ linear[:, :, 1] * 0.7152
|
| 180 |
+
+ linear[:, :, 2] * 0.0722
|
| 181 |
+
)
|
| 182 |
+
lum_values = luminance[mask]
|
| 183 |
+
lum_values = lum_values[np.isfinite(lum_values) & (lum_values > 1e-4)]
|
| 184 |
+
if lum_values.size < 256:
|
| 185 |
+
return None
|
| 186 |
+
|
| 187 |
+
median_lum = float(np.median(lum_values))
|
| 188 |
+
if median_lum < 1e-4:
|
| 189 |
+
return None
|
| 190 |
+
|
| 191 |
+
tint = []
|
| 192 |
+
for ch in range(3):
|
| 193 |
+
ch_values = linear[:, :, ch][mask]
|
| 194 |
+
ch_values = ch_values[np.isfinite(ch_values)]
|
| 195 |
+
if ch_values.size < 256:
|
| 196 |
+
tint.append(1.0)
|
| 197 |
+
else:
|
| 198 |
+
median_ch = float(np.median(ch_values))
|
| 199 |
+
tint.append(round(np.clip(median_ch / median_lum, 0.7, 1.4), 4))
|
| 200 |
+
return tint
|
| 201 |
+
|
| 202 |
+
|
| 203 |
# ---------------------------------------------------------------------------
|
| 204 |
# Statistics
|
| 205 |
# ---------------------------------------------------------------------------
|
services/lighting/showroom.py
CHANGED
|
@@ -50,7 +50,7 @@ def build_showroom_shade_map_from_relative(
|
|
| 50 |
return None
|
| 51 |
|
| 52 |
showroom = broad / broad_median
|
| 53 |
-
showroom = 1.0 + (showroom - 1.0) * 0.
|
| 54 |
showroom = np.clip(showroom, SHOWROOM_SHADE_MAP_MIN, SHOWROOM_SHADE_MAP_MAX)
|
| 55 |
showroom[mask == 0] = 1.0
|
| 56 |
return encode_relative_map(showroom, SHOWROOM_SHADE_MAP_MIN, SHOWROOM_SHADE_MAP_MAX)
|
|
|
|
| 50 |
return None
|
| 51 |
|
| 52 |
showroom = broad / broad_median
|
| 53 |
+
showroom = 1.0 + (showroom - 1.0) * 0.45
|
| 54 |
showroom = np.clip(showroom, SHOWROOM_SHADE_MAP_MIN, SHOWROOM_SHADE_MAP_MAX)
|
| 55 |
showroom[mask == 0] = 1.0
|
| 56 |
return encode_relative_map(showroom, SHOWROOM_SHADE_MAP_MIN, SHOWROOM_SHADE_MAP_MAX)
|
services/pipeline/builder.py
CHANGED
|
@@ -160,8 +160,10 @@ def build_segmentation_bundle(contents: bytes, debug: PipelineDebugCapture | Non
|
|
| 160 |
lighting_maps = build_luminance_lighting_maps(img_np, surface_mask, replaced_material_mask)
|
| 161 |
if lighting_maps and lighting_maps.get("shadeMap") is not None:
|
| 162 |
lighting_source = "luminance"
|
|
|
|
| 163 |
if lighting_maps:
|
| 164 |
shade_map, showroom_shade_map, reflection_map = lighting_maps.get("shadeMap"), lighting_maps.get("showroomShadeMap"), lighting_maps.get("reflectionMap")
|
|
|
|
| 165 |
shade_statistics = lighting_map_statistics(shade_map, surface_mask)
|
| 166 |
el = (time.perf_counter() - t0) * 1000
|
| 167 |
print(f"[TIMING] Lighting took {el / 1000:.3f} seconds (source={lighting_source})", flush=True)
|
|
@@ -211,6 +213,7 @@ def build_segmentation_bundle(contents: bytes, debug: PipelineDebugCapture | Non
|
|
| 211 |
homography=homography, plane=plane, surface_mapping=surface_mapping,
|
| 212 |
use_surface_uv=use_surface_uv, shade_map=shade_map,
|
| 213 |
showroom_shade_map=showroom_shade_map, reflection_map=reflection_map,
|
|
|
|
| 214 |
floor_mask=floor_mask, surface_mask=surface_mask,
|
| 215 |
replaced_material_mask=replaced_material_mask, occlusion_mask=occlusion_mask,
|
| 216 |
floor_direction=floor_direction, sam_metadata=sam_metadata,
|
|
|
|
| 160 |
lighting_maps = build_luminance_lighting_maps(img_np, surface_mask, replaced_material_mask)
|
| 161 |
if lighting_maps and lighting_maps.get("shadeMap") is not None:
|
| 162 |
lighting_source = "luminance"
|
| 163 |
+
color_tint = None
|
| 164 |
if lighting_maps:
|
| 165 |
shade_map, showroom_shade_map, reflection_map = lighting_maps.get("shadeMap"), lighting_maps.get("showroomShadeMap"), lighting_maps.get("reflectionMap")
|
| 166 |
+
color_tint = lighting_maps.get("colorTint")
|
| 167 |
shade_statistics = lighting_map_statistics(shade_map, surface_mask)
|
| 168 |
el = (time.perf_counter() - t0) * 1000
|
| 169 |
print(f"[TIMING] Lighting took {el / 1000:.3f} seconds (source={lighting_source})", flush=True)
|
|
|
|
| 213 |
homography=homography, plane=plane, surface_mapping=surface_mapping,
|
| 214 |
use_surface_uv=use_surface_uv, shade_map=shade_map,
|
| 215 |
showroom_shade_map=showroom_shade_map, reflection_map=reflection_map,
|
| 216 |
+
color_tint=color_tint,
|
| 217 |
floor_mask=floor_mask, surface_mask=surface_mask,
|
| 218 |
replaced_material_mask=replaced_material_mask, occlusion_mask=occlusion_mask,
|
| 219 |
floor_direction=floor_direction, sam_metadata=sam_metadata,
|
services/pipeline/bundle_assembly.py
CHANGED
|
@@ -39,6 +39,7 @@ def build_floor_segment(
|
|
| 39 |
shade_map: np.ndarray | None,
|
| 40 |
showroom_shade_map: np.ndarray | None,
|
| 41 |
reflection_map: np.ndarray | None,
|
|
|
|
| 42 |
floor_mask: np.ndarray,
|
| 43 |
surface_mask: np.ndarray,
|
| 44 |
replaced_material_mask: np.ndarray,
|
|
@@ -74,6 +75,7 @@ def build_floor_segment(
|
|
| 74 |
"shadeMap": encode_b64(shade_map),
|
| 75 |
"showroomShadeMap": encode_b64(showroom_shade_map),
|
| 76 |
"reflectionMap": encode_b64(reflection_map),
|
|
|
|
| 77 |
"metadata": {
|
| 78 |
"segmenter": segmenter_metadata_name(),
|
| 79 |
"segmentationModel": models["segmentation"]["name"],
|
|
|
|
| 39 |
shade_map: np.ndarray | None,
|
| 40 |
showroom_shade_map: np.ndarray | None,
|
| 41 |
reflection_map: np.ndarray | None,
|
| 42 |
+
color_tint: list[float] | None = None,
|
| 43 |
floor_mask: np.ndarray,
|
| 44 |
surface_mask: np.ndarray,
|
| 45 |
replaced_material_mask: np.ndarray,
|
|
|
|
| 75 |
"shadeMap": encode_b64(shade_map),
|
| 76 |
"showroomShadeMap": encode_b64(showroom_shade_map),
|
| 77 |
"reflectionMap": encode_b64(reflection_map),
|
| 78 |
+
"colorTint": color_tint,
|
| 79 |
"metadata": {
|
| 80 |
"segmenter": segmenter_metadata_name(),
|
| 81 |
"segmentationModel": models["segmentation"]["name"],
|