Spaces:
Paused
Paused
File size: 9,738 Bytes
1fe7075 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | """Segment dict construction for the visualization bundle."""
from __future__ import annotations
import numpy as np
from config.lighting import (
LIGHTING_TRANSFER_VERSION,
REFLECTION_MAP_MAX,
REFLECTION_MAP_MIN,
SHADE_MAP_ENCODING,
SHADE_MAP_MAX,
SHADE_MAP_MIN,
SHOWROOM_SHADE_MAP_MAX,
SHOWROOM_SHADE_MAP_MIN,
)
from config.runtime import (
ENABLE_FAST_SHADOW_TRANSFER,
ENABLE_MULTI_PLANE_SURFACES,
ENABLE_PLANE_FIT,
ENABLE_SAM_REFINEMENT,
ENABLE_SHADING_TRANSFER,
)
from models.segmentation import segmenter_metadata_name
from state import class_ids, class_name_for_id
from enums import SurfaceType, TextureMappingMode
from utils import encode_b64
def build_floor_segment(
*,
surface_indices: np.ndarray,
occlusion_indices: np.ndarray,
edge_alpha: np.ndarray | None = None,
homography,
plane: dict | None,
surface_mapping: dict | None,
use_surface_uv: bool,
shade_map: np.ndarray | None,
showroom_shade_map: np.ndarray | None,
reflection_map: np.ndarray | None,
floor_mask: np.ndarray,
surface_mask: np.ndarray,
replaced_material_mask: np.ndarray,
occlusion_mask: np.ndarray,
floor_direction: dict,
sam_metadata: dict,
edge_metadata: dict,
mapping_metadata: dict,
shade_statistics: dict | None,
lighting_source: str | None,
intrinsic_lighting_used: bool,
models: dict,
depth,
geometry: dict,
) -> dict:
return {
"id": 0,
"className": SurfaceType.FLOOR,
"mask": encode_b64(surface_indices),
"occlusionMask": encode_b64(occlusion_indices) if len(occlusion_indices) else None,
"homography": homography,
"floorTransform": plane.get("floorTransform") if plane else None,
"plane": plane,
# Pass raw numpy arrays for lazy assets — packaging writes them directly
# to disk, avoiding the costly base64 encode/decode round-trip.
"surfaceUv": (
surface_mapping["uv"].astype(np.float32)
if use_surface_uv else None
),
"surfacePlaneIds": (
surface_mapping["planeIds"].astype(np.uint8)
if use_surface_uv else None
),
"surfacePlanes": surface_mapping["planes"] if surface_mapping is not None else None,
"shadeMap": shade_map,
"showroomShadeMap": showroom_shade_map,
"reflectionMap": reflection_map,
# Anti-aliased coverage, one byte per `mask` pixel in the same order.
"edgeAlpha": edge_alpha.astype(np.uint8) if edge_alpha is not None else None,
"metadata": {
"segmenter": segmenter_metadata_name(),
"edgeAlphaEncoding": "per-mask-index-u8" if edge_alpha is not None else None,
"segmentationModel": models["segmentation"]["name"],
"floorPixels": int(floor_mask.sum()),
"surfacePixels": int(surface_mask.sum()),
"replacedSoftFloorPixels": int(replaced_material_mask.sum()),
"occlusionPixels": int(occlusion_mask.sum()),
"floorDirection": floor_direction,
**sam_metadata,
**edge_metadata,
**mapping_metadata,
"depthEnabled": depth is not None,
"shadingEnabled": shade_map is not None,
"planeFitEnabled": ENABLE_PLANE_FIT,
"multiPlaneSurfacesEnabled": ENABLE_MULTI_PLANE_SURFACES,
"multiPlaneSurfaceCount": len(surface_mapping["planes"]) if surface_mapping is not None else 0,
"multiPlaneNormalsReliable": (
bool(surface_mapping.get("normalsReliable"))
if surface_mapping is not None else False
),
"multiPlaneFitModes": (
[p.get("fitMode") for p in surface_mapping["planes"]]
if surface_mapping is not None else []
),
"shadingTransferEnabled": ENABLE_SHADING_TRANSFER or ENABLE_FAST_SHADOW_TRANSFER,
"fastShadowTransferEnabled": ENABLE_FAST_SHADOW_TRANSFER,
"lightingSource": lighting_source,
"lightingTransferVersion": LIGHTING_TRANSFER_VERSION,
"shadeMapEncoding": SHADE_MAP_ENCODING,
"shadeMapStatistics": shade_statistics,
"shadeMapRange": [SHADE_MAP_MIN, SHADE_MAP_MAX],
"shadeMapSize": (
[int(shade_map.shape[1]), int(shade_map.shape[0])]
if shade_map is not None else None
),
"showroomShadeMapRange": [SHOWROOM_SHADE_MAP_MIN, SHOWROOM_SHADE_MAP_MAX],
"renderPresetDefaults": {
"preset": "realistic",
"shadingStrength": 0.30,
"reflectionTransfer": True,
"edgeFeatherPixels": 2,
},
"reflectionTransferEnabled": reflection_map is not None,
"reflectionMapRange": [REFLECTION_MAP_MIN, REFLECTION_MAP_MAX],
"geometryConfidence": plane.get("geometryConfidence", 0.0) if plane else 0.0,
"metricGeometry": bool(plane and plane.get("floorTransform")),
"geometryProvider": (
plane.get("geometryProvider", geometry.get("provider"))
if plane else geometry.get("provider")
),
"geometryModel": models["geometry"]["name"],
},
}
def build_wall_segment(
*,
wall_id: int,
wall_index: int,
wall_indices: np.ndarray,
homography,
plane: dict,
lighting_maps: dict | None,
segmentation_model: str,
edge_alpha: np.ndarray | None = None,
) -> dict:
"""Bundle dict for one wall. All heavy inputs are prepared by services.wall.pipeline,
which also sets metadata["wallComponentCount"] once every wall is built."""
shade_map = lighting_maps.get("shadeMap") if lighting_maps else None
showroom_shade_map = lighting_maps.get("showroomShadeMap") if lighting_maps else None
reflection_map = lighting_maps.get("reflectionMap") if lighting_maps else None
mapping_mode = (
TextureMappingMode.WALL_PLANE if plane.get("surfaceTransform")
else TextureMappingMode.WALL_CONTOUR_HOMOGRAPHY
)
return {
"id": wall_id,
"className": SurfaceType.WALL,
"surfaceType": SurfaceType.WALL,
"mask": encode_b64(wall_indices),
# Wall masks already exclude (dilated) doors, windows and furniture, so
# a separate occlusion mask would only duplicate that cut-out.
"occlusionMask": None,
"homography": homography,
"floorTransform": plane.get("floorTransform"),
"surfaceTransform": plane.get("surfaceTransform"),
"plane": plane,
# A wall is one plane: its exact pixel -> metres mapping is the 3x3
# surfaceTransform, so no per-pixel UV arrays are shipped.
"surfaceUv": None,
"surfacePlaneIds": None,
"surfacePlanes": None,
"shadeMap": shade_map,
"showroomShadeMap": showroom_shade_map,
"reflectionMap": reflection_map,
# Coverage per `mask` pixel where the wall shows between the stems of decor (edgeAlpha
# of the floor's format); None keeps the browser's own edge feather.
"edgeAlpha": edge_alpha.astype(np.uint8) if edge_alpha is not None else None,
"metadata": {
"segmenter": segmenter_metadata_name(),
"edgeAlphaEncoding": "per-mask-index-u8" if edge_alpha is not None else None,
"segmentationModel": segmentation_model,
"wallPixels": int(len(wall_indices)),
"wallComponent": wall_index,
"shadingEnabled": shade_map is not None,
"reflectionTransferEnabled": reflection_map is not None,
"textureMappingMode": mapping_mode,
"geometryConfidence": plane.get("geometryConfidence", 0.0),
"geometryProvider": plane.get("geometryProvider"),
"surfaceWidthMeters": plane.get("surfaceWidthMeters"),
"surfaceHeightMeters": plane.get("surfaceHeightMeters"),
"lightingTransferVersion": LIGHTING_TRANSFER_VERSION,
"shadeMapEncoding": SHADE_MAP_ENCODING,
"shadeMapRange": [SHADE_MAP_MIN, SHADE_MAP_MAX],
"shadeMapSize": (
[int(shade_map.shape[1]), int(shade_map.shape[0])]
if shade_map is not None else None
),
"showroomShadeMapRange": [SHOWROOM_SHADE_MAP_MIN, SHOWROOM_SHADE_MAP_MAX],
"reflectionMapRange": [REFLECTION_MAP_MIN, REFLECTION_MAP_MAX],
},
}
def build_fallback_segments(
seg_map: np.ndarray,
depth,
models: dict,
) -> list[dict]:
segments = []
flat_seg = seg_map.ravel()
for seg_id, class_id in enumerate(np.unique(flat_seg)):
indices = np.where(flat_seg == class_id)[0].astype(np.uint32)
if len(indices) < 1000:
continue
segments.append({
"id": int(seg_id),
"className": class_name_for_id(int(class_id)),
"mask": encode_b64(indices),
"homography": None,
"plane": None,
"shadeMap": None,
"showroomShadeMap": None,
"reflectionMap": None,
"metadata": {
"segmenter": segmenter_metadata_name(),
"segmentationModel": models["segmentation"]["name"],
"depthEnabled": depth is not None,
"shadingEnabled": False,
"planeFitEnabled": ENABLE_PLANE_FIT,
"samRefinementEnabled": ENABLE_SAM_REFINEMENT,
"shadingTransferEnabled": ENABLE_SHADING_TRANSFER,
"reflectionTransferEnabled": False,
},
})
return segments
|