github-actions[bot] commited on
Commit ·
9365782
1
Parent(s): cb7cbc8
Automated Deploy: 68d5cff2736b313f6dfd11a1571c64ce37dccda6
Browse files- app.py +277 -173
- test_floor_geometry.py +157 -15
- visualizer.gpu.toml +13 -10
- visualizer.hf.toml +11 -9
- visualizer.local.toml +3 -0
- visualizer.segformer.toml +3 -0
app.py
CHANGED
|
@@ -276,17 +276,35 @@ SURFACE_EDGE_KERNEL_RATIO = float(config_value(
|
|
| 276 |
"edge_kernel_ratio",
|
| 277 |
"0.006",
|
| 278 |
))
|
| 279 |
-
SURFACE_EDGE_DILATION_ITERATIONS = int(config_value(
|
| 280 |
-
"SURFACE_EDGE_DILATION_ITERATIONS",
|
| 281 |
-
"surface_mapping",
|
| 282 |
-
"edge_dilation_iterations",
|
| 283 |
-
"2",
|
| 284 |
-
))
|
| 285 |
-
|
| 286 |
-
"
|
| 287 |
-
"surface_mapping",
|
| 288 |
-
"
|
| 289 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 290 |
))
|
| 291 |
SURFACE_UV_MAX_BAD_NEIGHBOR_RATIO = float(config_value(
|
| 292 |
"SURFACE_UV_MAX_BAD_NEIGHBOR_RATIO",
|
|
@@ -318,21 +336,21 @@ SURFACE_PLANE_DISTANCE_METERS = float(config_value(
|
|
| 318 |
"plane_distance_threshold_meters",
|
| 319 |
"0.12",
|
| 320 |
))
|
| 321 |
-
SURFACE_PLANE_NORMAL_MIN_COS = float(config_value(
|
| 322 |
-
"SURFACE_PLANE_NORMAL_MIN_COS",
|
| 323 |
-
"surface_mapping",
|
| 324 |
-
"plane_normal_min_cos",
|
| 325 |
-
"0.45",
|
| 326 |
-
))
|
| 327 |
-
SURFACE_PLANE_FILTER_MIN_KEEP_RATIO = float(config_value(
|
| 328 |
-
"SURFACE_PLANE_FILTER_MIN_KEEP_RATIO",
|
| 329 |
-
"surface_mapping",
|
| 330 |
-
"plane_filter_min_keep_ratio",
|
| 331 |
-
"0.45",
|
| 332 |
-
))
|
| 333 |
-
|
| 334 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 335 |
-
seg_processor = None
|
| 336 |
seg_model = None
|
| 337 |
segmentation_backend = "segformer"
|
| 338 |
oneformer_polygon_processor = None
|
|
@@ -469,13 +487,14 @@ FLOOR_SURFACE_CLASSES = {
|
|
| 469 |
"floor", "road", "sidewalk", "ground", "field", "grass", "sand",
|
| 470 |
"runway", "dirt track", "land", "stairs", "step",
|
| 471 |
}
|
| 472 |
-
REJECT_SURFACE_CLASSES = {"wall", "ceiling", "building", "sky", "window"}
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
"
|
| 476 |
-
"
|
| 477 |
-
"
|
| 478 |
-
"
|
|
|
|
| 479 |
"countertop", "stove", "kitchen island", "computer", "swivel chair",
|
| 480 |
"bar", "ottoman", "bottle", "buffet", "poster", "towel", "television",
|
| 481 |
"washer", "plaything", "stool", "basket", "bag", "cradle", "oven",
|
|
@@ -1109,23 +1128,23 @@ def wall_subtract(mask: np.ndarray, seg_map: np.ndarray, dilation: int = 1) -> n
|
|
| 1109 |
return result
|
| 1110 |
|
| 1111 |
|
| 1112 |
-
def scaled_odd_kernel_size(
|
| 1113 |
-
height: int,
|
| 1114 |
-
width: int,
|
| 1115 |
-
ratio: float,
|
| 1116 |
-
minimum: int = 3,
|
| 1117 |
-
maximum: int = 31,
|
| 1118 |
-
) -> int:
|
| 1119 |
-
min_side = max(3, min(height, width))
|
| 1120 |
-
size = max(minimum, int(round(min_side * ratio)))
|
| 1121 |
-
size = min(maximum, size)
|
| 1122 |
-
return size | 1
|
| 1123 |
-
|
| 1124 |
-
|
| 1125 |
-
def improve_surface_edge_coverage(
|
| 1126 |
-
surface: np.ndarray,
|
| 1127 |
-
protected_mask: np.ndarray,
|
| 1128 |
-
) -> tuple[np.ndarray, dict]:
|
| 1129 |
h, w = surface.shape[:2]
|
| 1130 |
before = int(surface.sum())
|
| 1131 |
repaired = surface.astype(np.uint8).copy()
|
|
@@ -1146,20 +1165,81 @@ def improve_surface_edge_coverage(
|
|
| 1146 |
repaired = (repaired > 0).astype(np.uint8)
|
| 1147 |
after = int(repaired.sum())
|
| 1148 |
|
| 1149 |
-
return repaired, {
|
| 1150 |
-
"surfacePixelsBeforeEdgeFix": before,
|
| 1151 |
-
"surfacePixelsAfterEdgeFix": after,
|
| 1152 |
-
"surfaceEdgeCloseKernel": close_size,
|
| 1153 |
-
"surfaceEdgeKernel": edge_size,
|
| 1154 |
-
"surfaceEdgeDilationIterations": SURFACE_EDGE_DILATION_ITERATIONS,
|
| 1155 |
-
}
|
| 1156 |
-
|
| 1157 |
-
|
| 1158 |
-
def
|
| 1159 |
-
surface: np.ndarray,
|
| 1160 |
-
|
| 1161 |
-
|
| 1162 |
-
) -> tuple[np.ndarray, dict]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1163 |
before = int(surface.sum())
|
| 1164 |
metadata = {
|
| 1165 |
"geometrySurfaceFilterEnabled": ENABLE_GEOMETRY_SURFACE_FILTER,
|
|
@@ -1277,7 +1357,7 @@ def detect_vanishing_point(img_np: np.ndarray, floor_mask: np.ndarray):
|
|
| 1277 |
|
| 1278 |
h, w = img_np.shape[:2]
|
| 1279 |
candidates = []
|
| 1280 |
-
for line in lines
|
| 1281 |
x1, y1, x2, y2 = [float(v) for v in line]
|
| 1282 |
dx, dy = x2 - x1, y2 - y1
|
| 1283 |
length = float(np.hypot(dx, dy))
|
|
@@ -2277,82 +2357,106 @@ def estimate_floor_plane(mask: np.ndarray, img_np: np.ndarray, geometry: dict |
|
|
| 2277 |
return homography, plane
|
| 2278 |
|
| 2279 |
|
| 2280 |
-
def build_floor_surface_mask(
|
| 2281 |
-
floor_mask: np.ndarray,
|
| 2282 |
-
seg_map: np.ndarray,
|
| 2283 |
-
quad: np.ndarray | None,
|
| 2284 |
-
depth: np.ndarray | None,
|
| 2285 |
-
geometry: dict | None,
|
| 2286 |
-
plane: dict | None,
|
| 2287 |
-
):
|
| 2288 |
h, w = floor_mask.shape[:2]
|
| 2289 |
kern_size = max(5, min(h, w) // 160) | 1
|
| 2290 |
kern = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (kern_size, kern_size))
|
| 2291 |
occluder_mask = np.isin(seg_map, class_ids(OCCLUDER_CLASSES)).astype(np.uint8)
|
| 2292 |
occ_dilated = cv2.dilate(occluder_mask, kern, iterations=2)
|
|
|
|
| 2293 |
reject_mask = np.isin(seg_map, class_ids(REJECT_SURFACE_CLASSES)).astype(np.uint8)
|
| 2294 |
reject_dilated = cv2.dilate(reject_mask, kern, iterations=2)
|
|
|
|
|
|
|
| 2295 |
|
| 2296 |
surface = floor_mask.copy()
|
| 2297 |
surface[reject_dilated > 0] = 0
|
|
|
|
| 2298 |
if not surface.any():
|
| 2299 |
surface = floor_mask.copy()
|
|
|
|
| 2300 |
|
| 2301 |
contours, _ = cv2.findContours(surface, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 2302 |
if contours:
|
| 2303 |
filled = np.zeros((h, w), dtype=np.uint8)
|
| 2304 |
cv2.drawContours(filled, contours, -1, 1, cv2.FILLED)
|
| 2305 |
filled[reject_dilated > 0] = 0
|
|
|
|
| 2306 |
surface = filled
|
| 2307 |
|
| 2308 |
if quad is not None and surface.any():
|
| 2309 |
plane_mask = np.zeros((h, w), dtype=np.uint8)
|
| 2310 |
cv2.fillConvexPoly(plane_mask, np.round(quad).astype(np.int32), 1)
|
| 2311 |
plane_mask[reject_dilated > 0] = 0
|
|
|
|
| 2312 |
near_floor = cv2.dilate(surface, kern, iterations=6)
|
| 2313 |
surface = cv2.bitwise_or(surface, cv2.bitwise_and(plane_mask, near_floor))
|
|
|
|
| 2314 |
|
| 2315 |
surface[occ_dilated > 0] = 0
|
|
|
|
| 2316 |
if depth is not None and floor_mask.any():
|
| 2317 |
floor_depth = depth[(floor_mask > 0) & np.isfinite(depth)]
|
| 2318 |
if floor_depth.size == 0:
|
| 2319 |
floor_depth = None
|
| 2320 |
-
else:
|
| 2321 |
-
floor_depth = None
|
| 2322 |
-
if floor_depth is not None:
|
| 2323 |
-
lo, hi = float(np.percentile(floor_depth, 2)), float(np.percentile(floor_depth, 98))
|
| 2324 |
-
margin = max(0.08, (hi - lo) * 0.35)
|
| 2325 |
-
depth_keep = (depth >= lo - margin) & (depth <= hi + margin)
|
| 2326 |
surface = (surface & depth_keep.astype(np.uint8)).astype(np.uint8)
|
| 2327 |
surface[floor_mask > 0] = np.maximum(surface[floor_mask > 0], 1)
|
| 2328 |
surface[occ_dilated > 0] = 0
|
| 2329 |
surface[reject_dilated > 0] = 0
|
|
|
|
| 2330 |
|
| 2331 |
surface, geometry_filter_metadata = filter_surface_by_floor_geometry(surface, geometry, plane)
|
| 2332 |
surface[occ_dilated > 0] = 0
|
| 2333 |
surface[reject_dilated > 0] = 0
|
|
|
|
| 2334 |
|
| 2335 |
surface = clean_floor_mask(surface)
|
| 2336 |
surface[occ_dilated > 0] = 0
|
| 2337 |
surface[reject_dilated > 0] = 0
|
|
|
|
| 2338 |
|
| 2339 |
-
|
| 2340 |
-
surface_before_edge = int(surface.sum())
|
| 2341 |
-
surface, edge_metadata = improve_surface_edge_coverage(surface, protected_mask)
|
| 2342 |
surface, post_edge_filter_metadata = filter_surface_by_floor_geometry(surface, geometry, plane)
|
| 2343 |
surface[occ_dilated > 0] = 0
|
| 2344 |
surface[reject_dilated > 0] = 0
|
|
|
|
| 2345 |
post_edge_filter_metadata = {
|
| 2346 |
f"postEdge{k[0].upper()}{k[1:]}": value
|
| 2347 |
for k, value in post_edge_filter_metadata.items()
|
| 2348 |
}
|
| 2349 |
-
|
| 2350 |
-
|
| 2351 |
-
|
| 2352 |
-
|
| 2353 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2354 |
}
|
| 2355 |
-
return surface, {**geometry_filter_metadata, **edge_metadata, **post_edge_filter_metadata, **mask_metadata}
|
| 2356 |
|
| 2357 |
|
| 2358 |
def run_segmentation(img: Image.Image, img_np: np.ndarray):
|
|
@@ -2691,10 +2795,10 @@ def build_segmentation_bundle(contents: bytes):
|
|
| 2691 |
homography, plane = estimate_floor_plane(floor_mask, img_np, geometry)
|
| 2692 |
print(f"[TIMING] Plane fitting / homography calculation took {time.perf_counter() - t0:.3f} seconds", flush=True)
|
| 2693 |
|
| 2694 |
-
t0 = time.perf_counter()
|
| 2695 |
-
quad = np.asarray(plane["quad"], dtype=np.float32).reshape(4, 2) if plane and plane.get("quad") else None
|
| 2696 |
-
surface_mask, edge_metadata = build_floor_surface_mask(floor_mask, seg_map, quad, depth, geometry, plane)
|
| 2697 |
-
surface_indices = np.flatnonzero(surface_mask.ravel()).astype(np.uint32)
|
| 2698 |
print(f"[TIMING] Surface masking took {time.perf_counter() - t0:.3f} seconds", flush=True)
|
| 2699 |
|
| 2700 |
t0 = time.perf_counter()
|
|
@@ -2755,25 +2859,25 @@ def build_segmentation_bundle(contents: bytes):
|
|
| 2755 |
shade_map = lighting_maps.get("shadeMap")
|
| 2756 |
showroom_shade_map = lighting_maps.get("showroomShadeMap")
|
| 2757 |
reflection_map = lighting_maps.get("reflectionMap")
|
| 2758 |
-
print(
|
| 2759 |
-
"[TIMING] Lighting map construction took "
|
| 2760 |
-
f"{time.perf_counter() - t0:.3f} seconds "
|
| 2761 |
-
f"(source={lighting_source}, reflections={reflection_map is not None})",
|
| 2762 |
-
flush=True,
|
| 2763 |
-
)
|
| 2764 |
-
|
| 2765 |
-
t0 = time.perf_counter()
|
| 2766 |
-
floor_direction = analyze_floor_direction_for_surface_mask(img_np, surface_mask)
|
| 2767 |
-
print(
|
| 2768 |
-
"[TIMING] Floor direction analysis took "
|
| 2769 |
-
f"{time.perf_counter() - t0:.3f} seconds "
|
| 2770 |
-
f"(angle={floor_direction.get('angleDegrees')}, confidence={floor_direction.get('confidence')})",
|
| 2771 |
-
flush=True,
|
| 2772 |
-
)
|
| 2773 |
-
|
| 2774 |
-
t0 = time.perf_counter()
|
| 2775 |
-
segments = []
|
| 2776 |
-
models = active_model_metadata(geometry, depth, shade_map, intrinsic_lighting_used)
|
| 2777 |
|
| 2778 |
if len(surface_indices) >= min_floor_area:
|
| 2779 |
segments.append({
|
|
@@ -2804,12 +2908,12 @@ def build_segmentation_bundle(contents: bytes):
|
|
| 2804 |
"metadata": {
|
| 2805 |
"segmenter": segmenter_metadata_name(),
|
| 2806 |
"segmentationModel": models["segmentation"]["name"],
|
| 2807 |
-
"floorPixels": int(floor_mask.sum()),
|
| 2808 |
-
"surfacePixels": int(surface_mask.sum()),
|
| 2809 |
-
"floorDirection": floor_direction,
|
| 2810 |
-
**sam_metadata,
|
| 2811 |
-
**edge_metadata,
|
| 2812 |
-
**mapping_metadata,
|
| 2813 |
"depthEnabled": depth is not None,
|
| 2814 |
"shadingEnabled": shade_map is not None,
|
| 2815 |
"planeFitEnabled": ENABLE_PLANE_FIT,
|
|
@@ -3214,10 +3318,10 @@ def encode_png_data_url(image_bgr: np.ndarray) -> str:
|
|
| 3214 |
return f"data:image/png;base64,{payload}"
|
| 3215 |
|
| 3216 |
|
| 3217 |
-
def floor_direction_response(result, segmentation) -> dict:
|
| 3218 |
-
overlay_image_base64 = None
|
| 3219 |
-
if result.overlay_bgr is not None:
|
| 3220 |
-
overlay_image_base64 = encode_png_data_url(result.overlay_bgr)
|
| 3221 |
|
| 3222 |
return {
|
| 3223 |
"angle_degrees": result.angle_degrees,
|
|
@@ -3247,58 +3351,58 @@ def floor_direction_response(result, segmentation) -> dict:
|
|
| 3247 |
for line in result.dominant_lines
|
| 3248 |
],
|
| 3249 |
"warnings": result.warnings,
|
| 3250 |
-
"overlay_image_base64": overlay_image_base64,
|
| 3251 |
-
}
|
| 3252 |
-
|
| 3253 |
-
|
| 3254 |
-
def floor_direction_metadata(result, segmentation) -> dict:
|
| 3255 |
-
return {
|
| 3256 |
-
"source": "backend-floor-direction",
|
| 3257 |
-
"angleDegrees": result.angle_degrees,
|
| 3258 |
-
"secondaryAngleDegrees": result.secondary_angle_degrees,
|
| 3259 |
-
"directionLabel": result.direction_label,
|
| 3260 |
-
"confidence": result.confidence,
|
| 3261 |
-
"floorAreaRatio": result.floor_area_ratio,
|
| 3262 |
-
"lineCount": result.line_count,
|
| 3263 |
-
"dominantLineCount": result.dominant_line_count,
|
| 3264 |
-
"gridPatternDetected": result.grid_pattern_detected,
|
| 3265 |
-
"warnings": result.warnings,
|
| 3266 |
-
"segmentation": {
|
| 3267 |
-
"label": segmentation.label,
|
| 3268 |
-
"confidence": segmentation.confidence,
|
| 3269 |
-
"polygonCount": len(segmentation.polygons),
|
| 3270 |
-
"sourcePath": segmentation.source_path,
|
| 3271 |
-
},
|
| 3272 |
-
}
|
| 3273 |
-
|
| 3274 |
-
|
| 3275 |
-
def analyze_floor_direction_for_surface_mask(
|
| 3276 |
-
img_np: np.ndarray,
|
| 3277 |
-
surface_mask: np.ndarray,
|
| 3278 |
-
) -> dict:
|
| 3279 |
-
h, w = img_np.shape[:2]
|
| 3280 |
-
polygons = mask_to_polygons(
|
| 3281 |
-
surface_mask,
|
| 3282 |
-
min_area=max(32.0, float(w * h) * 0.001),
|
| 3283 |
-
simplify_epsilon=2.0,
|
| 3284 |
-
)
|
| 3285 |
-
floor_segmentation = parse_floor_segmentation({
|
| 3286 |
-
"label": "floor",
|
| 3287 |
-
"confidence": 1.0,
|
| 3288 |
-
"width": int(w),
|
| 3289 |
-
"height": int(h),
|
| 3290 |
-
"polygons": polygons,
|
| 3291 |
-
})
|
| 3292 |
-
result = analyze_floor_direction_from_segmentation(
|
| 3293 |
-
cv2.cvtColor(img_np, cv2.COLOR_RGB2BGR),
|
| 3294 |
-
floor_segmentation,
|
| 3295 |
-
include_overlay=False,
|
| 3296 |
-
)
|
| 3297 |
-
return floor_direction_metadata(result, floor_segmentation)
|
| 3298 |
-
|
| 3299 |
-
|
| 3300 |
-
@app.post("/oneformer/polygons")
|
| 3301 |
-
async def oneformer_polygons(
|
| 3302 |
file: UploadFile = File(...),
|
| 3303 |
threshold: float = Form(0.5),
|
| 3304 |
mask_threshold: float = Form(0.5),
|
|
|
|
| 276 |
"edge_kernel_ratio",
|
| 277 |
"0.006",
|
| 278 |
))
|
| 279 |
+
SURFACE_EDGE_DILATION_ITERATIONS = int(config_value(
|
| 280 |
+
"SURFACE_EDGE_DILATION_ITERATIONS",
|
| 281 |
+
"surface_mapping",
|
| 282 |
+
"edge_dilation_iterations",
|
| 283 |
+
"2",
|
| 284 |
+
))
|
| 285 |
+
ENABLE_SOFT_FLOOR_COVERING_EXPANSION = str(config_value(
|
| 286 |
+
"ENABLE_SOFT_FLOOR_COVERING_EXPANSION",
|
| 287 |
+
"surface_mapping",
|
| 288 |
+
"soft_floor_covering_expansion_enabled",
|
| 289 |
+
"1",
|
| 290 |
+
)).lower() in {"1", "true", "yes", "on"}
|
| 291 |
+
SOFT_FLOOR_COVERING_KERNEL_RATIO = float(config_value(
|
| 292 |
+
"SOFT_FLOOR_COVERING_KERNEL_RATIO",
|
| 293 |
+
"surface_mapping",
|
| 294 |
+
"soft_floor_covering_kernel_ratio",
|
| 295 |
+
"0.008",
|
| 296 |
+
))
|
| 297 |
+
SOFT_FLOOR_COVERING_DILATION_ITERATIONS = int(config_value(
|
| 298 |
+
"SOFT_FLOOR_COVERING_DILATION_ITERATIONS",
|
| 299 |
+
"surface_mapping",
|
| 300 |
+
"soft_floor_covering_dilation_iterations",
|
| 301 |
+
"2",
|
| 302 |
+
))
|
| 303 |
+
SURFACE_UV_MIN_COVERAGE = float(config_value(
|
| 304 |
+
"SURFACE_UV_MIN_COVERAGE",
|
| 305 |
+
"surface_mapping",
|
| 306 |
+
"uv_min_coverage",
|
| 307 |
+
"0.995",
|
| 308 |
))
|
| 309 |
SURFACE_UV_MAX_BAD_NEIGHBOR_RATIO = float(config_value(
|
| 310 |
"SURFACE_UV_MAX_BAD_NEIGHBOR_RATIO",
|
|
|
|
| 336 |
"plane_distance_threshold_meters",
|
| 337 |
"0.12",
|
| 338 |
))
|
| 339 |
+
SURFACE_PLANE_NORMAL_MIN_COS = float(config_value(
|
| 340 |
+
"SURFACE_PLANE_NORMAL_MIN_COS",
|
| 341 |
+
"surface_mapping",
|
| 342 |
+
"plane_normal_min_cos",
|
| 343 |
+
"0.45",
|
| 344 |
+
))
|
| 345 |
+
SURFACE_PLANE_FILTER_MIN_KEEP_RATIO = float(config_value(
|
| 346 |
+
"SURFACE_PLANE_FILTER_MIN_KEEP_RATIO",
|
| 347 |
+
"surface_mapping",
|
| 348 |
+
"plane_filter_min_keep_ratio",
|
| 349 |
+
"0.45",
|
| 350 |
+
))
|
| 351 |
+
|
| 352 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 353 |
+
seg_processor = None
|
| 354 |
seg_model = None
|
| 355 |
segmentation_backend = "segformer"
|
| 356 |
oneformer_polygon_processor = None
|
|
|
|
| 487 |
"floor", "road", "sidewalk", "ground", "field", "grass", "sand",
|
| 488 |
"runway", "dirt track", "land", "stairs", "step",
|
| 489 |
}
|
| 490 |
+
REJECT_SURFACE_CLASSES = {"wall", "ceiling", "building", "sky", "window"}
|
| 491 |
+
SOFT_FLOOR_COVERING_CLASSES = {"rug", "carpet"}
|
| 492 |
+
OCCLUDER_CLASSES = {
|
| 493 |
+
"bed", "cabinet", "person", "door", "table", "plant", "curtain", "chair",
|
| 494 |
+
"car", "painting", "sofa", "shelf", "mirror", "armchair", "seat", "desk",
|
| 495 |
+
"wardrobe", "lamp", "bathtub", "railing", "cushion", "base", "box",
|
| 496 |
+
"column", "chest of drawers", "counter", "sink", "fireplace",
|
| 497 |
+
"refrigerator", "bookcase", "blind", "coffee table", "toilet", "bench",
|
| 498 |
"countertop", "stove", "kitchen island", "computer", "swivel chair",
|
| 499 |
"bar", "ottoman", "bottle", "buffet", "poster", "towel", "television",
|
| 500 |
"washer", "plaything", "stool", "basket", "bag", "cradle", "oven",
|
|
|
|
| 1128 |
return result
|
| 1129 |
|
| 1130 |
|
| 1131 |
+
def scaled_odd_kernel_size(
|
| 1132 |
+
height: int,
|
| 1133 |
+
width: int,
|
| 1134 |
+
ratio: float,
|
| 1135 |
+
minimum: int = 3,
|
| 1136 |
+
maximum: int = 31,
|
| 1137 |
+
) -> int:
|
| 1138 |
+
min_side = max(3, min(height, width))
|
| 1139 |
+
size = max(minimum, int(round(min_side * ratio)))
|
| 1140 |
+
size = min(maximum, size)
|
| 1141 |
+
return size | 1
|
| 1142 |
+
|
| 1143 |
+
|
| 1144 |
+
def improve_surface_edge_coverage(
|
| 1145 |
+
surface: np.ndarray,
|
| 1146 |
+
protected_mask: np.ndarray,
|
| 1147 |
+
) -> tuple[np.ndarray, dict]:
|
| 1148 |
h, w = surface.shape[:2]
|
| 1149 |
before = int(surface.sum())
|
| 1150 |
repaired = surface.astype(np.uint8).copy()
|
|
|
|
| 1165 |
repaired = (repaired > 0).astype(np.uint8)
|
| 1166 |
after = int(repaired.sum())
|
| 1167 |
|
| 1168 |
+
return repaired, {
|
| 1169 |
+
"surfacePixelsBeforeEdgeFix": before,
|
| 1170 |
+
"surfacePixelsAfterEdgeFix": after,
|
| 1171 |
+
"surfaceEdgeCloseKernel": close_size,
|
| 1172 |
+
"surfaceEdgeKernel": edge_size,
|
| 1173 |
+
"surfaceEdgeDilationIterations": SURFACE_EDGE_DILATION_ITERATIONS,
|
| 1174 |
+
}
|
| 1175 |
+
|
| 1176 |
+
|
| 1177 |
+
def expand_surface_over_soft_floor_coverings(
|
| 1178 |
+
surface: np.ndarray,
|
| 1179 |
+
soft_covering_mask: np.ndarray,
|
| 1180 |
+
protected_mask: np.ndarray,
|
| 1181 |
+
) -> tuple[np.ndarray, dict]:
|
| 1182 |
+
h, w = surface.shape[:2]
|
| 1183 |
+
before = int(surface.sum())
|
| 1184 |
+
soft_pixels = int(soft_covering_mask.sum())
|
| 1185 |
+
metadata = {
|
| 1186 |
+
"softFloorCoveringExpansionEnabled": ENABLE_SOFT_FLOOR_COVERING_EXPANSION,
|
| 1187 |
+
"softFloorCoveringExpansionApplied": False,
|
| 1188 |
+
"softFloorCoveringExpansionReason": (
|
| 1189 |
+
"disabled" if not ENABLE_SOFT_FLOOR_COVERING_EXPANSION else "unavailable"
|
| 1190 |
+
),
|
| 1191 |
+
"softFloorCoveringPixels": soft_pixels,
|
| 1192 |
+
"softFloorCoveringPixelsBefore": before,
|
| 1193 |
+
"softFloorCoveringPixelsAfter": before,
|
| 1194 |
+
"softFloorCoveringKernel": 0,
|
| 1195 |
+
"softFloorCoveringDilationIterations": SOFT_FLOOR_COVERING_DILATION_ITERATIONS,
|
| 1196 |
+
}
|
| 1197 |
+
if not ENABLE_SOFT_FLOOR_COVERING_EXPANSION:
|
| 1198 |
+
return surface, metadata
|
| 1199 |
+
if before == 0:
|
| 1200 |
+
metadata["softFloorCoveringExpansionReason"] = "empty-surface"
|
| 1201 |
+
return surface, metadata
|
| 1202 |
+
if soft_pixels == 0:
|
| 1203 |
+
metadata["softFloorCoveringExpansionReason"] = "no-soft-floor-covering"
|
| 1204 |
+
return surface, metadata
|
| 1205 |
+
if SOFT_FLOOR_COVERING_DILATION_ITERATIONS <= 0:
|
| 1206 |
+
metadata["softFloorCoveringExpansionReason"] = "zero-dilation"
|
| 1207 |
+
return surface, metadata
|
| 1208 |
+
|
| 1209 |
+
kernel_size = scaled_odd_kernel_size(
|
| 1210 |
+
h,
|
| 1211 |
+
w,
|
| 1212 |
+
SOFT_FLOOR_COVERING_KERNEL_RATIO,
|
| 1213 |
+
minimum=3,
|
| 1214 |
+
)
|
| 1215 |
+
metadata["softFloorCoveringKernel"] = kernel_size
|
| 1216 |
+
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (kernel_size, kernel_size))
|
| 1217 |
+
surface_reach = cv2.dilate(
|
| 1218 |
+
surface.astype(np.uint8),
|
| 1219 |
+
kernel,
|
| 1220 |
+
iterations=SOFT_FLOOR_COVERING_DILATION_ITERATIONS,
|
| 1221 |
+
)
|
| 1222 |
+
soft_reach = cv2.dilate(soft_covering_mask.astype(np.uint8), kernel, iterations=1)
|
| 1223 |
+
protected = protected_mask.astype(bool)
|
| 1224 |
+
candidates = (surface_reach > 0) & (soft_reach > 0) & (~protected)
|
| 1225 |
+
expanded = surface.astype(np.uint8).copy()
|
| 1226 |
+
expanded[candidates] = 1
|
| 1227 |
+
expanded[protected] = 0
|
| 1228 |
+
expanded = (expanded > 0).astype(np.uint8)
|
| 1229 |
+
after = int(expanded.sum())
|
| 1230 |
+
metadata["softFloorCoveringPixelsAfter"] = after
|
| 1231 |
+
metadata["softFloorCoveringExpansionApplied"] = after > before
|
| 1232 |
+
metadata["softFloorCoveringExpansionReason"] = (
|
| 1233 |
+
"expanded-near-soft-floor-covering" if after > before else "no-adjacent-candidates"
|
| 1234 |
+
)
|
| 1235 |
+
return expanded, metadata
|
| 1236 |
+
|
| 1237 |
+
|
| 1238 |
+
def filter_surface_by_floor_geometry(
|
| 1239 |
+
surface: np.ndarray,
|
| 1240 |
+
geometry: dict | None,
|
| 1241 |
+
plane: dict | None,
|
| 1242 |
+
) -> tuple[np.ndarray, dict]:
|
| 1243 |
before = int(surface.sum())
|
| 1244 |
metadata = {
|
| 1245 |
"geometrySurfaceFilterEnabled": ENABLE_GEOMETRY_SURFACE_FILTER,
|
|
|
|
| 1357 |
|
| 1358 |
h, w = img_np.shape[:2]
|
| 1359 |
candidates = []
|
| 1360 |
+
for line in lines[:, 0, :]:
|
| 1361 |
x1, y1, x2, y2 = [float(v) for v in line]
|
| 1362 |
dx, dy = x2 - x1, y2 - y1
|
| 1363 |
length = float(np.hypot(dx, dy))
|
|
|
|
| 2357 |
return homography, plane
|
| 2358 |
|
| 2359 |
|
| 2360 |
+
def build_floor_surface_mask(
|
| 2361 |
+
floor_mask: np.ndarray,
|
| 2362 |
+
seg_map: np.ndarray,
|
| 2363 |
+
quad: np.ndarray | None,
|
| 2364 |
+
depth: np.ndarray | None,
|
| 2365 |
+
geometry: dict | None,
|
| 2366 |
+
plane: dict | None,
|
| 2367 |
+
):
|
| 2368 |
h, w = floor_mask.shape[:2]
|
| 2369 |
kern_size = max(5, min(h, w) // 160) | 1
|
| 2370 |
kern = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (kern_size, kern_size))
|
| 2371 |
occluder_mask = np.isin(seg_map, class_ids(OCCLUDER_CLASSES)).astype(np.uint8)
|
| 2372 |
occ_dilated = cv2.dilate(occluder_mask, kern, iterations=2)
|
| 2373 |
+
soft_covering_mask = np.isin(seg_map, class_ids(SOFT_FLOOR_COVERING_CLASSES)).astype(np.uint8)
|
| 2374 |
reject_mask = np.isin(seg_map, class_ids(REJECT_SURFACE_CLASSES)).astype(np.uint8)
|
| 2375 |
reject_dilated = cv2.dilate(reject_mask, kern, iterations=2)
|
| 2376 |
+
hard_protected_mask = (occ_dilated > 0) | (reject_dilated > 0)
|
| 2377 |
+
normal_edge_protected_mask = hard_protected_mask | (soft_covering_mask > 0)
|
| 2378 |
|
| 2379 |
surface = floor_mask.copy()
|
| 2380 |
surface[reject_dilated > 0] = 0
|
| 2381 |
+
surface[soft_covering_mask > 0] = 0
|
| 2382 |
if not surface.any():
|
| 2383 |
surface = floor_mask.copy()
|
| 2384 |
+
surface[soft_covering_mask > 0] = 0
|
| 2385 |
|
| 2386 |
contours, _ = cv2.findContours(surface, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 2387 |
if contours:
|
| 2388 |
filled = np.zeros((h, w), dtype=np.uint8)
|
| 2389 |
cv2.drawContours(filled, contours, -1, 1, cv2.FILLED)
|
| 2390 |
filled[reject_dilated > 0] = 0
|
| 2391 |
+
filled[soft_covering_mask > 0] = 0
|
| 2392 |
surface = filled
|
| 2393 |
|
| 2394 |
if quad is not None and surface.any():
|
| 2395 |
plane_mask = np.zeros((h, w), dtype=np.uint8)
|
| 2396 |
cv2.fillConvexPoly(plane_mask, np.round(quad).astype(np.int32), 1)
|
| 2397 |
plane_mask[reject_dilated > 0] = 0
|
| 2398 |
+
plane_mask[soft_covering_mask > 0] = 0
|
| 2399 |
near_floor = cv2.dilate(surface, kern, iterations=6)
|
| 2400 |
surface = cv2.bitwise_or(surface, cv2.bitwise_and(plane_mask, near_floor))
|
| 2401 |
+
surface[soft_covering_mask > 0] = 0
|
| 2402 |
|
| 2403 |
surface[occ_dilated > 0] = 0
|
| 2404 |
+
surface[soft_covering_mask > 0] = 0
|
| 2405 |
if depth is not None and floor_mask.any():
|
| 2406 |
floor_depth = depth[(floor_mask > 0) & np.isfinite(depth)]
|
| 2407 |
if floor_depth.size == 0:
|
| 2408 |
floor_depth = None
|
| 2409 |
+
else:
|
| 2410 |
+
floor_depth = None
|
| 2411 |
+
if floor_depth is not None:
|
| 2412 |
+
lo, hi = float(np.percentile(floor_depth, 2)), float(np.percentile(floor_depth, 98))
|
| 2413 |
+
margin = max(0.08, (hi - lo) * 0.35)
|
| 2414 |
+
depth_keep = (depth >= lo - margin) & (depth <= hi + margin)
|
| 2415 |
surface = (surface & depth_keep.astype(np.uint8)).astype(np.uint8)
|
| 2416 |
surface[floor_mask > 0] = np.maximum(surface[floor_mask > 0], 1)
|
| 2417 |
surface[occ_dilated > 0] = 0
|
| 2418 |
surface[reject_dilated > 0] = 0
|
| 2419 |
+
surface[soft_covering_mask > 0] = 0
|
| 2420 |
|
| 2421 |
surface, geometry_filter_metadata = filter_surface_by_floor_geometry(surface, geometry, plane)
|
| 2422 |
surface[occ_dilated > 0] = 0
|
| 2423 |
surface[reject_dilated > 0] = 0
|
| 2424 |
+
surface[soft_covering_mask > 0] = 0
|
| 2425 |
|
| 2426 |
surface = clean_floor_mask(surface)
|
| 2427 |
surface[occ_dilated > 0] = 0
|
| 2428 |
surface[reject_dilated > 0] = 0
|
| 2429 |
+
surface[soft_covering_mask > 0] = 0
|
| 2430 |
|
| 2431 |
+
surface, edge_metadata = improve_surface_edge_coverage(surface, normal_edge_protected_mask)
|
|
|
|
|
|
|
| 2432 |
surface, post_edge_filter_metadata = filter_surface_by_floor_geometry(surface, geometry, plane)
|
| 2433 |
surface[occ_dilated > 0] = 0
|
| 2434 |
surface[reject_dilated > 0] = 0
|
| 2435 |
+
surface[soft_covering_mask > 0] = 0
|
| 2436 |
post_edge_filter_metadata = {
|
| 2437 |
f"postEdge{k[0].upper()}{k[1:]}": value
|
| 2438 |
for k, value in post_edge_filter_metadata.items()
|
| 2439 |
}
|
| 2440 |
+
|
| 2441 |
+
surface, soft_covering_metadata = expand_surface_over_soft_floor_coverings(
|
| 2442 |
+
surface,
|
| 2443 |
+
soft_covering_mask,
|
| 2444 |
+
hard_protected_mask,
|
| 2445 |
+
)
|
| 2446 |
+
surface, post_soft_covering_filter_metadata = filter_surface_by_floor_geometry(surface, geometry, plane)
|
| 2447 |
+
surface[occ_dilated > 0] = 0
|
| 2448 |
+
surface[reject_dilated > 0] = 0
|
| 2449 |
+
post_soft_covering_filter_metadata = {
|
| 2450 |
+
f"postSoftFloorCovering{k[0].upper()}{k[1:]}": value
|
| 2451 |
+
for k, value in post_soft_covering_filter_metadata.items()
|
| 2452 |
+
}
|
| 2453 |
+
return surface, {
|
| 2454 |
+
**geometry_filter_metadata,
|
| 2455 |
+
**edge_metadata,
|
| 2456 |
+
**post_edge_filter_metadata,
|
| 2457 |
+
**soft_covering_metadata,
|
| 2458 |
+
**post_soft_covering_filter_metadata,
|
| 2459 |
}
|
|
|
|
| 2460 |
|
| 2461 |
|
| 2462 |
def run_segmentation(img: Image.Image, img_np: np.ndarray):
|
|
|
|
| 2795 |
homography, plane = estimate_floor_plane(floor_mask, img_np, geometry)
|
| 2796 |
print(f"[TIMING] Plane fitting / homography calculation took {time.perf_counter() - t0:.3f} seconds", flush=True)
|
| 2797 |
|
| 2798 |
+
t0 = time.perf_counter()
|
| 2799 |
+
quad = np.asarray(plane["quad"], dtype=np.float32).reshape(4, 2) if plane and plane.get("quad") else None
|
| 2800 |
+
surface_mask, edge_metadata = build_floor_surface_mask(floor_mask, seg_map, quad, depth, geometry, plane)
|
| 2801 |
+
surface_indices = np.flatnonzero(surface_mask.ravel()).astype(np.uint32)
|
| 2802 |
print(f"[TIMING] Surface masking took {time.perf_counter() - t0:.3f} seconds", flush=True)
|
| 2803 |
|
| 2804 |
t0 = time.perf_counter()
|
|
|
|
| 2859 |
shade_map = lighting_maps.get("shadeMap")
|
| 2860 |
showroom_shade_map = lighting_maps.get("showroomShadeMap")
|
| 2861 |
reflection_map = lighting_maps.get("reflectionMap")
|
| 2862 |
+
print(
|
| 2863 |
+
"[TIMING] Lighting map construction took "
|
| 2864 |
+
f"{time.perf_counter() - t0:.3f} seconds "
|
| 2865 |
+
f"(source={lighting_source}, reflections={reflection_map is not None})",
|
| 2866 |
+
flush=True,
|
| 2867 |
+
)
|
| 2868 |
+
|
| 2869 |
+
t0 = time.perf_counter()
|
| 2870 |
+
floor_direction = analyze_floor_direction_for_surface_mask(img_np, surface_mask)
|
| 2871 |
+
print(
|
| 2872 |
+
"[TIMING] Floor direction analysis took "
|
| 2873 |
+
f"{time.perf_counter() - t0:.3f} seconds "
|
| 2874 |
+
f"(angle={floor_direction.get('angleDegrees')}, confidence={floor_direction.get('confidence')})",
|
| 2875 |
+
flush=True,
|
| 2876 |
+
)
|
| 2877 |
+
|
| 2878 |
+
t0 = time.perf_counter()
|
| 2879 |
+
segments = []
|
| 2880 |
+
models = active_model_metadata(geometry, depth, shade_map, intrinsic_lighting_used)
|
| 2881 |
|
| 2882 |
if len(surface_indices) >= min_floor_area:
|
| 2883 |
segments.append({
|
|
|
|
| 2908 |
"metadata": {
|
| 2909 |
"segmenter": segmenter_metadata_name(),
|
| 2910 |
"segmentationModel": models["segmentation"]["name"],
|
| 2911 |
+
"floorPixels": int(floor_mask.sum()),
|
| 2912 |
+
"surfacePixels": int(surface_mask.sum()),
|
| 2913 |
+
"floorDirection": floor_direction,
|
| 2914 |
+
**sam_metadata,
|
| 2915 |
+
**edge_metadata,
|
| 2916 |
+
**mapping_metadata,
|
| 2917 |
"depthEnabled": depth is not None,
|
| 2918 |
"shadingEnabled": shade_map is not None,
|
| 2919 |
"planeFitEnabled": ENABLE_PLANE_FIT,
|
|
|
|
| 3318 |
return f"data:image/png;base64,{payload}"
|
| 3319 |
|
| 3320 |
|
| 3321 |
+
def floor_direction_response(result, segmentation) -> dict:
|
| 3322 |
+
overlay_image_base64 = None
|
| 3323 |
+
if result.overlay_bgr is not None:
|
| 3324 |
+
overlay_image_base64 = encode_png_data_url(result.overlay_bgr)
|
| 3325 |
|
| 3326 |
return {
|
| 3327 |
"angle_degrees": result.angle_degrees,
|
|
|
|
| 3351 |
for line in result.dominant_lines
|
| 3352 |
],
|
| 3353 |
"warnings": result.warnings,
|
| 3354 |
+
"overlay_image_base64": overlay_image_base64,
|
| 3355 |
+
}
|
| 3356 |
+
|
| 3357 |
+
|
| 3358 |
+
def floor_direction_metadata(result, segmentation) -> dict:
|
| 3359 |
+
return {
|
| 3360 |
+
"source": "backend-floor-direction",
|
| 3361 |
+
"angleDegrees": result.angle_degrees,
|
| 3362 |
+
"secondaryAngleDegrees": result.secondary_angle_degrees,
|
| 3363 |
+
"directionLabel": result.direction_label,
|
| 3364 |
+
"confidence": result.confidence,
|
| 3365 |
+
"floorAreaRatio": result.floor_area_ratio,
|
| 3366 |
+
"lineCount": result.line_count,
|
| 3367 |
+
"dominantLineCount": result.dominant_line_count,
|
| 3368 |
+
"gridPatternDetected": result.grid_pattern_detected,
|
| 3369 |
+
"warnings": result.warnings,
|
| 3370 |
+
"segmentation": {
|
| 3371 |
+
"label": segmentation.label,
|
| 3372 |
+
"confidence": segmentation.confidence,
|
| 3373 |
+
"polygonCount": len(segmentation.polygons),
|
| 3374 |
+
"sourcePath": segmentation.source_path,
|
| 3375 |
+
},
|
| 3376 |
+
}
|
| 3377 |
+
|
| 3378 |
+
|
| 3379 |
+
def analyze_floor_direction_for_surface_mask(
|
| 3380 |
+
img_np: np.ndarray,
|
| 3381 |
+
surface_mask: np.ndarray,
|
| 3382 |
+
) -> dict:
|
| 3383 |
+
h, w = img_np.shape[:2]
|
| 3384 |
+
polygons = mask_to_polygons(
|
| 3385 |
+
surface_mask,
|
| 3386 |
+
min_area=max(32.0, float(w * h) * 0.001),
|
| 3387 |
+
simplify_epsilon=2.0,
|
| 3388 |
+
)
|
| 3389 |
+
floor_segmentation = parse_floor_segmentation({
|
| 3390 |
+
"label": "floor",
|
| 3391 |
+
"confidence": 1.0,
|
| 3392 |
+
"width": int(w),
|
| 3393 |
+
"height": int(h),
|
| 3394 |
+
"polygons": polygons,
|
| 3395 |
+
})
|
| 3396 |
+
result = analyze_floor_direction_from_segmentation(
|
| 3397 |
+
cv2.cvtColor(img_np, cv2.COLOR_RGB2BGR),
|
| 3398 |
+
floor_segmentation,
|
| 3399 |
+
include_overlay=False,
|
| 3400 |
+
)
|
| 3401 |
+
return floor_direction_metadata(result, floor_segmentation)
|
| 3402 |
+
|
| 3403 |
+
|
| 3404 |
+
@app.post("/oneformer/polygons")
|
| 3405 |
+
async def oneformer_polygons(
|
| 3406 |
file: UploadFile = File(...),
|
| 3407 |
threshold: float = Form(0.5),
|
| 3408 |
mask_threshold: float = Form(0.5),
|
test_floor_geometry.py
CHANGED
|
@@ -156,13 +156,93 @@ class MetricFloorGeometryTests(unittest.TestCase):
|
|
| 156 |
self.assertFalse(repaired[protected].any())
|
| 157 |
self.assertEqual(int(repaired[42, 99]), 0)
|
| 158 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
def test_floor_surface_mask_does_not_readd_off_plane_edge_pixels(self):
|
| 160 |
height, width = 80, 120
|
| 161 |
yy, xx = np.mgrid[0:height, 0:width]
|
| 162 |
floor_mask = np.zeros((height, width), dtype=np.uint8)
|
| 163 |
floor_mask[45:74, 22:98] = 1
|
| 164 |
-
seg_map = np.full((height, width), app.class_ids({"floor"})[0], dtype=np.uint8)
|
| 165 |
-
|
| 166 |
points = np.zeros((height, width, 3), dtype=np.float32)
|
| 167 |
points[:, :, 0] = (xx - width / 2) / 40.0
|
| 168 |
points[:, :, 2] = (yy - 45) / 40.0 + 1.0
|
|
@@ -185,10 +265,69 @@ class MetricFloorGeometryTests(unittest.TestCase):
|
|
| 185 |
original_filter = app.ENABLE_GEOMETRY_SURFACE_FILTER
|
| 186 |
original_min = app.SURFACE_MIN_PLANE_PIXELS
|
| 187 |
original_distance = app.SURFACE_PLANE_DISTANCE_METERS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
try:
|
| 189 |
app.ENABLE_GEOMETRY_SURFACE_FILTER = True
|
| 190 |
app.SURFACE_MIN_PLANE_PIXELS = 250
|
| 191 |
app.SURFACE_PLANE_DISTANCE_METERS = 0.12
|
|
|
|
|
|
|
|
|
|
| 192 |
surface, metadata = app.build_floor_surface_mask(
|
| 193 |
floor_mask,
|
| 194 |
seg_map,
|
|
@@ -197,19 +336,22 @@ class MetricFloorGeometryTests(unittest.TestCase):
|
|
| 197 |
geometry=geometry,
|
| 198 |
plane=plane,
|
| 199 |
)
|
| 200 |
-
finally:
|
| 201 |
-
app.ENABLE_GEOMETRY_SURFACE_FILTER = original_filter
|
| 202 |
-
app.SURFACE_MIN_PLANE_PIXELS = original_min
|
| 203 |
-
app.SURFACE_PLANE_DISTANCE_METERS = original_distance
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
|
|
|
|
|
|
|
|
|
| 213 |
focal = max(height, width) * 0.95
|
| 214 |
depth = np.full((height, width), 20.0, dtype=np.float32)
|
| 215 |
depth[mask > 0] = 1.6 * focal / (yy[mask > 0] - (height - 1) * 0.5)
|
|
|
|
| 156 |
self.assertFalse(repaired[protected].any())
|
| 157 |
self.assertEqual(int(repaired[42, 99]), 0)
|
| 158 |
|
| 159 |
+
def test_soft_floor_covering_expansion_adds_only_nearby_rug_pixels(self):
|
| 160 |
+
surface = np.zeros((80, 120), dtype=np.uint8)
|
| 161 |
+
surface[40:75, 20:100] = 1
|
| 162 |
+
rug = np.zeros_like(surface)
|
| 163 |
+
rug[45:70, 100:108] = 1
|
| 164 |
+
protected = np.zeros_like(surface, dtype=bool)
|
| 165 |
+
|
| 166 |
+
original_enabled = app.ENABLE_SOFT_FLOOR_COVERING_EXPANSION
|
| 167 |
+
original_ratio = app.SOFT_FLOOR_COVERING_KERNEL_RATIO
|
| 168 |
+
original_iterations = app.SOFT_FLOOR_COVERING_DILATION_ITERATIONS
|
| 169 |
+
try:
|
| 170 |
+
app.ENABLE_SOFT_FLOOR_COVERING_EXPANSION = True
|
| 171 |
+
app.SOFT_FLOOR_COVERING_KERNEL_RATIO = 0.008
|
| 172 |
+
app.SOFT_FLOOR_COVERING_DILATION_ITERATIONS = 2
|
| 173 |
+
expanded, metadata = app.expand_surface_over_soft_floor_coverings(
|
| 174 |
+
surface,
|
| 175 |
+
rug,
|
| 176 |
+
protected,
|
| 177 |
+
)
|
| 178 |
+
finally:
|
| 179 |
+
app.ENABLE_SOFT_FLOOR_COVERING_EXPANSION = original_enabled
|
| 180 |
+
app.SOFT_FLOOR_COVERING_KERNEL_RATIO = original_ratio
|
| 181 |
+
app.SOFT_FLOOR_COVERING_DILATION_ITERATIONS = original_iterations
|
| 182 |
+
|
| 183 |
+
self.assertTrue(metadata["softFloorCoveringExpansionApplied"])
|
| 184 |
+
self.assertEqual(int(expanded[50, 100]), 1)
|
| 185 |
+
self.assertEqual(int(expanded[50, 107]), 0)
|
| 186 |
+
|
| 187 |
+
def test_soft_floor_covering_expansion_respects_hard_protected_pixels(self):
|
| 188 |
+
surface = np.zeros((80, 120), dtype=np.uint8)
|
| 189 |
+
surface[40:75, 20:100] = 1
|
| 190 |
+
rug = np.zeros_like(surface)
|
| 191 |
+
rug[45:70, 100:104] = 1
|
| 192 |
+
protected = np.zeros_like(surface, dtype=bool)
|
| 193 |
+
protected[45:70, 100:104] = True
|
| 194 |
+
|
| 195 |
+
expanded, metadata = app.expand_surface_over_soft_floor_coverings(
|
| 196 |
+
surface,
|
| 197 |
+
rug,
|
| 198 |
+
protected,
|
| 199 |
+
)
|
| 200 |
+
|
| 201 |
+
self.assertFalse(metadata["softFloorCoveringExpansionApplied"])
|
| 202 |
+
self.assertFalse(expanded[protected].any())
|
| 203 |
+
|
| 204 |
+
def test_floor_surface_mask_expands_on_rug_side_without_filling_entire_rug(self):
|
| 205 |
+
height, width = 80, 120
|
| 206 |
+
floor_id = app.class_ids({"floor"})[0]
|
| 207 |
+
rug_ids = app.class_ids({"rug"})
|
| 208 |
+
self.assertTrue(rug_ids)
|
| 209 |
+
rug_id = rug_ids[0]
|
| 210 |
+
floor_mask = np.zeros((height, width), dtype=np.uint8)
|
| 211 |
+
floor_mask[40:75, 20:100] = 1
|
| 212 |
+
seg_map = np.full((height, width), floor_id, dtype=np.uint8)
|
| 213 |
+
seg_map[45:70, 100:108] = rug_id
|
| 214 |
+
|
| 215 |
+
original_enabled = app.ENABLE_SOFT_FLOOR_COVERING_EXPANSION
|
| 216 |
+
original_ratio = app.SOFT_FLOOR_COVERING_KERNEL_RATIO
|
| 217 |
+
original_iterations = app.SOFT_FLOOR_COVERING_DILATION_ITERATIONS
|
| 218 |
+
try:
|
| 219 |
+
app.ENABLE_SOFT_FLOOR_COVERING_EXPANSION = True
|
| 220 |
+
app.SOFT_FLOOR_COVERING_KERNEL_RATIO = 0.008
|
| 221 |
+
app.SOFT_FLOOR_COVERING_DILATION_ITERATIONS = 2
|
| 222 |
+
surface, metadata = app.build_floor_surface_mask(
|
| 223 |
+
floor_mask,
|
| 224 |
+
seg_map,
|
| 225 |
+
quad=None,
|
| 226 |
+
depth=None,
|
| 227 |
+
geometry=None,
|
| 228 |
+
plane=None,
|
| 229 |
+
)
|
| 230 |
+
finally:
|
| 231 |
+
app.ENABLE_SOFT_FLOOR_COVERING_EXPANSION = original_enabled
|
| 232 |
+
app.SOFT_FLOOR_COVERING_KERNEL_RATIO = original_ratio
|
| 233 |
+
app.SOFT_FLOOR_COVERING_DILATION_ITERATIONS = original_iterations
|
| 234 |
+
|
| 235 |
+
self.assertTrue(metadata["softFloorCoveringExpansionApplied"])
|
| 236 |
+
self.assertEqual(int(surface[50, 100]), 1)
|
| 237 |
+
self.assertEqual(int(surface[50, 107]), 0)
|
| 238 |
+
|
| 239 |
def test_floor_surface_mask_does_not_readd_off_plane_edge_pixels(self):
|
| 240 |
height, width = 80, 120
|
| 241 |
yy, xx = np.mgrid[0:height, 0:width]
|
| 242 |
floor_mask = np.zeros((height, width), dtype=np.uint8)
|
| 243 |
floor_mask[45:74, 22:98] = 1
|
| 244 |
+
seg_map = np.full((height, width), app.class_ids({"floor"})[0], dtype=np.uint8)
|
| 245 |
+
|
| 246 |
points = np.zeros((height, width, 3), dtype=np.float32)
|
| 247 |
points[:, :, 0] = (xx - width / 2) / 40.0
|
| 248 |
points[:, :, 2] = (yy - 45) / 40.0 + 1.0
|
|
|
|
| 265 |
original_filter = app.ENABLE_GEOMETRY_SURFACE_FILTER
|
| 266 |
original_min = app.SURFACE_MIN_PLANE_PIXELS
|
| 267 |
original_distance = app.SURFACE_PLANE_DISTANCE_METERS
|
| 268 |
+
try:
|
| 269 |
+
app.ENABLE_GEOMETRY_SURFACE_FILTER = True
|
| 270 |
+
app.SURFACE_MIN_PLANE_PIXELS = 250
|
| 271 |
+
app.SURFACE_PLANE_DISTANCE_METERS = 0.12
|
| 272 |
+
surface, metadata = app.build_floor_surface_mask(
|
| 273 |
+
floor_mask,
|
| 274 |
+
seg_map,
|
| 275 |
+
quad=None,
|
| 276 |
+
depth=None,
|
| 277 |
+
geometry=geometry,
|
| 278 |
+
plane=plane,
|
| 279 |
+
)
|
| 280 |
+
finally:
|
| 281 |
+
app.ENABLE_GEOMETRY_SURFACE_FILTER = original_filter
|
| 282 |
+
app.SURFACE_MIN_PLANE_PIXELS = original_min
|
| 283 |
+
app.SURFACE_PLANE_DISTANCE_METERS = original_distance
|
| 284 |
+
|
| 285 |
+
self.assertGreater(metadata["surfacePixelsAfterEdgeFix"], metadata["surfacePixelsBeforeEdgeFix"])
|
| 286 |
+
self.assertTrue(metadata["postEdgeGeometrySurfaceFilterApplied"])
|
| 287 |
+
self.assertEqual(int(surface[:45].sum()), 0)
|
| 288 |
+
|
| 289 |
+
def test_floor_surface_mask_filters_off_plane_rug_expansion(self):
|
| 290 |
+
height, width = 80, 120
|
| 291 |
+
yy, xx = np.mgrid[0:height, 0:width]
|
| 292 |
+
floor_id = app.class_ids({"floor"})[0]
|
| 293 |
+
rug_id = app.class_ids({"rug"})[0]
|
| 294 |
+
floor_mask = np.zeros((height, width), dtype=np.uint8)
|
| 295 |
+
floor_mask[45:74, 22:98] = 1
|
| 296 |
+
seg_map = np.full((height, width), floor_id, dtype=np.uint8)
|
| 297 |
+
seg_map[42:45, 40:52] = rug_id
|
| 298 |
+
|
| 299 |
+
points = np.zeros((height, width, 3), dtype=np.float32)
|
| 300 |
+
points[:, :, 0] = (xx - width / 2) / 40.0
|
| 301 |
+
points[:, :, 2] = (yy - 45) / 40.0 + 1.0
|
| 302 |
+
points[:, :, 1] = 0.0
|
| 303 |
+
points[:45, :, 1] = 0.5
|
| 304 |
+
normals = np.zeros_like(points)
|
| 305 |
+
normals[:, :, 1] = 1.0
|
| 306 |
+
geometry = {
|
| 307 |
+
"provider": "synthetic",
|
| 308 |
+
"points": points,
|
| 309 |
+
"normals": normals,
|
| 310 |
+
"validMask": np.ones((height, width), dtype=bool),
|
| 311 |
+
"intrinsics": np.eye(3, dtype=np.float32),
|
| 312 |
+
}
|
| 313 |
+
plane = {
|
| 314 |
+
"planeNormal": [0.0, 1.0, 0.0],
|
| 315 |
+
"planeOrigin": [0.0, 0.0, 0.0],
|
| 316 |
+
}
|
| 317 |
+
|
| 318 |
+
original_filter = app.ENABLE_GEOMETRY_SURFACE_FILTER
|
| 319 |
+
original_min = app.SURFACE_MIN_PLANE_PIXELS
|
| 320 |
+
original_distance = app.SURFACE_PLANE_DISTANCE_METERS
|
| 321 |
+
original_enabled = app.ENABLE_SOFT_FLOOR_COVERING_EXPANSION
|
| 322 |
+
original_ratio = app.SOFT_FLOOR_COVERING_KERNEL_RATIO
|
| 323 |
+
original_iterations = app.SOFT_FLOOR_COVERING_DILATION_ITERATIONS
|
| 324 |
try:
|
| 325 |
app.ENABLE_GEOMETRY_SURFACE_FILTER = True
|
| 326 |
app.SURFACE_MIN_PLANE_PIXELS = 250
|
| 327 |
app.SURFACE_PLANE_DISTANCE_METERS = 0.12
|
| 328 |
+
app.ENABLE_SOFT_FLOOR_COVERING_EXPANSION = True
|
| 329 |
+
app.SOFT_FLOOR_COVERING_KERNEL_RATIO = 0.008
|
| 330 |
+
app.SOFT_FLOOR_COVERING_DILATION_ITERATIONS = 2
|
| 331 |
surface, metadata = app.build_floor_surface_mask(
|
| 332 |
floor_mask,
|
| 333 |
seg_map,
|
|
|
|
| 336 |
geometry=geometry,
|
| 337 |
plane=plane,
|
| 338 |
)
|
| 339 |
+
finally:
|
| 340 |
+
app.ENABLE_GEOMETRY_SURFACE_FILTER = original_filter
|
| 341 |
+
app.SURFACE_MIN_PLANE_PIXELS = original_min
|
| 342 |
+
app.SURFACE_PLANE_DISTANCE_METERS = original_distance
|
| 343 |
+
app.ENABLE_SOFT_FLOOR_COVERING_EXPANSION = original_enabled
|
| 344 |
+
app.SOFT_FLOOR_COVERING_KERNEL_RATIO = original_ratio
|
| 345 |
+
app.SOFT_FLOOR_COVERING_DILATION_ITERATIONS = original_iterations
|
| 346 |
+
|
| 347 |
+
self.assertTrue(metadata["softFloorCoveringExpansionApplied"])
|
| 348 |
+
self.assertTrue(metadata["postSoftFloorCoveringGeometrySurfaceFilterApplied"])
|
| 349 |
+
self.assertEqual(int(surface[:45].sum()), 0)
|
| 350 |
+
|
| 351 |
+
def test_point_map_produces_moge_transform(self):
|
| 352 |
+
height, width = 360, 640
|
| 353 |
+
yy, xx = np.mgrid[0:height, 0:width]
|
| 354 |
+
mask = (yy > 220).astype(np.uint8)
|
| 355 |
focal = max(height, width) * 0.95
|
| 356 |
depth = np.full((height, width), 20.0, dtype=np.float32)
|
| 357 |
depth[mask > 0] = 1.6 * focal / (yy[mask > 0] - (height - 1) * 0.5)
|
visualizer.gpu.toml
CHANGED
|
@@ -22,8 +22,8 @@ enable_plane_fit = true
|
|
| 22 |
enable_multi_plane_surfaces = true
|
| 23 |
enable_intrinsic_shading = true
|
| 24 |
enable_shading_transfer = true
|
| 25 |
-
enable_sam_refinement =
|
| 26 |
-
enable_floor_mask_cleanup =
|
| 27 |
data_dir = "data"
|
| 28 |
|
| 29 |
[surface_mapping]
|
|
@@ -32,17 +32,20 @@ max_planes = 5
|
|
| 32 |
normal_angle_threshold_degrees = 35
|
| 33 |
normal_smoothing_kernel = 5
|
| 34 |
min_plane_pixels = 1500
|
| 35 |
-
edge_close_ratio = 0.
|
| 36 |
-
edge_kernel_ratio = 0.
|
| 37 |
-
edge_dilation_iterations =
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
| 40 |
uv_max_jump_ratio = 8.0
|
| 41 |
uv_absolute_jump_meters = 0.35
|
| 42 |
enable_geometry_surface_filter = true
|
| 43 |
-
plane_distance_threshold_meters = 0.
|
| 44 |
-
plane_normal_min_cos = 0.
|
| 45 |
-
plane_filter_min_keep_ratio = 0.
|
| 46 |
|
| 47 |
[gemini]
|
| 48 |
api_key = "AQ.Ab8RN6KTPAiQRFWdPOr2UezAr3b4h1fLTeMPHa2pTcOx6TS-IQ"
|
|
|
|
| 22 |
enable_multi_plane_surfaces = true
|
| 23 |
enable_intrinsic_shading = true
|
| 24 |
enable_shading_transfer = true
|
| 25 |
+
enable_sam_refinement = false
|
| 26 |
+
enable_floor_mask_cleanup = false
|
| 27 |
data_dir = "data"
|
| 28 |
|
| 29 |
[surface_mapping]
|
|
|
|
| 32 |
normal_angle_threshold_degrees = 35
|
| 33 |
normal_smoothing_kernel = 5
|
| 34 |
min_plane_pixels = 1500
|
| 35 |
+
edge_close_ratio = 0.009
|
| 36 |
+
edge_kernel_ratio = 0.006
|
| 37 |
+
edge_dilation_iterations = 2
|
| 38 |
+
soft_floor_covering_expansion_enabled = true
|
| 39 |
+
soft_floor_covering_kernel_ratio = 0.008
|
| 40 |
+
soft_floor_covering_dilation_iterations = 2
|
| 41 |
+
uv_min_coverage = 0.995
|
| 42 |
+
uv_max_bad_neighbor_ratio = 0.035
|
| 43 |
uv_max_jump_ratio = 8.0
|
| 44 |
uv_absolute_jump_meters = 0.35
|
| 45 |
enable_geometry_surface_filter = true
|
| 46 |
+
plane_distance_threshold_meters = 0.12
|
| 47 |
+
plane_normal_min_cos = 0.45
|
| 48 |
+
plane_filter_min_keep_ratio = 0.45
|
| 49 |
|
| 50 |
[gemini]
|
| 51 |
api_key = "AQ.Ab8RN6KTPAiQRFWdPOr2UezAr3b4h1fLTeMPHa2pTcOx6TS-IQ"
|
visualizer.hf.toml
CHANGED
|
@@ -23,7 +23,6 @@ enable_multi_plane_surfaces = true
|
|
| 23 |
enable_intrinsic_shading = true
|
| 24 |
enable_shading_transfer = true
|
| 25 |
enable_sam_refinement = true
|
| 26 |
-
enable_floor_mask_cleanup = true
|
| 27 |
data_dir = "data"
|
| 28 |
|
| 29 |
[surface_mapping]
|
|
@@ -32,14 +31,17 @@ max_planes = 5
|
|
| 32 |
normal_angle_threshold_degrees = 35
|
| 33 |
normal_smoothing_kernel = 5
|
| 34 |
min_plane_pixels = 1500
|
| 35 |
-
edge_close_ratio = 0.
|
| 36 |
-
edge_kernel_ratio = 0.
|
| 37 |
-
edge_dilation_iterations =
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
| 40 |
uv_max_jump_ratio = 8.0
|
| 41 |
uv_absolute_jump_meters = 0.35
|
| 42 |
enable_geometry_surface_filter = true
|
| 43 |
-
plane_distance_threshold_meters = 0.
|
| 44 |
-
plane_normal_min_cos = 0.
|
| 45 |
-
plane_filter_min_keep_ratio = 0.
|
|
|
|
| 23 |
enable_intrinsic_shading = true
|
| 24 |
enable_shading_transfer = true
|
| 25 |
enable_sam_refinement = true
|
|
|
|
| 26 |
data_dir = "data"
|
| 27 |
|
| 28 |
[surface_mapping]
|
|
|
|
| 31 |
normal_angle_threshold_degrees = 35
|
| 32 |
normal_smoothing_kernel = 5
|
| 33 |
min_plane_pixels = 1500
|
| 34 |
+
edge_close_ratio = 0.009
|
| 35 |
+
edge_kernel_ratio = 0.006
|
| 36 |
+
edge_dilation_iterations = 2
|
| 37 |
+
soft_floor_covering_expansion_enabled = true
|
| 38 |
+
soft_floor_covering_kernel_ratio = 0.008
|
| 39 |
+
soft_floor_covering_dilation_iterations = 2
|
| 40 |
+
uv_min_coverage = 0.995
|
| 41 |
+
uv_max_bad_neighbor_ratio = 0.035
|
| 42 |
uv_max_jump_ratio = 8.0
|
| 43 |
uv_absolute_jump_meters = 0.35
|
| 44 |
enable_geometry_surface_filter = true
|
| 45 |
+
plane_distance_threshold_meters = 0.12
|
| 46 |
+
plane_normal_min_cos = 0.45
|
| 47 |
+
plane_filter_min_keep_ratio = 0.45
|
visualizer.local.toml
CHANGED
|
@@ -28,6 +28,9 @@ min_plane_pixels = 1500
|
|
| 28 |
edge_close_ratio = 0.009
|
| 29 |
edge_kernel_ratio = 0.006
|
| 30 |
edge_dilation_iterations = 2
|
|
|
|
|
|
|
|
|
|
| 31 |
uv_min_coverage = 0.995
|
| 32 |
uv_max_bad_neighbor_ratio = 0.035
|
| 33 |
uv_max_jump_ratio = 8.0
|
|
|
|
| 28 |
edge_close_ratio = 0.009
|
| 29 |
edge_kernel_ratio = 0.006
|
| 30 |
edge_dilation_iterations = 2
|
| 31 |
+
soft_floor_covering_expansion_enabled = true
|
| 32 |
+
soft_floor_covering_kernel_ratio = 0.008
|
| 33 |
+
soft_floor_covering_dilation_iterations = 2
|
| 34 |
uv_min_coverage = 0.995
|
| 35 |
uv_max_bad_neighbor_ratio = 0.035
|
| 36 |
uv_max_jump_ratio = 8.0
|
visualizer.segformer.toml
CHANGED
|
@@ -29,6 +29,9 @@ min_plane_pixels = 1500
|
|
| 29 |
edge_close_ratio = 0.009
|
| 30 |
edge_kernel_ratio = 0.006
|
| 31 |
edge_dilation_iterations = 2
|
|
|
|
|
|
|
|
|
|
| 32 |
uv_min_coverage = 0.995
|
| 33 |
uv_max_bad_neighbor_ratio = 0.035
|
| 34 |
uv_max_jump_ratio = 8.0
|
|
|
|
| 29 |
edge_close_ratio = 0.009
|
| 30 |
edge_kernel_ratio = 0.006
|
| 31 |
edge_dilation_iterations = 2
|
| 32 |
+
soft_floor_covering_expansion_enabled = true
|
| 33 |
+
soft_floor_covering_kernel_ratio = 0.008
|
| 34 |
+
soft_floor_covering_dilation_iterations = 2
|
| 35 |
uv_min_coverage = 0.995
|
| 36 |
uv_max_bad_neighbor_ratio = 0.035
|
| 37 |
uv_max_jump_ratio = 8.0
|