Spaces:
Paused
Paused
Codex staging deploy commited on
Commit ·
55e3c0e
1
Parent(s): c77cb97
Preview feature/realistic-floor-rendering at 647d5c4
Browse files- app.py +132 -12
- visualizer.gpu.toml +1 -0
- visualizer.hf.toml +1 -0
- visualizer.local.toml +1 -0
- visualizer.segformer.toml +1 -0
app.py
CHANGED
|
@@ -199,12 +199,18 @@ ENABLE_PLANE_FIT = str(config_value(
|
|
| 199 |
"enable_plane_fit",
|
| 200 |
"1",
|
| 201 |
)).lower() in {"1", "true", "yes", "on"}
|
| 202 |
-
ENABLE_MULTI_PLANE_SURFACES = str(config_value(
|
| 203 |
"ENABLE_MULTI_PLANE_SURFACES",
|
| 204 |
"runtime",
|
| 205 |
"enable_multi_plane_surfaces",
|
| 206 |
"1",
|
| 207 |
-
)).lower() in {"1", "true", "yes", "on"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
INTRINSIC_MODEL_VERSION = str(config_value(
|
| 209 |
"INTRINSIC_MODEL_VERSION",
|
| 210 |
"models",
|
|
@@ -3044,7 +3050,7 @@ def build_surface_uv_mapping(
|
|
| 3044 |
}
|
| 3045 |
|
| 3046 |
|
| 3047 |
-
def estimate_floor_plane(mask: np.ndarray, img_np: np.ndarray, geometry: dict | None = None):
|
| 3048 |
ys, xs = np.where(mask > 0)
|
| 3049 |
if len(xs) < 1000:
|
| 3050 |
return None, None
|
|
@@ -3151,7 +3157,71 @@ def estimate_floor_plane(mask: np.ndarray, img_np: np.ndarray, geometry: dict |
|
|
| 3151 |
"confidence": 0.25 if vanishing_point is not None else 0.1,
|
| 3152 |
},
|
| 3153 |
})
|
| 3154 |
-
return homography, plane
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3155 |
|
| 3156 |
|
| 3157 |
def build_floor_surface_mask(
|
|
@@ -4017,10 +4087,10 @@ def build_segmentation_bundle(contents: bytes, debug: PipelineDebugCapture | Non
|
|
| 4017 |
)
|
| 4018 |
if lighting_maps and lighting_maps.get("shadeMap") is not None:
|
| 4019 |
lighting_source = "luminance"
|
| 4020 |
-
if lighting_maps:
|
| 4021 |
-
shade_map = lighting_maps.get("shadeMap")
|
| 4022 |
-
showroom_shade_map = lighting_maps.get("showroomShadeMap")
|
| 4023 |
-
reflection_map = lighting_maps.get("reflectionMap")
|
| 4024 |
shade_statistics = lighting_map_statistics(shade_map, surface_mask)
|
| 4025 |
lighting_elapsed = time.perf_counter() - t0
|
| 4026 |
print(
|
|
@@ -4050,6 +4120,14 @@ def build_segmentation_bundle(contents: bytes, debug: PipelineDebugCapture | Non
|
|
| 4050 |
lighting_elapsed * 1000,
|
| 4051 |
)
|
| 4052 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4053 |
if progress_callback:
|
| 4054 |
progress_callback("lighting", 100)
|
| 4055 |
progress_callback("direction", 0)
|
|
@@ -4121,8 +4199,8 @@ def build_segmentation_bundle(contents: bytes, debug: PipelineDebugCapture | Non
|
|
| 4121 |
segments = []
|
| 4122 |
models = active_model_metadata(geometry, depth, shade_map, intrinsic_lighting_used)
|
| 4123 |
|
| 4124 |
-
if len(surface_indices) >= min_floor_area:
|
| 4125 |
-
segments.append({
|
| 4126 |
"id": 0,
|
| 4127 |
"className": "floor",
|
| 4128 |
"mask": base64.b64encode(surface_indices.tobytes()).decode(),
|
|
@@ -4194,8 +4272,50 @@ def build_segmentation_bundle(contents: bytes, debug: PipelineDebugCapture | Non
|
|
| 4194 |
"metricGeometry": bool(plane and plane.get("floorTransform")),
|
| 4195 |
"geometryProvider": plane.get("geometryProvider", geometry.get("provider")) if plane else geometry.get("provider"),
|
| 4196 |
"geometryModel": models["geometry"]["name"],
|
| 4197 |
-
},
|
| 4198 |
-
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4199 |
|
| 4200 |
if not segments:
|
| 4201 |
flat_seg = seg_map.ravel()
|
|
|
|
| 199 |
"enable_plane_fit",
|
| 200 |
"1",
|
| 201 |
)).lower() in {"1", "true", "yes", "on"}
|
| 202 |
+
ENABLE_MULTI_PLANE_SURFACES = str(config_value(
|
| 203 |
"ENABLE_MULTI_PLANE_SURFACES",
|
| 204 |
"runtime",
|
| 205 |
"enable_multi_plane_surfaces",
|
| 206 |
"1",
|
| 207 |
+
)).lower() in {"1", "true", "yes", "on"}
|
| 208 |
+
ENABLE_WALL_SEGMENTS = str(config_value(
|
| 209 |
+
"ENABLE_WALL_SEGMENTS",
|
| 210 |
+
"runtime",
|
| 211 |
+
"enable_wall_segments",
|
| 212 |
+
"1",
|
| 213 |
+
)).lower() in {"1", "true", "yes", "on"}
|
| 214 |
INTRINSIC_MODEL_VERSION = str(config_value(
|
| 215 |
"INTRINSIC_MODEL_VERSION",
|
| 216 |
"models",
|
|
|
|
| 3050 |
}
|
| 3051 |
|
| 3052 |
|
| 3053 |
+
def estimate_floor_plane(mask: np.ndarray, img_np: np.ndarray, geometry: dict | None = None):
|
| 3054 |
ys, xs = np.where(mask > 0)
|
| 3055 |
if len(xs) < 1000:
|
| 3056 |
return None, None
|
|
|
|
| 3157 |
"confidence": 0.25 if vanishing_point is not None else 0.1,
|
| 3158 |
},
|
| 3159 |
})
|
| 3160 |
+
return homography, plane
|
| 3161 |
+
|
| 3162 |
+
|
| 3163 |
+
def estimate_wall_plane(mask: np.ndarray):
|
| 3164 |
+
"""Return a stable image-space homography for a semantic wall mask.
|
| 3165 |
+
|
| 3166 |
+
Walls do not currently have metric depth fitting, so use the visible wall
|
| 3167 |
+
contour as the source quad and its bounding rectangle as the planar target.
|
| 3168 |
+
This preserves perspective convergence while keeping physical tile sizes
|
| 3169 |
+
deterministic until wall-specific depth calibration is available.
|
| 3170 |
+
"""
|
| 3171 |
+
ys, xs = np.where(mask > 0)
|
| 3172 |
+
if len(xs) < 1000:
|
| 3173 |
+
return None, None
|
| 3174 |
+
|
| 3175 |
+
h, w = mask.shape[:2]
|
| 3176 |
+
contours, _ = cv2.findContours((mask > 0).astype(np.uint8), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 3177 |
+
contour = max(contours, key=cv2.contourArea) if contours else None
|
| 3178 |
+
if contour is None or cv2.contourArea(contour) < 1000:
|
| 3179 |
+
return None, None
|
| 3180 |
+
|
| 3181 |
+
perimeter = cv2.arcLength(contour, True)
|
| 3182 |
+
approx = cv2.approxPolyDP(contour, max(2.0, perimeter * 0.02), True)
|
| 3183 |
+
if len(approx) >= 4:
|
| 3184 |
+
points = approx.reshape(-1, 2).astype(np.float32)
|
| 3185 |
+
hull = cv2.convexHull(points)
|
| 3186 |
+
if len(hull) >= 4:
|
| 3187 |
+
points = hull.reshape(-1, 2)
|
| 3188 |
+
else:
|
| 3189 |
+
points = np.empty((0, 2), dtype=np.float32)
|
| 3190 |
+
|
| 3191 |
+
if len(points) != 4:
|
| 3192 |
+
rect = cv2.minAreaRect(contour)
|
| 3193 |
+
points = cv2.boxPoints(rect).astype(np.float32)
|
| 3194 |
+
|
| 3195 |
+
# Order corners as top-left, top-right, bottom-right, bottom-left.
|
| 3196 |
+
sums = points.sum(axis=1)
|
| 3197 |
+
diffs = points[:, 1] - points[:, 0]
|
| 3198 |
+
ordered = np.float32([
|
| 3199 |
+
points[np.argmin(sums)],
|
| 3200 |
+
points[np.argmin(diffs)],
|
| 3201 |
+
points[np.argmax(sums)],
|
| 3202 |
+
points[np.argmax(diffs)],
|
| 3203 |
+
])
|
| 3204 |
+
x1, y1 = float(np.min(ordered[:, 0])), float(np.min(ordered[:, 1]))
|
| 3205 |
+
x2, y2 = float(np.max(ordered[:, 0])), float(np.max(ordered[:, 1]))
|
| 3206 |
+
if x2 - x1 < 20 or y2 - y1 < 20:
|
| 3207 |
+
return None, None
|
| 3208 |
+
|
| 3209 |
+
destination = np.float32([[x1, y1], [x2, y1], [x2, y2], [x1, y2]])
|
| 3210 |
+
homography = cv2.getPerspectiveTransform(ordered, destination).flatten().tolist()
|
| 3211 |
+
plane = {
|
| 3212 |
+
"x": x1,
|
| 3213 |
+
"y": y1,
|
| 3214 |
+
"width": x2 - x1,
|
| 3215 |
+
"height": y2 - y1,
|
| 3216 |
+
"quad": ordered.flatten().tolist(),
|
| 3217 |
+
"geometryConfidence": 0.45,
|
| 3218 |
+
"geometryProvider": "wall-contour-homography",
|
| 3219 |
+
"floorTransform": None,
|
| 3220 |
+
"metersPerUnit": None,
|
| 3221 |
+
"cameraIntrinsics": None,
|
| 3222 |
+
"camera": {"focalLength": float(max(h, w) * 0.95), "horizonY": y1, "confidence": 0.45},
|
| 3223 |
+
}
|
| 3224 |
+
return homography, plane
|
| 3225 |
|
| 3226 |
|
| 3227 |
def build_floor_surface_mask(
|
|
|
|
| 4087 |
)
|
| 4088 |
if lighting_maps and lighting_maps.get("shadeMap") is not None:
|
| 4089 |
lighting_source = "luminance"
|
| 4090 |
+
if lighting_maps:
|
| 4091 |
+
shade_map = lighting_maps.get("shadeMap")
|
| 4092 |
+
showroom_shade_map = lighting_maps.get("showroomShadeMap")
|
| 4093 |
+
reflection_map = lighting_maps.get("reflectionMap")
|
| 4094 |
shade_statistics = lighting_map_statistics(shade_map, surface_mask)
|
| 4095 |
lighting_elapsed = time.perf_counter() - t0
|
| 4096 |
print(
|
|
|
|
| 4120 |
lighting_elapsed * 1000,
|
| 4121 |
)
|
| 4122 |
|
| 4123 |
+
wall_lighting_maps = None
|
| 4124 |
+
wall_homography = None
|
| 4125 |
+
wall_plane = None
|
| 4126 |
+
if ENABLE_WALL_SEGMENTS and int(wall_mask.sum()) >= min_floor_area:
|
| 4127 |
+
wall_homography, wall_plane = estimate_wall_plane(wall_mask)
|
| 4128 |
+
if wall_homography is not None:
|
| 4129 |
+
wall_lighting_maps = build_luminance_lighting_maps(img_np, wall_mask)
|
| 4130 |
+
|
| 4131 |
if progress_callback:
|
| 4132 |
progress_callback("lighting", 100)
|
| 4133 |
progress_callback("direction", 0)
|
|
|
|
| 4199 |
segments = []
|
| 4200 |
models = active_model_metadata(geometry, depth, shade_map, intrinsic_lighting_used)
|
| 4201 |
|
| 4202 |
+
if len(surface_indices) >= min_floor_area:
|
| 4203 |
+
segments.append({
|
| 4204 |
"id": 0,
|
| 4205 |
"className": "floor",
|
| 4206 |
"mask": base64.b64encode(surface_indices.tobytes()).decode(),
|
|
|
|
| 4272 |
"metricGeometry": bool(plane and plane.get("floorTransform")),
|
| 4273 |
"geometryProvider": plane.get("geometryProvider", geometry.get("provider")) if plane else geometry.get("provider"),
|
| 4274 |
"geometryModel": models["geometry"]["name"],
|
| 4275 |
+
},
|
| 4276 |
+
})
|
| 4277 |
+
|
| 4278 |
+
if ENABLE_WALL_SEGMENTS and wall_homography is not None and wall_plane is not None:
|
| 4279 |
+
wall_indices = np.flatnonzero(wall_mask.ravel()).astype(np.uint32)
|
| 4280 |
+
wall_shade_map = wall_lighting_maps.get("shadeMap") if wall_lighting_maps else None
|
| 4281 |
+
wall_showroom_shade_map = wall_lighting_maps.get("showroomShadeMap") if wall_lighting_maps else None
|
| 4282 |
+
wall_reflection_map = wall_lighting_maps.get("reflectionMap") if wall_lighting_maps else None
|
| 4283 |
+
segments.append({
|
| 4284 |
+
"id": 1,
|
| 4285 |
+
"className": "wall",
|
| 4286 |
+
"mask": base64.b64encode(wall_indices.tobytes()).decode(),
|
| 4287 |
+
"occlusionMask": None,
|
| 4288 |
+
"homography": wall_homography,
|
| 4289 |
+
"floorTransform": None,
|
| 4290 |
+
"plane": wall_plane,
|
| 4291 |
+
"surfaceUv": None,
|
| 4292 |
+
"surfacePlaneIds": None,
|
| 4293 |
+
"surfacePlanes": None,
|
| 4294 |
+
"shadeMap": base64.b64encode(wall_shade_map.tobytes()).decode() if wall_shade_map is not None else None,
|
| 4295 |
+
"showroomShadeMap": (
|
| 4296 |
+
base64.b64encode(wall_showroom_shade_map.tobytes()).decode()
|
| 4297 |
+
if wall_showroom_shade_map is not None else None
|
| 4298 |
+
),
|
| 4299 |
+
"reflectionMap": (
|
| 4300 |
+
base64.b64encode(wall_reflection_map.tobytes()).decode()
|
| 4301 |
+
if wall_reflection_map is not None else None
|
| 4302 |
+
),
|
| 4303 |
+
"metadata": {
|
| 4304 |
+
"segmenter": segmenter_metadata_name(),
|
| 4305 |
+
"segmentationModel": models["segmentation"]["name"],
|
| 4306 |
+
"wallPixels": int(wall_mask.sum()),
|
| 4307 |
+
"shadingEnabled": wall_shade_map is not None,
|
| 4308 |
+
"reflectionTransferEnabled": wall_reflection_map is not None,
|
| 4309 |
+
"textureMappingMode": "wall-contour-homography",
|
| 4310 |
+
"geometryConfidence": wall_plane.get("geometryConfidence", 0.0),
|
| 4311 |
+
"geometryProvider": wall_plane.get("geometryProvider"),
|
| 4312 |
+
"lightingTransferVersion": LIGHTING_TRANSFER_VERSION,
|
| 4313 |
+
"shadeMapEncoding": SHADE_MAP_ENCODING,
|
| 4314 |
+
"shadeMapRange": [SHADE_MAP_MIN, SHADE_MAP_MAX],
|
| 4315 |
+
"showroomShadeMapRange": [SHOWROOM_SHADE_MAP_MIN, SHOWROOM_SHADE_MAP_MAX],
|
| 4316 |
+
"reflectionMapRange": [REFLECTION_MAP_MIN, REFLECTION_MAP_MAX],
|
| 4317 |
+
},
|
| 4318 |
+
})
|
| 4319 |
|
| 4320 |
if not segments:
|
| 4321 |
flat_seg = seg_map.ravel()
|
visualizer.gpu.toml
CHANGED
|
@@ -20,6 +20,7 @@ enable_depth_estimation = true
|
|
| 20 |
enable_moge_geometry = true
|
| 21 |
enable_plane_fit = true
|
| 22 |
enable_multi_plane_surfaces = true
|
|
|
|
| 23 |
enable_intrinsic_shading = true
|
| 24 |
enable_shading_transfer = true
|
| 25 |
enable_sam_refinement = false
|
|
|
|
| 20 |
enable_moge_geometry = true
|
| 21 |
enable_plane_fit = true
|
| 22 |
enable_multi_plane_surfaces = true
|
| 23 |
+
enable_wall_segments = true
|
| 24 |
enable_intrinsic_shading = true
|
| 25 |
enable_shading_transfer = true
|
| 26 |
enable_sam_refinement = false
|
visualizer.hf.toml
CHANGED
|
@@ -20,6 +20,7 @@ enable_depth_estimation = true
|
|
| 20 |
enable_moge_geometry = true
|
| 21 |
enable_plane_fit = true
|
| 22 |
enable_multi_plane_surfaces = true
|
|
|
|
| 23 |
enable_intrinsic_shading = true
|
| 24 |
enable_shading_transfer = true
|
| 25 |
enable_sam_refinement = true
|
|
|
|
| 20 |
enable_moge_geometry = true
|
| 21 |
enable_plane_fit = true
|
| 22 |
enable_multi_plane_surfaces = true
|
| 23 |
+
enable_wall_segments = true
|
| 24 |
enable_intrinsic_shading = true
|
| 25 |
enable_shading_transfer = true
|
| 26 |
enable_sam_refinement = true
|
visualizer.local.toml
CHANGED
|
@@ -14,6 +14,7 @@ sam2_checkpoint = ""
|
|
| 14 |
enable_depth_estimation = false
|
| 15 |
enable_plane_fit = true
|
| 16 |
enable_multi_plane_surfaces = true
|
|
|
|
| 17 |
enable_intrinsic_shading = false
|
| 18 |
enable_shading_transfer = true
|
| 19 |
enable_sam_refinement = false
|
|
|
|
| 14 |
enable_depth_estimation = false
|
| 15 |
enable_plane_fit = true
|
| 16 |
enable_multi_plane_surfaces = true
|
| 17 |
+
enable_wall_segments = true
|
| 18 |
enable_intrinsic_shading = false
|
| 19 |
enable_shading_transfer = true
|
| 20 |
enable_sam_refinement = false
|
visualizer.segformer.toml
CHANGED
|
@@ -15,6 +15,7 @@ sam2_checkpoint = ""
|
|
| 15 |
enable_depth_estimation = false
|
| 16 |
enable_plane_fit = true
|
| 17 |
enable_multi_plane_surfaces = true
|
|
|
|
| 18 |
enable_intrinsic_shading = false
|
| 19 |
enable_shading_transfer = true
|
| 20 |
enable_sam_refinement = false
|
|
|
|
| 15 |
enable_depth_estimation = false
|
| 16 |
enable_plane_fit = true
|
| 17 |
enable_multi_plane_surfaces = true
|
| 18 |
+
enable_wall_segments = true
|
| 19 |
enable_intrinsic_shading = false
|
| 20 |
enable_shading_transfer = true
|
| 21 |
enable_sam_refinement = false
|