github-actions[bot] commited on
Commit ·
6a5eed7
1
Parent(s): 658eae3
Automated Deploy: 1aa7113861a4ca00d34fdbe19462d066d23e7c29
Browse files- app.py +150 -60
- frontend/viz2d-demo/e2e/lighting-preservation.spec.ts +242 -0
- test_floor_geometry.py +102 -5
- visualizer.gpu.toml +0 -1
app.py
CHANGED
|
@@ -511,10 +511,12 @@ moge_model = None
|
|
| 511 |
sam2_predictor = None
|
| 512 |
intrinsic_models = None
|
| 513 |
|
| 514 |
-
|
|
|
|
| 515 |
SHADE_MAP_MAX = 1.85
|
| 516 |
SHOWROOM_SHADE_MAP_MIN = 0.85
|
| 517 |
SHOWROOM_SHADE_MAP_MAX = 1.12
|
|
|
|
| 518 |
REFLECTION_RELATIVE_FLOOR = 1.04
|
| 519 |
REFLECTION_RELATIVE_CEILING = 1.95
|
| 520 |
REFLECTION_MAP_MIN = 0.0
|
|
@@ -1325,6 +1327,22 @@ def image_luminance(img_np: np.ndarray) -> np.ndarray:
|
|
| 1325 |
)
|
| 1326 |
|
| 1327 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1328 |
def inpaint_surface_values(values: np.ndarray, mask: np.ndarray, fill_value: float) -> np.ndarray:
|
| 1329 |
h, w = mask.shape[:2]
|
| 1330 |
filled = values.astype(np.float32).copy()
|
|
@@ -1459,7 +1477,7 @@ def build_luminance_lighting_maps(
|
|
| 1459 |
return None
|
| 1460 |
|
| 1461 |
mask = surface_mask.astype(np.uint8)
|
| 1462 |
-
luminance =
|
| 1463 |
h, w = mask.shape[:2]
|
| 1464 |
floor_values = luminance[mask > 0]
|
| 1465 |
if floor_values.size < max(256, int(h * w * 0.002)):
|
|
@@ -1471,10 +1489,11 @@ def build_luminance_lighting_maps(
|
|
| 1471 |
|
| 1472 |
filled = inpaint_surface_values(luminance, mask, median_lum)
|
| 1473 |
|
| 1474 |
-
|
| 1475 |
-
|
| 1476 |
-
shade =
|
| 1477 |
-
shade
|
|
|
|
| 1478 |
return {
|
| 1479 |
"shadeMap": encode_relative_map(shade, SHADE_MAP_MIN, SHADE_MAP_MAX),
|
| 1480 |
"showroomShadeMap": build_showroom_shade_map_from_luminance(luminance, mask, median_lum),
|
|
@@ -1568,7 +1587,7 @@ def broad_luminance_relative(
|
|
| 1568 |
img_np: np.ndarray,
|
| 1569 |
surface_mask: np.ndarray,
|
| 1570 |
) -> np.ndarray | None:
|
| 1571 |
-
luminance =
|
| 1572 |
floor_values = luminance[surface_mask > 0]
|
| 1573 |
floor_values = floor_values[np.isfinite(floor_values) & (floor_values > 1e-6)]
|
| 1574 |
if floor_values.size == 0:
|
|
@@ -1617,20 +1636,76 @@ def align_intrinsic_shading_to_luminance(
|
|
| 1617 |
return relative_shading, "intrinsic-aligned" if correlation >= 0.10 else "intrinsic-low-confidence"
|
| 1618 |
|
| 1619 |
|
| 1620 |
-
def smooth_relative_lighting(relative_shading: np.ndarray, surface_mask: np.ndarray) -> np.ndarray | None:
|
| 1621 |
-
"""Keep room-
|
| 1622 |
-
|
| 1623 |
-
|
| 1624 |
-
|
| 1625 |
-
|
| 1626 |
-
|
| 1627 |
-
|
| 1628 |
-
|
| 1629 |
-
|
| 1630 |
-
|
| 1631 |
-
|
| 1632 |
-
|
| 1633 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1634 |
img_np: np.ndarray,
|
| 1635 |
surface_mask: np.ndarray,
|
| 1636 |
) -> dict[str, np.ndarray | None] | None:
|
|
@@ -1649,10 +1724,20 @@ def build_intrinsic_lighting_maps(
|
|
| 1649 |
if relative is None:
|
| 1650 |
return None
|
| 1651 |
relative, alignment = align_intrinsic_shading_to_luminance(relative, img_np, surface_mask)
|
| 1652 |
-
smoothed = smooth_relative_lighting(relative, surface_mask)
|
| 1653 |
-
if smoothed is None:
|
| 1654 |
-
return None
|
| 1655 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1656 |
|
| 1657 |
return {
|
| 1658 |
"shadeMap": encode_relative_map(smoothed, SHADE_MAP_MIN, SHADE_MAP_MAX),
|
|
@@ -1819,9 +1904,8 @@ def expand_surface_over_soft_floor_coverings(
|
|
| 1819 |
kernel,
|
| 1820 |
iterations=SOFT_FLOOR_COVERING_DILATION_ITERATIONS,
|
| 1821 |
)
|
| 1822 |
-
|
| 1823 |
-
|
| 1824 |
-
candidates = (surface_reach > 0) & (soft_reach > 0) & (~protected)
|
| 1825 |
expanded = surface.astype(np.uint8).copy()
|
| 1826 |
expanded[candidates] = 1
|
| 1827 |
expanded[protected] = 0
|
|
@@ -2966,18 +3050,16 @@ def build_floor_surface_mask(
|
|
| 2966 |
plane: dict | None,
|
| 2967 |
):
|
| 2968 |
h, w = floor_mask.shape[:2]
|
| 2969 |
-
kern_size = max(5, min(h, w) // 160) | 1
|
| 2970 |
-
kern = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (kern_size, kern_size))
|
| 2971 |
-
occluder_mask = np.isin(seg_map, class_ids(OCCLUDER_CLASSES)).astype(np.uint8)
|
| 2972 |
-
|
| 2973 |
-
|
| 2974 |
-
|
| 2975 |
-
|
| 2976 |
-
|
| 2977 |
-
|
| 2978 |
-
|
| 2979 |
-
surface = floor_mask.copy()
|
| 2980 |
-
surface[reject_dilated > 0] = 0
|
| 2981 |
surface[soft_covering_mask > 0] = 0
|
| 2982 |
if not surface.any():
|
| 2983 |
surface = floor_mask.copy()
|
|
@@ -2987,20 +3069,20 @@ def build_floor_surface_mask(
|
|
| 2987 |
if contours:
|
| 2988 |
filled = np.zeros((h, w), dtype=np.uint8)
|
| 2989 |
cv2.drawContours(filled, contours, -1, 1, cv2.FILLED)
|
| 2990 |
-
filled[
|
| 2991 |
filled[soft_covering_mask > 0] = 0
|
| 2992 |
surface = filled
|
| 2993 |
|
| 2994 |
if quad is not None and surface.any():
|
| 2995 |
plane_mask = np.zeros((h, w), dtype=np.uint8)
|
| 2996 |
cv2.fillConvexPoly(plane_mask, np.round(quad).astype(np.int32), 1)
|
| 2997 |
-
plane_mask[
|
| 2998 |
plane_mask[soft_covering_mask > 0] = 0
|
| 2999 |
near_floor = cv2.dilate(surface, kern, iterations=6)
|
| 3000 |
surface = cv2.bitwise_or(surface, cv2.bitwise_and(plane_mask, near_floor))
|
| 3001 |
surface[soft_covering_mask > 0] = 0
|
| 3002 |
|
| 3003 |
-
surface[
|
| 3004 |
surface[soft_covering_mask > 0] = 0
|
| 3005 |
if depth is not None and floor_mask.any():
|
| 3006 |
floor_depth = depth[(floor_mask > 0) & np.isfinite(depth)]
|
|
@@ -3014,24 +3096,24 @@ def build_floor_surface_mask(
|
|
| 3014 |
depth_keep = (depth >= lo - margin) & (depth <= hi + margin)
|
| 3015 |
surface = (surface & depth_keep.astype(np.uint8)).astype(np.uint8)
|
| 3016 |
surface[floor_mask > 0] = np.maximum(surface[floor_mask > 0], 1)
|
| 3017 |
-
surface[
|
| 3018 |
-
surface[
|
| 3019 |
surface[soft_covering_mask > 0] = 0
|
| 3020 |
|
| 3021 |
surface, geometry_filter_metadata = filter_surface_by_floor_geometry(surface, geometry, plane)
|
| 3022 |
-
surface[
|
| 3023 |
-
surface[
|
| 3024 |
surface[soft_covering_mask > 0] = 0
|
| 3025 |
|
| 3026 |
surface = clean_floor_mask(surface)
|
| 3027 |
-
surface[
|
| 3028 |
-
surface[
|
| 3029 |
surface[soft_covering_mask > 0] = 0
|
| 3030 |
|
| 3031 |
surface, edge_metadata = improve_surface_edge_coverage(surface, normal_edge_protected_mask)
|
| 3032 |
surface, post_edge_filter_metadata = filter_surface_by_floor_geometry(surface, geometry, plane)
|
| 3033 |
-
surface[
|
| 3034 |
-
surface[
|
| 3035 |
surface[soft_covering_mask > 0] = 0
|
| 3036 |
post_edge_filter_metadata = {
|
| 3037 |
f"postEdge{k[0].upper()}{k[1:]}": value
|
|
@@ -3044,8 +3126,8 @@ def build_floor_surface_mask(
|
|
| 3044 |
hard_protected_mask,
|
| 3045 |
)
|
| 3046 |
surface, post_soft_covering_filter_metadata = filter_surface_by_floor_geometry(surface, geometry, plane)
|
| 3047 |
-
surface[
|
| 3048 |
-
surface[
|
| 3049 |
post_soft_covering_filter_metadata = {
|
| 3050 |
f"postSoftFloorCovering{k[0].upper()}{k[1:]}": value
|
| 3051 |
for k, value in post_soft_covering_filter_metadata.items()
|
|
@@ -3102,8 +3184,13 @@ def run_segmentation(img: Image.Image, img_np: np.ndarray):
|
|
| 3102 |
inputs = seg_processor(images=img, return_tensors="pt").to(device)
|
| 3103 |
with torch.no_grad():
|
| 3104 |
outputs = seg_model(**inputs)
|
| 3105 |
-
|
| 3106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3107 |
|
| 3108 |
|
| 3109 |
def segmenter_metadata_name() -> str:
|
|
@@ -3473,8 +3560,7 @@ def foreground_occlusion_mask(surface_mask: np.ndarray, seg_map: np.ndarray) ->
|
|
| 3473 |
return np.zeros((h, w), dtype=np.uint8)
|
| 3474 |
|
| 3475 |
near_surface = cv2.dilate(surface_mask.astype(np.uint8), kern, iterations=5)
|
| 3476 |
-
|
| 3477 |
-
return ((occluders > 0) & (near_surface > 0)).astype(np.uint8)
|
| 3478 |
|
| 3479 |
|
| 3480 |
def build_manifest_segmentation_bundle(
|
|
@@ -3611,12 +3697,12 @@ def build_segmentation_bundle(contents: bytes, debug: PipelineDebugCapture | Non
|
|
| 3611 |
floor_class_ids = class_ids(FLOOR_SURFACE_CLASSES)
|
| 3612 |
raw_primary_floor_mask = np.isin(seg_map, primary_floor_ids).astype(np.uint8)
|
| 3613 |
floor_mask = raw_primary_floor_mask.copy()
|
| 3614 |
-
floor_mask = wall_subtract(floor_mask, seg_map, dilation=
|
| 3615 |
floor_mask = clean_floor_mask(floor_mask)
|
| 3616 |
floor_mask_source = "primary-floor"
|
| 3617 |
if int(floor_mask.sum()) < min_floor_area:
|
| 3618 |
floor_mask = np.isin(seg_map, floor_class_ids).astype(np.uint8)
|
| 3619 |
-
floor_mask = wall_subtract(floor_mask, seg_map, dilation=
|
| 3620 |
floor_mask = clean_floor_mask(floor_mask)
|
| 3621 |
floor_mask_source = "floor-surface-fallback"
|
| 3622 |
cleaned_floor_mask = floor_mask.copy()
|
|
@@ -3792,6 +3878,7 @@ def build_segmentation_bundle(contents: bytes, debug: PipelineDebugCapture | Non
|
|
| 3792 |
shade_map = lighting_maps.get("shadeMap")
|
| 3793 |
showroom_shade_map = lighting_maps.get("showroomShadeMap")
|
| 3794 |
reflection_map = lighting_maps.get("reflectionMap")
|
|
|
|
| 3795 |
lighting_elapsed = time.perf_counter() - t0
|
| 3796 |
print(
|
| 3797 |
"[TIMING] Lighting map construction took "
|
|
@@ -3940,11 +4027,14 @@ def build_segmentation_bundle(contents: bytes, debug: PipelineDebugCapture | Non
|
|
| 3940 |
),
|
| 3941 |
"shadingTransferEnabled": ENABLE_SHADING_TRANSFER,
|
| 3942 |
"lightingSource": lighting_source,
|
|
|
|
|
|
|
|
|
|
| 3943 |
"shadeMapRange": [SHADE_MAP_MIN, SHADE_MAP_MAX],
|
| 3944 |
"showroomShadeMapRange": [SHOWROOM_SHADE_MAP_MIN, SHOWROOM_SHADE_MAP_MAX],
|
| 3945 |
"renderPresetDefaults": {
|
| 3946 |
"preset": "realistic",
|
| 3947 |
-
"shadingStrength":
|
| 3948 |
"reflectionTransfer": True,
|
| 3949 |
"edgeFeatherPixels": 2,
|
| 3950 |
},
|
|
|
|
| 511 |
sam2_predictor = None
|
| 512 |
intrinsic_models = None
|
| 513 |
|
| 514 |
+
LIGHTING_TRANSFER_VERSION = 3
|
| 515 |
+
SHADE_MAP_MIN = 0.08
|
| 516 |
SHADE_MAP_MAX = 1.85
|
| 517 |
SHOWROOM_SHADE_MAP_MIN = 0.85
|
| 518 |
SHOWROOM_SHADE_MAP_MAX = 1.12
|
| 519 |
+
SHADE_MAP_ENCODING = "linear-relative"
|
| 520 |
REFLECTION_RELATIVE_FLOOR = 1.04
|
| 521 |
REFLECTION_RELATIVE_CEILING = 1.95
|
| 522 |
REFLECTION_MAP_MIN = 0.0
|
|
|
|
| 1327 |
)
|
| 1328 |
|
| 1329 |
|
| 1330 |
+
def image_linear_luminance(img_np: np.ndarray) -> np.ndarray:
|
| 1331 |
+
"""Return linear-light luminance in the same 0..255 scale as image_luminance."""
|
| 1332 |
+
srgb = np.clip(img_np.astype(np.float32) / 255.0, 0.0, 1.0)
|
| 1333 |
+
linear = np.where(
|
| 1334 |
+
srgb <= 0.04045,
|
| 1335 |
+
srgb / 12.92,
|
| 1336 |
+
np.power((srgb + 0.055) / 1.055, 2.4),
|
| 1337 |
+
)
|
| 1338 |
+
luminance = (
|
| 1339 |
+
linear[:, :, 0] * 0.2126
|
| 1340 |
+
+ linear[:, :, 1] * 0.7152
|
| 1341 |
+
+ linear[:, :, 2] * 0.0722
|
| 1342 |
+
)
|
| 1343 |
+
return luminance * 255.0
|
| 1344 |
+
|
| 1345 |
+
|
| 1346 |
def inpaint_surface_values(values: np.ndarray, mask: np.ndarray, fill_value: float) -> np.ndarray:
|
| 1347 |
h, w = mask.shape[:2]
|
| 1348 |
filled = values.astype(np.float32).copy()
|
|
|
|
| 1477 |
return None
|
| 1478 |
|
| 1479 |
mask = surface_mask.astype(np.uint8)
|
| 1480 |
+
luminance = image_linear_luminance(img_np)
|
| 1481 |
h, w = mask.shape[:2]
|
| 1482 |
floor_values = luminance[mask > 0]
|
| 1483 |
if floor_values.size < max(256, int(h * w * 0.002)):
|
|
|
|
| 1489 |
|
| 1490 |
filled = inpaint_surface_values(luminance, mask, median_lum)
|
| 1491 |
|
| 1492 |
+
relative = filled / median_lum
|
| 1493 |
+
relative[~np.isfinite(relative)] = 1.0
|
| 1494 |
+
shade = smooth_relative_lighting(relative, mask)
|
| 1495 |
+
if shade is None:
|
| 1496 |
+
return None
|
| 1497 |
return {
|
| 1498 |
"shadeMap": encode_relative_map(shade, SHADE_MAP_MIN, SHADE_MAP_MAX),
|
| 1499 |
"showroomShadeMap": build_showroom_shade_map_from_luminance(luminance, mask, median_lum),
|
|
|
|
| 1587 |
img_np: np.ndarray,
|
| 1588 |
surface_mask: np.ndarray,
|
| 1589 |
) -> np.ndarray | None:
|
| 1590 |
+
luminance = image_linear_luminance(img_np) / 255.0
|
| 1591 |
floor_values = luminance[surface_mask > 0]
|
| 1592 |
floor_values = floor_values[np.isfinite(floor_values) & (floor_values > 1e-6)]
|
| 1593 |
if floor_values.size == 0:
|
|
|
|
| 1636 |
return relative_shading, "intrinsic-aligned" if correlation >= 0.10 else "intrinsic-low-confidence"
|
| 1637 |
|
| 1638 |
|
| 1639 |
+
def smooth_relative_lighting(relative_shading: np.ndarray, surface_mask: np.ndarray) -> np.ndarray | None:
|
| 1640 |
+
"""Keep room and cast-shadow illumination while rejecting old material detail."""
|
| 1641 |
+
min_side = min(surface_mask.shape[:2])
|
| 1642 |
+
broad_sigma = max(12.0, min_side / 32.0)
|
| 1643 |
+
local_sigma = max(2.5, min_side / 160.0)
|
| 1644 |
+
bounded = np.clip(relative_shading.astype(np.float32), 0.04, 4.0)
|
| 1645 |
+
broad = masked_gaussian_blur(bounded, surface_mask, broad_sigma)
|
| 1646 |
+
local = masked_gaussian_blur(bounded, surface_mask, local_sigma)
|
| 1647 |
+
broad_relative = normalize_relative_field(broad, surface_mask)
|
| 1648 |
+
if broad_relative is None:
|
| 1649 |
+
return None
|
| 1650 |
+
|
| 1651 |
+
local_contrast = local / np.maximum(broad, 1e-4)
|
| 1652 |
+
local_log = np.log(np.maximum(local_contrast, 1e-4))
|
| 1653 |
+
local_log = np.clip(local_log, np.log(0.25), np.log(1.45))
|
| 1654 |
+
combined = broad_relative * np.exp(local_log * 0.70)
|
| 1655 |
+
normalized = normalize_relative_field(combined, surface_mask)
|
| 1656 |
+
if normalized is None:
|
| 1657 |
+
return None
|
| 1658 |
+
normalized = np.clip(normalized, SHADE_MAP_MIN, 1.65)
|
| 1659 |
+
normalized[surface_mask == 0] = 1.0
|
| 1660 |
+
return normalized
|
| 1661 |
+
|
| 1662 |
+
|
| 1663 |
+
def lighting_map_statistics(encoded: np.ndarray | None, surface_mask: np.ndarray) -> dict | None:
|
| 1664 |
+
if encoded is None or encoded.shape[:2] != surface_mask.shape[:2]:
|
| 1665 |
+
return None
|
| 1666 |
+
decoded = SHADE_MAP_MIN + (encoded.astype(np.float32) / 255.0) * (SHADE_MAP_MAX - SHADE_MAP_MIN)
|
| 1667 |
+
values = decoded[surface_mask > 0]
|
| 1668 |
+
if values.size == 0:
|
| 1669 |
+
return None
|
| 1670 |
+
percentiles = np.percentile(values, [1, 5, 50, 95, 99])
|
| 1671 |
+
return {
|
| 1672 |
+
"minimum": round(float(values.min()), 4),
|
| 1673 |
+
"p01": round(float(percentiles[0]), 4),
|
| 1674 |
+
"p05": round(float(percentiles[1]), 4),
|
| 1675 |
+
"median": round(float(percentiles[2]), 4),
|
| 1676 |
+
"p95": round(float(percentiles[3]), 4),
|
| 1677 |
+
"p99": round(float(percentiles[4]), 4),
|
| 1678 |
+
"maximum": round(float(values.max()), 4),
|
| 1679 |
+
}
|
| 1680 |
+
|
| 1681 |
+
|
| 1682 |
+
def decode_relative_map(encoded: np.ndarray, minimum: float, maximum: float) -> np.ndarray:
|
| 1683 |
+
return minimum + (encoded.astype(np.float32) / 255.0) * (maximum - minimum)
|
| 1684 |
+
|
| 1685 |
+
|
| 1686 |
+
def preserve_observed_lighting(
|
| 1687 |
+
intrinsic_relative: np.ndarray,
|
| 1688 |
+
observed_relative: np.ndarray,
|
| 1689 |
+
surface_mask: np.ndarray,
|
| 1690 |
+
) -> np.ndarray:
|
| 1691 |
+
"""Keep observed room shadows and highlights from being weakened by intrinsic decomposition."""
|
| 1692 |
+
fused = intrinsic_relative.astype(np.float32).copy()
|
| 1693 |
+
shadow_pixels = (surface_mask > 0) & (observed_relative < 1.0)
|
| 1694 |
+
highlight_pixels = (surface_mask > 0) & (observed_relative > 1.0)
|
| 1695 |
+
fused[shadow_pixels] = np.minimum(
|
| 1696 |
+
fused[shadow_pixels],
|
| 1697 |
+
observed_relative[shadow_pixels],
|
| 1698 |
+
)
|
| 1699 |
+
fused[highlight_pixels] = np.maximum(
|
| 1700 |
+
fused[highlight_pixels],
|
| 1701 |
+
observed_relative[highlight_pixels],
|
| 1702 |
+
)
|
| 1703 |
+
fused = np.clip(fused, SHADE_MAP_MIN, 1.65)
|
| 1704 |
+
fused[surface_mask == 0] = 1.0
|
| 1705 |
+
return fused
|
| 1706 |
+
|
| 1707 |
+
|
| 1708 |
+
def build_intrinsic_lighting_maps(
|
| 1709 |
img_np: np.ndarray,
|
| 1710 |
surface_mask: np.ndarray,
|
| 1711 |
) -> dict[str, np.ndarray | None] | None:
|
|
|
|
| 1724 |
if relative is None:
|
| 1725 |
return None
|
| 1726 |
relative, alignment = align_intrinsic_shading_to_luminance(relative, img_np, surface_mask)
|
| 1727 |
+
smoothed = smooth_relative_lighting(relative, surface_mask)
|
| 1728 |
+
if smoothed is None:
|
| 1729 |
+
return None
|
| 1730 |
+
|
| 1731 |
+
luminance_maps = build_luminance_lighting_maps(img_np, surface_mask)
|
| 1732 |
+
if luminance_maps and luminance_maps.get("shadeMap") is not None:
|
| 1733 |
+
observed = decode_relative_map(
|
| 1734 |
+
luminance_maps["shadeMap"],
|
| 1735 |
+
SHADE_MAP_MIN,
|
| 1736 |
+
SHADE_MAP_MAX,
|
| 1737 |
+
)
|
| 1738 |
+
smoothed = preserve_observed_lighting(smoothed, observed, surface_mask)
|
| 1739 |
+
alignment = f"{alignment}+observed-lighting"
|
| 1740 |
+
print(f"[LIGHTING] intrinsic shading alignment={alignment}", flush=True)
|
| 1741 |
|
| 1742 |
return {
|
| 1743 |
"shadeMap": encode_relative_map(smoothed, SHADE_MAP_MIN, SHADE_MAP_MAX),
|
|
|
|
| 1904 |
kernel,
|
| 1905 |
iterations=SOFT_FLOOR_COVERING_DILATION_ITERATIONS,
|
| 1906 |
)
|
| 1907 |
+
protected = protected_mask.astype(bool)
|
| 1908 |
+
candidates = (surface_reach > 0) & (soft_covering_mask > 0) & (~protected)
|
|
|
|
| 1909 |
expanded = surface.astype(np.uint8).copy()
|
| 1910 |
expanded[candidates] = 1
|
| 1911 |
expanded[protected] = 0
|
|
|
|
| 3050 |
plane: dict | None,
|
| 3051 |
):
|
| 3052 |
h, w = floor_mask.shape[:2]
|
| 3053 |
+
kern_size = max(5, min(h, w) // 160) | 1
|
| 3054 |
+
kern = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (kern_size, kern_size))
|
| 3055 |
+
occluder_mask = np.isin(seg_map, class_ids(OCCLUDER_CLASSES)).astype(np.uint8)
|
| 3056 |
+
soft_covering_mask = np.isin(seg_map, class_ids(SOFT_FLOOR_COVERING_CLASSES)).astype(np.uint8)
|
| 3057 |
+
reject_mask = np.isin(seg_map, class_ids(REJECT_SURFACE_CLASSES)).astype(np.uint8)
|
| 3058 |
+
hard_protected_mask = (occluder_mask > 0) | (reject_mask > 0)
|
| 3059 |
+
normal_edge_protected_mask = hard_protected_mask | (soft_covering_mask > 0)
|
| 3060 |
+
|
| 3061 |
+
surface = floor_mask.copy()
|
| 3062 |
+
surface[reject_mask > 0] = 0
|
|
|
|
|
|
|
| 3063 |
surface[soft_covering_mask > 0] = 0
|
| 3064 |
if not surface.any():
|
| 3065 |
surface = floor_mask.copy()
|
|
|
|
| 3069 |
if contours:
|
| 3070 |
filled = np.zeros((h, w), dtype=np.uint8)
|
| 3071 |
cv2.drawContours(filled, contours, -1, 1, cv2.FILLED)
|
| 3072 |
+
filled[reject_mask > 0] = 0
|
| 3073 |
filled[soft_covering_mask > 0] = 0
|
| 3074 |
surface = filled
|
| 3075 |
|
| 3076 |
if quad is not None and surface.any():
|
| 3077 |
plane_mask = np.zeros((h, w), dtype=np.uint8)
|
| 3078 |
cv2.fillConvexPoly(plane_mask, np.round(quad).astype(np.int32), 1)
|
| 3079 |
+
plane_mask[reject_mask > 0] = 0
|
| 3080 |
plane_mask[soft_covering_mask > 0] = 0
|
| 3081 |
near_floor = cv2.dilate(surface, kern, iterations=6)
|
| 3082 |
surface = cv2.bitwise_or(surface, cv2.bitwise_and(plane_mask, near_floor))
|
| 3083 |
surface[soft_covering_mask > 0] = 0
|
| 3084 |
|
| 3085 |
+
surface[occluder_mask > 0] = 0
|
| 3086 |
surface[soft_covering_mask > 0] = 0
|
| 3087 |
if depth is not None and floor_mask.any():
|
| 3088 |
floor_depth = depth[(floor_mask > 0) & np.isfinite(depth)]
|
|
|
|
| 3096 |
depth_keep = (depth >= lo - margin) & (depth <= hi + margin)
|
| 3097 |
surface = (surface & depth_keep.astype(np.uint8)).astype(np.uint8)
|
| 3098 |
surface[floor_mask > 0] = np.maximum(surface[floor_mask > 0], 1)
|
| 3099 |
+
surface[occluder_mask > 0] = 0
|
| 3100 |
+
surface[reject_mask > 0] = 0
|
| 3101 |
surface[soft_covering_mask > 0] = 0
|
| 3102 |
|
| 3103 |
surface, geometry_filter_metadata = filter_surface_by_floor_geometry(surface, geometry, plane)
|
| 3104 |
+
surface[occluder_mask > 0] = 0
|
| 3105 |
+
surface[reject_mask > 0] = 0
|
| 3106 |
surface[soft_covering_mask > 0] = 0
|
| 3107 |
|
| 3108 |
surface = clean_floor_mask(surface)
|
| 3109 |
+
surface[occluder_mask > 0] = 0
|
| 3110 |
+
surface[reject_mask > 0] = 0
|
| 3111 |
surface[soft_covering_mask > 0] = 0
|
| 3112 |
|
| 3113 |
surface, edge_metadata = improve_surface_edge_coverage(surface, normal_edge_protected_mask)
|
| 3114 |
surface, post_edge_filter_metadata = filter_surface_by_floor_geometry(surface, geometry, plane)
|
| 3115 |
+
surface[occluder_mask > 0] = 0
|
| 3116 |
+
surface[reject_mask > 0] = 0
|
| 3117 |
surface[soft_covering_mask > 0] = 0
|
| 3118 |
post_edge_filter_metadata = {
|
| 3119 |
f"postEdge{k[0].upper()}{k[1:]}": value
|
|
|
|
| 3126 |
hard_protected_mask,
|
| 3127 |
)
|
| 3128 |
surface, post_soft_covering_filter_metadata = filter_surface_by_floor_geometry(surface, geometry, plane)
|
| 3129 |
+
surface[occluder_mask > 0] = 0
|
| 3130 |
+
surface[reject_mask > 0] = 0
|
| 3131 |
post_soft_covering_filter_metadata = {
|
| 3132 |
f"postSoftFloorCovering{k[0].upper()}{k[1:]}": value
|
| 3133 |
for k, value in post_soft_covering_filter_metadata.items()
|
|
|
|
| 3184 |
inputs = seg_processor(images=img, return_tensors="pt").to(device)
|
| 3185 |
with torch.no_grad():
|
| 3186 |
outputs = seg_model(**inputs)
|
| 3187 |
+
logits = torch.nn.functional.interpolate(
|
| 3188 |
+
outputs.logits,
|
| 3189 |
+
size=(h, w),
|
| 3190 |
+
mode="bilinear",
|
| 3191 |
+
align_corners=False,
|
| 3192 |
+
)
|
| 3193 |
+
return logits.argmax(dim=1).squeeze().cpu().numpy().astype(np.uint8)
|
| 3194 |
|
| 3195 |
|
| 3196 |
def segmenter_metadata_name() -> str:
|
|
|
|
| 3560 |
return np.zeros((h, w), dtype=np.uint8)
|
| 3561 |
|
| 3562 |
near_surface = cv2.dilate(surface_mask.astype(np.uint8), kern, iterations=5)
|
| 3563 |
+
return ((occluder_mask > 0) & (near_surface > 0)).astype(np.uint8)
|
|
|
|
| 3564 |
|
| 3565 |
|
| 3566 |
def build_manifest_segmentation_bundle(
|
|
|
|
| 3697 |
floor_class_ids = class_ids(FLOOR_SURFACE_CLASSES)
|
| 3698 |
raw_primary_floor_mask = np.isin(seg_map, primary_floor_ids).astype(np.uint8)
|
| 3699 |
floor_mask = raw_primary_floor_mask.copy()
|
| 3700 |
+
floor_mask = wall_subtract(floor_mask, seg_map, dilation=0)
|
| 3701 |
floor_mask = clean_floor_mask(floor_mask)
|
| 3702 |
floor_mask_source = "primary-floor"
|
| 3703 |
if int(floor_mask.sum()) < min_floor_area:
|
| 3704 |
floor_mask = np.isin(seg_map, floor_class_ids).astype(np.uint8)
|
| 3705 |
+
floor_mask = wall_subtract(floor_mask, seg_map, dilation=0)
|
| 3706 |
floor_mask = clean_floor_mask(floor_mask)
|
| 3707 |
floor_mask_source = "floor-surface-fallback"
|
| 3708 |
cleaned_floor_mask = floor_mask.copy()
|
|
|
|
| 3878 |
shade_map = lighting_maps.get("shadeMap")
|
| 3879 |
showroom_shade_map = lighting_maps.get("showroomShadeMap")
|
| 3880 |
reflection_map = lighting_maps.get("reflectionMap")
|
| 3881 |
+
shade_statistics = lighting_map_statistics(shade_map, surface_mask)
|
| 3882 |
lighting_elapsed = time.perf_counter() - t0
|
| 3883 |
print(
|
| 3884 |
"[TIMING] Lighting map construction took "
|
|
|
|
| 4027 |
),
|
| 4028 |
"shadingTransferEnabled": ENABLE_SHADING_TRANSFER,
|
| 4029 |
"lightingSource": lighting_source,
|
| 4030 |
+
"lightingTransferVersion": LIGHTING_TRANSFER_VERSION,
|
| 4031 |
+
"shadeMapEncoding": SHADE_MAP_ENCODING,
|
| 4032 |
+
"shadeMapStatistics": shade_statistics,
|
| 4033 |
"shadeMapRange": [SHADE_MAP_MIN, SHADE_MAP_MAX],
|
| 4034 |
"showroomShadeMapRange": [SHOWROOM_SHADE_MAP_MIN, SHOWROOM_SHADE_MAP_MAX],
|
| 4035 |
"renderPresetDefaults": {
|
| 4036 |
"preset": "realistic",
|
| 4037 |
+
"shadingStrength": 1.0,
|
| 4038 |
"reflectionTransfer": True,
|
| 4039 |
"edgeFeatherPixels": 2,
|
| 4040 |
},
|
frontend/viz2d-demo/e2e/lighting-preservation.spec.ts
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { expect, test, type Page } from "@playwright/test";
|
| 2 |
+
|
| 3 |
+
const WIDTH = 120;
|
| 4 |
+
const HEIGHT = 72;
|
| 5 |
+
|
| 6 |
+
for (const renderer of ["webgl", "canvas"] as const) {
|
| 7 |
+
test(`applied tile preserves scene lighting in ${renderer}`, async ({ page }) => {
|
| 8 |
+
if (renderer === "canvas") {
|
| 9 |
+
await disableWebGl(page);
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
await page.goto("/");
|
| 13 |
+
await page.getByTestId("room-file-input").setInputFiles({
|
| 14 |
+
name: "lighting.vizbundle.json",
|
| 15 |
+
mimeType: "application/json",
|
| 16 |
+
buffer: Buffer.from(JSON.stringify(buildLightingBundle())),
|
| 17 |
+
});
|
| 18 |
+
|
| 19 |
+
await expect(page.getByTestId("visualizer-editor")).toBeVisible();
|
| 20 |
+
await expect(page.getByTestId("surface-card")).toBeVisible();
|
| 21 |
+
await page.getByTestId("surface-card").click();
|
| 22 |
+
await expect(page.getByTestId("texture-selector")).toBeVisible();
|
| 23 |
+
await page.getByTestId("texture-card").filter({ hasText: "White Wood Flooring" }).click();
|
| 24 |
+
await expect(page.getByTestId("texture-selector")).toBeHidden();
|
| 25 |
+
await expect(page.getByTestId("surface-card").filter({ hasText: "White Wood Flooring" })).toBeVisible();
|
| 26 |
+
await page.mouse.move(4, 4);
|
| 27 |
+
|
| 28 |
+
const luminance = await readLightingRegions(page);
|
| 29 |
+
expect(luminance.dark, "deep source shadow should fully cover the applied tile").toBeLessThan(luminance.neutral * 0.55);
|
| 30 |
+
// Near-white tiles saturate before preserving a large relative ratio.
|
| 31 |
+
expect(luminance.bright, "bright source region should remain visibly illuminated").toBeGreaterThan(luminance.neutral + 15);
|
| 32 |
+
expect(luminance.bright, "bright source region should approach white").toBeGreaterThan(240);
|
| 33 |
+
});
|
| 34 |
+
|
| 35 |
+
test(`applied tile fully covers the floor-mask edge in ${renderer}`, async ({ page }) => {
|
| 36 |
+
if (renderer === "canvas") {
|
| 37 |
+
await disableWebGl(page);
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
await page.goto("/");
|
| 41 |
+
await page.getByTestId("room-file-input").setInputFiles({
|
| 42 |
+
name: "edge-coverage.vizbundle.json",
|
| 43 |
+
mimeType: "application/json",
|
| 44 |
+
buffer: Buffer.from(JSON.stringify(buildEdgeCoverageBundle())),
|
| 45 |
+
});
|
| 46 |
+
|
| 47 |
+
await expect(page.getByTestId("visualizer-editor")).toBeVisible();
|
| 48 |
+
await page.getByTestId("surface-card").click();
|
| 49 |
+
await page.getByTestId("texture-card").filter({ hasText: "White Wood Flooring" }).click();
|
| 50 |
+
await expect(page.getByTestId("texture-selector")).toBeHidden();
|
| 51 |
+
|
| 52 |
+
const colors = await readEdgeColors(page);
|
| 53 |
+
expect(colors.boundary.green, "first floor pixels should have full tile coverage").toBeGreaterThan(
|
| 54 |
+
colors.interior.green * 0.82,
|
| 55 |
+
);
|
| 56 |
+
expect(colors.outside.green, "tile should not bleed outside the floor mask").toBeLessThan(50);
|
| 57 |
+
expect(colors.outside.red).toBeGreaterThan(200);
|
| 58 |
+
expect(colors.outside.blue).toBeGreaterThan(200);
|
| 59 |
+
});
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
function buildLightingBundle() {
|
| 63 |
+
const pixelCount = WIDTH * HEIGHT;
|
| 64 |
+
const pixels = new Uint8Array(pixelCount * 4);
|
| 65 |
+
const mask = new Uint32Array(pixelCount);
|
| 66 |
+
const shadeMap = new Uint8Array(pixelCount);
|
| 67 |
+
|
| 68 |
+
for (let index = 0; index < pixelCount; index += 1) {
|
| 69 |
+
const x = index % WIDTH;
|
| 70 |
+
const sourceValue = x < 40 ? 70 : x >= 80 ? 220 : 145;
|
| 71 |
+
const shade = x < 40 ? 0.14 : x >= 80 ? 1.5 : 1;
|
| 72 |
+
const pixelIndex = index * 4;
|
| 73 |
+
pixels[pixelIndex] = sourceValue;
|
| 74 |
+
pixels[pixelIndex + 1] = sourceValue;
|
| 75 |
+
pixels[pixelIndex + 2] = sourceValue;
|
| 76 |
+
pixels[pixelIndex + 3] = 255;
|
| 77 |
+
mask[index] = index;
|
| 78 |
+
shadeMap[index] = Math.round(((shade - 0.08) / (1.85 - 0.08)) * 255);
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
return {
|
| 82 |
+
format: "baetes-vizbundle",
|
| 83 |
+
bundleVersion: 3,
|
| 84 |
+
width: WIDTH,
|
| 85 |
+
height: HEIGHT,
|
| 86 |
+
pixels: Buffer.from(pixels).toString("base64"),
|
| 87 |
+
segments: [
|
| 88 |
+
{
|
| 89 |
+
id: 0,
|
| 90 |
+
className: "floor",
|
| 91 |
+
mask: Buffer.from(mask.buffer).toString("base64"),
|
| 92 |
+
homography: null,
|
| 93 |
+
plane: null,
|
| 94 |
+
shadeMap: Buffer.from(shadeMap).toString("base64"),
|
| 95 |
+
showroomShadeMap: null,
|
| 96 |
+
reflectionMap: null,
|
| 97 |
+
metadata: {
|
| 98 |
+
lightingSource: "synthetic-test",
|
| 99 |
+
lightingTransferVersion: 3,
|
| 100 |
+
shadeMapEncoding: "linear-relative",
|
| 101 |
+
shadeMapRange: [0.08, 1.85],
|
| 102 |
+
renderPresetDefaults: {
|
| 103 |
+
preset: "realistic",
|
| 104 |
+
shadingStrength: 1,
|
| 105 |
+
},
|
| 106 |
+
},
|
| 107 |
+
},
|
| 108 |
+
],
|
| 109 |
+
};
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
function buildEdgeCoverageBundle() {
|
| 113 |
+
const pixelCount = WIDTH * HEIGHT;
|
| 114 |
+
const pixels = new Uint8Array(pixelCount * 4);
|
| 115 |
+
const maskValues: number[] = [];
|
| 116 |
+
const shadeMap = new Uint8Array(pixelCount);
|
| 117 |
+
const neutralShade = Math.round(((1 - 0.08) / (1.85 - 0.08)) * 255);
|
| 118 |
+
|
| 119 |
+
for (let index = 0; index < pixelCount; index += 1) {
|
| 120 |
+
const x = index % WIDTH;
|
| 121 |
+
const y = Math.floor(index / WIDTH);
|
| 122 |
+
const pixelIndex = index * 4;
|
| 123 |
+
pixels[pixelIndex] = 230;
|
| 124 |
+
pixels[pixelIndex + 1] = 20;
|
| 125 |
+
pixels[pixelIndex + 2] = 230;
|
| 126 |
+
pixels[pixelIndex + 3] = 255;
|
| 127 |
+
shadeMap[index] = neutralShade;
|
| 128 |
+
if (x >= 10 && x < 110 && y >= 8 && y < 64) {
|
| 129 |
+
maskValues.push(index);
|
| 130 |
+
}
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
const mask = Uint32Array.from(maskValues);
|
| 134 |
+
return {
|
| 135 |
+
format: "baetes-vizbundle",
|
| 136 |
+
bundleVersion: 3,
|
| 137 |
+
width: WIDTH,
|
| 138 |
+
height: HEIGHT,
|
| 139 |
+
pixels: Buffer.from(pixels).toString("base64"),
|
| 140 |
+
segments: [
|
| 141 |
+
{
|
| 142 |
+
id: 0,
|
| 143 |
+
className: "floor",
|
| 144 |
+
mask: Buffer.from(mask.buffer).toString("base64"),
|
| 145 |
+
homography: null,
|
| 146 |
+
plane: null,
|
| 147 |
+
shadeMap: Buffer.from(shadeMap).toString("base64"),
|
| 148 |
+
showroomShadeMap: null,
|
| 149 |
+
reflectionMap: null,
|
| 150 |
+
metadata: {
|
| 151 |
+
lightingTransferVersion: 3,
|
| 152 |
+
shadeMapEncoding: "linear-relative",
|
| 153 |
+
shadeMapRange: [0.08, 1.85],
|
| 154 |
+
renderPresetDefaults: {
|
| 155 |
+
preset: "realistic",
|
| 156 |
+
shadingStrength: 1,
|
| 157 |
+
},
|
| 158 |
+
},
|
| 159 |
+
},
|
| 160 |
+
],
|
| 161 |
+
};
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
async function disableWebGl(page: Page) {
|
| 165 |
+
await page.addInitScript(() => {
|
| 166 |
+
const originalGetContext = HTMLCanvasElement.prototype.getContext;
|
| 167 |
+
HTMLCanvasElement.prototype.getContext = function getContext(
|
| 168 |
+
this: HTMLCanvasElement,
|
| 169 |
+
contextId: string,
|
| 170 |
+
...args: unknown[]
|
| 171 |
+
) {
|
| 172 |
+
if (contextId === "webgl2") return null;
|
| 173 |
+
return originalGetContext.call(this, contextId as "2d", ...args as []);
|
| 174 |
+
} as typeof HTMLCanvasElement.prototype.getContext;
|
| 175 |
+
});
|
| 176 |
+
}
|
| 177 |
+
|
| 178 |
+
async function readLightingRegions(page: Page) {
|
| 179 |
+
return page.getByTestId("visualizer-canvas").evaluate((node) => {
|
| 180 |
+
const canvas = node as HTMLCanvasElement;
|
| 181 |
+
const context = canvas.getContext("2d", { willReadFrequently: true });
|
| 182 |
+
if (!context) throw new Error("Canvas does not expose a 2D context.");
|
| 183 |
+
const image = context.getImageData(0, 0, canvas.width, canvas.height);
|
| 184 |
+
|
| 185 |
+
const meanLuminance = (startX: number, endX: number) => {
|
| 186 |
+
let total = 0;
|
| 187 |
+
let count = 0;
|
| 188 |
+
for (let y = 8; y < canvas.height - 8; y += 1) {
|
| 189 |
+
for (let x = startX; x < endX; x += 1) {
|
| 190 |
+
const index = (y * canvas.width + x) * 4;
|
| 191 |
+
total += (
|
| 192 |
+
image.data[index] * 0.2126 +
|
| 193 |
+
image.data[index + 1] * 0.7152 +
|
| 194 |
+
image.data[index + 2] * 0.0722
|
| 195 |
+
);
|
| 196 |
+
count += 1;
|
| 197 |
+
}
|
| 198 |
+
}
|
| 199 |
+
return total / Math.max(count, 1);
|
| 200 |
+
};
|
| 201 |
+
|
| 202 |
+
return {
|
| 203 |
+
dark: meanLuminance(8, 32),
|
| 204 |
+
neutral: meanLuminance(48, 72),
|
| 205 |
+
bright: meanLuminance(88, 112),
|
| 206 |
+
};
|
| 207 |
+
});
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
async function readEdgeColors(page: Page) {
|
| 211 |
+
return page.getByTestId("visualizer-canvas").evaluate((node) => {
|
| 212 |
+
const canvas = node as HTMLCanvasElement;
|
| 213 |
+
const context = canvas.getContext("2d", { willReadFrequently: true });
|
| 214 |
+
if (!context) throw new Error("Canvas does not expose a 2D context.");
|
| 215 |
+
const image = context.getImageData(0, 0, canvas.width, canvas.height);
|
| 216 |
+
|
| 217 |
+
const meanColor = (startX: number, endX: number) => {
|
| 218 |
+
const total = [0, 0, 0];
|
| 219 |
+
let count = 0;
|
| 220 |
+
for (let y = 16; y < 56; y += 1) {
|
| 221 |
+
for (let x = startX; x < endX; x += 1) {
|
| 222 |
+
const index = (y * canvas.width + x) * 4;
|
| 223 |
+
total[0] += image.data[index];
|
| 224 |
+
total[1] += image.data[index + 1];
|
| 225 |
+
total[2] += image.data[index + 2];
|
| 226 |
+
count += 1;
|
| 227 |
+
}
|
| 228 |
+
}
|
| 229 |
+
return {
|
| 230 |
+
red: total[0] / count,
|
| 231 |
+
green: total[1] / count,
|
| 232 |
+
blue: total[2] / count,
|
| 233 |
+
};
|
| 234 |
+
};
|
| 235 |
+
|
| 236 |
+
return {
|
| 237 |
+
outside: meanColor(8, 10),
|
| 238 |
+
boundary: meanColor(10, 12),
|
| 239 |
+
interior: meanColor(14, 16),
|
| 240 |
+
};
|
| 241 |
+
});
|
| 242 |
+
}
|
test_floor_geometry.py
CHANGED
|
@@ -87,6 +87,66 @@ class MetricFloorGeometryTests(unittest.TestCase):
|
|
| 87 |
self.assertLessEqual(float(decoded.max()), app.SHOWROOM_SHADE_MAP_MAX)
|
| 88 |
self.assertGreater(float(decoded[52, 64]), 0.90)
|
| 89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
def test_reflection_map_only_transfers_bright_highlights(self):
|
| 91 |
height, width = 128, 160
|
| 92 |
image = np.full((height, width, 3), 128, dtype=np.uint8)
|
|
@@ -213,6 +273,43 @@ class MetricFloorGeometryTests(unittest.TestCase):
|
|
| 213 |
self.assertFalse(repaired[protected].any())
|
| 214 |
self.assertEqual(int(repaired[42, 99]), 0)
|
| 215 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
def test_soft_floor_covering_expansion_adds_only_nearby_rug_pixels(self):
|
| 217 |
surface = np.zeros((80, 120), dtype=np.uint8)
|
| 218 |
surface[40:75, 20:100] = 1
|
|
@@ -258,14 +355,14 @@ class MetricFloorGeometryTests(unittest.TestCase):
|
|
| 258 |
self.assertFalse(metadata["softFloorCoveringExpansionApplied"])
|
| 259 |
self.assertFalse(expanded[protected].any())
|
| 260 |
|
| 261 |
-
def
|
| 262 |
height, width = 80, 120
|
| 263 |
floor_id = app.class_ids({"floor"})[0]
|
| 264 |
rug_ids = app.class_ids({"rug"})
|
| 265 |
self.assertTrue(rug_ids)
|
| 266 |
rug_id = rug_ids[0]
|
| 267 |
floor_mask = np.zeros((height, width), dtype=np.uint8)
|
| 268 |
-
floor_mask[40:75, 20:
|
| 269 |
seg_map = np.full((height, width), floor_id, dtype=np.uint8)
|
| 270 |
seg_map[45:70, 100:108] = rug_id
|
| 271 |
|
|
@@ -289,9 +386,9 @@ class MetricFloorGeometryTests(unittest.TestCase):
|
|
| 289 |
app.SOFT_FLOOR_COVERING_KERNEL_RATIO = original_ratio
|
| 290 |
app.SOFT_FLOOR_COVERING_DILATION_ITERATIONS = original_iterations
|
| 291 |
|
| 292 |
-
self.
|
| 293 |
self.assertEqual(int(surface[50, 100]), 1)
|
| 294 |
-
self.assertEqual(int(surface[50, 107]),
|
| 295 |
|
| 296 |
def test_floor_surface_mask_does_not_readd_off_plane_edge_pixels(self):
|
| 297 |
height, width = 80, 120
|
|
@@ -401,7 +498,7 @@ class MetricFloorGeometryTests(unittest.TestCase):
|
|
| 401 |
app.SOFT_FLOOR_COVERING_KERNEL_RATIO = original_ratio
|
| 402 |
app.SOFT_FLOOR_COVERING_DILATION_ITERATIONS = original_iterations
|
| 403 |
|
| 404 |
-
self.
|
| 405 |
self.assertTrue(metadata["postSoftFloorCoveringGeometrySurfaceFilterApplied"])
|
| 406 |
self.assertEqual(int(surface[:45].sum()), 0)
|
| 407 |
|
|
|
|
| 87 |
self.assertLessEqual(float(decoded.max()), app.SHOWROOM_SHADE_MAP_MAX)
|
| 88 |
self.assertGreater(float(decoded[52, 64]), 0.90)
|
| 89 |
|
| 90 |
+
def test_realistic_lighting_retains_dark_corner_and_bright_region(self):
|
| 91 |
+
height, width = 180, 240
|
| 92 |
+
image = np.full((height, width, 3), 150, dtype=np.uint8)
|
| 93 |
+
image[:, :80, :] = 55
|
| 94 |
+
image[:, 170:, :] = 225
|
| 95 |
+
mask = np.ones((height, width), dtype=np.uint8)
|
| 96 |
+
|
| 97 |
+
maps = app.build_luminance_lighting_maps(image, mask)
|
| 98 |
+
|
| 99 |
+
self.assertIsNotNone(maps)
|
| 100 |
+
shade = maps["shadeMap"]
|
| 101 |
+
self.assertIsNotNone(shade)
|
| 102 |
+
decoded = (
|
| 103 |
+
app.SHADE_MAP_MIN
|
| 104 |
+
+ (shade.astype(np.float32) / 255.0)
|
| 105 |
+
* (app.SHADE_MAP_MAX - app.SHADE_MAP_MIN)
|
| 106 |
+
)
|
| 107 |
+
dark = float(np.median(decoded[:, 20:60]))
|
| 108 |
+
neutral = float(np.median(decoded[:, 105:135]))
|
| 109 |
+
bright = float(np.median(decoded[:, 190:225]))
|
| 110 |
+
self.assertLess(dark, neutral * 0.25)
|
| 111 |
+
self.assertGreater(bright, neutral * 1.25)
|
| 112 |
+
|
| 113 |
+
def test_realistic_lighting_suppresses_old_floor_texture(self):
|
| 114 |
+
height, width = 180, 240
|
| 115 |
+
image = np.full((height, width, 3), 165, dtype=np.uint8)
|
| 116 |
+
image[:, :90, :] = 75
|
| 117 |
+
for x in range(0, width, 8):
|
| 118 |
+
image[:, x:x + 3, :] = np.clip(image[:, x:x + 3, :].astype(np.int16) - 35, 0, 255)
|
| 119 |
+
mask = np.ones((height, width), dtype=np.uint8)
|
| 120 |
+
|
| 121 |
+
maps = app.build_luminance_lighting_maps(image, mask)
|
| 122 |
+
|
| 123 |
+
self.assertIsNotNone(maps)
|
| 124 |
+
shade = maps["shadeMap"]
|
| 125 |
+
decoded = (
|
| 126 |
+
app.SHADE_MAP_MIN
|
| 127 |
+
+ (shade.astype(np.float32) / 255.0)
|
| 128 |
+
* (app.SHADE_MAP_MAX - app.SHADE_MAP_MIN)
|
| 129 |
+
)
|
| 130 |
+
shadow_contrast = float(np.median(decoded[:, 120:180]) - np.median(decoded[:, 20:70]))
|
| 131 |
+
stripe_contrast = float(np.mean(np.abs(np.diff(decoded[:, 120:180], axis=1))))
|
| 132 |
+
self.assertGreater(shadow_contrast, 0.25)
|
| 133 |
+
self.assertLess(stripe_contrast, 0.035)
|
| 134 |
+
|
| 135 |
+
def test_intrinsic_fusion_cannot_weaken_observed_shadow_or_highlight(self):
|
| 136 |
+
mask = np.ones((8, 12), dtype=np.uint8)
|
| 137 |
+
intrinsic = np.ones((8, 12), dtype=np.float32)
|
| 138 |
+
intrinsic[:, :4] = 0.72
|
| 139 |
+
intrinsic[:, 8:] = 1.18
|
| 140 |
+
observed = np.ones((8, 12), dtype=np.float32)
|
| 141 |
+
observed[:, :4] = 0.18
|
| 142 |
+
observed[:, 8:] = 1.55
|
| 143 |
+
|
| 144 |
+
fused = app.preserve_observed_lighting(intrinsic, observed, mask)
|
| 145 |
+
|
| 146 |
+
self.assertTrue(np.all(fused[:, :4] <= observed[:, :4]))
|
| 147 |
+
self.assertTrue(np.all(fused[:, 8:] >= observed[:, 8:]))
|
| 148 |
+
self.assertTrue(np.allclose(fused[:, 4:8], 1.0))
|
| 149 |
+
|
| 150 |
def test_reflection_map_only_transfers_bright_highlights(self):
|
| 151 |
height, width = 128, 160
|
| 152 |
image = np.full((height, width, 3), 128, dtype=np.uint8)
|
|
|
|
| 273 |
self.assertFalse(repaired[protected].any())
|
| 274 |
self.assertEqual(int(repaired[42, 99]), 0)
|
| 275 |
|
| 276 |
+
def test_floor_surface_reaches_wall_and_door_contact_edges(self):
|
| 277 |
+
height, width = 80, 120
|
| 278 |
+
floor_id = app.class_ids({"floor"})[0]
|
| 279 |
+
wall_id = app.class_ids({"wall"})[0]
|
| 280 |
+
door_id = app.class_ids({"door"})[0]
|
| 281 |
+
floor_mask = np.zeros((height, width), dtype=np.uint8)
|
| 282 |
+
floor_mask[40:75, 15:105] = 1
|
| 283 |
+
seg_map = np.full((height, width), floor_id, dtype=np.uint8)
|
| 284 |
+
seg_map[:40, :] = wall_id
|
| 285 |
+
seg_map[15:40, 50:70] = door_id
|
| 286 |
+
|
| 287 |
+
surface, _ = app.build_floor_surface_mask(
|
| 288 |
+
floor_mask,
|
| 289 |
+
seg_map,
|
| 290 |
+
quad=None,
|
| 291 |
+
depth=None,
|
| 292 |
+
geometry=None,
|
| 293 |
+
plane=None,
|
| 294 |
+
)
|
| 295 |
+
|
| 296 |
+
self.assertTrue(surface[40, 20:100].all())
|
| 297 |
+
self.assertFalse(surface[:40].any())
|
| 298 |
+
|
| 299 |
+
def test_foreground_occlusion_does_not_create_floor_halo_below_door(self):
|
| 300 |
+
height, width = 80, 120
|
| 301 |
+
floor_id = app.class_ids({"floor"})[0]
|
| 302 |
+
door_id = app.class_ids({"door"})[0]
|
| 303 |
+
surface = np.zeros((height, width), dtype=np.uint8)
|
| 304 |
+
surface[40:75, 15:105] = 1
|
| 305 |
+
seg_map = np.full((height, width), floor_id, dtype=np.uint8)
|
| 306 |
+
seg_map[15:40, 50:70] = door_id
|
| 307 |
+
|
| 308 |
+
occlusion = app.foreground_occlusion_mask(surface, seg_map)
|
| 309 |
+
|
| 310 |
+
self.assertTrue(occlusion[30:40, 50:70].all())
|
| 311 |
+
self.assertFalse(occlusion[40:, 50:70].any())
|
| 312 |
+
|
| 313 |
def test_soft_floor_covering_expansion_adds_only_nearby_rug_pixels(self):
|
| 314 |
surface = np.zeros((80, 120), dtype=np.uint8)
|
| 315 |
surface[40:75, 20:100] = 1
|
|
|
|
| 355 |
self.assertFalse(metadata["softFloorCoveringExpansionApplied"])
|
| 356 |
self.assertFalse(expanded[protected].any())
|
| 357 |
|
| 358 |
+
def test_floor_surface_mask_keeps_replaceable_rug_pixels(self):
|
| 359 |
height, width = 80, 120
|
| 360 |
floor_id = app.class_ids({"floor"})[0]
|
| 361 |
rug_ids = app.class_ids({"rug"})
|
| 362 |
self.assertTrue(rug_ids)
|
| 363 |
rug_id = rug_ids[0]
|
| 364 |
floor_mask = np.zeros((height, width), dtype=np.uint8)
|
| 365 |
+
floor_mask[40:75, 20:108] = 1
|
| 366 |
seg_map = np.full((height, width), floor_id, dtype=np.uint8)
|
| 367 |
seg_map[45:70, 100:108] = rug_id
|
| 368 |
|
|
|
|
| 386 |
app.SOFT_FLOOR_COVERING_KERNEL_RATIO = original_ratio
|
| 387 |
app.SOFT_FLOOR_COVERING_DILATION_ITERATIONS = original_iterations
|
| 388 |
|
| 389 |
+
self.assertFalse(metadata["softFloorCoveringExpansionApplied"])
|
| 390 |
self.assertEqual(int(surface[50, 100]), 1)
|
| 391 |
+
self.assertEqual(int(surface[50, 107]), 1)
|
| 392 |
|
| 393 |
def test_floor_surface_mask_does_not_readd_off_plane_edge_pixels(self):
|
| 394 |
height, width = 80, 120
|
|
|
|
| 498 |
app.SOFT_FLOOR_COVERING_KERNEL_RATIO = original_ratio
|
| 499 |
app.SOFT_FLOOR_COVERING_DILATION_ITERATIONS = original_iterations
|
| 500 |
|
| 501 |
+
self.assertFalse(metadata["softFloorCoveringExpansionApplied"])
|
| 502 |
self.assertTrue(metadata["postSoftFloorCoveringGeometrySurfaceFilterApplied"])
|
| 503 |
self.assertEqual(int(surface[:45].sum()), 0)
|
| 504 |
|
visualizer.gpu.toml
CHANGED
|
@@ -51,5 +51,4 @@ plane_normal_min_cos = 0.45
|
|
| 51 |
plane_filter_min_keep_ratio = 0.45
|
| 52 |
|
| 53 |
[gemini]
|
| 54 |
-
api_key = "AQ.Ab8RN6KTPAiQRFWdPOr2UezAr3b4h1fLTeMPHa2pTcOx6TS-IQ"
|
| 55 |
image_model = "gemini-3.1-flash-image"
|
|
|
|
| 51 |
plane_filter_min_keep_ratio = 0.45
|
| 52 |
|
| 53 |
[gemini]
|
|
|
|
| 54 |
image_model = "gemini-3.1-flash-image"
|