github-actions[bot] commited on
Commit ·
a7c4f3e
1
Parent(s): 434eb27
Automated Deploy: 7e8765bbc55caea37e0360a074b9a0debc3e8b86
Browse files- app.py +0 -0
- direction_finder.py +1009 -0
- test_floor_geometry.py +173 -0
app.py
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|
direction_finder.py
ADDED
|
@@ -0,0 +1,1009 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Multi-cue floor direction estimation in renderer coordinates."""
|
| 2 |
+
|
| 3 |
+
from dataclasses import asdict, dataclass
|
| 4 |
+
import math
|
| 5 |
+
|
| 6 |
+
import cv2
|
| 7 |
+
import numpy as np
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
@dataclass(frozen=True)
|
| 11 |
+
class DirectionCue:
|
| 12 |
+
source: str
|
| 13 |
+
angle_degrees: float
|
| 14 |
+
confidence: float
|
| 15 |
+
support: int
|
| 16 |
+
region_support: int
|
| 17 |
+
ambiguity_degrees: float | None
|
| 18 |
+
details: dict
|
| 19 |
+
|
| 20 |
+
def metadata(self) -> dict:
|
| 21 |
+
payload = asdict(self)
|
| 22 |
+
payload["angleDegrees"] = payload.pop("angle_degrees")
|
| 23 |
+
payload["regionSupport"] = payload.pop("region_support")
|
| 24 |
+
payload["ambiguityDegrees"] = payload.pop("ambiguity_degrees")
|
| 25 |
+
return payload
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def normalize_angle(angle_degrees: float, period: float = 180.0) -> float:
|
| 29 |
+
return float(angle_degrees % period)
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def angular_distance(first: float, second: float, period: float = 180.0) -> float:
|
| 33 |
+
difference = abs((first - second) % period)
|
| 34 |
+
return float(min(difference, period - difference))
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def periodic_mean(
|
| 38 |
+
angles_degrees: np.ndarray,
|
| 39 |
+
weights: np.ndarray,
|
| 40 |
+
*,
|
| 41 |
+
period: float,
|
| 42 |
+
) -> tuple[float, float]:
|
| 43 |
+
if len(angles_degrees) == 0:
|
| 44 |
+
return 0.0, 0.0
|
| 45 |
+
weights = np.maximum(np.asarray(weights, dtype=np.float64), 0.0)
|
| 46 |
+
total_weight = float(weights.sum())
|
| 47 |
+
if total_weight <= 1e-9:
|
| 48 |
+
return 0.0, 0.0
|
| 49 |
+
phase = np.asarray(angles_degrees, dtype=np.float64) * (2.0 * np.pi / period)
|
| 50 |
+
vector_x = float(np.sum(np.cos(phase) * weights))
|
| 51 |
+
vector_y = float(np.sum(np.sin(phase) * weights))
|
| 52 |
+
angle = math.atan2(vector_y, vector_x) * (period / (2.0 * np.pi))
|
| 53 |
+
concentration = math.hypot(vector_x, vector_y) / total_weight
|
| 54 |
+
return normalize_angle(angle, period), float(np.clip(concentration, 0.0, 1.0))
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def build_surface_uv_grids(
|
| 58 |
+
*,
|
| 59 |
+
image_shape: tuple[int, int],
|
| 60 |
+
surface_uv: np.ndarray | None,
|
| 61 |
+
surface_indices: np.ndarray | None,
|
| 62 |
+
surface_plane_ids: np.ndarray | None,
|
| 63 |
+
) -> tuple[np.ndarray | None, np.ndarray | None]:
|
| 64 |
+
height, width = image_shape
|
| 65 |
+
if surface_uv is None or surface_indices is None:
|
| 66 |
+
return None, None
|
| 67 |
+
if surface_uv.ndim != 2 or surface_uv.shape[1] != 2:
|
| 68 |
+
return None, None
|
| 69 |
+
if len(surface_uv) != len(surface_indices):
|
| 70 |
+
return None, None
|
| 71 |
+
|
| 72 |
+
flat_size = height * width
|
| 73 |
+
indices = np.asarray(surface_indices, dtype=np.int64)
|
| 74 |
+
valid_indices = (indices >= 0) & (indices < flat_size)
|
| 75 |
+
uv_grid = np.full((flat_size, 2), np.nan, dtype=np.float32)
|
| 76 |
+
uv_grid[indices[valid_indices]] = surface_uv[valid_indices].astype(np.float32)
|
| 77 |
+
|
| 78 |
+
plane_grid = None
|
| 79 |
+
if surface_plane_ids is not None and len(surface_plane_ids) == len(indices):
|
| 80 |
+
plane_grid = np.full(flat_size, 255, dtype=np.uint8)
|
| 81 |
+
plane_grid[indices[valid_indices]] = surface_plane_ids[valid_indices].astype(np.uint8)
|
| 82 |
+
|
| 83 |
+
return (
|
| 84 |
+
uv_grid.reshape(height, width, 2),
|
| 85 |
+
plane_grid.reshape(height, width) if plane_grid is not None else None,
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
def rectify_floor_image(
|
| 90 |
+
image_rgb: np.ndarray,
|
| 91 |
+
surface_mask: np.ndarray,
|
| 92 |
+
*,
|
| 93 |
+
render_transform: list[float] | np.ndarray | None,
|
| 94 |
+
surface_uv: np.ndarray | None,
|
| 95 |
+
surface_indices: np.ndarray | None,
|
| 96 |
+
surface_plane_ids: np.ndarray | None,
|
| 97 |
+
) -> tuple[np.ndarray, np.ndarray, dict] | None:
|
| 98 |
+
height, width = surface_mask.shape[:2]
|
| 99 |
+
uv_grid, plane_grid = build_surface_uv_grids(
|
| 100 |
+
image_shape=(height, width),
|
| 101 |
+
surface_uv=surface_uv,
|
| 102 |
+
surface_indices=surface_indices,
|
| 103 |
+
surface_plane_ids=surface_plane_ids,
|
| 104 |
+
)
|
| 105 |
+
if uv_grid is not None:
|
| 106 |
+
result = rectify_from_uv(image_rgb, uv_grid, plane_grid)
|
| 107 |
+
if result is not None:
|
| 108 |
+
gray, mask = result
|
| 109 |
+
return gray, mask, {"source": "surface-uv", "width": gray.shape[1], "height": gray.shape[0]}
|
| 110 |
+
|
| 111 |
+
transform = normalize_transform(render_transform)
|
| 112 |
+
if transform is None:
|
| 113 |
+
return None
|
| 114 |
+
result = rectify_from_transform(image_rgb, surface_mask, transform)
|
| 115 |
+
if result is None:
|
| 116 |
+
return None
|
| 117 |
+
gray, mask = result
|
| 118 |
+
return gray, mask, {"source": "floor-transform", "width": gray.shape[1], "height": gray.shape[0]}
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
def rectify_from_uv(
|
| 122 |
+
image_rgb: np.ndarray,
|
| 123 |
+
uv_grid: np.ndarray,
|
| 124 |
+
plane_grid: np.ndarray | None,
|
| 125 |
+
) -> tuple[np.ndarray, np.ndarray] | None:
|
| 126 |
+
valid = np.isfinite(uv_grid).all(axis=2)
|
| 127 |
+
if plane_grid is not None:
|
| 128 |
+
plane_values = plane_grid[valid & (plane_grid != 255)]
|
| 129 |
+
if plane_values.size:
|
| 130 |
+
dominant_plane = int(np.bincount(plane_values).argmax())
|
| 131 |
+
valid &= plane_grid == dominant_plane
|
| 132 |
+
if int(valid.sum()) < 800:
|
| 133 |
+
return None
|
| 134 |
+
|
| 135 |
+
uv = uv_grid[valid].astype(np.float64)
|
| 136 |
+
lower = np.percentile(uv, 0.5, axis=0)
|
| 137 |
+
upper = np.percentile(uv, 99.5, axis=0)
|
| 138 |
+
span = upper - lower
|
| 139 |
+
if not np.isfinite(span).all() or float(np.min(span)) <= 1e-5:
|
| 140 |
+
return None
|
| 141 |
+
|
| 142 |
+
scale = choose_rectification_scale(span, int(valid.sum()))
|
| 143 |
+
output_width = int(np.clip(round(float(span[0]) * scale) + 1, 96, 896))
|
| 144 |
+
output_height = int(np.clip(round(float(span[1]) * scale) + 1, 96, 896))
|
| 145 |
+
if output_width < 96 or output_height < 96:
|
| 146 |
+
return None
|
| 147 |
+
|
| 148 |
+
gray = cv2.cvtColor(image_rgb, cv2.COLOR_RGB2GRAY)
|
| 149 |
+
values = gray[valid].astype(np.float64)
|
| 150 |
+
xy = np.rint((uv - lower) * scale).astype(np.int32)
|
| 151 |
+
xs = np.clip(xy[:, 0], 0, output_width - 1)
|
| 152 |
+
ys = np.clip(xy[:, 1], 0, output_height - 1)
|
| 153 |
+
flat_indices = ys * output_width + xs
|
| 154 |
+
pixel_count = output_width * output_height
|
| 155 |
+
counts = np.bincount(flat_indices, minlength=pixel_count)
|
| 156 |
+
sums = np.bincount(flat_indices, weights=values, minlength=pixel_count)
|
| 157 |
+
covered = counts > 0
|
| 158 |
+
if int(covered.sum()) < max(600, int(pixel_count * 0.1)):
|
| 159 |
+
return None
|
| 160 |
+
|
| 161 |
+
fill_value = int(np.median(values))
|
| 162 |
+
output = np.full(pixel_count, fill_value, dtype=np.uint8)
|
| 163 |
+
output[covered] = np.clip(sums[covered] / counts[covered], 0, 255).astype(np.uint8)
|
| 164 |
+
mask = (covered.reshape(output_height, output_width).astype(np.uint8) * 255)
|
| 165 |
+
return output.reshape(output_height, output_width), mask
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
def rectify_from_transform(
|
| 169 |
+
image_rgb: np.ndarray,
|
| 170 |
+
surface_mask: np.ndarray,
|
| 171 |
+
transform: np.ndarray,
|
| 172 |
+
) -> tuple[np.ndarray, np.ndarray] | None:
|
| 173 |
+
ys, xs = np.where(surface_mask > 0)
|
| 174 |
+
if len(xs) < 800:
|
| 175 |
+
return None
|
| 176 |
+
stride = max(1, len(xs) // 30000)
|
| 177 |
+
source_points = np.column_stack((xs[::stride], ys[::stride])).astype(np.float32).reshape(1, -1, 2)
|
| 178 |
+
mapped = cv2.perspectiveTransform(source_points, transform)[0]
|
| 179 |
+
mapped = mapped[np.isfinite(mapped).all(axis=1)]
|
| 180 |
+
if len(mapped) < 500:
|
| 181 |
+
return None
|
| 182 |
+
|
| 183 |
+
lower = np.percentile(mapped, 0.5, axis=0)
|
| 184 |
+
upper = np.percentile(mapped, 99.5, axis=0)
|
| 185 |
+
span = upper - lower
|
| 186 |
+
if not np.isfinite(span).all() or float(np.min(span)) <= 1e-5:
|
| 187 |
+
return None
|
| 188 |
+
|
| 189 |
+
scale = choose_rectification_scale(span, len(mapped))
|
| 190 |
+
output_width = int(np.clip(round(float(span[0]) * scale) + 1, 96, 896))
|
| 191 |
+
output_height = int(np.clip(round(float(span[1]) * scale) + 1, 96, 896))
|
| 192 |
+
if output_width < 96 or output_height < 96:
|
| 193 |
+
return None
|
| 194 |
+
|
| 195 |
+
normalization = np.array([
|
| 196 |
+
[scale, 0.0, -float(lower[0]) * scale],
|
| 197 |
+
[0.0, scale, -float(lower[1]) * scale],
|
| 198 |
+
[0.0, 0.0, 1.0],
|
| 199 |
+
], dtype=np.float64)
|
| 200 |
+
warp = normalization @ transform
|
| 201 |
+
gray = cv2.cvtColor(image_rgb, cv2.COLOR_RGB2GRAY)
|
| 202 |
+
rectified = cv2.warpPerspective(
|
| 203 |
+
gray,
|
| 204 |
+
warp,
|
| 205 |
+
(output_width, output_height),
|
| 206 |
+
flags=cv2.INTER_LINEAR,
|
| 207 |
+
borderMode=cv2.BORDER_REPLICATE,
|
| 208 |
+
)
|
| 209 |
+
rectified_mask = cv2.warpPerspective(
|
| 210 |
+
(surface_mask > 0).astype(np.uint8) * 255,
|
| 211 |
+
warp,
|
| 212 |
+
(output_width, output_height),
|
| 213 |
+
flags=cv2.INTER_NEAREST,
|
| 214 |
+
)
|
| 215 |
+
return rectified, rectified_mask
|
| 216 |
+
|
| 217 |
+
|
| 218 |
+
def choose_rectification_scale(span: np.ndarray, sample_count: int) -> float:
|
| 219 |
+
aspect = float(span[0] / span[1])
|
| 220 |
+
target_long_side = int(np.clip(
|
| 221 |
+
round(math.sqrt(sample_count * max(aspect, 1.0 / aspect))),
|
| 222 |
+
320,
|
| 223 |
+
896,
|
| 224 |
+
))
|
| 225 |
+
return target_long_side / max(float(np.max(span)), 1e-6)
|
| 226 |
+
|
| 227 |
+
|
| 228 |
+
def estimate_material_cue(gray: np.ndarray, mask: np.ndarray) -> DirectionCue | None:
|
| 229 |
+
valid = mask > 0
|
| 230 |
+
if int(valid.sum()) < 1000:
|
| 231 |
+
return None
|
| 232 |
+
|
| 233 |
+
kernel_size = max(3, int(round(min(gray.shape) * 0.012))) | 1
|
| 234 |
+
inner_mask = cv2.erode(mask, np.ones((kernel_size, kernel_size), dtype=np.uint8))
|
| 235 |
+
if int(np.count_nonzero(inner_mask)) < 800:
|
| 236 |
+
inner_mask = mask
|
| 237 |
+
valid = inner_mask > 0
|
| 238 |
+
|
| 239 |
+
fill_value = int(np.median(gray[valid]))
|
| 240 |
+
prepared = gray.copy()
|
| 241 |
+
prepared[~valid] = fill_value
|
| 242 |
+
prepared = cv2.createCLAHE(clipLimit=1.8, tileGridSize=(8, 8)).apply(prepared)
|
| 243 |
+
sigma = max(3.0, min(gray.shape) / 36.0)
|
| 244 |
+
illumination = cv2.GaussianBlur(prepared, (0, 0), sigmaX=sigma)
|
| 245 |
+
detail = prepared.astype(np.float32) - illumination.astype(np.float32)
|
| 246 |
+
gx = cv2.Sobel(detail, cv2.CV_32F, 1, 0, ksize=3)
|
| 247 |
+
gy = cv2.Sobel(detail, cv2.CV_32F, 0, 1, ksize=3)
|
| 248 |
+
magnitude = np.hypot(gx, gy)
|
| 249 |
+
gradient_values = magnitude[valid]
|
| 250 |
+
if gradient_values.size < 800 or float(np.percentile(gradient_values, 90)) < 2.0:
|
| 251 |
+
return None
|
| 252 |
+
threshold = float(np.percentile(gradient_values, 68))
|
| 253 |
+
|
| 254 |
+
block_angles = []
|
| 255 |
+
block_weights = []
|
| 256 |
+
block_regions = []
|
| 257 |
+
rows, columns = 4, 4
|
| 258 |
+
height, width = gray.shape[:2]
|
| 259 |
+
for row in range(rows):
|
| 260 |
+
y0, y1 = row * height // rows, (row + 1) * height // rows
|
| 261 |
+
for column in range(columns):
|
| 262 |
+
x0, x1 = column * width // columns, (column + 1) * width // columns
|
| 263 |
+
block_valid = valid[y0:y1, x0:x1]
|
| 264 |
+
selected = block_valid & (magnitude[y0:y1, x0:x1] >= threshold)
|
| 265 |
+
if int(selected.sum()) < 45:
|
| 266 |
+
continue
|
| 267 |
+
angles = (
|
| 268 |
+
np.degrees(np.arctan2(
|
| 269 |
+
gy[y0:y1, x0:x1][selected],
|
| 270 |
+
gx[y0:y1, x0:x1][selected],
|
| 271 |
+
))
|
| 272 |
+
+ 90.0
|
| 273 |
+
) % 180.0
|
| 274 |
+
weights = magnitude[y0:y1, x0:x1][selected].astype(np.float64)
|
| 275 |
+
angle, concentration = periodic_mean(angles, weights, period=180.0)
|
| 276 |
+
if concentration < 0.22:
|
| 277 |
+
continue
|
| 278 |
+
block_angles.append(angle)
|
| 279 |
+
block_weights.append(float(weights.sum()) * concentration)
|
| 280 |
+
block_regions.append(row * columns + column)
|
| 281 |
+
|
| 282 |
+
tensor_cue = cue_from_region_angles(
|
| 283 |
+
source="rectified-structure-tensor",
|
| 284 |
+
angles=block_angles,
|
| 285 |
+
weights=block_weights,
|
| 286 |
+
regions=block_regions,
|
| 287 |
+
period=180.0,
|
| 288 |
+
minimum_regions=3,
|
| 289 |
+
)
|
| 290 |
+
line_cue = estimate_rectified_line_cue(gray, mask)
|
| 291 |
+
if line_cue is None:
|
| 292 |
+
line_cue = estimate_rectified_line_cue(prepared, mask)
|
| 293 |
+
return fuse_material_cues(tensor_cue, line_cue)
|
| 294 |
+
|
| 295 |
+
|
| 296 |
+
def estimate_rectified_line_cue(gray: np.ndarray, mask: np.ndarray) -> DirectionCue | None:
|
| 297 |
+
detector = cv2.createLineSegmentDetector(cv2.LSD_REFINE_STD)
|
| 298 |
+
detected = detector.detect(gray)[0]
|
| 299 |
+
if detected is None:
|
| 300 |
+
return None
|
| 301 |
+
|
| 302 |
+
height, width = gray.shape[:2]
|
| 303 |
+
min_length = max(18.0, min(height, width) * 0.055)
|
| 304 |
+
angles = []
|
| 305 |
+
weights = []
|
| 306 |
+
regions = []
|
| 307 |
+
for raw_line in detected.reshape(-1, 4):
|
| 308 |
+
x1, y1, x2, y2 = [float(value) for value in raw_line]
|
| 309 |
+
dx, dy = x2 - x1, y2 - y1
|
| 310 |
+
length = math.hypot(dx, dy)
|
| 311 |
+
if length < min_length:
|
| 312 |
+
continue
|
| 313 |
+
sample_x = np.clip(np.rint(np.linspace(x1, x2, 9)).astype(np.int32), 0, width - 1)
|
| 314 |
+
sample_y = np.clip(np.rint(np.linspace(y1, y2, 9)).astype(np.int32), 0, height - 1)
|
| 315 |
+
mask_support = float(np.mean(mask[sample_y, sample_x] > 0))
|
| 316 |
+
if mask_support < 0.78:
|
| 317 |
+
continue
|
| 318 |
+
midpoint_x = (x1 + x2) * 0.5
|
| 319 |
+
midpoint_y = (y1 + y2) * 0.5
|
| 320 |
+
angles.append(normalize_angle(math.degrees(math.atan2(dy, dx))))
|
| 321 |
+
weights.append(length * mask_support)
|
| 322 |
+
regions.append(
|
| 323 |
+
min(3, int(midpoint_y * 4 / max(height, 1))) * 4
|
| 324 |
+
+ min(3, int(midpoint_x * 4 / max(width, 1)))
|
| 325 |
+
)
|
| 326 |
+
|
| 327 |
+
return cue_from_region_angles(
|
| 328 |
+
source="rectified-repeated-lines",
|
| 329 |
+
angles=angles,
|
| 330 |
+
weights=weights,
|
| 331 |
+
regions=regions,
|
| 332 |
+
period=180.0,
|
| 333 |
+
minimum_regions=3,
|
| 334 |
+
)
|
| 335 |
+
|
| 336 |
+
|
| 337 |
+
def cue_from_region_angles(
|
| 338 |
+
*,
|
| 339 |
+
source: str,
|
| 340 |
+
angles: list[float],
|
| 341 |
+
weights: list[float],
|
| 342 |
+
regions: list[int],
|
| 343 |
+
period: float,
|
| 344 |
+
minimum_regions: int,
|
| 345 |
+
) -> DirectionCue | None:
|
| 346 |
+
if len(angles) < 4:
|
| 347 |
+
return None
|
| 348 |
+
angle_array = np.asarray(angles, dtype=np.float64)
|
| 349 |
+
weight_array = np.asarray(weights, dtype=np.float64)
|
| 350 |
+
angle, concentration = periodic_mean(angle_array, weight_array, period=period)
|
| 351 |
+
tolerance = 12.0 if period == 180.0 else 9.0
|
| 352 |
+
agreement = np.array(
|
| 353 |
+
[angular_distance(float(value), angle, period) <= tolerance for value in angle_array],
|
| 354 |
+
dtype=bool,
|
| 355 |
+
)
|
| 356 |
+
if int(agreement.sum()) < 4:
|
| 357 |
+
return None
|
| 358 |
+
agreeing_regions = {region for region, keep in zip(regions, agreement, strict=False) if keep}
|
| 359 |
+
if len(agreeing_regions) < minimum_regions:
|
| 360 |
+
return None
|
| 361 |
+
|
| 362 |
+
agreement_ratio = float(weight_array[agreement].sum() / max(float(weight_array.sum()), 1e-6))
|
| 363 |
+
region_score = min(1.0, len(agreeing_regions) / 7.0)
|
| 364 |
+
support_score = min(1.0, int(agreement.sum()) / 12.0)
|
| 365 |
+
confidence = float(np.clip(
|
| 366 |
+
0.38 * concentration
|
| 367 |
+
+ 0.28 * agreement_ratio
|
| 368 |
+
+ 0.20 * region_score
|
| 369 |
+
+ 0.14 * support_score,
|
| 370 |
+
0.0,
|
| 371 |
+
1.0,
|
| 372 |
+
))
|
| 373 |
+
if confidence < 0.48:
|
| 374 |
+
return None
|
| 375 |
+
return DirectionCue(
|
| 376 |
+
source=source,
|
| 377 |
+
angle_degrees=round(angle, 3),
|
| 378 |
+
confidence=round(confidence, 3),
|
| 379 |
+
support=int(agreement.sum()),
|
| 380 |
+
region_support=len(agreeing_regions),
|
| 381 |
+
ambiguity_degrees=None,
|
| 382 |
+
details={
|
| 383 |
+
"concentration": round(concentration, 4),
|
| 384 |
+
"agreementRatio": round(agreement_ratio, 4),
|
| 385 |
+
},
|
| 386 |
+
)
|
| 387 |
+
|
| 388 |
+
|
| 389 |
+
def fuse_material_cues(
|
| 390 |
+
tensor_cue: DirectionCue | None,
|
| 391 |
+
line_cue: DirectionCue | None,
|
| 392 |
+
) -> DirectionCue | None:
|
| 393 |
+
if tensor_cue is None:
|
| 394 |
+
return line_cue
|
| 395 |
+
if line_cue is None:
|
| 396 |
+
return tensor_cue
|
| 397 |
+
difference = angular_distance(tensor_cue.angle_degrees, line_cue.angle_degrees)
|
| 398 |
+
if difference > 15.0:
|
| 399 |
+
stronger = max((tensor_cue, line_cue), key=lambda cue: cue.confidence)
|
| 400 |
+
return DirectionCue(
|
| 401 |
+
source=stronger.source,
|
| 402 |
+
angle_degrees=stronger.angle_degrees,
|
| 403 |
+
confidence=round(stronger.confidence * 0.72, 3),
|
| 404 |
+
support=stronger.support,
|
| 405 |
+
region_support=stronger.region_support,
|
| 406 |
+
ambiguity_degrees=None,
|
| 407 |
+
details={**stronger.details, "cueDisagreementDegrees": round(difference, 3)},
|
| 408 |
+
)
|
| 409 |
+
|
| 410 |
+
angles = np.array([tensor_cue.angle_degrees, line_cue.angle_degrees])
|
| 411 |
+
weights = np.array([tensor_cue.confidence, line_cue.confidence])
|
| 412 |
+
angle, concentration = periodic_mean(angles, weights, period=180.0)
|
| 413 |
+
confidence = float(np.clip(
|
| 414 |
+
max(tensor_cue.confidence, line_cue.confidence) * 0.72
|
| 415 |
+
+ min(tensor_cue.confidence, line_cue.confidence) * 0.28
|
| 416 |
+
+ concentration * 0.08,
|
| 417 |
+
0.0,
|
| 418 |
+
1.0,
|
| 419 |
+
))
|
| 420 |
+
return DirectionCue(
|
| 421 |
+
source="rectified-material-consensus",
|
| 422 |
+
angle_degrees=round(angle, 3),
|
| 423 |
+
confidence=round(confidence, 3),
|
| 424 |
+
support=tensor_cue.support + line_cue.support,
|
| 425 |
+
region_support=max(tensor_cue.region_support, line_cue.region_support),
|
| 426 |
+
ambiguity_degrees=None,
|
| 427 |
+
details={
|
| 428 |
+
"tensorAngleDegrees": tensor_cue.angle_degrees,
|
| 429 |
+
"lineAngleDegrees": line_cue.angle_degrees,
|
| 430 |
+
"cueAgreementDegrees": round(difference, 3),
|
| 431 |
+
},
|
| 432 |
+
)
|
| 433 |
+
|
| 434 |
+
|
| 435 |
+
def normalize_vector(vector: np.ndarray) -> np.ndarray | None:
|
| 436 |
+
vector = np.asarray(vector, dtype=np.float64)
|
| 437 |
+
length = float(np.linalg.norm(vector))
|
| 438 |
+
if not np.isfinite(vector).all() or length <= 1e-7:
|
| 439 |
+
return None
|
| 440 |
+
return vector / length
|
| 441 |
+
|
| 442 |
+
|
| 443 |
+
def normalize_transform(value: list[float] | np.ndarray | None) -> np.ndarray | None:
|
| 444 |
+
if value is None:
|
| 445 |
+
return None
|
| 446 |
+
transform = np.asarray(value, dtype=np.float64)
|
| 447 |
+
if transform.size != 9:
|
| 448 |
+
return None
|
| 449 |
+
transform = transform.reshape(3, 3)
|
| 450 |
+
if not np.isfinite(transform).all() or abs(float(np.linalg.det(transform))) < 1e-12:
|
| 451 |
+
return None
|
| 452 |
+
return transform
|
| 453 |
+
|
| 454 |
+
|
| 455 |
+
def world_direction_to_render_angle(
|
| 456 |
+
direction: np.ndarray,
|
| 457 |
+
*,
|
| 458 |
+
render_u_axis: np.ndarray | None,
|
| 459 |
+
render_v_axis: np.ndarray | None,
|
| 460 |
+
intrinsics: np.ndarray | None,
|
| 461 |
+
render_transform: np.ndarray | None,
|
| 462 |
+
) -> float | None:
|
| 463 |
+
direction = normalize_vector(direction)
|
| 464 |
+
if direction is None:
|
| 465 |
+
return None
|
| 466 |
+
if render_u_axis is not None and render_v_axis is not None:
|
| 467 |
+
u_axis = normalize_vector(render_u_axis)
|
| 468 |
+
v_axis = normalize_vector(render_v_axis)
|
| 469 |
+
if u_axis is not None and v_axis is not None:
|
| 470 |
+
return normalize_angle(math.degrees(math.atan2(
|
| 471 |
+
float(direction @ v_axis),
|
| 472 |
+
float(direction @ u_axis),
|
| 473 |
+
)))
|
| 474 |
+
|
| 475 |
+
if intrinsics is None or render_transform is None:
|
| 476 |
+
return None
|
| 477 |
+
intrinsics = np.asarray(intrinsics, dtype=np.float64).reshape(3, 3)
|
| 478 |
+
vanishing_point = intrinsics @ direction
|
| 479 |
+
mapped = render_transform @ vanishing_point
|
| 480 |
+
if not np.isfinite(mapped).all() or float(np.linalg.norm(mapped[:2])) <= 1e-7:
|
| 481 |
+
return None
|
| 482 |
+
return normalize_angle(math.degrees(math.atan2(float(mapped[1]), float(mapped[0]))))
|
| 483 |
+
|
| 484 |
+
|
| 485 |
+
def choose_depth_axis(
|
| 486 |
+
base_angle_degrees: float,
|
| 487 |
+
*,
|
| 488 |
+
floor_normal: np.ndarray,
|
| 489 |
+
render_u_axis: np.ndarray | None,
|
| 490 |
+
render_v_axis: np.ndarray | None,
|
| 491 |
+
intrinsics: np.ndarray | None,
|
| 492 |
+
render_transform: np.ndarray | None,
|
| 493 |
+
) -> float:
|
| 494 |
+
camera_forward = np.array([0.0, 0.0, 1.0], dtype=np.float64)
|
| 495 |
+
floor_normal = normalize_vector(floor_normal)
|
| 496 |
+
if floor_normal is None:
|
| 497 |
+
return normalize_angle(base_angle_degrees)
|
| 498 |
+
projected_forward = camera_forward - floor_normal * float(camera_forward @ floor_normal)
|
| 499 |
+
forward_angle = world_direction_to_render_angle(
|
| 500 |
+
projected_forward,
|
| 501 |
+
render_u_axis=render_u_axis,
|
| 502 |
+
render_v_axis=render_v_axis,
|
| 503 |
+
intrinsics=intrinsics,
|
| 504 |
+
render_transform=render_transform,
|
| 505 |
+
)
|
| 506 |
+
first = normalize_angle(base_angle_degrees)
|
| 507 |
+
second = normalize_angle(base_angle_degrees + 90.0)
|
| 508 |
+
if forward_angle is None:
|
| 509 |
+
return first
|
| 510 |
+
return first if angular_distance(first, forward_angle) <= angular_distance(second, forward_angle) else second
|
| 511 |
+
|
| 512 |
+
|
| 513 |
+
def estimate_wall_normal_cue(
|
| 514 |
+
normals_map: np.ndarray | None,
|
| 515 |
+
valid_mask: np.ndarray | None,
|
| 516 |
+
wall_mask: np.ndarray,
|
| 517 |
+
*,
|
| 518 |
+
floor_normal: np.ndarray,
|
| 519 |
+
render_u_axis: np.ndarray | None,
|
| 520 |
+
render_v_axis: np.ndarray | None,
|
| 521 |
+
intrinsics: np.ndarray | None,
|
| 522 |
+
render_transform: np.ndarray | None,
|
| 523 |
+
) -> DirectionCue | None:
|
| 524 |
+
if normals_map is None or normals_map.shape[:2] != wall_mask.shape:
|
| 525 |
+
return None
|
| 526 |
+
floor_normal = normalize_vector(floor_normal)
|
| 527 |
+
if floor_normal is None:
|
| 528 |
+
return None
|
| 529 |
+
|
| 530 |
+
lengths = np.linalg.norm(normals_map, axis=2)
|
| 531 |
+
valid = (wall_mask > 0) & np.isfinite(normals_map).all(axis=2) & (lengths > 1e-4)
|
| 532 |
+
if valid_mask is not None and valid_mask.shape == wall_mask.shape:
|
| 533 |
+
valid &= valid_mask.astype(bool)
|
| 534 |
+
normalized = np.zeros_like(normals_map, dtype=np.float64)
|
| 535 |
+
normalized[valid] = normals_map[valid] / lengths[valid, None]
|
| 536 |
+
vertical_alignment = np.abs(normalized @ floor_normal)
|
| 537 |
+
valid &= vertical_alignment <= 0.42
|
| 538 |
+
ys, xs = np.where(valid)
|
| 539 |
+
if len(xs) < 250:
|
| 540 |
+
return None
|
| 541 |
+
|
| 542 |
+
stride = max(1, len(xs) // 6000)
|
| 543 |
+
ys, xs = ys[::stride], xs[::stride]
|
| 544 |
+
normals = normalized[ys, xs]
|
| 545 |
+
horizontal = normals - (normals @ floor_normal)[:, None] * floor_normal
|
| 546 |
+
horizontal_lengths = np.linalg.norm(horizontal, axis=1)
|
| 547 |
+
keep = horizontal_lengths > 0.6
|
| 548 |
+
horizontal = horizontal[keep] / horizontal_lengths[keep, None]
|
| 549 |
+
ys, xs = ys[keep], xs[keep]
|
| 550 |
+
if len(horizontal) < 200:
|
| 551 |
+
return None
|
| 552 |
+
|
| 553 |
+
angles = []
|
| 554 |
+
weights = []
|
| 555 |
+
regions = []
|
| 556 |
+
height, width = wall_mask.shape
|
| 557 |
+
for direction, y, x in zip(horizontal, ys, xs, strict=False):
|
| 558 |
+
angle = world_direction_to_render_angle(
|
| 559 |
+
direction,
|
| 560 |
+
render_u_axis=render_u_axis,
|
| 561 |
+
render_v_axis=render_v_axis,
|
| 562 |
+
intrinsics=intrinsics,
|
| 563 |
+
render_transform=render_transform,
|
| 564 |
+
)
|
| 565 |
+
if angle is None:
|
| 566 |
+
continue
|
| 567 |
+
angles.append(angle % 90.0)
|
| 568 |
+
weights.append(1.0)
|
| 569 |
+
regions.append(min(3, int(y * 4 / max(height, 1))) * 4 + min(3, int(x * 4 / max(width, 1))))
|
| 570 |
+
if len(angles) < 200:
|
| 571 |
+
return None
|
| 572 |
+
|
| 573 |
+
base_angle, concentration = periodic_mean(
|
| 574 |
+
np.asarray(angles),
|
| 575 |
+
np.asarray(weights),
|
| 576 |
+
period=90.0,
|
| 577 |
+
)
|
| 578 |
+
agreement = np.array(
|
| 579 |
+
[angular_distance(angle, base_angle, 90.0) <= 10.0 for angle in angles],
|
| 580 |
+
dtype=bool,
|
| 581 |
+
)
|
| 582 |
+
agreeing_regions = {region for region, accepted in zip(regions, agreement, strict=False) if accepted}
|
| 583 |
+
if int(agreement.sum()) < 160 or len(agreeing_regions) < 2:
|
| 584 |
+
return None
|
| 585 |
+
agreement_ratio = float(agreement.mean())
|
| 586 |
+
confidence = float(np.clip(
|
| 587 |
+
0.46 * concentration
|
| 588 |
+
+ 0.30 * agreement_ratio
|
| 589 |
+
+ 0.14 * min(1.0, len(agreeing_regions) / 6.0)
|
| 590 |
+
+ 0.10 * min(1.0, int(agreement.sum()) / 1200.0),
|
| 591 |
+
0.0,
|
| 592 |
+
1.0,
|
| 593 |
+
))
|
| 594 |
+
angle = choose_depth_axis(
|
| 595 |
+
base_angle,
|
| 596 |
+
floor_normal=floor_normal,
|
| 597 |
+
render_u_axis=render_u_axis,
|
| 598 |
+
render_v_axis=render_v_axis,
|
| 599 |
+
intrinsics=intrinsics,
|
| 600 |
+
render_transform=render_transform,
|
| 601 |
+
)
|
| 602 |
+
return DirectionCue(
|
| 603 |
+
source="wall-normal-manhattan",
|
| 604 |
+
angle_degrees=round(angle, 3),
|
| 605 |
+
confidence=round(confidence, 3),
|
| 606 |
+
support=int(agreement.sum()),
|
| 607 |
+
region_support=len(agreeing_regions),
|
| 608 |
+
ambiguity_degrees=90.0,
|
| 609 |
+
details={
|
| 610 |
+
"baseAxisDegrees": round(base_angle, 3),
|
| 611 |
+
"concentration": round(concentration, 4),
|
| 612 |
+
"agreementRatio": round(agreement_ratio, 4),
|
| 613 |
+
},
|
| 614 |
+
)
|
| 615 |
+
|
| 616 |
+
|
| 617 |
+
def estimate_architectural_line_cue(
|
| 618 |
+
image_rgb: np.ndarray,
|
| 619 |
+
structure_mask: np.ndarray,
|
| 620 |
+
*,
|
| 621 |
+
floor_normal: np.ndarray,
|
| 622 |
+
render_u_axis: np.ndarray | None,
|
| 623 |
+
render_v_axis: np.ndarray | None,
|
| 624 |
+
intrinsics: np.ndarray | None,
|
| 625 |
+
render_transform: np.ndarray | None,
|
| 626 |
+
) -> DirectionCue | None:
|
| 627 |
+
if intrinsics is None:
|
| 628 |
+
return None
|
| 629 |
+
floor_normal = normalize_vector(floor_normal)
|
| 630 |
+
if floor_normal is None:
|
| 631 |
+
return None
|
| 632 |
+
intrinsics = np.asarray(intrinsics, dtype=np.float64).reshape(3, 3)
|
| 633 |
+
|
| 634 |
+
gray = cv2.cvtColor(image_rgb, cv2.COLOR_RGB2GRAY)
|
| 635 |
+
detector = cv2.createLineSegmentDetector(cv2.LSD_REFINE_STD)
|
| 636 |
+
detected = detector.detect(gray)[0]
|
| 637 |
+
if detected is None:
|
| 638 |
+
return None
|
| 639 |
+
|
| 640 |
+
height, width = gray.shape
|
| 641 |
+
minimum_length = max(30.0, min(height, width) * 0.07)
|
| 642 |
+
vertical_vanishing = intrinsics @ floor_normal
|
| 643 |
+
finite_vertical = abs(float(vertical_vanishing[2])) > 1e-7
|
| 644 |
+
vertical_xy = (
|
| 645 |
+
vertical_vanishing[:2] / vertical_vanishing[2]
|
| 646 |
+
if finite_vertical
|
| 647 |
+
else vertical_vanishing[:2]
|
| 648 |
+
)
|
| 649 |
+
|
| 650 |
+
angles = []
|
| 651 |
+
weights = []
|
| 652 |
+
regions = []
|
| 653 |
+
for raw_line in detected.reshape(-1, 4):
|
| 654 |
+
x1, y1, x2, y2 = [float(value) for value in raw_line]
|
| 655 |
+
line_vector = np.array([x2 - x1, y2 - y1], dtype=np.float64)
|
| 656 |
+
length = float(np.linalg.norm(line_vector))
|
| 657 |
+
if length < minimum_length:
|
| 658 |
+
continue
|
| 659 |
+
line_unit = line_vector / length
|
| 660 |
+
sample_x = np.clip(np.rint(np.linspace(x1, x2, 11)).astype(np.int32), 0, width - 1)
|
| 661 |
+
sample_y = np.clip(np.rint(np.linspace(y1, y2, 11)).astype(np.int32), 0, height - 1)
|
| 662 |
+
structure_support = float(np.mean(structure_mask[sample_y, sample_x] > 0))
|
| 663 |
+
if structure_support < 0.58:
|
| 664 |
+
continue
|
| 665 |
+
|
| 666 |
+
midpoint = np.array([(x1 + x2) * 0.5, (y1 + y2) * 0.5], dtype=np.float64)
|
| 667 |
+
vertical_direction = vertical_xy - midpoint if finite_vertical else vertical_xy
|
| 668 |
+
vertical_length = float(np.linalg.norm(vertical_direction))
|
| 669 |
+
if vertical_length > 1e-6:
|
| 670 |
+
vertical_direction /= vertical_length
|
| 671 |
+
if abs(float(line_unit @ vertical_direction)) >= math.cos(math.radians(14.0)):
|
| 672 |
+
continue
|
| 673 |
+
|
| 674 |
+
p1 = np.array([x1, y1, 1.0], dtype=np.float64)
|
| 675 |
+
p2 = np.array([x2, y2, 1.0], dtype=np.float64)
|
| 676 |
+
image_line = np.cross(p1, p2)
|
| 677 |
+
interpretation_plane = intrinsics.T @ image_line
|
| 678 |
+
world_direction = np.cross(interpretation_plane, floor_normal)
|
| 679 |
+
angle = world_direction_to_render_angle(
|
| 680 |
+
world_direction,
|
| 681 |
+
render_u_axis=render_u_axis,
|
| 682 |
+
render_v_axis=render_v_axis,
|
| 683 |
+
intrinsics=intrinsics,
|
| 684 |
+
render_transform=render_transform,
|
| 685 |
+
)
|
| 686 |
+
if angle is None:
|
| 687 |
+
continue
|
| 688 |
+
angles.append(angle % 90.0)
|
| 689 |
+
weights.append(length * structure_support)
|
| 690 |
+
regions.append(
|
| 691 |
+
min(3, int(midpoint[1] * 4 / max(height, 1))) * 4
|
| 692 |
+
+ min(3, int(midpoint[0] * 4 / max(width, 1)))
|
| 693 |
+
)
|
| 694 |
+
|
| 695 |
+
base_cue = cue_from_region_angles(
|
| 696 |
+
source="architectural-manhattan",
|
| 697 |
+
angles=angles,
|
| 698 |
+
weights=weights,
|
| 699 |
+
regions=regions,
|
| 700 |
+
period=90.0,
|
| 701 |
+
minimum_regions=3,
|
| 702 |
+
)
|
| 703 |
+
if base_cue is None:
|
| 704 |
+
return None
|
| 705 |
+
angle = choose_depth_axis(
|
| 706 |
+
base_cue.angle_degrees,
|
| 707 |
+
floor_normal=floor_normal,
|
| 708 |
+
render_u_axis=render_u_axis,
|
| 709 |
+
render_v_axis=render_v_axis,
|
| 710 |
+
intrinsics=intrinsics,
|
| 711 |
+
render_transform=render_transform,
|
| 712 |
+
)
|
| 713 |
+
return DirectionCue(
|
| 714 |
+
source=base_cue.source,
|
| 715 |
+
angle_degrees=round(angle, 3),
|
| 716 |
+
confidence=base_cue.confidence,
|
| 717 |
+
support=base_cue.support,
|
| 718 |
+
region_support=base_cue.region_support,
|
| 719 |
+
ambiguity_degrees=90.0,
|
| 720 |
+
details={**base_cue.details, "baseAxisDegrees": base_cue.angle_degrees},
|
| 721 |
+
)
|
| 722 |
+
|
| 723 |
+
|
| 724 |
+
def fuse_structural_cues(cues: list[DirectionCue]) -> DirectionCue | None:
|
| 725 |
+
cues = [cue for cue in cues if cue is not None and cue.confidence >= 0.42]
|
| 726 |
+
if not cues:
|
| 727 |
+
return None
|
| 728 |
+
if len(cues) == 1:
|
| 729 |
+
return cues[0]
|
| 730 |
+
|
| 731 |
+
base_angles = np.array([cue.angle_degrees % 90.0 for cue in cues])
|
| 732 |
+
weights = np.array([cue.confidence for cue in cues])
|
| 733 |
+
angle, concentration = periodic_mean(base_angles, weights, period=90.0)
|
| 734 |
+
disagreement = max(
|
| 735 |
+
angular_distance(float(value), angle, 90.0)
|
| 736 |
+
for value in base_angles
|
| 737 |
+
)
|
| 738 |
+
if disagreement > 14.0:
|
| 739 |
+
strongest = max(cues, key=lambda cue: cue.confidence)
|
| 740 |
+
return DirectionCue(
|
| 741 |
+
source=strongest.source,
|
| 742 |
+
angle_degrees=strongest.angle_degrees,
|
| 743 |
+
confidence=round(strongest.confidence * 0.72, 3),
|
| 744 |
+
support=strongest.support,
|
| 745 |
+
region_support=strongest.region_support,
|
| 746 |
+
ambiguity_degrees=90.0,
|
| 747 |
+
details={**strongest.details, "structuralCueDisagreementDegrees": round(disagreement, 3)},
|
| 748 |
+
)
|
| 749 |
+
|
| 750 |
+
strongest = max(cues, key=lambda cue: cue.confidence)
|
| 751 |
+
representative = strongest.angle_degrees
|
| 752 |
+
if angular_distance(representative, angle) > angular_distance(representative + 90.0, angle):
|
| 753 |
+
representative += 90.0
|
| 754 |
+
confidence = float(np.clip(
|
| 755 |
+
np.average([cue.confidence for cue in cues], weights=weights)
|
| 756 |
+
+ concentration * 0.12,
|
| 757 |
+
0.0,
|
| 758 |
+
1.0,
|
| 759 |
+
))
|
| 760 |
+
return DirectionCue(
|
| 761 |
+
source="room-manhattan-consensus",
|
| 762 |
+
angle_degrees=round(normalize_angle(representative), 3),
|
| 763 |
+
confidence=round(confidence, 3),
|
| 764 |
+
support=sum(cue.support for cue in cues),
|
| 765 |
+
region_support=max(cue.region_support for cue in cues),
|
| 766 |
+
ambiguity_degrees=90.0,
|
| 767 |
+
details={
|
| 768 |
+
"cueSources": [cue.source for cue in cues],
|
| 769 |
+
"baseAxisDegrees": round(angle, 3),
|
| 770 |
+
"concentration": round(concentration, 4),
|
| 771 |
+
"cueDisagreementDegrees": round(disagreement, 3),
|
| 772 |
+
},
|
| 773 |
+
)
|
| 774 |
+
|
| 775 |
+
|
| 776 |
+
def select_direction(
|
| 777 |
+
material_cue: DirectionCue | None,
|
| 778 |
+
structural_cue: DirectionCue | None,
|
| 779 |
+
) -> tuple[DirectionCue | None, str]:
|
| 780 |
+
if material_cue is not None and material_cue.confidence >= 0.82:
|
| 781 |
+
if structural_cue is None:
|
| 782 |
+
return material_cue, "strong-material-direction"
|
| 783 |
+
axis_difference = min(
|
| 784 |
+
angular_distance(material_cue.angle_degrees, structural_cue.angle_degrees),
|
| 785 |
+
angular_distance(material_cue.angle_degrees, structural_cue.angle_degrees + 90.0),
|
| 786 |
+
)
|
| 787 |
+
if axis_difference <= 14.0:
|
| 788 |
+
boosted = DirectionCue(
|
| 789 |
+
source="material-room-consensus",
|
| 790 |
+
angle_degrees=material_cue.angle_degrees,
|
| 791 |
+
confidence=round(min(1.0, material_cue.confidence * 0.78 + structural_cue.confidence * 0.30), 3),
|
| 792 |
+
support=material_cue.support + structural_cue.support,
|
| 793 |
+
region_support=max(material_cue.region_support, structural_cue.region_support),
|
| 794 |
+
ambiguity_degrees=None,
|
| 795 |
+
details={
|
| 796 |
+
"materialSource": material_cue.source,
|
| 797 |
+
"structuralSource": structural_cue.source,
|
| 798 |
+
"axisDifferenceDegrees": round(axis_difference, 3),
|
| 799 |
+
},
|
| 800 |
+
)
|
| 801 |
+
return boosted, "material-and-room-agree"
|
| 802 |
+
return material_cue, "strong-diagonal-material-direction"
|
| 803 |
+
|
| 804 |
+
if structural_cue is not None and structural_cue.confidence >= 0.52:
|
| 805 |
+
return structural_cue, "room-structure-direction"
|
| 806 |
+
if material_cue is not None and material_cue.confidence >= 0.62:
|
| 807 |
+
return material_cue, "moderate-material-direction"
|
| 808 |
+
return None, "insufficient-observable-direction-evidence"
|
| 809 |
+
|
| 810 |
+
|
| 811 |
+
def direction_label(angle_degrees: float) -> str:
|
| 812 |
+
angle = normalize_angle(angle_degrees)
|
| 813 |
+
if angle < 12.0 or angle >= 168.0:
|
| 814 |
+
return "across_render_u"
|
| 815 |
+
if 78.0 <= angle < 102.0:
|
| 816 |
+
return "along_render_v"
|
| 817 |
+
return "diagonal"
|
| 818 |
+
|
| 819 |
+
|
| 820 |
+
def direction_axis_for_overlay(
|
| 821 |
+
*,
|
| 822 |
+
angle_degrees: float,
|
| 823 |
+
image_shape: tuple[int, int],
|
| 824 |
+
surface_mask: np.ndarray,
|
| 825 |
+
surface_uv: np.ndarray | None,
|
| 826 |
+
surface_indices: np.ndarray | None,
|
| 827 |
+
surface_plane_ids: np.ndarray | None,
|
| 828 |
+
render_transform: np.ndarray | None,
|
| 829 |
+
) -> dict | None:
|
| 830 |
+
height, width = image_shape
|
| 831 |
+
direction = np.array([
|
| 832 |
+
math.cos(math.radians(angle_degrees)),
|
| 833 |
+
math.sin(math.radians(angle_degrees)),
|
| 834 |
+
])
|
| 835 |
+
if surface_uv is not None and surface_indices is not None and len(surface_uv) == len(surface_indices):
|
| 836 |
+
valid = np.isfinite(surface_uv).all(axis=1)
|
| 837 |
+
if surface_plane_ids is not None and len(surface_plane_ids) == len(surface_uv):
|
| 838 |
+
plane_values = surface_plane_ids[valid & (surface_plane_ids != 255)]
|
| 839 |
+
if plane_values.size:
|
| 840 |
+
dominant_plane = int(np.bincount(plane_values.astype(np.int64)).argmax())
|
| 841 |
+
valid &= surface_plane_ids == dominant_plane
|
| 842 |
+
uv = surface_uv[valid]
|
| 843 |
+
indices = np.asarray(surface_indices)[valid]
|
| 844 |
+
if len(uv) >= 100:
|
| 845 |
+
center = np.median(uv, axis=0)
|
| 846 |
+
span = np.percentile(uv, 95, axis=0) - np.percentile(uv, 5, axis=0)
|
| 847 |
+
radius = max(float(np.min(span)) * 0.28, 1e-3)
|
| 848 |
+
endpoints = [center - direction * radius, center + direction * radius]
|
| 849 |
+
image_points = []
|
| 850 |
+
for endpoint in endpoints:
|
| 851 |
+
nearest = int(np.argmin(np.sum((uv - endpoint) ** 2, axis=1)))
|
| 852 |
+
pixel_index = int(indices[nearest])
|
| 853 |
+
image_points.append((float(pixel_index % width), float(pixel_index // width)))
|
| 854 |
+
return {
|
| 855 |
+
"startX": image_points[0][0],
|
| 856 |
+
"startY": image_points[0][1],
|
| 857 |
+
"endX": image_points[1][0],
|
| 858 |
+
"endY": image_points[1][1],
|
| 859 |
+
"source": "surface-uv",
|
| 860 |
+
}
|
| 861 |
+
|
| 862 |
+
if render_transform is None:
|
| 863 |
+
return None
|
| 864 |
+
ys, xs = np.where(surface_mask > 0)
|
| 865 |
+
if len(xs) < 50:
|
| 866 |
+
return None
|
| 867 |
+
sample_points = np.column_stack((xs, ys)).astype(np.float32).reshape(1, -1, 2)
|
| 868 |
+
mapped = cv2.perspectiveTransform(sample_points, render_transform.astype(np.float32))[0]
|
| 869 |
+
finite = np.isfinite(mapped).all(axis=1)
|
| 870 |
+
mapped = mapped[finite]
|
| 871 |
+
if len(mapped) < 50:
|
| 872 |
+
return None
|
| 873 |
+
center = np.median(mapped, axis=0)
|
| 874 |
+
span = np.percentile(mapped, 95, axis=0) - np.percentile(mapped, 5, axis=0)
|
| 875 |
+
radius = max(float(np.min(span)) * 0.28, 1e-3)
|
| 876 |
+
floor_points = np.array(
|
| 877 |
+
[[center - direction * radius, center + direction * radius]],
|
| 878 |
+
dtype=np.float32,
|
| 879 |
+
)
|
| 880 |
+
inverse = np.linalg.inv(render_transform).astype(np.float32)
|
| 881 |
+
image_points = cv2.perspectiveTransform(floor_points, inverse)[0]
|
| 882 |
+
if not np.isfinite(image_points).all():
|
| 883 |
+
return None
|
| 884 |
+
return {
|
| 885 |
+
"startX": float(image_points[0, 0]),
|
| 886 |
+
"startY": float(image_points[0, 1]),
|
| 887 |
+
"endX": float(image_points[1, 0]),
|
| 888 |
+
"endY": float(image_points[1, 1]),
|
| 889 |
+
"source": "floor-transform",
|
| 890 |
+
}
|
| 891 |
+
|
| 892 |
+
|
| 893 |
+
def find_floor_direction(
|
| 894 |
+
image_rgb: np.ndarray,
|
| 895 |
+
surface_mask: np.ndarray,
|
| 896 |
+
*,
|
| 897 |
+
wall_mask: np.ndarray,
|
| 898 |
+
structure_mask: np.ndarray,
|
| 899 |
+
normals_map: np.ndarray | None,
|
| 900 |
+
geometry_valid_mask: np.ndarray | None,
|
| 901 |
+
intrinsics: np.ndarray | list[float] | None,
|
| 902 |
+
floor_normal: np.ndarray | list[float] | None,
|
| 903 |
+
render_u_axis: np.ndarray | list[float] | None,
|
| 904 |
+
render_v_axis: np.ndarray | list[float] | None,
|
| 905 |
+
render_transform: np.ndarray | list[float] | None,
|
| 906 |
+
surface_uv: np.ndarray | None,
|
| 907 |
+
surface_indices: np.ndarray | None,
|
| 908 |
+
surface_plane_ids: np.ndarray | None,
|
| 909 |
+
) -> dict:
|
| 910 |
+
height, width = surface_mask.shape[:2]
|
| 911 |
+
transform = normalize_transform(render_transform)
|
| 912 |
+
camera_matrix = None
|
| 913 |
+
if intrinsics is not None:
|
| 914 |
+
raw_intrinsics = np.asarray(intrinsics, dtype=np.float64)
|
| 915 |
+
if raw_intrinsics.size == 9 and np.isfinite(raw_intrinsics).all():
|
| 916 |
+
camera_matrix = raw_intrinsics.reshape(3, 3)
|
| 917 |
+
normal = normalize_vector(np.asarray(floor_normal)) if floor_normal is not None else None
|
| 918 |
+
u_axis = normalize_vector(np.asarray(render_u_axis)) if render_u_axis is not None else None
|
| 919 |
+
v_axis = normalize_vector(np.asarray(render_v_axis)) if render_v_axis is not None else None
|
| 920 |
+
|
| 921 |
+
rectified = rectify_floor_image(
|
| 922 |
+
image_rgb,
|
| 923 |
+
surface_mask,
|
| 924 |
+
render_transform=transform,
|
| 925 |
+
surface_uv=surface_uv,
|
| 926 |
+
surface_indices=surface_indices,
|
| 927 |
+
surface_plane_ids=surface_plane_ids,
|
| 928 |
+
)
|
| 929 |
+
material_cue = estimate_material_cue(rectified[0], rectified[1]) if rectified is not None else None
|
| 930 |
+
|
| 931 |
+
wall_cue = None
|
| 932 |
+
architecture_cue = None
|
| 933 |
+
if normal is not None:
|
| 934 |
+
wall_cue = estimate_wall_normal_cue(
|
| 935 |
+
normals_map,
|
| 936 |
+
geometry_valid_mask,
|
| 937 |
+
wall_mask,
|
| 938 |
+
floor_normal=normal,
|
| 939 |
+
render_u_axis=u_axis,
|
| 940 |
+
render_v_axis=v_axis,
|
| 941 |
+
intrinsics=camera_matrix,
|
| 942 |
+
render_transform=transform,
|
| 943 |
+
)
|
| 944 |
+
architecture_cue = estimate_architectural_line_cue(
|
| 945 |
+
image_rgb,
|
| 946 |
+
structure_mask,
|
| 947 |
+
floor_normal=normal,
|
| 948 |
+
render_u_axis=u_axis,
|
| 949 |
+
render_v_axis=v_axis,
|
| 950 |
+
intrinsics=camera_matrix,
|
| 951 |
+
render_transform=transform,
|
| 952 |
+
)
|
| 953 |
+
structural_cue = fuse_structural_cues([
|
| 954 |
+
cue for cue in (wall_cue, architecture_cue) if cue is not None
|
| 955 |
+
])
|
| 956 |
+
selected, selection_reason = select_direction(material_cue, structural_cue)
|
| 957 |
+
|
| 958 |
+
warnings = []
|
| 959 |
+
needs_user_direction = selected is None
|
| 960 |
+
if selected is None:
|
| 961 |
+
selected = DirectionCue(
|
| 962 |
+
source="canonical-render-axis",
|
| 963 |
+
angle_degrees=0.0,
|
| 964 |
+
confidence=0.0,
|
| 965 |
+
support=0,
|
| 966 |
+
region_support=0,
|
| 967 |
+
ambiguity_degrees=90.0,
|
| 968 |
+
details={"reason": "direction-is-not-observable-in-this-image"},
|
| 969 |
+
)
|
| 970 |
+
warnings.append("direction_not_observable_user_rotation_recommended")
|
| 971 |
+
if selected.ambiguity_degrees is not None:
|
| 972 |
+
warnings.append("direction_has_90_degree_axis_ambiguity")
|
| 973 |
+
|
| 974 |
+
axis = direction_axis_for_overlay(
|
| 975 |
+
angle_degrees=selected.angle_degrees,
|
| 976 |
+
image_shape=(height, width),
|
| 977 |
+
surface_mask=surface_mask,
|
| 978 |
+
surface_uv=surface_uv,
|
| 979 |
+
surface_indices=surface_indices,
|
| 980 |
+
surface_plane_ids=surface_plane_ids,
|
| 981 |
+
render_transform=transform,
|
| 982 |
+
)
|
| 983 |
+
floor_area_ratio = float(np.count_nonzero(surface_mask)) / max(int(surface_mask.size), 1)
|
| 984 |
+
return {
|
| 985 |
+
"source": "backend-direction-finder-v2",
|
| 986 |
+
"directionMethod": selected.source,
|
| 987 |
+
"selectionReason": selection_reason,
|
| 988 |
+
"angleDegrees": selected.angle_degrees,
|
| 989 |
+
"renderAngleDegrees": selected.angle_degrees,
|
| 990 |
+
"secondaryAngleDegrees": normalize_angle(selected.angle_degrees + 90.0),
|
| 991 |
+
"directionLabel": direction_label(selected.angle_degrees),
|
| 992 |
+
"confidence": selected.confidence,
|
| 993 |
+
"floorAreaRatio": round(floor_area_ratio, 4),
|
| 994 |
+
"lineCount": 0,
|
| 995 |
+
"dominantLineCount": 0,
|
| 996 |
+
"gridPatternDetected": bool(material_cue and material_cue.ambiguity_degrees == 90.0),
|
| 997 |
+
"axisAmbiguous90": selected.ambiguity_degrees == 90.0,
|
| 998 |
+
"needsUserDirection": needs_user_direction,
|
| 999 |
+
"warnings": warnings,
|
| 1000 |
+
"dominantLines": [],
|
| 1001 |
+
"directionAxis": axis,
|
| 1002 |
+
"rectification": rectified[2] if rectified is not None else None,
|
| 1003 |
+
"cues": {
|
| 1004 |
+
"material": material_cue.metadata() if material_cue is not None else None,
|
| 1005 |
+
"wallNormals": wall_cue.metadata() if wall_cue is not None else None,
|
| 1006 |
+
"architecture": architecture_cue.metadata() if architecture_cue is not None else None,
|
| 1007 |
+
"structural": structural_cue.metadata() if structural_cue is not None else None,
|
| 1008 |
+
},
|
| 1009 |
+
}
|
test_floor_geometry.py
CHANGED
|
@@ -9,6 +9,7 @@ from PIL import Image
|
|
| 9 |
from fastapi.testclient import TestClient
|
| 10 |
|
| 11 |
import app
|
|
|
|
| 12 |
import floor_direction
|
| 13 |
|
| 14 |
|
|
@@ -831,6 +832,178 @@ class MetricFloorGeometryTests(unittest.TestCase):
|
|
| 831 |
)
|
| 832 |
self.assertIn("grout_direction_projected_to_surface_uv", metadata["warnings"])
|
| 833 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 834 |
def test_floor_direction_parser_does_not_use_non_floor_segment(self):
|
| 835 |
payload = {
|
| 836 |
"width": 120,
|
|
|
|
| 9 |
from fastapi.testclient import TestClient
|
| 10 |
|
| 11 |
import app
|
| 12 |
+
import direction_finder
|
| 13 |
import floor_direction
|
| 14 |
|
| 15 |
|
|
|
|
| 832 |
)
|
| 833 |
self.assertIn("grout_direction_projected_to_surface_uv", metadata["warnings"])
|
| 834 |
|
| 835 |
+
def test_direction_finder_detects_multi_region_material_axis(self):
|
| 836 |
+
size = 320
|
| 837 |
+
gray = np.full((size, size), 205, dtype=np.uint8)
|
| 838 |
+
angle_degrees = 27.0
|
| 839 |
+
radians = np.deg2rad(angle_degrees)
|
| 840 |
+
direction = np.array([np.cos(radians), np.sin(radians)])
|
| 841 |
+
normal = np.array([-direction[1], direction[0]])
|
| 842 |
+
center = np.array([size / 2, size / 2])
|
| 843 |
+
for offset in range(-360, 361, 30):
|
| 844 |
+
line_center = center + normal * offset
|
| 845 |
+
start = np.rint(line_center - direction * 300).astype(int)
|
| 846 |
+
end = np.rint(line_center + direction * 300).astype(int)
|
| 847 |
+
cv2.line(gray, tuple(start), tuple(end), 45, 3, cv2.LINE_AA)
|
| 848 |
+
|
| 849 |
+
cue = direction_finder.estimate_material_cue(
|
| 850 |
+
gray,
|
| 851 |
+
np.full_like(gray, 255),
|
| 852 |
+
)
|
| 853 |
+
|
| 854 |
+
self.assertIsNotNone(cue)
|
| 855 |
+
self.assertLess(
|
| 856 |
+
direction_finder.angular_distance(cue.angle_degrees, angle_degrees),
|
| 857 |
+
4.0,
|
| 858 |
+
)
|
| 859 |
+
self.assertGreaterEqual(cue.region_support, 3)
|
| 860 |
+
|
| 861 |
+
def test_direction_finder_rejects_blank_material(self):
|
| 862 |
+
cue = direction_finder.estimate_material_cue(
|
| 863 |
+
np.full((240, 320), 180, dtype=np.uint8),
|
| 864 |
+
np.full((240, 320), 255, dtype=np.uint8),
|
| 865 |
+
)
|
| 866 |
+
|
| 867 |
+
self.assertIsNone(cue)
|
| 868 |
+
|
| 869 |
+
def test_direction_finder_uses_wall_normals_for_manhattan_axis(self):
|
| 870 |
+
height, width = 240, 320
|
| 871 |
+
wall_mask = np.ones((height, width), dtype=np.uint8)
|
| 872 |
+
normals = np.zeros((height, width, 3), dtype=np.float32)
|
| 873 |
+
radians = np.deg2rad(20.0)
|
| 874 |
+
normals[:, :, 0] = np.cos(radians)
|
| 875 |
+
normals[:, :, 2] = np.sin(radians)
|
| 876 |
+
|
| 877 |
+
cue = direction_finder.estimate_wall_normal_cue(
|
| 878 |
+
normals,
|
| 879 |
+
np.ones((height, width), dtype=bool),
|
| 880 |
+
wall_mask,
|
| 881 |
+
floor_normal=np.array([0.0, 1.0, 0.0]),
|
| 882 |
+
render_u_axis=np.array([1.0, 0.0, 0.0]),
|
| 883 |
+
render_v_axis=np.array([0.0, 0.0, 1.0]),
|
| 884 |
+
intrinsics=np.eye(3),
|
| 885 |
+
render_transform=np.eye(3),
|
| 886 |
+
)
|
| 887 |
+
|
| 888 |
+
self.assertIsNotNone(cue)
|
| 889 |
+
self.assertLess(
|
| 890 |
+
direction_finder.angular_distance(cue.angle_degrees, 20.0, period=90.0),
|
| 891 |
+
1.0,
|
| 892 |
+
)
|
| 893 |
+
self.assertGreater(cue.confidence, 0.7)
|
| 894 |
+
self.assertEqual(cue.ambiguity_degrees, 90.0)
|
| 895 |
+
|
| 896 |
+
def test_direction_finder_uses_room_architectural_lines(self):
|
| 897 |
+
height, width = 240, 320
|
| 898 |
+
image = np.full((height, width, 3), 220, dtype=np.uint8)
|
| 899 |
+
for y in (30, 75, 120, 165, 210):
|
| 900 |
+
cv2.line(image, (10, y), (width - 10, y), (30, 30, 30), 3, cv2.LINE_AA)
|
| 901 |
+
intrinsics = np.array(
|
| 902 |
+
[[300.0, 0.0, width / 2], [0.0, 300.0, height / 2], [0.0, 0.0, 1.0]],
|
| 903 |
+
dtype=np.float64,
|
| 904 |
+
)
|
| 905 |
+
|
| 906 |
+
cue = direction_finder.estimate_architectural_line_cue(
|
| 907 |
+
image,
|
| 908 |
+
np.ones((height, width), dtype=np.uint8),
|
| 909 |
+
floor_normal=np.array([0.0, 1.0, 0.0]),
|
| 910 |
+
render_u_axis=np.array([1.0, 0.0, 0.0]),
|
| 911 |
+
render_v_axis=np.array([0.0, 0.0, 1.0]),
|
| 912 |
+
intrinsics=intrinsics,
|
| 913 |
+
render_transform=np.eye(3),
|
| 914 |
+
)
|
| 915 |
+
|
| 916 |
+
self.assertIsNotNone(cue)
|
| 917 |
+
self.assertLess(
|
| 918 |
+
direction_finder.angular_distance(cue.angle_degrees, 0.0, period=90.0),
|
| 919 |
+
1.0,
|
| 920 |
+
)
|
| 921 |
+
self.assertGreater(cue.confidence, 0.7)
|
| 922 |
+
self.assertGreaterEqual(cue.region_support, 3)
|
| 923 |
+
|
| 924 |
+
def test_direction_finder_rectifies_perspective_material_before_scoring(self):
|
| 925 |
+
floor_height, floor_width = 190, 250
|
| 926 |
+
source_height, source_width = 260, 340
|
| 927 |
+
floor = np.full((floor_height, floor_width, 3), 210, dtype=np.uint8)
|
| 928 |
+
angle_degrees = 24.0
|
| 929 |
+
radians = np.deg2rad(angle_degrees)
|
| 930 |
+
direction = np.array([np.cos(radians), np.sin(radians)])
|
| 931 |
+
normal = np.array([-direction[1], direction[0]])
|
| 932 |
+
center = np.array([floor_width / 2, floor_height / 2])
|
| 933 |
+
for offset in range(-320, 321, 28):
|
| 934 |
+
line_center = center + normal * offset
|
| 935 |
+
start = np.rint(line_center - direction * 280).astype(int)
|
| 936 |
+
end = np.rint(line_center + direction * 280).astype(int)
|
| 937 |
+
cv2.line(floor, tuple(start), tuple(end), (45, 45, 45), 3, cv2.LINE_AA)
|
| 938 |
+
|
| 939 |
+
source_corners = np.array(
|
| 940 |
+
[[72, 42], [290, 64], [310, 235], [28, 218]],
|
| 941 |
+
dtype=np.float32,
|
| 942 |
+
)
|
| 943 |
+
floor_corners = np.array(
|
| 944 |
+
[[0, 0], [floor_width - 1, 0], [floor_width - 1, floor_height - 1], [0, floor_height - 1]],
|
| 945 |
+
dtype=np.float32,
|
| 946 |
+
)
|
| 947 |
+
uv_to_source = cv2.getPerspectiveTransform(floor_corners, source_corners)
|
| 948 |
+
source = cv2.warpPerspective(floor, uv_to_source, (source_width, source_height))
|
| 949 |
+
surface_mask = np.zeros((source_height, source_width), dtype=np.uint8)
|
| 950 |
+
cv2.fillConvexPoly(surface_mask, source_corners.astype(np.int32), 1)
|
| 951 |
+
yy, xx = np.mgrid[0:source_height, 0:source_width]
|
| 952 |
+
source_points = np.column_stack((xx.ravel(), yy.ravel())).astype(np.float32).reshape(1, -1, 2)
|
| 953 |
+
source_to_uv = np.linalg.inv(uv_to_source)
|
| 954 |
+
all_uv = cv2.perspectiveTransform(source_points, source_to_uv)[0]
|
| 955 |
+
surface_indices = np.flatnonzero(surface_mask.ravel()).astype(np.uint32)
|
| 956 |
+
surface_uv = all_uv[surface_indices]
|
| 957 |
+
|
| 958 |
+
result = direction_finder.find_floor_direction(
|
| 959 |
+
source,
|
| 960 |
+
surface_mask,
|
| 961 |
+
wall_mask=np.zeros_like(surface_mask),
|
| 962 |
+
structure_mask=np.zeros_like(surface_mask),
|
| 963 |
+
normals_map=None,
|
| 964 |
+
geometry_valid_mask=None,
|
| 965 |
+
intrinsics=np.eye(3),
|
| 966 |
+
floor_normal=None,
|
| 967 |
+
render_u_axis=None,
|
| 968 |
+
render_v_axis=None,
|
| 969 |
+
render_transform=source_to_uv,
|
| 970 |
+
surface_uv=surface_uv,
|
| 971 |
+
surface_indices=surface_indices,
|
| 972 |
+
surface_plane_ids=np.zeros(len(surface_indices), dtype=np.uint8),
|
| 973 |
+
)
|
| 974 |
+
|
| 975 |
+
self.assertFalse(result["needsUserDirection"])
|
| 976 |
+
self.assertLess(
|
| 977 |
+
direction_finder.angular_distance(result["renderAngleDegrees"], angle_degrees),
|
| 978 |
+
4.0,
|
| 979 |
+
)
|
| 980 |
+
self.assertEqual(result["rectification"]["source"], "surface-uv")
|
| 981 |
+
|
| 982 |
+
def test_direction_finder_marks_unobservable_image(self):
|
| 983 |
+
height, width = 180, 240
|
| 984 |
+
surface_mask = np.ones((height, width), dtype=np.uint8)
|
| 985 |
+
result = direction_finder.find_floor_direction(
|
| 986 |
+
np.full((height, width, 3), 180, dtype=np.uint8),
|
| 987 |
+
surface_mask,
|
| 988 |
+
wall_mask=np.zeros_like(surface_mask),
|
| 989 |
+
structure_mask=np.zeros_like(surface_mask),
|
| 990 |
+
normals_map=None,
|
| 991 |
+
geometry_valid_mask=None,
|
| 992 |
+
intrinsics=np.eye(3),
|
| 993 |
+
floor_normal=None,
|
| 994 |
+
render_u_axis=None,
|
| 995 |
+
render_v_axis=None,
|
| 996 |
+
render_transform=np.eye(3),
|
| 997 |
+
surface_uv=None,
|
| 998 |
+
surface_indices=None,
|
| 999 |
+
surface_plane_ids=None,
|
| 1000 |
+
)
|
| 1001 |
+
|
| 1002 |
+
self.assertEqual(result["source"], "backend-direction-finder-v2")
|
| 1003 |
+
self.assertEqual(result["directionMethod"], "canonical-render-axis")
|
| 1004 |
+
self.assertTrue(result["needsUserDirection"])
|
| 1005 |
+
self.assertEqual(result["confidence"], 0.0)
|
| 1006 |
+
|
| 1007 |
def test_floor_direction_parser_does_not_use_non_floor_segment(self):
|
| 1008 |
payload = {
|
| 1009 |
"width": 120,
|