Spaces:
Paused
Paused
GitHub Actions commited on
Commit ·
8704d80
1
Parent(s): 802aa1a
Deploy from GitHub commit dca219cf6c8f03294b24d5ad5bc45d2a9d1b4017
Browse files- app.py +91 -17
- verify_p1v2_sim.py +136 -0
- verify_p2_sim.py +151 -0
app.py
CHANGED
|
@@ -959,6 +959,42 @@ def build_floor_surface_mask(
|
|
| 959 |
kern_size = max(5, min(h, w) // 160) | 1
|
| 960 |
kern = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (kern_size, kern_size))
|
| 961 |
occluder_mask = np.isin(seg_map, class_ids(OCCLUDER_CLASSES)).astype(np.uint8)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 962 |
|
| 963 |
# One dilation pass instead of two — keeps the occluder boundary tight so
|
| 964 |
# furniture feet don't leave a visible halo on the replaced tile surface.
|
|
@@ -1014,16 +1050,12 @@ def build_floor_surface_mask(
|
|
| 1014 |
surface[occ_dilated > 0] = 0
|
| 1015 |
surface[reject_dilated > 0] = 0
|
| 1016 |
|
| 1017 |
-
#
|
| 1018 |
-
#
|
| 1019 |
-
|
| 1020 |
-
|
| 1021 |
-
|
| 1022 |
-
|
| 1023 |
-
contact_zone = cv2.dilate(occ_bottom_edge, contact_tiny, iterations=1)
|
| 1024 |
-
restore = cv2.bitwise_and(contact_zone, floor_mask)
|
| 1025 |
-
surface = cv2.bitwise_or(surface, restore)
|
| 1026 |
-
surface[reject_dilated > 0] = 0
|
| 1027 |
|
| 1028 |
# P1-6 — the dilated reject band also ate a thin strip of true floor below
|
| 1029 |
# the skirting, letting the original floor peek out along the back wall.
|
|
@@ -1032,10 +1064,36 @@ def build_floor_surface_mask(
|
|
| 1032 |
# raw reject or occluder pixels).
|
| 1033 |
reclaim = cv2.bitwise_and(floor_mask, cv2.bitwise_not(reject_mask))
|
| 1034 |
reclaim[occ_dilated > 0] = 0
|
| 1035 |
-
|
|
|
|
|
|
|
|
|
|
| 1036 |
surface = cv2.bitwise_or(surface, wall_band)
|
| 1037 |
|
| 1038 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1039 |
|
| 1040 |
|
| 1041 |
# ---------------------------------------------------------------------------
|
|
@@ -1045,15 +1103,31 @@ def build_floor_surface_mask(
|
|
| 1045 |
# boundary transitions instead of a hard cut.
|
| 1046 |
# ---------------------------------------------------------------------------
|
| 1047 |
|
| 1048 |
-
def build_confidence_map(
|
|
|
|
|
|
|
| 1049 |
if not surface_mask.any():
|
| 1050 |
return None
|
| 1051 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1052 |
# Narrow feather: a ~6px transition instead of ~26px. The wide feather made
|
| 1053 |
# a thick translucent ring at every boundary, which is what let the bright
|
| 1054 |
# background show through as a halo. Keep the blend confined to a thin edge.
|
| 1055 |
feather = max(4.0, min(surface_mask.shape[:2]) / 200.0)
|
| 1056 |
confidence = np.clip(dist / feather, 0.0, 1.0)
|
|
|
|
| 1057 |
return (confidence * 255).astype(np.uint8)
|
| 1058 |
|
| 1059 |
|
|
@@ -1181,7 +1255,7 @@ def build_segmentation_bundle(contents: bytes):
|
|
| 1181 |
|
| 1182 |
t0 = time.perf_counter()
|
| 1183 |
quad = np.asarray(plane["quad"], dtype=np.float32).reshape(4, 2) if plane and plane.get("quad") else None
|
| 1184 |
-
surface_mask = build_floor_surface_mask(floor_mask, seg_map, quad, depth)
|
| 1185 |
print(f"[TIMING] Surface masking took {time.perf_counter() - t0:.3f} seconds", flush=True)
|
| 1186 |
|
| 1187 |
t0 = time.perf_counter()
|
|
@@ -1198,7 +1272,7 @@ def build_segmentation_bundle(contents: bytes):
|
|
| 1198 |
t0 = time.perf_counter()
|
| 1199 |
color_temperature = estimate_color_temperature(img_np, surface_mask) # B2
|
| 1200 |
light_vector = estimate_light_vector(shade_map, surface_mask) # B3
|
| 1201 |
-
confidence_map = build_confidence_map(surface_mask)
|
| 1202 |
print(f"[TIMING] Lighting analysis took {time.perf_counter() - t0:.3f} seconds", flush=True)
|
| 1203 |
|
| 1204 |
# B7 — split the surface mask into connected regions; all share the same
|
|
@@ -1218,7 +1292,7 @@ def build_segmentation_bundle(contents: bytes):
|
|
| 1218 |
continue
|
| 1219 |
|
| 1220 |
# Per-region confidence sub-map
|
| 1221 |
-
region_conf = build_confidence_map(region_mask)
|
| 1222 |
|
| 1223 |
segments.append({
|
| 1224 |
"id": region_idx,
|
|
|
|
| 959 |
kern_size = max(5, min(h, w) // 160) | 1
|
| 960 |
kern = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (kern_size, kern_size))
|
| 961 |
occluder_mask = np.isin(seg_map, class_ids(OCCLUDER_CLASSES)).astype(np.uint8)
|
| 962 |
+
occ_raw = occluder_mask # kept for the confidence hole-fill below
|
| 963 |
+
|
| 964 |
+
# T8 — segmentation logits are upsampled, so occluder boundaries arrive
|
| 965 |
+
# stair-stepped (worst on curtain hems and plant fronds). Gaussian blur +
|
| 966 |
+
# 0.5 re-threshold collapses the staircase (the kernel must span the stair
|
| 967 |
+
# period or the level-set just reproduces it). The blur also erases thin
|
| 968 |
+
# structures, so two guards restore them verbatim: whole components that
|
| 969 |
+
# lost most of their area (free-standing legs, stems), and any large
|
| 970 |
+
# removed chunk of the raw mask (legs attached to a surviving body).
|
| 971 |
+
# Jaggy teeth are small chunks and stay removed.
|
| 972 |
+
smooth_k = max(13, min(h, w) // 100) | 1
|
| 973 |
+
occ_smooth = (
|
| 974 |
+
cv2.GaussianBlur(occluder_mask.astype(np.float32), (smooth_k, smooth_k), 0) >= 0.5
|
| 975 |
+
).astype(np.uint8)
|
| 976 |
+
n_comp, comp_labels, comp_stats, _ = cv2.connectedComponentsWithStats(occluder_mask)
|
| 977 |
+
if n_comp > 1:
|
| 978 |
+
surviving = np.bincount(
|
| 979 |
+
(comp_labels * occ_smooth.astype(np.int64)).ravel(), minlength=n_comp
|
| 980 |
+
)
|
| 981 |
+
lost = [
|
| 982 |
+
c for c in range(1, n_comp)
|
| 983 |
+
if surviving[c] * 2 < comp_stats[c, cv2.CC_STAT_AREA]
|
| 984 |
+
]
|
| 985 |
+
if lost:
|
| 986 |
+
occ_smooth[np.isin(comp_labels, lost)] = 1
|
| 987 |
+
removed = cv2.bitwise_and(occluder_mask, cv2.bitwise_not(occ_smooth))
|
| 988 |
+
n_rem, rem_labels, rem_stats, _ = cv2.connectedComponentsWithStats(removed)
|
| 989 |
+
if n_rem > 1:
|
| 990 |
+
chunk_min = max(30, smooth_k * smooth_k // 4)
|
| 991 |
+
big = [
|
| 992 |
+
c for c in range(1, n_rem)
|
| 993 |
+
if rem_stats[c, cv2.CC_STAT_AREA] >= chunk_min
|
| 994 |
+
]
|
| 995 |
+
if big:
|
| 996 |
+
occ_smooth[np.isin(rem_labels, big)] = 1
|
| 997 |
+
occluder_mask = occ_smooth
|
| 998 |
|
| 999 |
# One dilation pass instead of two — keeps the occluder boundary tight so
|
| 1000 |
# furniture feet don't leave a visible halo on the replaced tile surface.
|
|
|
|
| 1050 |
surface[occ_dilated > 0] = 0
|
| 1051 |
surface[reject_dilated > 0] = 0
|
| 1052 |
|
| 1053 |
+
# B5's bottom-edge contact stamp used to live here; the T7 perimeter
|
| 1054 |
+
# reclaim below brings the tile to a uniform 1px margin on ALL occluder
|
| 1055 |
+
# sides, which supersedes it — and the stamp scalloped the tile edge
|
| 1056 |
+
# wherever the boundary wiggled, since side-edge column minima read as
|
| 1057 |
+
# "bottoms". The 1px margin at feet is the contact-shadow line, so the
|
| 1058 |
+
# furniture still sits flush visually.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1059 |
|
| 1060 |
# P1-6 — the dilated reject band also ate a thin strip of true floor below
|
| 1061 |
# the skirting, letting the original floor peek out along the back wall.
|
|
|
|
| 1064 |
# raw reject or occluder pixels).
|
| 1065 |
reclaim = cv2.bitwise_and(floor_mask, cv2.bitwise_not(reject_mask))
|
| 1066 |
reclaim[occ_dilated > 0] = 0
|
| 1067 |
+
# Extend the band 1px past reject_dilated: the boundary erode above pulled
|
| 1068 |
+
# the surface 1px further back, which otherwise leaves a 1px line of the
|
| 1069 |
+
# original floor along the wall.
|
| 1070 |
+
wall_band = cv2.bitwise_and(reclaim, cv2.dilate(reject_dilated, boundary_kern, iterations=1))
|
| 1071 |
surface = cv2.bitwise_or(surface, wall_band)
|
| 1072 |
|
| 1073 |
+
# T7 — occ_dilated removes a kernel-wide ring of TRUE floor around every
|
| 1074 |
+
# occluder, and only the bottom-edge contact zone was restored above; the
|
| 1075 |
+
# rest of the ring shows the original floor as a 2-5px fringe around
|
| 1076 |
+
# furniture and curtains. Reclaim that ring up to a 1px safety margin from
|
| 1077 |
+
# the (smoothed) occluder so the tile runs flush; the margin keeps tile off
|
| 1078 |
+
# the occluder's anti-aliased edge pixels, and the shade map repaints the
|
| 1079 |
+
# natural contact shadow on top of the tile.
|
| 1080 |
+
occ_safe = cv2.dilate(occluder_mask, boundary_kern, iterations=1)
|
| 1081 |
+
# The boundary erode above also pulled the surface 1px further back from
|
| 1082 |
+
# the occluder hole, so the reclaim band must extend 1px past occ_dilated
|
| 1083 |
+
# or that erosion ring stays untiled.
|
| 1084 |
+
occ_ring = cv2.dilate(occ_dilated, boundary_kern, iterations=1)
|
| 1085 |
+
fringe = cv2.bitwise_and(floor_mask, occ_ring)
|
| 1086 |
+
fringe = cv2.bitwise_and(fringe, cv2.bitwise_not(occ_safe))
|
| 1087 |
+
fringe = cv2.bitwise_and(fringe, cv2.bitwise_not(reject_mask))
|
| 1088 |
+
fringe[reject_dilated > 0] = 0
|
| 1089 |
+
surface = cv2.bitwise_or(surface, fringe)
|
| 1090 |
+
|
| 1091 |
+
# The occluder zone is handed to the confidence map so occlusion holes are
|
| 1092 |
+
# treated as hard depth edges rather than feathered boundaries. Union with
|
| 1093 |
+
# the RAW mask: smoothing (T8) shaves jagged teeth off the segmentation
|
| 1094 |
+
# boundary, and those orphan pixels would otherwise read as holes that
|
| 1095 |
+
# erode confidence beside furniture.
|
| 1096 |
+
return surface, cv2.bitwise_or(occ_dilated, occ_raw)
|
| 1097 |
|
| 1098 |
|
| 1099 |
# ---------------------------------------------------------------------------
|
|
|
|
| 1103 |
# boundary transitions instead of a hard cut.
|
| 1104 |
# ---------------------------------------------------------------------------
|
| 1105 |
|
| 1106 |
+
def build_confidence_map(
|
| 1107 |
+
surface_mask: np.ndarray, hard_edge_mask: np.ndarray | None = None
|
| 1108 |
+
) -> np.ndarray | None:
|
| 1109 |
if not surface_mask.any():
|
| 1110 |
return None
|
| 1111 |
+
fill = surface_mask.astype(np.uint8)
|
| 1112 |
+
if hard_edge_mask is not None:
|
| 1113 |
+
# T7 — occlusion boundaries are true depth edges: the tile must stay
|
| 1114 |
+
# fully opaque right up to the furniture/curtain silhouette. Fill the
|
| 1115 |
+
# occluder holes before the distance transform so only outer
|
| 1116 |
+
# floor↔wall boundaries get the soft feather; feathering the occluder
|
| 1117 |
+
# ring let the original floor bleed back in as a translucent fringe.
|
| 1118 |
+
fill = cv2.bitwise_or(fill, hard_edge_mask.astype(np.uint8))
|
| 1119 |
+
# Stray 1-2px holes between the tile edge and the occluder (left by
|
| 1120 |
+
# mask smoothing) are not real boundaries — close them so they don't
|
| 1121 |
+
# punch translucent dots into the feather.
|
| 1122 |
+
close_kern = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
|
| 1123 |
+
fill = cv2.morphologyEx(fill, cv2.MORPH_CLOSE, close_kern)
|
| 1124 |
+
dist = cv2.distanceTransform(fill, cv2.DIST_L2, 5)
|
| 1125 |
# Narrow feather: a ~6px transition instead of ~26px. The wide feather made
|
| 1126 |
# a thick translucent ring at every boundary, which is what let the bright
|
| 1127 |
# background show through as a halo. Keep the blend confined to a thin edge.
|
| 1128 |
feather = max(4.0, min(surface_mask.shape[:2]) / 200.0)
|
| 1129 |
confidence = np.clip(dist / feather, 0.0, 1.0)
|
| 1130 |
+
confidence[surface_mask == 0] = 0.0
|
| 1131 |
return (confidence * 255).astype(np.uint8)
|
| 1132 |
|
| 1133 |
|
|
|
|
| 1255 |
|
| 1256 |
t0 = time.perf_counter()
|
| 1257 |
quad = np.asarray(plane["quad"], dtype=np.float32).reshape(4, 2) if plane and plane.get("quad") else None
|
| 1258 |
+
surface_mask, occluder_zone = build_floor_surface_mask(floor_mask, seg_map, quad, depth)
|
| 1259 |
print(f"[TIMING] Surface masking took {time.perf_counter() - t0:.3f} seconds", flush=True)
|
| 1260 |
|
| 1261 |
t0 = time.perf_counter()
|
|
|
|
| 1272 |
t0 = time.perf_counter()
|
| 1273 |
color_temperature = estimate_color_temperature(img_np, surface_mask) # B2
|
| 1274 |
light_vector = estimate_light_vector(shade_map, surface_mask) # B3
|
| 1275 |
+
confidence_map = build_confidence_map(surface_mask, occluder_zone) # B10
|
| 1276 |
print(f"[TIMING] Lighting analysis took {time.perf_counter() - t0:.3f} seconds", flush=True)
|
| 1277 |
|
| 1278 |
# B7 — split the surface mask into connected regions; all share the same
|
|
|
|
| 1292 |
continue
|
| 1293 |
|
| 1294 |
# Per-region confidence sub-map
|
| 1295 |
+
region_conf = build_confidence_map(region_mask, occluder_zone)
|
| 1296 |
|
| 1297 |
segments.append({
|
| 1298 |
"id": region_idx,
|
verify_p1v2_sim.py
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
verify_p1v2_sim.py — P1-1 v2 certification against the deployed P1-1.
|
| 3 |
+
|
| 4 |
+
deployed (left): global 4-tap seamless blend + per-cell random flips.
|
| 5 |
+
Field result: ghost medallions on stone, mushy wood grain,
|
| 6 |
+
mirror joint lines on planks.
|
| 7 |
+
v2 (right): masked-shift wrap — interior byte-identical to the source,
|
| 8 |
+
only a ~12% border band crossfades; plain repeat, no flips.
|
| 9 |
+
|
| 10 |
+
Usage:
|
| 11 |
+
python verify_p1v2_sim.py <bundle.json> <tile> <out_prefix>
|
| 12 |
+
"""
|
| 13 |
+
|
| 14 |
+
import base64
|
| 15 |
+
import io
|
| 16 |
+
import json
|
| 17 |
+
import sys
|
| 18 |
+
|
| 19 |
+
import numpy as np
|
| 20 |
+
from PIL import Image
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def make_seamless_4tap(tex):
|
| 24 |
+
h, w, _ = tex.shape
|
| 25 |
+
half_w, half_h = w // 2, h // 2
|
| 26 |
+
|
| 27 |
+
def win(n):
|
| 28 |
+
t = 1 - np.abs(2 * np.arange(n) / (n - 1) - 1)
|
| 29 |
+
return t * t * (3 - 2 * t)
|
| 30 |
+
|
| 31 |
+
wx = win(w)[None, :, None]
|
| 32 |
+
wy = win(h)[:, None, None]
|
| 33 |
+
t0 = tex.astype(np.float32)
|
| 34 |
+
t1 = np.roll(t0, -half_w, axis=1)
|
| 35 |
+
t2 = np.roll(t0, -half_h, axis=0)
|
| 36 |
+
t3 = np.roll(t1, -half_h, axis=0)
|
| 37 |
+
out = t0 * (wx * wy) + t1 * ((1 - wx) * wy) + t2 * (wx * (1 - wy)) + t3 * ((1 - wx) * (1 - wy))
|
| 38 |
+
return np.clip(out, 0, 255).astype(np.uint8)
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def make_wrappable_masked_shift(tex):
|
| 42 |
+
h, w, _ = tex.shape
|
| 43 |
+
band_x = max(2, round(w * 0.12))
|
| 44 |
+
band_y = max(2, round(h * 0.12))
|
| 45 |
+
|
| 46 |
+
def edge(n, band):
|
| 47 |
+
i = np.arange(n)
|
| 48 |
+
t = np.minimum(np.minimum(i, n - 1 - i) / band, 1.0)
|
| 49 |
+
return t * t * (3 - 2 * t)
|
| 50 |
+
|
| 51 |
+
m = (edge(w, band_x)[None, :] * edge(h, band_y)[:, None])[..., None]
|
| 52 |
+
shifted = np.roll(np.roll(tex, -(w // 2), axis=1), -(h // 2), axis=0).astype(np.float32)
|
| 53 |
+
out = tex.astype(np.float32) * m + shifted * (1 - m)
|
| 54 |
+
return np.clip(out, 0, 255).astype(np.uint8)
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def soft_clip(v, knee=220.0):
|
| 58 |
+
rng = 255.0 - knee
|
| 59 |
+
t = np.maximum(v - knee, 0.0)
|
| 60 |
+
return np.where(v <= knee, v, knee + t * rng / (t + rng))
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def sample_bilinear_wrap(tex, x, y):
|
| 64 |
+
h, w, _ = tex.shape
|
| 65 |
+
x0 = np.floor(x).astype(int) % w
|
| 66 |
+
y0 = np.floor(y).astype(int) % h
|
| 67 |
+
x1, y1 = (x0 + 1) % w, (y0 + 1) % h
|
| 68 |
+
fx = (x - np.floor(x))[:, None]
|
| 69 |
+
fy = (y - np.floor(y))[:, None]
|
| 70 |
+
t = tex.astype(np.float32)
|
| 71 |
+
return (t[y0, x0] * (1 - fx) * (1 - fy) + t[y0, x1] * fx * (1 - fy)
|
| 72 |
+
+ t[y1, x0] * (1 - fx) * fy + t[y1, x1] * fx * fy)
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def render(bundle, tex_raw, mode):
|
| 76 |
+
w, h = bundle["width"], bundle["height"]
|
| 77 |
+
raw = base64.b64decode(bundle["pixels"])
|
| 78 |
+
if len(raw) == w * h * 4:
|
| 79 |
+
img = np.frombuffer(raw, np.uint8).reshape(h, w, 4)[:, :, :3].copy()
|
| 80 |
+
else:
|
| 81 |
+
img = np.array(Image.open(io.BytesIO(raw)).convert("RGB"))
|
| 82 |
+
seg = bundle["segments"][0]
|
| 83 |
+
idx = np.frombuffer(base64.b64decode(seg["mask"]), dtype=np.uint32)
|
| 84 |
+
H = np.array(seg["homography"], dtype=np.float64).reshape(3, 3)
|
| 85 |
+
p = seg["plane"]
|
| 86 |
+
|
| 87 |
+
tex = make_seamless_4tap(tex_raw) if mode == "p1" else make_wrappable_masked_shift(tex_raw)
|
| 88 |
+
texH, texW = tex.shape[:2]
|
| 89 |
+
|
| 90 |
+
ys, xs = idx // w, idx % w
|
| 91 |
+
pw = p["width"]
|
| 92 |
+
cx, cy = p["x"] + pw / 2, p["y"] + p["height"] / 2
|
| 93 |
+
repeat_w = max(32.0, pw * 0.18)
|
| 94 |
+
repeat_h = repeat_w * (texH / texW)
|
| 95 |
+
|
| 96 |
+
pts = np.column_stack([xs, ys, np.ones(len(xs))]) @ H.T
|
| 97 |
+
fx = pts[:, 0] / pts[:, 2]
|
| 98 |
+
fy = pts[:, 1] / pts[:, 2]
|
| 99 |
+
cu = (fx - cx) / repeat_w
|
| 100 |
+
cv = (fy - cy) / repeat_h
|
| 101 |
+
|
| 102 |
+
u = cu - np.floor(cu)
|
| 103 |
+
v = cv - np.floor(cv)
|
| 104 |
+
if mode == "p1": # deployed: per-cell random flips
|
| 105 |
+
ci = np.floor(cu).astype(np.int64)
|
| 106 |
+
cj = np.floor(cv).astype(np.int64)
|
| 107 |
+
hsh = ((ci * 73856093) ^ (cj * 19349663)).astype(np.uint32)
|
| 108 |
+
u = np.where(hsh & 1, 1 - u, u)
|
| 109 |
+
v = np.where(hsh & 2, 1 - v, v)
|
| 110 |
+
sample = sample_bilinear_wrap(tex, u * texW, v * texH)
|
| 111 |
+
|
| 112 |
+
sr = seg.get("shadeRange") or [0.55, 1.35]
|
| 113 |
+
sm = np.frombuffer(base64.b64decode(seg["shadeMap"]), dtype=np.uint8)
|
| 114 |
+
shade = sr[0] + (sm[idx].astype(np.float32) / 255.0) * (sr[1] - sr[0])
|
| 115 |
+
|
| 116 |
+
lit = soft_clip(sample * shade[:, None])
|
| 117 |
+
out = img.copy()
|
| 118 |
+
out[ys, xs] = np.clip(lit, 0, 255).astype(np.uint8)
|
| 119 |
+
return out
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
def main():
|
| 123 |
+
bundle_path, tile_path, prefix = sys.argv[1], sys.argv[2], sys.argv[3]
|
| 124 |
+
with open(bundle_path) as f:
|
| 125 |
+
bundle = json.load(f)
|
| 126 |
+
tex = np.array(Image.open(tile_path).convert("RGB"))
|
| 127 |
+
print(f"tile: {tile_path} {tex.shape}")
|
| 128 |
+
|
| 129 |
+
a = render(bundle, tex, "p1")
|
| 130 |
+
b = render(bundle, tex, "v2")
|
| 131 |
+
Image.fromarray(np.hstack([a, b])).save(f"verify_out/{prefix}_v2_compare.png")
|
| 132 |
+
print(f"saved verify_out/{prefix}_v2_compare.png (left=deployed P1, right=v2)")
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
if __name__ == "__main__":
|
| 136 |
+
main()
|
verify_p2_sim.py
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
verify_p2_sim.py — synthetic certification for the P2 edge-quality fixes.
|
| 3 |
+
|
| 4 |
+
T7: the dilated occluder ring used to leave 2-5px of ORIGINAL floor around
|
| 5 |
+
furniture, and the confidence feather made the tile translucent there.
|
| 6 |
+
Expect: tile reclaimed up to a 1px margin, full opacity at depth edges,
|
| 7 |
+
feather kept only at the floor↔wall boundary.
|
| 8 |
+
T8: stair-stepped segmentation boundaries on occluders.
|
| 9 |
+
Expect: rounded edges, thin components (chair legs) never erased.
|
| 10 |
+
|
| 11 |
+
Runs the REAL functions extracted from app.py against a synthetic room,
|
| 12 |
+
asserts the geometry, and renders an old-vs-new composite.
|
| 13 |
+
|
| 14 |
+
Usage:
|
| 15 |
+
python verify_p2_sim.py
|
| 16 |
+
"""
|
| 17 |
+
|
| 18 |
+
import numpy as np
|
| 19 |
+
import cv2
|
| 20 |
+
from PIL import Image
|
| 21 |
+
|
| 22 |
+
# --- extract the real implementations from app.py -------------------------
|
| 23 |
+
src = open("app.py").read()
|
| 24 |
+
ns = {
|
| 25 |
+
"np": np,
|
| 26 |
+
"cv2": cv2,
|
| 27 |
+
"OCCLUDER_CLASSES": {"occ"},
|
| 28 |
+
"REJECT_SURFACE_CLASSES": {"rej"},
|
| 29 |
+
"class_ids": lambda names: [10] if "occ" in names else [20],
|
| 30 |
+
}
|
| 31 |
+
for fn in ["clean_floor_mask", "build_floor_surface_mask", "build_confidence_map"]:
|
| 32 |
+
start = src.index(f"def {fn}")
|
| 33 |
+
end = src.index("\ndef ", start + 10)
|
| 34 |
+
exec(compile(src[start:end], "app.py", "exec"), ns)
|
| 35 |
+
|
| 36 |
+
# --- synthetic room --------------------------------------------------------
|
| 37 |
+
H, W = 600, 800
|
| 38 |
+
FLOOR_Y = 320
|
| 39 |
+
seg = np.zeros((H, W), np.int32)
|
| 40 |
+
seg[:FLOOR_Y, :] = 20 # wall
|
| 41 |
+
|
| 42 |
+
# sofa with stair-stepped right edge (4px steps every 6 rows)
|
| 43 |
+
for y in range(250, 450):
|
| 44 |
+
step = 4 * ((y // 6) % 2)
|
| 45 |
+
seg[y, 100:300 + step] = 10
|
| 46 |
+
|
| 47 |
+
# thin 3px free-standing chair leg
|
| 48 |
+
seg[350:430, 500:503] = 10
|
| 49 |
+
|
| 50 |
+
# thin 3px leg ATTACHED to the sofa body (blur erases it, body survives —
|
| 51 |
+
# must be restored by the removed-chunk guard, not the component guard)
|
| 52 |
+
seg[450:480, 150:153] = 10
|
| 53 |
+
|
| 54 |
+
# curtain with zigzag hem
|
| 55 |
+
for x in range(600, 700):
|
| 56 |
+
hem = 380 - 10 * ((x // 8) % 2)
|
| 57 |
+
seg[0:hem, x] = 10
|
| 58 |
+
|
| 59 |
+
floor_mask = ((seg == 0) & (np.arange(H)[:, None] >= FLOOR_Y)).astype(np.uint8)
|
| 60 |
+
|
| 61 |
+
surface, occ_zone = ns["build_floor_surface_mask"](floor_mask, seg, None, None)
|
| 62 |
+
conf_new = ns["build_confidence_map"](surface, occ_zone)
|
| 63 |
+
conf_old = ns["build_confidence_map"](surface, None)
|
| 64 |
+
occ_raw = (seg == 10).astype(np.uint8)
|
| 65 |
+
|
| 66 |
+
# --- assertions ------------------------------------------------------------
|
| 67 |
+
def first_surface_right_of(y, x_edge):
|
| 68 |
+
row = surface[y, x_edge + 1:]
|
| 69 |
+
nz = np.flatnonzero(row)
|
| 70 |
+
return (x_edge + 1 + nz[0]) if len(nz) else None
|
| 71 |
+
|
| 72 |
+
print("== T7: fringe gap + opacity at depth edges ==")
|
| 73 |
+
for y in (380, 400, 420):
|
| 74 |
+
edge = 300 + 4 * ((y // 6) % 2) - 1 # sofa's true right edge
|
| 75 |
+
fx = first_surface_right_of(y, edge)
|
| 76 |
+
gap = fx - edge - 1
|
| 77 |
+
print(f" y={y}: gap={gap}px conf_new={conf_new[y, fx]} conf_old={conf_old[y, fx]}")
|
| 78 |
+
assert gap <= 3, "fringe gap should be <=3px"
|
| 79 |
+
assert conf_new[y, fx] == 255, "tile must be opaque at the depth edge"
|
| 80 |
+
assert conf_old[y, fx] < 200, "old feather should have been translucent here"
|
| 81 |
+
|
| 82 |
+
print("== T7: floor-wall boundary keeps its feather, no gap line ==")
|
| 83 |
+
col = 50
|
| 84 |
+
ys = np.flatnonzero(surface[:, col])
|
| 85 |
+
top = ys[0]
|
| 86 |
+
ramp = [int(conf_new[top + d, col]) for d in range(0, 9)]
|
| 87 |
+
print(f" top surface row at x={col}: y={top}, conf ramp: {ramp}")
|
| 88 |
+
assert conf_new[top, col] < 200, "outer boundary must still feather"
|
| 89 |
+
assert 0 not in ramp, "no untiled gap line inside the wall feather"
|
| 90 |
+
assert ramp[-1] == 255, "feather must finish within ~8px"
|
| 91 |
+
assert all(b >= a for a, b in zip(ramp, ramp[1:])), "ramp must be monotonic"
|
| 92 |
+
|
| 93 |
+
print("== T8: free-standing leg survives smoothing, no tile painted on it ==")
|
| 94 |
+
leg = surface[355:425, 500:503]
|
| 95 |
+
assert leg.sum() == 0, "tile must not cover the leg"
|
| 96 |
+
assert occ_zone[390, 501] > 0, "leg must remain in the occluder zone"
|
| 97 |
+
fx = first_surface_right_of(390, 502)
|
| 98 |
+
print(f" leg untouched, tile resumes {fx - 503}px right of it")
|
| 99 |
+
assert fx - 503 <= 3
|
| 100 |
+
|
| 101 |
+
print("== T8: ATTACHED leg survives via removed-chunk guard ==")
|
| 102 |
+
att = surface[455:478, 150:153]
|
| 103 |
+
assert att.sum() == 0, "tile must not cover the attached leg"
|
| 104 |
+
assert occ_zone[465, 151] > 0, "attached leg must remain in the occluder zone"
|
| 105 |
+
print(" attached leg untouched")
|
| 106 |
+
|
| 107 |
+
print("== T8: boundary roughness (perimeter ratio, lower = smoother) ==")
|
| 108 |
+
def perimeter(m):
|
| 109 |
+
cs, _ = cv2.findContours(m, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
|
| 110 |
+
return sum(cv2.arcLength(c, True) for c in cs)
|
| 111 |
+
# re-run just the T8 blur (without the guards) for the perimeter metric
|
| 112 |
+
smooth_k = max(13, min(H, W) // 100) | 1
|
| 113 |
+
occ_smooth = (cv2.GaussianBlur(occ_raw.astype(np.float32), (smooth_k, smooth_k), 0) >= 0.5).astype(np.uint8)
|
| 114 |
+
p_raw, p_smooth = perimeter(occ_raw), perimeter(occ_smooth)
|
| 115 |
+
print(f" k={smooth_k}: perimeter raw={p_raw:.0f} smoothed={p_smooth:.0f} ({p_smooth / p_raw:.2f}x)")
|
| 116 |
+
assert p_smooth < p_raw * 0.92, "staircase must actually collapse"
|
| 117 |
+
|
| 118 |
+
print("== T8: tile edge beside the sofa is straighter than the raw stairs ==")
|
| 119 |
+
tile_edge = [np.flatnonzero(surface[y, 250:340])[0] for y in range(370, 430)]
|
| 120 |
+
amp = max(tile_edge) - min(tile_edge)
|
| 121 |
+
print(f" tile-edge amplitude over 60 rows: {amp}px (raw stair amplitude: 4px)")
|
| 122 |
+
assert amp <= 2, "tile edge must be straighter than the raw staircase"
|
| 123 |
+
|
| 124 |
+
print("== T7: furniture feet sit flush (small gap below sofa bottom) ==")
|
| 125 |
+
below = np.flatnonzero(surface[450:, 200])
|
| 126 |
+
gap = below[0] if len(below) else 99
|
| 127 |
+
print(f" first tile row below sofa bottom: {gap}px")
|
| 128 |
+
assert gap <= 3, "feet must sit within 3px of the tile"
|
| 129 |
+
|
| 130 |
+
# --- composite render: old vs new ------------------------------------------
|
| 131 |
+
room = np.full((H, W, 3), (150, 110, 70), np.uint8) # warm original floor
|
| 132 |
+
room[:FLOOR_Y] = (210, 205, 195) # wall
|
| 133 |
+
room[occ_raw > 0] = (45, 40, 38) # dark furniture
|
| 134 |
+
|
| 135 |
+
yy, xx = np.mgrid[0:H, 0:W]
|
| 136 |
+
checker = (((yy // 24) + (xx // 24)) % 2).astype(bool)
|
| 137 |
+
tile = np.where(checker[..., None], (235, 235, 230), (200, 200, 195)).astype(np.uint8)
|
| 138 |
+
|
| 139 |
+
def composite(conf):
|
| 140 |
+
a = (conf.astype(np.float32) / 255.0)[..., None]
|
| 141 |
+
out = room.astype(np.float32) * (1 - a) + tile.astype(np.float32) * a
|
| 142 |
+
return out.astype(np.uint8)
|
| 143 |
+
|
| 144 |
+
old, new = composite(conf_old), composite(conf_new)
|
| 145 |
+
side = np.hstack([old, new])
|
| 146 |
+
Image.fromarray(side).save("verify_out/p2_compare.png")
|
| 147 |
+
crop = np.hstack([old[330:470, 250:560], new[330:470, 250:560]])
|
| 148 |
+
crop = cv2.resize(crop, None, fx=2.5, fy=2.5, interpolation=cv2.INTER_NEAREST)
|
| 149 |
+
Image.fromarray(crop).save("verify_out/p2_compare_crop.png")
|
| 150 |
+
print("saved verify_out/p2_compare.png + p2_compare_crop.png (left=old, right=new)")
|
| 151 |
+
print("ALL P2 CHECKS PASSED")
|