Spaces:
Paused
Paused
Dev Deploy: 4059186
Browse files- app.py +264 -231
- visualizer.gpu.toml +9 -0
- visualizer.hf.toml +11 -0
- visualizer.local.toml +7 -0
- visualizer.segformer.toml +7 -0
app.py
CHANGED
|
@@ -106,6 +106,22 @@ SEGFORMER_MODEL_NAME = str(config_value(
|
|
| 106 |
"segformer_model_name",
|
| 107 |
"nvidia/segformer-b2-finetuned-ade-512-512",
|
| 108 |
))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
DEPTH_MODEL_NAME = str(config_value(
|
| 110 |
"DEPTH_MODEL_NAME",
|
| 111 |
"models",
|
|
@@ -220,6 +236,56 @@ SURFACE_EDGE_DILATION_ITERATIONS = int(config_value(
|
|
| 220 |
"edge_dilation_iterations",
|
| 221 |
"1",
|
| 222 |
))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
SURFACE_UV_MIN_COVERAGE = float(config_value(
|
| 224 |
"SURFACE_UV_MIN_COVERAGE",
|
| 225 |
"surface_mapping",
|
|
@@ -254,16 +320,32 @@ depth_model = None
|
|
| 254 |
moge_model = None
|
| 255 |
intrinsic_models = None
|
| 256 |
|
| 257 |
-
SHADE_MAP_MIN = 0.35
|
| 258 |
-
SHADE_MAP_MAX = 1.85
|
| 259 |
-
REFLECTION_RELATIVE_FLOOR = 1.04
|
| 260 |
-
REFLECTION_RELATIVE_CEILING = 1.95
|
| 261 |
-
|
| 262 |
|
| 263 |
def hf_offline() -> bool:
|
| 264 |
return os.getenv("HF_HUB_OFFLINE") == "1" or os.getenv("TRANSFORMERS_OFFLINE") == "1"
|
| 265 |
|
| 266 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 267 |
def _load_segmentation_model():
|
| 268 |
global seg_processor, seg_model, segmentation_backend
|
| 269 |
|
|
@@ -274,6 +356,7 @@ def _load_segmentation_model():
|
|
| 274 |
ONEFORMER_MODEL_NAME,
|
| 275 |
local_files_only=hf_offline(),
|
| 276 |
)
|
|
|
|
| 277 |
seg_model = OneFormerForUniversalSegmentation.from_pretrained(
|
| 278 |
ONEFORMER_MODEL_NAME,
|
| 279 |
local_files_only=hf_offline(),
|
|
@@ -501,230 +584,113 @@ def estimate_scene_geometry(img: Image.Image, img_np: np.ndarray, width: int, he
|
|
| 501 |
}
|
| 502 |
|
| 503 |
|
| 504 |
-
def
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
return np.round((clipped - minimum) * scale).clip(0, 255).astype(np.uint8)
|
| 508 |
-
|
| 509 |
|
| 510 |
-
|
| 511 |
-
|
| 512 |
img_np[:, :, 0].astype(np.float32) * 0.299
|
| 513 |
+ img_np[:, :, 1].astype(np.float32) * 0.587
|
| 514 |
+ img_np[:, :, 2].astype(np.float32) * 0.114
|
| 515 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 516 |
|
|
|
|
|
|
|
|
|
|
| 517 |
|
| 518 |
-
|
| 519 |
-
|
| 520 |
-
filled = values.astype(np.float32).copy()
|
| 521 |
-
filled[mask == 0] = fill_value
|
| 522 |
missing = (mask == 0).astype(np.uint8) * 255
|
| 523 |
try:
|
| 524 |
-
source = np.clip(filled, 0, 255).astype(np.uint8)
|
| 525 |
filled = cv2.inpaint(
|
| 526 |
-
|
| 527 |
missing,
|
| 528 |
max(3, min(h, w) // 160),
|
| 529 |
cv2.INPAINT_TELEA,
|
| 530 |
).astype(np.float32)
|
| 531 |
except cv2.error:
|
| 532 |
pass
|
| 533 |
-
return filled
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
def build_reflection_map(
|
| 537 |
-
img_np: np.ndarray,
|
| 538 |
-
surface_mask: np.ndarray,
|
| 539 |
-
relative_shading: np.ndarray | None = None,
|
| 540 |
-
) -> np.ndarray | None:
|
| 541 |
-
if not surface_mask.any():
|
| 542 |
-
return None
|
| 543 |
-
|
| 544 |
-
mask = surface_mask.astype(np.uint8)
|
| 545 |
-
h, w = mask.shape[:2]
|
| 546 |
-
highlight = np.zeros((h, w), dtype=np.float32)
|
| 547 |
-
|
| 548 |
-
if relative_shading is not None:
|
| 549 |
-
rel = relative_shading.astype(np.float32).copy()
|
| 550 |
-
rel[mask == 0] = 1.0
|
| 551 |
-
broad_sigma = max(6.0, min(h, w) / 44.0)
|
| 552 |
-
broad = cv2.GaussianBlur(rel, (0, 0), sigmaX=broad_sigma, sigmaY=broad_sigma)
|
| 553 |
-
detail = rel / np.maximum(broad, 1e-3)
|
| 554 |
-
highlight = np.maximum(
|
| 555 |
-
highlight,
|
| 556 |
-
(detail - REFLECTION_RELATIVE_FLOOR)
|
| 557 |
-
/ max(REFLECTION_RELATIVE_CEILING - REFLECTION_RELATIVE_FLOOR, 1e-6),
|
| 558 |
-
)
|
| 559 |
-
|
| 560 |
-
luminance = image_luminance(img_np) / 255.0
|
| 561 |
-
floor_values = luminance[mask > 0]
|
| 562 |
-
floor_values = floor_values[np.isfinite(floor_values)]
|
| 563 |
-
if floor_values.size >= max(256, int(h * w * 0.002)):
|
| 564 |
-
median_lum = float(np.median(floor_values))
|
| 565 |
-
if median_lum > 1e-3:
|
| 566 |
-
filled = luminance.copy()
|
| 567 |
-
filled[mask == 0] = median_lum
|
| 568 |
-
broad_sigma = max(8.0, min(h, w) / 36.0)
|
| 569 |
-
broad = cv2.GaussianBlur(filled, (0, 0), sigmaX=broad_sigma, sigmaY=broad_sigma)
|
| 570 |
-
detail = filled / np.maximum(broad, 1e-3)
|
| 571 |
-
highlight = np.maximum(
|
| 572 |
-
highlight,
|
| 573 |
-
0.75
|
| 574 |
-
* (detail - (REFLECTION_RELATIVE_FLOOR + 0.04))
|
| 575 |
-
/ max(REFLECTION_RELATIVE_CEILING - REFLECTION_RELATIVE_FLOOR, 1e-6),
|
| 576 |
-
)
|
| 577 |
-
|
| 578 |
-
highlight = np.clip(highlight, 0.0, 1.0)
|
| 579 |
-
if min(h, w) > 120:
|
| 580 |
-
sigma = max(0.6, min(h, w) / 520.0)
|
| 581 |
-
highlight = cv2.GaussianBlur(highlight, (0, 0), sigmaX=sigma, sigmaY=sigma)
|
| 582 |
-
highlight[mask == 0] = 0.0
|
| 583 |
-
if float(np.max(highlight[mask > 0])) < 0.025:
|
| 584 |
-
return None
|
| 585 |
-
return np.round(highlight * 255.0).clip(0, 255).astype(np.uint8)
|
| 586 |
-
|
| 587 |
-
|
| 588 |
-
def build_luminance_lighting_maps(
|
| 589 |
-
img_np: np.ndarray,
|
| 590 |
-
surface_mask: np.ndarray,
|
| 591 |
-
) -> dict[str, np.ndarray | None] | None:
|
| 592 |
-
if not surface_mask.any():
|
| 593 |
-
return None
|
| 594 |
-
|
| 595 |
-
mask = surface_mask.astype(np.uint8)
|
| 596 |
-
luminance = image_luminance(img_np)
|
| 597 |
-
h, w = mask.shape[:2]
|
| 598 |
-
floor_values = luminance[mask > 0]
|
| 599 |
-
if floor_values.size < max(256, int(h * w * 0.002)):
|
| 600 |
-
return None
|
| 601 |
-
|
| 602 |
-
median_lum = float(np.median(floor_values))
|
| 603 |
-
if median_lum < 1e-3:
|
| 604 |
-
return None
|
| 605 |
-
|
| 606 |
-
filled = inpaint_surface_values(luminance, mask, median_lum)
|
| 607 |
|
| 608 |
-
sigma = max(
|
| 609 |
smooth = cv2.GaussianBlur(filled, (0, 0), sigmaX=sigma, sigmaY=sigma)
|
| 610 |
-
shade = np.clip(smooth / median_lum,
|
| 611 |
shade[mask == 0] = 1.0
|
| 612 |
-
return
|
| 613 |
-
"shadeMap": encode_relative_map(shade, SHADE_MAP_MIN, SHADE_MAP_MAX),
|
| 614 |
-
"reflectionMap": build_reflection_map(img_np, surface_mask, None),
|
| 615 |
-
}
|
| 616 |
|
| 617 |
|
| 618 |
-
def
|
| 619 |
-
|
| 620 |
-
if "gry_shd" in results:
|
| 621 |
-
shading = results["gry_shd"]
|
| 622 |
-
elif "dif_shd" in results:
|
| 623 |
-
shading = results["dif_shd"]
|
| 624 |
-
else:
|
| 625 |
-
for key, value in results.items():
|
| 626 |
-
if "shd" in key or "shading" in key:
|
| 627 |
-
shading = value
|
| 628 |
-
break
|
| 629 |
-
|
| 630 |
-
if shading is None:
|
| 631 |
-
return None
|
| 632 |
-
|
| 633 |
-
shading = _to_numpy(shading)
|
| 634 |
-
if shading is None:
|
| 635 |
-
return None
|
| 636 |
-
if shading.ndim == 3:
|
| 637 |
-
if shading.shape[2] == 1:
|
| 638 |
-
shading = shading[:, :, 0]
|
| 639 |
-
else:
|
| 640 |
-
shading = (
|
| 641 |
-
shading[:, :, 0] * 0.299
|
| 642 |
-
+ shading[:, :, 1] * 0.587
|
| 643 |
-
+ shading[:, :, 2] * 0.114
|
| 644 |
-
)
|
| 645 |
-
if shading.ndim != 2:
|
| 646 |
return None
|
| 647 |
-
return shading.astype(np.float32)
|
| 648 |
|
|
|
|
|
|
|
|
|
|
| 649 |
|
| 650 |
-
|
| 651 |
-
|
| 652 |
-
surface_mask: np.ndarray,
|
| 653 |
-
) -> np.ndarray | None:
|
| 654 |
-
h, w = surface_mask.shape[:2]
|
| 655 |
-
if shading.shape[:2] != (h, w):
|
| 656 |
-
shading = cv2.resize(shading, (w, h), interpolation=cv2.INTER_LINEAR)
|
| 657 |
|
| 658 |
-
|
| 659 |
-
|
| 660 |
-
floor_vals = floor_vals[np.isfinite(floor_vals) & (floor_vals > 1e-6)]
|
| 661 |
-
if floor_vals.size == 0:
|
| 662 |
-
return None
|
| 663 |
|
| 664 |
-
|
| 665 |
-
|
| 666 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 667 |
|
| 668 |
-
|
| 669 |
-
|
| 670 |
-
relative[surface_mask == 0] = 1.0
|
| 671 |
-
return relative.astype(np.float32)
|
| 672 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 673 |
|
| 674 |
-
|
| 675 |
-
|
| 676 |
-
|
| 677 |
-
|
| 678 |
-
if not surface_mask.any() or intrinsic_models is None:
|
| 679 |
-
return None
|
| 680 |
|
| 681 |
-
|
| 682 |
-
|
| 683 |
-
from intrinsic.pipeline import run_pipeline
|
| 684 |
-
results = run_pipeline(intrinsic_models, img_float, device=str(device))
|
| 685 |
-
shading = extract_intrinsic_shading(results)
|
| 686 |
-
if shading is None:
|
| 687 |
return None
|
| 688 |
|
| 689 |
-
|
| 690 |
-
if
|
| 691 |
return None
|
| 692 |
|
| 693 |
-
|
| 694 |
-
|
| 695 |
-
try:
|
| 696 |
-
smoothed = cv2.bilateralFilter(
|
| 697 |
-
smoothed.astype(np.float32),
|
| 698 |
-
d=0,
|
| 699 |
-
sigmaColor=0.14,
|
| 700 |
-
sigmaSpace=max(2.0, min(h, w) / 140.0),
|
| 701 |
-
)
|
| 702 |
-
except cv2.error:
|
| 703 |
-
smoothed = relative
|
| 704 |
-
sigma = max(0.6, min(h, w) / 420.0)
|
| 705 |
-
smoothed = cv2.GaussianBlur(smoothed, (0, 0), sigmaX=sigma, sigmaY=sigma)
|
| 706 |
-
smoothed = np.clip(smoothed, SHADE_MAP_MIN, SHADE_MAP_MAX)
|
| 707 |
-
smoothed[surface_mask == 0] = 1.0
|
| 708 |
|
| 709 |
-
|
| 710 |
-
|
| 711 |
-
|
| 712 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 713 |
except Exception as exc:
|
| 714 |
print(f"Intrinsic shading decomposition failed: {exc}. Falling back to default luminance shading.", flush=True)
|
| 715 |
return None
|
| 716 |
|
| 717 |
|
| 718 |
-
def build_shade_map(img_np: np.ndarray, surface_mask: np.ndarray) -> np.ndarray | None:
|
| 719 |
-
lighting_maps = build_luminance_lighting_maps(img_np, surface_mask)
|
| 720 |
-
return lighting_maps.get("shadeMap") if lighting_maps else None
|
| 721 |
-
|
| 722 |
-
|
| 723 |
-
def build_intrinsic_shade_map(img_np: np.ndarray, surface_mask: np.ndarray) -> np.ndarray | None:
|
| 724 |
-
lighting_maps = build_intrinsic_lighting_maps(img_np, surface_mask)
|
| 725 |
-
return lighting_maps.get("shadeMap") if lighting_maps else None
|
| 726 |
-
|
| 727 |
-
|
| 728 |
def clean_floor_mask(mask: np.ndarray) -> np.ndarray:
|
| 729 |
if mask.dtype != np.uint8:
|
| 730 |
mask = mask.astype(np.uint8)
|
|
@@ -822,6 +788,100 @@ def improve_surface_edge_coverage(
|
|
| 822 |
}
|
| 823 |
|
| 824 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 825 |
def fit_floor_edges(mask: np.ndarray):
|
| 826 |
h, w = mask.shape[:2]
|
| 827 |
row_ys, lefts, rights = [], [], []
|
|
@@ -1832,14 +1892,15 @@ def build_floor_surface_mask(
|
|
| 1832 |
seg_map: np.ndarray,
|
| 1833 |
quad: np.ndarray | None,
|
| 1834 |
depth: np.ndarray | None,
|
|
|
|
| 1835 |
):
|
| 1836 |
h, w = floor_mask.shape[:2]
|
| 1837 |
kern_size = max(5, min(h, w) // 160) | 1
|
| 1838 |
kern = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (kern_size, kern_size))
|
| 1839 |
occluder_mask = np.isin(seg_map, class_ids(OCCLUDER_CLASSES)).astype(np.uint8)
|
| 1840 |
-
occ_dilated = cv2.dilate(occluder_mask, kern, iterations=
|
| 1841 |
reject_mask = np.isin(seg_map, class_ids(REJECT_SURFACE_CLASSES)).astype(np.uint8)
|
| 1842 |
-
reject_dilated = cv2.dilate(reject_mask, kern, iterations=
|
| 1843 |
|
| 1844 |
surface = floor_mask.copy()
|
| 1845 |
surface[reject_dilated > 0] = 0
|
|
@@ -1882,6 +1943,9 @@ def build_floor_surface_mask(
|
|
| 1882 |
|
| 1883 |
protected_mask = (occ_dilated > 0) | (reject_dilated > 0)
|
| 1884 |
surface, edge_metadata = improve_surface_edge_coverage(surface, protected_mask)
|
|
|
|
|
|
|
|
|
|
| 1885 |
return surface, edge_metadata
|
| 1886 |
|
| 1887 |
|
|
@@ -1956,14 +2020,9 @@ def active_geometry_model_name(provider: str | None) -> str | None:
|
|
| 1956 |
return None
|
| 1957 |
|
| 1958 |
|
| 1959 |
-
def active_model_metadata(
|
| 1960 |
-
geometry: dict | None,
|
| 1961 |
-
depth: np.ndarray | None,
|
| 1962 |
-
shade_map: np.ndarray | None,
|
| 1963 |
-
intrinsic_lighting_used: bool = False,
|
| 1964 |
-
) -> dict:
|
| 1965 |
geometry_provider = geometry.get("provider") if geometry else None
|
| 1966 |
-
intrinsic_enabled = ENABLE_INTRINSIC_SHADING and
|
| 1967 |
return {
|
| 1968 |
"segmentation": {
|
| 1969 |
"backend": segmentation_backend,
|
|
@@ -2030,7 +2089,7 @@ def build_segmentation_bundle(contents: bytes):
|
|
| 2030 |
|
| 2031 |
t0 = time.perf_counter()
|
| 2032 |
quad = np.asarray(plane["quad"], dtype=np.float32).reshape(4, 2) if plane and plane.get("quad") else None
|
| 2033 |
-
surface_mask, edge_metadata = build_floor_surface_mask(floor_mask, seg_map, quad, depth)
|
| 2034 |
surface_indices = np.flatnonzero(surface_mask.ravel()).astype(np.uint32)
|
| 2035 |
print(f"[TIMING] Surface masking took {time.perf_counter() - t0:.3f} seconds", flush=True)
|
| 2036 |
|
|
@@ -2060,36 +2119,19 @@ def build_segmentation_bundle(contents: bytes):
|
|
| 2060 |
print(f"[TIMING] Multi-plane surface mapping took {time.perf_counter() - t0:.3f} seconds", flush=True)
|
| 2061 |
|
| 2062 |
t0 = time.perf_counter()
|
| 2063 |
-
lighting_maps = None
|
| 2064 |
shade_map = None
|
| 2065 |
-
reflection_map = None
|
| 2066 |
-
lighting_source = None
|
| 2067 |
-
intrinsic_lighting_used = False
|
| 2068 |
if ENABLE_SHADING_TRANSFER and ENABLE_INTRINSIC_SHADING:
|
| 2069 |
if intrinsic_models is None:
|
| 2070 |
_load_intrinsic_model()
|
| 2071 |
if intrinsic_models is not None:
|
| 2072 |
-
|
| 2073 |
-
|
| 2074 |
-
|
| 2075 |
-
|
| 2076 |
-
if ENABLE_SHADING_TRANSFER and (not lighting_maps or lighting_maps.get("shadeMap") is None):
|
| 2077 |
-
lighting_maps = build_luminance_lighting_maps(img_np, surface_mask)
|
| 2078 |
-
if lighting_maps and lighting_maps.get("shadeMap") is not None:
|
| 2079 |
-
lighting_source = "luminance"
|
| 2080 |
-
if lighting_maps:
|
| 2081 |
-
shade_map = lighting_maps.get("shadeMap")
|
| 2082 |
-
reflection_map = lighting_maps.get("reflectionMap")
|
| 2083 |
-
print(
|
| 2084 |
-
"[TIMING] Lighting map construction took "
|
| 2085 |
-
f"{time.perf_counter() - t0:.3f} seconds "
|
| 2086 |
-
f"(source={lighting_source}, reflections={reflection_map is not None})",
|
| 2087 |
-
flush=True,
|
| 2088 |
-
)
|
| 2089 |
|
| 2090 |
t0 = time.perf_counter()
|
| 2091 |
segments = []
|
| 2092 |
-
models = active_model_metadata(geometry, depth, shade_map
|
| 2093 |
|
| 2094 |
if len(surface_indices) >= min_floor_area:
|
| 2095 |
segments.append({
|
|
@@ -2109,13 +2151,10 @@ def build_segmentation_bundle(contents: bytes):
|
|
| 2109 |
),
|
| 2110 |
"surfacePlanes": surface_mapping["planes"] if surface_mapping is not None else None,
|
| 2111 |
"shadeMap": base64.b64encode(shade_map.tobytes()).decode() if shade_map is not None else None,
|
| 2112 |
-
"reflectionMap": (
|
| 2113 |
-
base64.b64encode(reflection_map.tobytes()).decode()
|
| 2114 |
-
if reflection_map is not None else None
|
| 2115 |
-
),
|
| 2116 |
"metadata": {
|
| 2117 |
"segmenter": segmenter_metadata_name(),
|
| 2118 |
"segmentationModel": models["segmentation"]["name"],
|
|
|
|
| 2119 |
"floorPixels": int(floor_mask.sum()),
|
| 2120 |
"surfacePixels": int(surface_mask.sum()),
|
| 2121 |
**edge_metadata,
|
|
@@ -2134,10 +2173,6 @@ def build_segmentation_bundle(contents: bytes):
|
|
| 2134 |
if surface_mapping is not None else []
|
| 2135 |
),
|
| 2136 |
"shadingTransferEnabled": ENABLE_SHADING_TRANSFER,
|
| 2137 |
-
"lightingSource": lighting_source,
|
| 2138 |
-
"shadeMapRange": [SHADE_MAP_MIN, SHADE_MAP_MAX],
|
| 2139 |
-
"reflectionTransferEnabled": reflection_map is not None,
|
| 2140 |
-
"reflectionMapRange": [0.0, 1.0],
|
| 2141 |
"geometryConfidence": plane.get("geometryConfidence", 0.0) if plane else 0.0,
|
| 2142 |
"metricGeometry": bool(plane and plane.get("floorTransform")),
|
| 2143 |
"geometryProvider": plane.get("geometryProvider", geometry.get("provider")) if plane else geometry.get("provider"),
|
|
@@ -2158,7 +2193,6 @@ def build_segmentation_bundle(contents: bytes):
|
|
| 2158 |
"homography": None,
|
| 2159 |
"plane": None,
|
| 2160 |
"shadeMap": None,
|
| 2161 |
-
"reflectionMap": None,
|
| 2162 |
"metadata": {
|
| 2163 |
"segmenter": segmenter_metadata_name(),
|
| 2164 |
"segmentationModel": models["segmentation"]["name"],
|
|
@@ -2166,7 +2200,6 @@ def build_segmentation_bundle(contents: bytes):
|
|
| 2166 |
"shadingEnabled": False,
|
| 2167 |
"planeFitEnabled": ENABLE_PLANE_FIT,
|
| 2168 |
"shadingTransferEnabled": ENABLE_SHADING_TRANSFER,
|
| 2169 |
-
"reflectionTransferEnabled": False,
|
| 2170 |
},
|
| 2171 |
})
|
| 2172 |
|
|
|
|
| 106 |
"segformer_model_name",
|
| 107 |
"nvidia/segformer-b2-finetuned-ade-512-512",
|
| 108 |
))
|
| 109 |
+
# Resolution the segmenter runs at. The model's default (~640px shortest edge) decides the
|
| 110 |
+
# floor-mask boundary at low res and upscales it, which is the root cause of the coarse
|
| 111 |
+
# "pale rim" around objects. Raising this sharpens silhouettes at the cost of GPU VRAM /
|
| 112 |
+
# latency. Set 0 to keep the model default. Only applied to the OneFormer backend.
|
| 113 |
+
SEGMENTATION_SHORTEST_EDGE = int(config_value(
|
| 114 |
+
"SEGMENTATION_SHORTEST_EDGE",
|
| 115 |
+
"models",
|
| 116 |
+
"segmentation_shortest_edge",
|
| 117 |
+
"1024",
|
| 118 |
+
))
|
| 119 |
+
SEGMENTATION_LONGEST_EDGE = int(config_value(
|
| 120 |
+
"SEGMENTATION_LONGEST_EDGE",
|
| 121 |
+
"models",
|
| 122 |
+
"segmentation_longest_edge",
|
| 123 |
+
"2048",
|
| 124 |
+
))
|
| 125 |
DEPTH_MODEL_NAME = str(config_value(
|
| 126 |
"DEPTH_MODEL_NAME",
|
| 127 |
"models",
|
|
|
|
| 236 |
"edge_dilation_iterations",
|
| 237 |
"1",
|
| 238 |
))
|
| 239 |
+
# Occluder/wall protection ring removed from the floor surface. Smaller = the tile
|
| 240 |
+
# hugs objects more tightly (less original-floor halo), larger = safer against bleed.
|
| 241 |
+
SURFACE_OCCLUDER_DILATION_ITERATIONS = int(config_value(
|
| 242 |
+
"SURFACE_OCCLUDER_DILATION_ITERATIONS",
|
| 243 |
+
"surface_mapping",
|
| 244 |
+
"occluder_dilation_iterations",
|
| 245 |
+
"1",
|
| 246 |
+
))
|
| 247 |
+
# Image-guided refinement that snaps the surface-mask boundary to true image edges,
|
| 248 |
+
# so the tile reaches the object silhouette instead of leaving a visible halo.
|
| 249 |
+
SURFACE_EDGE_REFINE_ENABLED = str(config_value(
|
| 250 |
+
"SURFACE_EDGE_REFINE_ENABLED",
|
| 251 |
+
"surface_mapping",
|
| 252 |
+
"edge_refine_enabled",
|
| 253 |
+
"1",
|
| 254 |
+
)).lower() not in ("0", "false", "no", "off", "")
|
| 255 |
+
# How far the surface may grow to fill the original-floor halo around objects.
|
| 256 |
+
SURFACE_EDGE_REFINE_FILL_RATIO = float(config_value(
|
| 257 |
+
"SURFACE_EDGE_REFINE_FILL_RATIO",
|
| 258 |
+
"surface_mapping",
|
| 259 |
+
"edge_refine_fill_ratio",
|
| 260 |
+
"0.010",
|
| 261 |
+
))
|
| 262 |
+
# Colour-gradient strength (0..1, normalised) above which the edge-aware grow stops.
|
| 263 |
+
# Higher = grow crosses weaker edges (fills more, risks bleed); lower = stops sooner.
|
| 264 |
+
SURFACE_EDGE_REFINE_GRAD_THRESHOLD = float(config_value(
|
| 265 |
+
"SURFACE_EDGE_REFINE_GRAD_THRESHOLD",
|
| 266 |
+
"surface_mapping",
|
| 267 |
+
"edge_refine_grad_threshold",
|
| 268 |
+
"0.10",
|
| 269 |
+
))
|
| 270 |
+
# Guided-filter window used to snap the grown boundary back onto real image edges.
|
| 271 |
+
SURFACE_EDGE_REFINE_RADIUS_RATIO = float(config_value(
|
| 272 |
+
"SURFACE_EDGE_REFINE_RADIUS_RATIO",
|
| 273 |
+
"surface_mapping",
|
| 274 |
+
"edge_refine_radius_ratio",
|
| 275 |
+
"0.006",
|
| 276 |
+
))
|
| 277 |
+
SURFACE_EDGE_REFINE_EPS = float(config_value(
|
| 278 |
+
"SURFACE_EDGE_REFINE_EPS",
|
| 279 |
+
"surface_mapping",
|
| 280 |
+
"edge_refine_eps",
|
| 281 |
+
"0.0001",
|
| 282 |
+
))
|
| 283 |
+
SURFACE_EDGE_REFINE_THRESHOLD = float(config_value(
|
| 284 |
+
"SURFACE_EDGE_REFINE_THRESHOLD",
|
| 285 |
+
"surface_mapping",
|
| 286 |
+
"edge_refine_threshold",
|
| 287 |
+
"0.5",
|
| 288 |
+
))
|
| 289 |
SURFACE_UV_MIN_COVERAGE = float(config_value(
|
| 290 |
"SURFACE_UV_MIN_COVERAGE",
|
| 291 |
"surface_mapping",
|
|
|
|
| 320 |
moge_model = None
|
| 321 |
intrinsic_models = None
|
| 322 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 323 |
|
| 324 |
def hf_offline() -> bool:
|
| 325 |
return os.getenv("HF_HUB_OFFLINE") == "1" or os.getenv("TRANSFORMERS_OFFLINE") == "1"
|
| 326 |
|
| 327 |
|
| 328 |
+
def _apply_segmentation_input_size(processor) -> dict | None:
|
| 329 |
+
"""Raise the segmenter's input resolution so the floor-mask boundary is decided at
|
| 330 |
+
higher resolution (sharper object silhouettes) instead of the model's low ~640px
|
| 331 |
+
default. Returns the applied size dict, or None if disabled/unsupported."""
|
| 332 |
+
if SEGMENTATION_SHORTEST_EDGE <= 0:
|
| 333 |
+
return None
|
| 334 |
+
image_processor = getattr(processor, "image_processor", processor)
|
| 335 |
+
try:
|
| 336 |
+
size = dict(getattr(image_processor, "size", {}) or {})
|
| 337 |
+
size["shortest_edge"] = SEGMENTATION_SHORTEST_EDGE
|
| 338 |
+
size["longest_edge"] = max(SEGMENTATION_LONGEST_EDGE, SEGMENTATION_SHORTEST_EDGE)
|
| 339 |
+
size.pop("height", None)
|
| 340 |
+
size.pop("width", None)
|
| 341 |
+
image_processor.size = size
|
| 342 |
+
print(f"Segmentation input size -> {size}", flush=True)
|
| 343 |
+
return size
|
| 344 |
+
except Exception as exc:
|
| 345 |
+
print(f"Could not override segmentation input size: {exc}", flush=True)
|
| 346 |
+
return None
|
| 347 |
+
|
| 348 |
+
|
| 349 |
def _load_segmentation_model():
|
| 350 |
global seg_processor, seg_model, segmentation_backend
|
| 351 |
|
|
|
|
| 356 |
ONEFORMER_MODEL_NAME,
|
| 357 |
local_files_only=hf_offline(),
|
| 358 |
)
|
| 359 |
+
_apply_segmentation_input_size(seg_processor)
|
| 360 |
seg_model = OneFormerForUniversalSegmentation.from_pretrained(
|
| 361 |
ONEFORMER_MODEL_NAME,
|
| 362 |
local_files_only=hf_offline(),
|
|
|
|
| 584 |
}
|
| 585 |
|
| 586 |
|
| 587 |
+
def build_shade_map(img_np: np.ndarray, surface_mask: np.ndarray) -> np.ndarray | None:
|
| 588 |
+
if not surface_mask.any():
|
| 589 |
+
return None
|
|
|
|
|
|
|
| 590 |
|
| 591 |
+
mask = surface_mask.astype(np.uint8)
|
| 592 |
+
luminance = (
|
| 593 |
img_np[:, :, 0].astype(np.float32) * 0.299
|
| 594 |
+ img_np[:, :, 1].astype(np.float32) * 0.587
|
| 595 |
+ img_np[:, :, 2].astype(np.float32) * 0.114
|
| 596 |
)
|
| 597 |
+
h, w = mask.shape[:2]
|
| 598 |
+
floor_values = luminance[mask > 0]
|
| 599 |
+
if floor_values.size < max(256, int(h * w * 0.002)):
|
| 600 |
+
return None
|
| 601 |
|
| 602 |
+
median_lum = float(np.median(floor_values))
|
| 603 |
+
if median_lum < 1e-3:
|
| 604 |
+
return None
|
| 605 |
|
| 606 |
+
filled = luminance.copy()
|
| 607 |
+
filled[mask == 0] = median_lum
|
|
|
|
|
|
|
| 608 |
missing = (mask == 0).astype(np.uint8) * 255
|
| 609 |
try:
|
|
|
|
| 610 |
filled = cv2.inpaint(
|
| 611 |
+
np.clip(filled, 0, 255).astype(np.uint8),
|
| 612 |
missing,
|
| 613 |
max(3, min(h, w) // 160),
|
| 614 |
cv2.INPAINT_TELEA,
|
| 615 |
).astype(np.float32)
|
| 616 |
except cv2.error:
|
| 617 |
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 618 |
|
| 619 |
+
sigma = max(8.0, min(h, w) / 28.0)
|
| 620 |
smooth = cv2.GaussianBlur(filled, (0, 0), sigmaX=sigma, sigmaY=sigma)
|
| 621 |
+
shade = np.clip(smooth / median_lum, 0.55, 1.35)
|
| 622 |
shade[mask == 0] = 1.0
|
| 623 |
+
return np.round((shade - 0.55) * (255.0 / 0.80)).clip(0, 255).astype(np.uint8)
|
|
|
|
|
|
|
|
|
|
| 624 |
|
| 625 |
|
| 626 |
+
def build_intrinsic_shade_map(img_np: np.ndarray, surface_mask: np.ndarray) -> np.ndarray | None:
|
| 627 |
+
if not surface_mask.any() or intrinsic_models is None:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 628 |
return None
|
|
|
|
| 629 |
|
| 630 |
+
try:
|
| 631 |
+
# Convert image to float32 range [0.0, 1.0] as expected by compphoto/Intrinsic
|
| 632 |
+
img_float = img_np.astype(np.float32) / 255.0
|
| 633 |
|
| 634 |
+
# Run pipeline
|
| 635 |
+
from intrinsic.pipeline import run_pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 636 |
|
| 637 |
+
# Use CPU/CUDA device string
|
| 638 |
+
results = run_pipeline(intrinsic_models, img_float, device=str(device))
|
|
|
|
|
|
|
|
|
|
| 639 |
|
| 640 |
+
# Extract shading map
|
| 641 |
+
shading = None
|
| 642 |
+
if "gry_shd" in results:
|
| 643 |
+
shading = results["gry_shd"]
|
| 644 |
+
elif "dif_shd" in results:
|
| 645 |
+
# If 3-channel diffuse shading, convert to grayscale luminance
|
| 646 |
+
dif = results["dif_shd"]
|
| 647 |
+
shading = dif[:, :, 0] * 0.299 + dif[:, :, 1] * 0.587 + dif[:, :, 2] * 0.114
|
| 648 |
+
else:
|
| 649 |
+
# Check other fallback keys matching "shd" or "shading"
|
| 650 |
+
for k in results.keys():
|
| 651 |
+
if "shd" in k or "shading" in k:
|
| 652 |
+
shading = results[k]
|
| 653 |
+
if len(shading.shape) == 3:
|
| 654 |
+
shading = shading[:, :, 0] * 0.299 + shading[:, :, 1] * 0.587 + shading[:, :, 2] * 0.114
|
| 655 |
+
break
|
| 656 |
|
| 657 |
+
if shading is None:
|
| 658 |
+
return None
|
|
|
|
|
|
|
| 659 |
|
| 660 |
+
# Resize shading to match original image size if there's any mismatch
|
| 661 |
+
h, w = surface_mask.shape[:2]
|
| 662 |
+
if shading.shape[:2] != (h, w):
|
| 663 |
+
shading = cv2.resize(shading, (w, h), interpolation=cv2.INTER_LINEAR)
|
| 664 |
|
| 665 |
+
# Smooth the shading map to eliminate high-frequency grout lines/patterns
|
| 666 |
+
# while preserving broad ambient shadows (e.g. plant shadows).
|
| 667 |
+
sigma = max(3.0, min(h, w) / 80.0)
|
| 668 |
+
shading = cv2.GaussianBlur(shading.astype(np.float32), (0, 0), sigmaX=sigma, sigmaY=sigma)
|
|
|
|
|
|
|
| 669 |
|
| 670 |
+
floor_vals = shading[surface_mask > 0]
|
| 671 |
+
if floor_vals.size == 0:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 672 |
return None
|
| 673 |
|
| 674 |
+
median_val = float(np.median(floor_vals))
|
| 675 |
+
if median_val < 1e-3:
|
| 676 |
return None
|
| 677 |
|
| 678 |
+
# Divide by median to get relative shading multiplier (1.0 is neutral)
|
| 679 |
+
relative_shading = shading / median_val
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 680 |
|
| 681 |
+
# Clip relative shading to [0.55, 1.35]
|
| 682 |
+
relative_shading = np.clip(relative_shading, 0.55, 1.35)
|
| 683 |
+
|
| 684 |
+
# Non-floor pixels are neutral (1.0)
|
| 685 |
+
relative_shading[surface_mask == 0] = 1.0
|
| 686 |
+
|
| 687 |
+
# Encode to [0, 255] byte range matching the frontend
|
| 688 |
+
return np.round((relative_shading - 0.55) * (255.0 / 0.80)).clip(0, 255).astype(np.uint8)
|
| 689 |
except Exception as exc:
|
| 690 |
print(f"Intrinsic shading decomposition failed: {exc}. Falling back to default luminance shading.", flush=True)
|
| 691 |
return None
|
| 692 |
|
| 693 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 694 |
def clean_floor_mask(mask: np.ndarray) -> np.ndarray:
|
| 695 |
if mask.dtype != np.uint8:
|
| 696 |
mask = mask.astype(np.uint8)
|
|
|
|
| 788 |
}
|
| 789 |
|
| 790 |
|
| 791 |
+
def _guided_filter(guide: np.ndarray, src: np.ndarray, radius: int, eps: float) -> np.ndarray:
|
| 792 |
+
"""He et al. guided filter implemented with box filters (base OpenCV only).
|
| 793 |
+
|
| 794 |
+
`guide` is the grayscale image (0..1), `src` is the soft mask (0..1). The output
|
| 795 |
+
follows the guide's edges, which lets a binary mask snap onto real image boundaries.
|
| 796 |
+
"""
|
| 797 |
+
ksize = (radius, radius)
|
| 798 |
+
mean_g = cv2.boxFilter(guide, cv2.CV_32F, ksize)
|
| 799 |
+
mean_s = cv2.boxFilter(src, cv2.CV_32F, ksize)
|
| 800 |
+
corr_g = cv2.boxFilter(guide * guide, cv2.CV_32F, ksize)
|
| 801 |
+
corr_gs = cv2.boxFilter(guide * src, cv2.CV_32F, ksize)
|
| 802 |
+
var_g = corr_g - mean_g * mean_g
|
| 803 |
+
cov_gs = corr_gs - mean_g * mean_s
|
| 804 |
+
a = cov_gs / (var_g + eps)
|
| 805 |
+
b = mean_s - a * mean_g
|
| 806 |
+
mean_a = cv2.boxFilter(a, cv2.CV_32F, ksize)
|
| 807 |
+
mean_b = cv2.boxFilter(b, cv2.CV_32F, ksize)
|
| 808 |
+
return mean_a * guide + mean_b
|
| 809 |
+
|
| 810 |
+
|
| 811 |
+
def refine_surface_edges(
|
| 812 |
+
surface: np.ndarray,
|
| 813 |
+
img_np: np.ndarray,
|
| 814 |
+
protected_mask: np.ndarray,
|
| 815 |
+
) -> tuple[np.ndarray, dict]:
|
| 816 |
+
"""Close the original-floor halo around objects and snap the edge to the photo.
|
| 817 |
+
|
| 818 |
+
The coarse segmentation (plus the occluder protection ring) leaves a band of the
|
| 819 |
+
original, often bright, floor around objects — the visible "halo". A guided filter
|
| 820 |
+
alone cannot fix it because that band is the same colour as the floor, so there is no
|
| 821 |
+
edge to snap to. Two steps instead:
|
| 822 |
+
|
| 823 |
+
1. Grow the surface into the surrounding non-protected pixels to fill the halo so the
|
| 824 |
+
tile reaches the object.
|
| 825 |
+
2. Run a guided filter with the photo as guide and re-decide a band around the new
|
| 826 |
+
boundary: growth that spilled onto an under-segmented object sits on a real image
|
| 827 |
+
edge and gets carved back, while growth across uniform floor is kept.
|
| 828 |
+
|
| 829 |
+
Protected occluder/wall pixels are always cleared at the end.
|
| 830 |
+
"""
|
| 831 |
+
h, w = surface.shape[:2]
|
| 832 |
+
if not SURFACE_EDGE_REFINE_ENABLED or not surface.any():
|
| 833 |
+
return surface, {"surfaceEdgeRefineEnabled": False}
|
| 834 |
+
|
| 835 |
+
protected = protected_mask.astype(bool)
|
| 836 |
+
before = int(surface.sum())
|
| 837 |
+
|
| 838 |
+
# 1. fill the halo with an edge-aware (geodesic) grow: expand the surface one step at
|
| 839 |
+
# a time only into non-protected, low-gradient pixels, so it spreads across the uniform
|
| 840 |
+
# original-floor halo but stops at real object/wall edges instead of rounding corners
|
| 841 |
+
# into bumps (the scalloping a blind ellipse dilation produces). A colour gradient is
|
| 842 |
+
# used so hue edges (e.g. a blue cabinet on a cream floor) are caught, not just luma.
|
| 843 |
+
fill_radius = scaled_odd_kernel_size(h, w, SURFACE_EDGE_REFINE_FILL_RATIO, minimum=3)
|
| 844 |
+
blurred = cv2.GaussianBlur(img_np, (0, 0), 1.2).astype(np.float32)
|
| 845 |
+
gx = cv2.Sobel(blurred, cv2.CV_32F, 1, 0, ksize=3)
|
| 846 |
+
gy = cv2.Sobel(blurred, cv2.CV_32F, 0, 1, ksize=3)
|
| 847 |
+
grad = np.sqrt((gx * gx + gy * gy).sum(axis=2))
|
| 848 |
+
grad /= float(grad.max()) + 1e-6
|
| 849 |
+
allowed = (((~protected) & (grad < SURFACE_EDGE_REFINE_GRAD_THRESHOLD)) | (surface > 0)).astype(np.uint8)
|
| 850 |
+
step = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
|
| 851 |
+
grown = surface.copy()
|
| 852 |
+
for _ in range(int(fill_radius)):
|
| 853 |
+
nxt = (((cv2.dilate(grown, step) & allowed) | grown) > 0).astype(np.uint8)
|
| 854 |
+
if int(nxt.sum()) == int(grown.sum()):
|
| 855 |
+
break
|
| 856 |
+
grown = nxt
|
| 857 |
+
grown[protected] = 0
|
| 858 |
+
grown = ((grown | surface) > 0).astype(np.uint8)
|
| 859 |
+
|
| 860 |
+
# 2. snap the grown boundary back onto real image edges
|
| 861 |
+
snap_radius = scaled_odd_kernel_size(h, w, SURFACE_EDGE_REFINE_RADIUS_RATIO, minimum=3)
|
| 862 |
+
guide = cv2.cvtColor(img_np, cv2.COLOR_RGB2GRAY).astype(np.float32) / 255.0
|
| 863 |
+
smoothed = _guided_filter(guide, grown.astype(np.float32), snap_radius, SURFACE_EDGE_REFINE_EPS)
|
| 864 |
+
snapped = (smoothed >= SURFACE_EDGE_REFINE_THRESHOLD).astype(np.uint8)
|
| 865 |
+
|
| 866 |
+
band_kern = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (snap_radius, snap_radius))
|
| 867 |
+
boundary_band = (cv2.dilate(grown, band_kern) > 0) & (cv2.erode(grown, band_kern) == 0)
|
| 868 |
+
|
| 869 |
+
refined = grown.copy()
|
| 870 |
+
refined[boundary_band] = snapped[boundary_band]
|
| 871 |
+
refined[protected] = 0
|
| 872 |
+
refined = (refined > 0).astype(np.uint8)
|
| 873 |
+
|
| 874 |
+
return refined, {
|
| 875 |
+
"surfaceEdgeRefineEnabled": True,
|
| 876 |
+
"surfaceEdgeRefineMode": "edge-aware",
|
| 877 |
+
"surfaceEdgeRefineFillRadius": int(fill_radius),
|
| 878 |
+
"surfaceEdgeRefineSnapRadius": int(snap_radius),
|
| 879 |
+
"surfaceEdgeRefineGradThreshold": SURFACE_EDGE_REFINE_GRAD_THRESHOLD,
|
| 880 |
+
"surfaceEdgeRefinePixelsBefore": before,
|
| 881 |
+
"surfaceEdgeRefinePixelsAfter": int(refined.sum()),
|
| 882 |
+
}
|
| 883 |
+
|
| 884 |
+
|
| 885 |
def fit_floor_edges(mask: np.ndarray):
|
| 886 |
h, w = mask.shape[:2]
|
| 887 |
row_ys, lefts, rights = [], [], []
|
|
|
|
| 1892 |
seg_map: np.ndarray,
|
| 1893 |
quad: np.ndarray | None,
|
| 1894 |
depth: np.ndarray | None,
|
| 1895 |
+
img_np: np.ndarray | None = None,
|
| 1896 |
):
|
| 1897 |
h, w = floor_mask.shape[:2]
|
| 1898 |
kern_size = max(5, min(h, w) // 160) | 1
|
| 1899 |
kern = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (kern_size, kern_size))
|
| 1900 |
occluder_mask = np.isin(seg_map, class_ids(OCCLUDER_CLASSES)).astype(np.uint8)
|
| 1901 |
+
occ_dilated = cv2.dilate(occluder_mask, kern, iterations=SURFACE_OCCLUDER_DILATION_ITERATIONS)
|
| 1902 |
reject_mask = np.isin(seg_map, class_ids(REJECT_SURFACE_CLASSES)).astype(np.uint8)
|
| 1903 |
+
reject_dilated = cv2.dilate(reject_mask, kern, iterations=SURFACE_OCCLUDER_DILATION_ITERATIONS)
|
| 1904 |
|
| 1905 |
surface = floor_mask.copy()
|
| 1906 |
surface[reject_dilated > 0] = 0
|
|
|
|
| 1943 |
|
| 1944 |
protected_mask = (occ_dilated > 0) | (reject_dilated > 0)
|
| 1945 |
surface, edge_metadata = improve_surface_edge_coverage(surface, protected_mask)
|
| 1946 |
+
if img_np is not None:
|
| 1947 |
+
surface, refine_metadata = refine_surface_edges(surface, img_np, protected_mask)
|
| 1948 |
+
edge_metadata.update(refine_metadata)
|
| 1949 |
return surface, edge_metadata
|
| 1950 |
|
| 1951 |
|
|
|
|
| 2020 |
return None
|
| 2021 |
|
| 2022 |
|
| 2023 |
+
def active_model_metadata(geometry: dict | None, depth: np.ndarray | None, shade_map: np.ndarray | None) -> dict:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2024 |
geometry_provider = geometry.get("provider") if geometry else None
|
| 2025 |
+
intrinsic_enabled = ENABLE_INTRINSIC_SHADING and intrinsic_models is not None and shade_map is not None
|
| 2026 |
return {
|
| 2027 |
"segmentation": {
|
| 2028 |
"backend": segmentation_backend,
|
|
|
|
| 2089 |
|
| 2090 |
t0 = time.perf_counter()
|
| 2091 |
quad = np.asarray(plane["quad"], dtype=np.float32).reshape(4, 2) if plane and plane.get("quad") else None
|
| 2092 |
+
surface_mask, edge_metadata = build_floor_surface_mask(floor_mask, seg_map, quad, depth, img_np)
|
| 2093 |
surface_indices = np.flatnonzero(surface_mask.ravel()).astype(np.uint32)
|
| 2094 |
print(f"[TIMING] Surface masking took {time.perf_counter() - t0:.3f} seconds", flush=True)
|
| 2095 |
|
|
|
|
| 2119 |
print(f"[TIMING] Multi-plane surface mapping took {time.perf_counter() - t0:.3f} seconds", flush=True)
|
| 2120 |
|
| 2121 |
t0 = time.perf_counter()
|
|
|
|
| 2122 |
shade_map = None
|
|
|
|
|
|
|
|
|
|
| 2123 |
if ENABLE_SHADING_TRANSFER and ENABLE_INTRINSIC_SHADING:
|
| 2124 |
if intrinsic_models is None:
|
| 2125 |
_load_intrinsic_model()
|
| 2126 |
if intrinsic_models is not None:
|
| 2127 |
+
shade_map = build_intrinsic_shade_map(img_np, surface_mask)
|
| 2128 |
+
if ENABLE_SHADING_TRANSFER and shade_map is None:
|
| 2129 |
+
shade_map = build_shade_map(img_np, surface_mask)
|
| 2130 |
+
print(f"[TIMING] Shade map construction took {time.perf_counter() - t0:.3f} seconds", flush=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2131 |
|
| 2132 |
t0 = time.perf_counter()
|
| 2133 |
segments = []
|
| 2134 |
+
models = active_model_metadata(geometry, depth, shade_map)
|
| 2135 |
|
| 2136 |
if len(surface_indices) >= min_floor_area:
|
| 2137 |
segments.append({
|
|
|
|
| 2151 |
),
|
| 2152 |
"surfacePlanes": surface_mapping["planes"] if surface_mapping is not None else None,
|
| 2153 |
"shadeMap": base64.b64encode(shade_map.tobytes()).decode() if shade_map is not None else None,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2154 |
"metadata": {
|
| 2155 |
"segmenter": segmenter_metadata_name(),
|
| 2156 |
"segmentationModel": models["segmentation"]["name"],
|
| 2157 |
+
"segmentationShortestEdge": SEGMENTATION_SHORTEST_EDGE,
|
| 2158 |
"floorPixels": int(floor_mask.sum()),
|
| 2159 |
"surfacePixels": int(surface_mask.sum()),
|
| 2160 |
**edge_metadata,
|
|
|
|
| 2173 |
if surface_mapping is not None else []
|
| 2174 |
),
|
| 2175 |
"shadingTransferEnabled": ENABLE_SHADING_TRANSFER,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2176 |
"geometryConfidence": plane.get("geometryConfidence", 0.0) if plane else 0.0,
|
| 2177 |
"metricGeometry": bool(plane and plane.get("floorTransform")),
|
| 2178 |
"geometryProvider": plane.get("geometryProvider", geometry.get("provider")) if plane else geometry.get("provider"),
|
|
|
|
| 2193 |
"homography": None,
|
| 2194 |
"plane": None,
|
| 2195 |
"shadeMap": None,
|
|
|
|
| 2196 |
"metadata": {
|
| 2197 |
"segmenter": segmenter_metadata_name(),
|
| 2198 |
"segmentationModel": models["segmentation"]["name"],
|
|
|
|
| 2200 |
"shadingEnabled": False,
|
| 2201 |
"planeFitEnabled": ENABLE_PLANE_FIT,
|
| 2202 |
"shadingTransferEnabled": ENABLE_SHADING_TRANSFER,
|
|
|
|
| 2203 |
},
|
| 2204 |
})
|
| 2205 |
|
visualizer.gpu.toml
CHANGED
|
@@ -9,6 +9,8 @@ moge_model_name = "Ruicheng/moge-2-vitl-normal"
|
|
| 9 |
oneformer_model_name = "shi-labs/oneformer_ade20k_swin_large"
|
| 10 |
mask2former_model_name = "facebook/mask2former-swin-small-ade-semantic"
|
| 11 |
segformer_model_name = "nvidia/segformer-b2-finetuned-ade-512-512"
|
|
|
|
|
|
|
| 12 |
depth_model_name = "depth-anything/Depth-Anything-V2-Metric-Indoor-Large-hf"
|
| 13 |
intrinsic_model_version = "v2"
|
| 14 |
|
|
@@ -30,6 +32,13 @@ min_plane_pixels = 1500
|
|
| 30 |
edge_close_ratio = 0.006
|
| 31 |
edge_kernel_ratio = 0.004
|
| 32 |
edge_dilation_iterations = 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
uv_min_coverage = 0.995
|
| 34 |
uv_max_bad_neighbor_ratio = 0.035
|
| 35 |
uv_max_jump_ratio = 8.0
|
|
|
|
| 9 |
oneformer_model_name = "shi-labs/oneformer_ade20k_swin_large"
|
| 10 |
mask2former_model_name = "facebook/mask2former-swin-small-ade-semantic"
|
| 11 |
segformer_model_name = "nvidia/segformer-b2-finetuned-ade-512-512"
|
| 12 |
+
segmentation_shortest_edge = 1024
|
| 13 |
+
segmentation_longest_edge = 2048
|
| 14 |
depth_model_name = "depth-anything/Depth-Anything-V2-Metric-Indoor-Large-hf"
|
| 15 |
intrinsic_model_version = "v2"
|
| 16 |
|
|
|
|
| 32 |
edge_close_ratio = 0.006
|
| 33 |
edge_kernel_ratio = 0.004
|
| 34 |
edge_dilation_iterations = 1
|
| 35 |
+
occluder_dilation_iterations = 1
|
| 36 |
+
edge_refine_enabled = 1
|
| 37 |
+
edge_refine_fill_ratio = 0.010
|
| 38 |
+
edge_refine_grad_threshold = 0.10
|
| 39 |
+
edge_refine_radius_ratio = 0.006
|
| 40 |
+
edge_refine_eps = 0.0001
|
| 41 |
+
edge_refine_threshold = 0.5
|
| 42 |
uv_min_coverage = 0.995
|
| 43 |
uv_max_bad_neighbor_ratio = 0.035
|
| 44 |
uv_max_jump_ratio = 8.0
|
visualizer.hf.toml
CHANGED
|
@@ -9,6 +9,8 @@ moge_model_name = "Ruicheng/moge-2-vitl-normal"
|
|
| 9 |
oneformer_model_name = "shi-labs/oneformer_ade20k_swin_large"
|
| 10 |
mask2former_model_name = "facebook/mask2former-swin-small-ade-semantic"
|
| 11 |
segformer_model_name = "nvidia/segformer-b2-finetuned-ade-512-512"
|
|
|
|
|
|
|
| 12 |
depth_model_name = "depth-anything/Depth-Anything-V2-Metric-Indoor-Large-hf"
|
| 13 |
intrinsic_model_version = "v2"
|
| 14 |
|
|
@@ -30,7 +32,16 @@ min_plane_pixels = 1500
|
|
| 30 |
edge_close_ratio = 0.006
|
| 31 |
edge_kernel_ratio = 0.004
|
| 32 |
edge_dilation_iterations = 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
uv_min_coverage = 0.995
|
| 34 |
uv_max_bad_neighbor_ratio = 0.035
|
| 35 |
uv_max_jump_ratio = 8.0
|
| 36 |
uv_absolute_jump_meters = 0.35
|
|
|
|
|
|
|
|
|
| 9 |
oneformer_model_name = "shi-labs/oneformer_ade20k_swin_large"
|
| 10 |
mask2former_model_name = "facebook/mask2former-swin-small-ade-semantic"
|
| 11 |
segformer_model_name = "nvidia/segformer-b2-finetuned-ade-512-512"
|
| 12 |
+
segmentation_shortest_edge = 1024
|
| 13 |
+
segmentation_longest_edge = 2048
|
| 14 |
depth_model_name = "depth-anything/Depth-Anything-V2-Metric-Indoor-Large-hf"
|
| 15 |
intrinsic_model_version = "v2"
|
| 16 |
|
|
|
|
| 32 |
edge_close_ratio = 0.006
|
| 33 |
edge_kernel_ratio = 0.004
|
| 34 |
edge_dilation_iterations = 1
|
| 35 |
+
occluder_dilation_iterations = 1
|
| 36 |
+
edge_refine_enabled = 1
|
| 37 |
+
edge_refine_fill_ratio = 0.010
|
| 38 |
+
edge_refine_grad_threshold = 0.10
|
| 39 |
+
edge_refine_radius_ratio = 0.006
|
| 40 |
+
edge_refine_eps = 0.0001
|
| 41 |
+
edge_refine_threshold = 0.5
|
| 42 |
uv_min_coverage = 0.995
|
| 43 |
uv_max_bad_neighbor_ratio = 0.035
|
| 44 |
uv_max_jump_ratio = 8.0
|
| 45 |
uv_absolute_jump_meters = 0.35
|
| 46 |
+
|
| 47 |
+
# redeploy trigger: SAM reverted, restore 1024 baseline
|
visualizer.local.toml
CHANGED
|
@@ -24,6 +24,13 @@ min_plane_pixels = 1500
|
|
| 24 |
edge_close_ratio = 0.006
|
| 25 |
edge_kernel_ratio = 0.004
|
| 26 |
edge_dilation_iterations = 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
uv_min_coverage = 0.995
|
| 28 |
uv_max_bad_neighbor_ratio = 0.035
|
| 29 |
uv_max_jump_ratio = 8.0
|
|
|
|
| 24 |
edge_close_ratio = 0.006
|
| 25 |
edge_kernel_ratio = 0.004
|
| 26 |
edge_dilation_iterations = 1
|
| 27 |
+
occluder_dilation_iterations = 1
|
| 28 |
+
edge_refine_enabled = 1
|
| 29 |
+
edge_refine_fill_ratio = 0.010
|
| 30 |
+
edge_refine_grad_threshold = 0.10
|
| 31 |
+
edge_refine_radius_ratio = 0.006
|
| 32 |
+
edge_refine_eps = 0.0001
|
| 33 |
+
edge_refine_threshold = 0.5
|
| 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
|
@@ -25,6 +25,13 @@ min_plane_pixels = 1500
|
|
| 25 |
edge_close_ratio = 0.006
|
| 26 |
edge_kernel_ratio = 0.004
|
| 27 |
edge_dilation_iterations = 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
uv_min_coverage = 0.995
|
| 29 |
uv_max_bad_neighbor_ratio = 0.035
|
| 30 |
uv_max_jump_ratio = 8.0
|
|
|
|
| 25 |
edge_close_ratio = 0.006
|
| 26 |
edge_kernel_ratio = 0.004
|
| 27 |
edge_dilation_iterations = 1
|
| 28 |
+
occluder_dilation_iterations = 1
|
| 29 |
+
edge_refine_enabled = 1
|
| 30 |
+
edge_refine_fill_ratio = 0.010
|
| 31 |
+
edge_refine_grad_threshold = 0.10
|
| 32 |
+
edge_refine_radius_ratio = 0.006
|
| 33 |
+
edge_refine_eps = 0.0001
|
| 34 |
+
edge_refine_threshold = 0.5
|
| 35 |
uv_min_coverage = 0.995
|
| 36 |
uv_max_bad_neighbor_ratio = 0.035
|
| 37 |
uv_max_jump_ratio = 8.0
|