Spaces:
Paused
Paused
Codex staging deploy commited on
Commit ·
2e640f8
1
Parent(s): 4af7e9b
Preview feature/realistic-floor-rendering at f64afec
Browse files
app.py
CHANGED
|
@@ -2306,14 +2306,15 @@ def project_points_to_plane(points: np.ndarray, origin: np.ndarray, normal: np.n
|
|
| 2306 |
return projected.astype(np.float32), signed_distance.astype(np.float32)
|
| 2307 |
|
| 2308 |
|
| 2309 |
-
def fit_metric_floor_transform(
|
| 2310 |
-
mask: np.ndarray,
|
| 2311 |
-
points_map: np.ndarray,
|
| 2312 |
-
valid_mask: np.ndarray | None = None,
|
| 2313 |
-
normals_map: np.ndarray | None = None,
|
| 2314 |
-
intrinsics: np.ndarray | None = None,
|
| 2315 |
-
provider: str = "metric-depth",
|
| 2316 |
-
|
|
|
|
| 2317 |
h, w = mask.shape[:2]
|
| 2318 |
valid = (mask > 0) & np.isfinite(points_map).all(axis=2) & (points_map[:, :, 2] > 0.05)
|
| 2319 |
if valid_mask is not None:
|
|
@@ -2369,17 +2370,15 @@ def fit_metric_floor_transform(
|
|
| 2369 |
if inlier_ratio < 0.45 or residual > 0.18:
|
| 2370 |
return None
|
| 2371 |
|
| 2372 |
-
#
|
| 2373 |
-
|
| 2374 |
-
|
| 2375 |
-
|
| 2376 |
-
|
| 2377 |
-
|
| 2378 |
-
|
| 2379 |
-
|
| 2380 |
-
|
| 2381 |
-
floor_points = projected_points - centroid
|
| 2382 |
-
uv = np.column_stack((floor_points @ u_axis, floor_points @ v_axis)).astype(np.float32)
|
| 2383 |
image_points = np.column_stack((xs[keep], ys[keep])).astype(np.float32)
|
| 2384 |
transform, ransac_mask = cv2.findHomography(
|
| 2385 |
image_points,
|
|
@@ -2407,8 +2406,14 @@ def fit_metric_floor_transform(
|
|
| 2407 |
else None
|
| 2408 |
)
|
| 2409 |
|
| 2410 |
-
return {
|
| 2411 |
-
"floorTransform": transform.flatten().tolist(),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2412 |
"metersPerUnit": 1.0,
|
| 2413 |
"geometryConfidence": confidence,
|
| 2414 |
"geometryProvider": provider,
|
|
@@ -2430,12 +2435,13 @@ def fit_metric_floor_transform(
|
|
| 2430 |
}
|
| 2431 |
|
| 2432 |
|
| 2433 |
-
def estimate_metric_floor_transform(
|
| 2434 |
-
mask: np.ndarray,
|
| 2435 |
-
depth: np.ndarray | None,
|
| 2436 |
-
provider: str = "depth-anything",
|
| 2437 |
-
intrinsics: np.ndarray | None = None,
|
| 2438 |
-
|
|
|
|
| 2439 |
if depth is None or not ENABLE_PLANE_FIT:
|
| 2440 |
return None
|
| 2441 |
h, w = mask.shape[:2]
|
|
@@ -2443,7 +2449,13 @@ def estimate_metric_floor_transform(
|
|
| 2443 |
depth_for_points, depth_scale = prepare_depth_for_backprojection(depth, mask, is_metric_depth)
|
| 2444 |
pixel_intrinsics = pixel_intrinsics_from_model(intrinsics, w, h)
|
| 2445 |
points = backproject_depth(depth_for_points, pixel_intrinsics)
|
| 2446 |
-
result = fit_metric_floor_transform(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2447 |
if result is not None:
|
| 2448 |
result["depthScale"] = depth_scale
|
| 2449 |
return result
|
|
@@ -3160,7 +3172,7 @@ def estimate_floor_plane(mask: np.ndarray, img_np: np.ndarray, geometry: dict |
|
|
| 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
|
|
@@ -3208,6 +3220,28 @@ def estimate_wall_plane(mask: np.ndarray):
|
|
| 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,
|
|
@@ -3217,11 +3251,49 @@ def estimate_wall_plane(mask: np.ndarray):
|
|
| 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(
|
|
@@ -4121,13 +4193,10 @@ def build_segmentation_bundle(contents: bytes, debug: PipelineDebugCapture | Non
|
|
| 4121 |
)
|
| 4122 |
|
| 4123 |
wall_mask = np.isin(seg_map, class_ids({"wall"})).astype(np.uint8)
|
| 4124 |
-
|
| 4125 |
-
|
| 4126 |
-
|
| 4127 |
-
|
| 4128 |
-
wall_homography, wall_plane = estimate_wall_plane(wall_mask)
|
| 4129 |
-
if wall_homography is not None:
|
| 4130 |
-
wall_lighting_maps = build_luminance_lighting_maps(img_np, wall_mask)
|
| 4131 |
|
| 4132 |
if progress_callback:
|
| 4133 |
progress_callback("lighting", 100)
|
|
@@ -4275,18 +4344,28 @@ def build_segmentation_bundle(contents: bytes, debug: PipelineDebugCapture | Non
|
|
| 4275 |
},
|
| 4276 |
})
|
| 4277 |
|
| 4278 |
-
|
| 4279 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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":
|
|
|
|
|
|
|
|
|
|
| 4288 |
"homography": wall_homography,
|
| 4289 |
-
"floorTransform":
|
|
|
|
| 4290 |
"plane": wall_plane,
|
| 4291 |
"surfaceUv": None,
|
| 4292 |
"surfacePlaneIds": None,
|
|
@@ -4303,12 +4382,17 @@ def build_segmentation_bundle(contents: bytes, debug: PipelineDebugCapture | Non
|
|
| 4303 |
"metadata": {
|
| 4304 |
"segmenter": segmenter_metadata_name(),
|
| 4305 |
"segmentationModel": models["segmentation"]["name"],
|
| 4306 |
-
"wallPixels": int(
|
|
|
|
|
|
|
|
|
|
| 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],
|
|
|
|
| 2306 |
return projected.astype(np.float32), signed_distance.astype(np.float32)
|
| 2307 |
|
| 2308 |
|
| 2309 |
+
def fit_metric_floor_transform(
|
| 2310 |
+
mask: np.ndarray,
|
| 2311 |
+
points_map: np.ndarray,
|
| 2312 |
+
valid_mask: np.ndarray | None = None,
|
| 2313 |
+
normals_map: np.ndarray | None = None,
|
| 2314 |
+
intrinsics: np.ndarray | None = None,
|
| 2315 |
+
provider: str = "metric-depth",
|
| 2316 |
+
surface_kind: str = "floor",
|
| 2317 |
+
):
|
| 2318 |
h, w = mask.shape[:2]
|
| 2319 |
valid = (mask > 0) & np.isfinite(points_map).all(axis=2) & (points_map[:, :, 2] > 0.05)
|
| 2320 |
if valid_mask is not None:
|
|
|
|
| 2370 |
if inlier_ratio < 0.45 or residual > 0.18:
|
| 2371 |
return None
|
| 2372 |
|
| 2373 |
+
# Use local horizontal/vertical axes for walls and horizontal/depth axes
|
| 2374 |
+
# for floors. This keeps physical tile dimensions stable across surfaces.
|
| 2375 |
+
u_axis, v_axis = plane_basis(normal, is_floor=surface_kind != "wall")
|
| 2376 |
+
|
| 2377 |
+
projected_points, _ = project_points_to_plane(points[keep], centroid, normal)
|
| 2378 |
+
floor_points = projected_points - centroid
|
| 2379 |
+
uv = np.column_stack((floor_points @ u_axis, floor_points @ v_axis)).astype(np.float32)
|
| 2380 |
+
uv_min = np.min(uv, axis=0)
|
| 2381 |
+
uv_max = np.max(uv, axis=0)
|
|
|
|
|
|
|
| 2382 |
image_points = np.column_stack((xs[keep], ys[keep])).astype(np.float32)
|
| 2383 |
transform, ransac_mask = cv2.findHomography(
|
| 2384 |
image_points,
|
|
|
|
| 2406 |
else None
|
| 2407 |
)
|
| 2408 |
|
| 2409 |
+
return {
|
| 2410 |
+
"floorTransform": transform.flatten().tolist(),
|
| 2411 |
+
"surfaceTransform": transform.flatten().tolist(),
|
| 2412 |
+
"surfaceType": surface_kind,
|
| 2413 |
+
"surfaceUAxis": u_axis.tolist(),
|
| 2414 |
+
"surfaceVAxis": v_axis.tolist(),
|
| 2415 |
+
"surfaceWidthMeters": float(max(uv_max[0] - uv_min[0], 0.05)),
|
| 2416 |
+
"surfaceHeightMeters": float(max(uv_max[1] - uv_min[1], 0.05)),
|
| 2417 |
"metersPerUnit": 1.0,
|
| 2418 |
"geometryConfidence": confidence,
|
| 2419 |
"geometryProvider": provider,
|
|
|
|
| 2435 |
}
|
| 2436 |
|
| 2437 |
|
| 2438 |
+
def estimate_metric_floor_transform(
|
| 2439 |
+
mask: np.ndarray,
|
| 2440 |
+
depth: np.ndarray | None,
|
| 2441 |
+
provider: str = "depth-anything",
|
| 2442 |
+
intrinsics: np.ndarray | None = None,
|
| 2443 |
+
surface_kind: str = "floor",
|
| 2444 |
+
):
|
| 2445 |
if depth is None or not ENABLE_PLANE_FIT:
|
| 2446 |
return None
|
| 2447 |
h, w = mask.shape[:2]
|
|
|
|
| 2449 |
depth_for_points, depth_scale = prepare_depth_for_backprojection(depth, mask, is_metric_depth)
|
| 2450 |
pixel_intrinsics = pixel_intrinsics_from_model(intrinsics, w, h)
|
| 2451 |
points = backproject_depth(depth_for_points, pixel_intrinsics)
|
| 2452 |
+
result = fit_metric_floor_transform(
|
| 2453 |
+
mask,
|
| 2454 |
+
points,
|
| 2455 |
+
intrinsics=pixel_intrinsics,
|
| 2456 |
+
provider=provider,
|
| 2457 |
+
surface_kind=surface_kind,
|
| 2458 |
+
)
|
| 2459 |
if result is not None:
|
| 2460 |
result["depthScale"] = depth_scale
|
| 2461 |
return result
|
|
|
|
| 3172 |
return homography, plane
|
| 3173 |
|
| 3174 |
|
| 3175 |
+
def estimate_wall_plane(mask: np.ndarray, geometry: dict | None = None):
|
| 3176 |
"""Return a stable image-space homography for a semantic wall mask.
|
| 3177 |
|
| 3178 |
Walls do not currently have metric depth fitting, so use the visible wall
|
|
|
|
| 3220 |
|
| 3221 |
destination = np.float32([[x1, y1], [x2, y1], [x2, y2], [x1, y2]])
|
| 3222 |
homography = cv2.getPerspectiveTransform(ordered, destination).flatten().tolist()
|
| 3223 |
+
metric_geometry = None
|
| 3224 |
+
if ENABLE_PLANE_FIT and geometry:
|
| 3225 |
+
points = geometry.get("points")
|
| 3226 |
+
if points is not None:
|
| 3227 |
+
metric_geometry = fit_metric_floor_transform(
|
| 3228 |
+
mask,
|
| 3229 |
+
points,
|
| 3230 |
+
geometry.get("validMask"),
|
| 3231 |
+
geometry.get("normals"),
|
| 3232 |
+
geometry.get("intrinsics"),
|
| 3233 |
+
geometry.get("provider", "metric-depth"),
|
| 3234 |
+
surface_kind="wall",
|
| 3235 |
+
)
|
| 3236 |
+
if metric_geometry is None:
|
| 3237 |
+
metric_geometry = estimate_metric_floor_transform(
|
| 3238 |
+
mask,
|
| 3239 |
+
geometry.get("depth"),
|
| 3240 |
+
geometry.get("provider", "depth-anything"),
|
| 3241 |
+
geometry.get("intrinsics"),
|
| 3242 |
+
surface_kind="wall",
|
| 3243 |
+
)
|
| 3244 |
+
|
| 3245 |
plane = {
|
| 3246 |
"x": x1,
|
| 3247 |
"y": y1,
|
|
|
|
| 3251 |
"geometryConfidence": 0.45,
|
| 3252 |
"geometryProvider": "wall-contour-homography",
|
| 3253 |
"floorTransform": None,
|
| 3254 |
+
"surfaceTransform": None,
|
| 3255 |
+
"surfaceType": "wall",
|
| 3256 |
"metersPerUnit": None,
|
| 3257 |
"cameraIntrinsics": None,
|
| 3258 |
"camera": {"focalLength": float(max(h, w) * 0.95), "horizonY": y1, "confidence": 0.45},
|
| 3259 |
}
|
| 3260 |
+
if metric_geometry:
|
| 3261 |
+
plane.update(metric_geometry)
|
| 3262 |
+
plane["surfaceType"] = "wall"
|
| 3263 |
+
plane["geometryProvider"] = metric_geometry.get("geometryProvider", "wall-depth")
|
| 3264 |
return homography, plane
|
| 3265 |
+
|
| 3266 |
+
|
| 3267 |
+
def wall_surface_components(seg_map: np.ndarray, min_area: int):
|
| 3268 |
+
"""Create clean, independently renderable wall regions.
|
| 3269 |
+
|
| 3270 |
+
Semantic wall predictions often bridge across corners and openings. Removing
|
| 3271 |
+
protected classes before connected-component analysis prevents one warped
|
| 3272 |
+
transform from being applied to several physically different walls.
|
| 3273 |
+
"""
|
| 3274 |
+
wall = np.isin(seg_map, class_ids({"wall"})).astype(np.uint8)
|
| 3275 |
+
protected = np.isin(seg_map, class_ids(OCCLUDER_CLASSES | {
|
| 3276 |
+
"door", "window", "column", "base", "railing", "ceiling",
|
| 3277 |
+
"building", "painting", "mirror", "curtain",
|
| 3278 |
+
})).astype(np.uint8)
|
| 3279 |
+
radius = max(1, int(round(min(seg_map.shape[:2]) * 0.006)))
|
| 3280 |
+
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (radius * 2 + 1, radius * 2 + 1))
|
| 3281 |
+
protected = cv2.dilate(protected, kernel, iterations=1)
|
| 3282 |
+
wall[protected > 0] = 0
|
| 3283 |
+
wall = cv2.morphologyEx(wall, cv2.MORPH_CLOSE, kernel)
|
| 3284 |
+
wall = cv2.morphologyEx(wall, cv2.MORPH_OPEN, kernel)
|
| 3285 |
+
wall[protected > 0] = 0
|
| 3286 |
+
|
| 3287 |
+
count, labels, stats, _ = cv2.connectedComponentsWithStats(wall, connectivity=8)
|
| 3288 |
+
components = []
|
| 3289 |
+
for component_id in range(1, count):
|
| 3290 |
+
area = int(stats[component_id, cv2.CC_STAT_AREA])
|
| 3291 |
+
if area < min_area:
|
| 3292 |
+
continue
|
| 3293 |
+
component = (labels == component_id).astype(np.uint8)
|
| 3294 |
+
components.append((area, component))
|
| 3295 |
+
components.sort(key=lambda item: item[0], reverse=True)
|
| 3296 |
+
return components, protected
|
| 3297 |
|
| 3298 |
|
| 3299 |
def build_floor_surface_mask(
|
|
|
|
| 4193 |
)
|
| 4194 |
|
| 4195 |
wall_mask = np.isin(seg_map, class_ids({"wall"})).astype(np.uint8)
|
| 4196 |
+
wall_components, wall_protected_mask = (
|
| 4197 |
+
wall_surface_components(seg_map, min_floor_area)
|
| 4198 |
+
if ENABLE_WALL_SEGMENTS else ([], np.zeros_like(wall_mask))
|
| 4199 |
+
)
|
|
|
|
|
|
|
|
|
|
| 4200 |
|
| 4201 |
if progress_callback:
|
| 4202 |
progress_callback("lighting", 100)
|
|
|
|
| 4344 |
},
|
| 4345 |
})
|
| 4346 |
|
| 4347 |
+
for wall_index, (wall_area, component_mask) in enumerate(wall_components):
|
| 4348 |
+
wall_homography, wall_plane = estimate_wall_plane(component_mask, geometry)
|
| 4349 |
+
if wall_homography is None or wall_plane is None:
|
| 4350 |
+
continue
|
| 4351 |
+
wall_lighting_maps = build_luminance_lighting_maps(img_np, component_mask)
|
| 4352 |
+
wall_indices = np.flatnonzero(component_mask.ravel()).astype(np.uint32)
|
| 4353 |
+
wall_occlusion_indices = np.flatnonzero(wall_protected_mask.ravel()).astype(np.uint32)
|
| 4354 |
wall_shade_map = wall_lighting_maps.get("shadeMap") if wall_lighting_maps else None
|
| 4355 |
wall_showroom_shade_map = wall_lighting_maps.get("showroomShadeMap") if wall_lighting_maps else None
|
| 4356 |
wall_reflection_map = wall_lighting_maps.get("reflectionMap") if wall_lighting_maps else None
|
| 4357 |
segments.append({
|
| 4358 |
+
"id": wall_index + 1,
|
| 4359 |
"className": "wall",
|
| 4360 |
+
"surfaceType": "wall",
|
| 4361 |
"mask": base64.b64encode(wall_indices.tobytes()).decode(),
|
| 4362 |
+
"occlusionMask": (
|
| 4363 |
+
base64.b64encode(wall_occlusion_indices.tobytes()).decode()
|
| 4364 |
+
if len(wall_occlusion_indices) else None
|
| 4365 |
+
),
|
| 4366 |
"homography": wall_homography,
|
| 4367 |
+
"floorTransform": wall_plane.get("floorTransform"),
|
| 4368 |
+
"surfaceTransform": wall_plane.get("surfaceTransform"),
|
| 4369 |
"plane": wall_plane,
|
| 4370 |
"surfaceUv": None,
|
| 4371 |
"surfacePlaneIds": None,
|
|
|
|
| 4382 |
"metadata": {
|
| 4383 |
"segmenter": segmenter_metadata_name(),
|
| 4384 |
"segmentationModel": models["segmentation"]["name"],
|
| 4385 |
+
"wallPixels": int(wall_area),
|
| 4386 |
+
"wallComponent": wall_index,
|
| 4387 |
+
"wallComponentCount": len(wall_components),
|
| 4388 |
+
"protectedPixels": int(wall_protected_mask.sum()),
|
| 4389 |
"shadingEnabled": wall_shade_map is not None,
|
| 4390 |
"reflectionTransferEnabled": wall_reflection_map is not None,
|
| 4391 |
+
"textureMappingMode": "wall-plane" if wall_plane.get("surfaceTransform") else "wall-contour-homography",
|
| 4392 |
"geometryConfidence": wall_plane.get("geometryConfidence", 0.0),
|
| 4393 |
"geometryProvider": wall_plane.get("geometryProvider"),
|
| 4394 |
+
"surfaceWidthMeters": wall_plane.get("surfaceWidthMeters"),
|
| 4395 |
+
"surfaceHeightMeters": wall_plane.get("surfaceHeightMeters"),
|
| 4396 |
"lightingTransferVersion": LIGHTING_TRANSFER_VERSION,
|
| 4397 |
"shadeMapEncoding": SHADE_MAP_ENCODING,
|
| 4398 |
"shadeMapRange": [SHADE_MAP_MIN, SHADE_MAP_MAX],
|