Codex staging deploy commited on
Commit
10996cb
·
1 Parent(s): 0b146dd

Preview feature/realistic-floor-rendering at 6b85a88

Browse files
Files changed (1) hide show
  1. app.py +136 -13
app.py CHANGED
@@ -533,7 +533,10 @@ SHADE_MAP_ENCODING = "linear-relative"
533
  REFLECTION_RELATIVE_FLOOR = 1.04
534
  REFLECTION_RELATIVE_CEILING = 1.95
535
  REFLECTION_MAP_MIN = 0.0
536
- REFLECTION_MAP_MAX = 1.0
 
 
 
537
 
538
 
539
  def hf_offline() -> bool:
@@ -1593,8 +1596,10 @@ def build_luminance_lighting_maps(
1593
  shade = smooth_relative_lighting(relative, mask)
1594
  if shade is None:
1595
  return None
 
1596
  return {
1597
  "shadeMap": encode_relative_map(shade, SHADE_MAP_MIN, SHADE_MAP_MAX),
 
1598
  "showroomShadeMap": build_showroom_shade_map_from_relative(shade, mask),
1599
  "reflectionMap": build_reflection_map(
1600
  img_np,
@@ -1605,7 +1610,7 @@ def build_luminance_lighting_maps(
1605
  }
1606
 
1607
 
1608
- def extract_intrinsic_shading(results: dict) -> np.ndarray | None:
1609
  shading = None
1610
  if "gry_shd" in results:
1611
  shading = results["gry_shd"]
@@ -1634,7 +1639,96 @@ def extract_intrinsic_shading(results: dict) -> np.ndarray | None:
1634
  )
1635
  if shading.ndim != 2:
1636
  return None
1637
- return shading.astype(np.float32)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1638
 
1639
 
1640
  def normalize_relative_shading(
@@ -1853,8 +1947,15 @@ def build_intrinsic_lighting_maps(
1853
  )
1854
  print(f"[LIGHTING] intrinsic shading alignment={alignment}", flush=True)
1855
 
1856
- return {
1857
- "shadeMap": encode_relative_map(smoothed, SHADE_MAP_MIN, SHADE_MAP_MAX),
 
 
 
 
 
 
 
1858
  "showroomShadeMap": build_showroom_shade_map_from_relative(smoothed, surface_mask),
1859
  "reflectionMap": build_reflection_map(
1860
  img_np,
@@ -1862,7 +1963,11 @@ def build_intrinsic_lighting_maps(
1862
  smoothed,
1863
  replaced_material_mask,
1864
  ),
1865
- }
 
 
 
 
1866
  except Exception as exc:
1867
  print(f"Intrinsic shading decomposition failed: {exc}. Falling back to default luminance shading.", flush=True)
1868
  return None
@@ -3934,7 +4039,9 @@ def build_manifest_segmentation_bundle(
3934
  "surfacePlaneIds": ("surface-plane-ids.u8", "uint8"),
3935
  "shadeMap": ("shade.u8", "uint8"),
3936
  "showroomShadeMap": ("showroom-shade.u8", "uint8"),
3937
- "reflectionMap": ("reflection.u8", "uint8"),
 
 
3938
  }
3939
 
3940
  for segment_index, segment in enumerate(bundle.get("segments", [])):
@@ -4216,7 +4323,9 @@ def build_segmentation_bundle(contents: bytes, debug: PipelineDebugCapture | Non
4216
  lighting_maps = None
4217
  shade_map = None
4218
  showroom_shade_map = None
4219
- reflection_map = None
 
 
4220
  lighting_source = None
4221
  intrinsic_lighting_used = False
4222
  if ENABLE_SHADING_TRANSFER and ENABLE_INTRINSIC_SHADING:
@@ -4243,6 +4352,8 @@ def build_segmentation_bundle(contents: bytes, debug: PipelineDebugCapture | Non
4243
  shade_map = lighting_maps.get("shadeMap")
4244
  showroom_shade_map = lighting_maps.get("showroomShadeMap")
4245
  reflection_map = lighting_maps.get("reflectionMap")
 
 
4246
  shade_statistics = lighting_map_statistics(shade_map, surface_mask)
4247
  lighting_elapsed = time.perf_counter() - t0
4248
  print(
@@ -4374,10 +4485,15 @@ def build_segmentation_bundle(contents: bytes, debug: PipelineDebugCapture | Non
4374
  base64.b64encode(showroom_shade_map.tobytes()).decode()
4375
  if showroom_shade_map is not None else None
4376
  ),
4377
- "reflectionMap": (
4378
- base64.b64encode(reflection_map.tobytes()).decode()
4379
- if reflection_map is not None else None
4380
- ),
 
 
 
 
 
4381
  "metadata": {
4382
  "segmenter": segmenter_metadata_name(),
4383
  "segmentationModel": models["segmentation"]["name"],
@@ -4415,7 +4531,11 @@ def build_segmentation_bundle(contents: bytes, debug: PipelineDebugCapture | Non
4415
  "reflectionTransfer": True,
4416
  "edgeFeatherPixels": 2,
4417
  },
4418
- "reflectionTransferEnabled": reflection_map is not None,
 
 
 
 
4419
  "reflectionMapRange": [REFLECTION_MAP_MIN, REFLECTION_MAP_MAX],
4420
  "geometryConfidence": plane.get("geometryConfidence", 0.0) if plane else 0.0,
4421
  "metricGeometry": bool(plane and plane.get("floorTransform")),
@@ -4434,6 +4554,7 @@ def build_segmentation_bundle(contents: bytes, debug: PipelineDebugCapture | Non
4434
  wall_shade_map = wall_lighting_maps.get("shadeMap") if wall_lighting_maps else None
4435
  wall_showroom_shade_map = wall_lighting_maps.get("showroomShadeMap") if wall_lighting_maps else None
4436
  wall_reflection_map = wall_lighting_maps.get("reflectionMap") if wall_lighting_maps else None
 
4437
  segments.append({
4438
  "id": wall_index + 1,
4439
  "className": "wall",
@@ -4459,6 +4580,7 @@ def build_segmentation_bundle(contents: bytes, debug: PipelineDebugCapture | Non
4459
  base64.b64encode(wall_reflection_map.tobytes()).decode()
4460
  if wall_reflection_map is not None else None
4461
  ),
 
4462
  "metadata": {
4463
  "segmenter": segmenter_metadata_name(),
4464
  "segmentationModel": models["segmentation"]["name"],
@@ -4468,6 +4590,7 @@ def build_segmentation_bundle(contents: bytes, debug: PipelineDebugCapture | Non
4468
  "protectedPixels": int(wall_protected_mask.sum()),
4469
  "shadingEnabled": wall_shade_map is not None,
4470
  "reflectionTransferEnabled": wall_reflection_map is not None,
 
4471
  "textureMappingMode": "wall-plane" if wall_plane.get("surfaceTransform") else "wall-contour-homography",
4472
  "geometryConfidence": wall_plane.get("geometryConfidence", 0.0),
4473
  "geometryProvider": wall_plane.get("geometryProvider"),
 
533
  REFLECTION_RELATIVE_FLOOR = 1.04
534
  REFLECTION_RELATIVE_CEILING = 1.95
535
  REFLECTION_MAP_MIN = 0.0
536
+ REFLECTION_MAP_MAX = 1.0
537
+ COLOR_ILLUMINATION_MIN = 0.65
538
+ COLOR_ILLUMINATION_MAX = 1.35
539
+ SPECULAR_RESIDUAL_MAX = 0.20
540
 
541
 
542
  def hf_offline() -> bool:
 
1596
  shade = smooth_relative_lighting(relative, mask)
1597
  if shade is None:
1598
  return None
1599
+ shadow = np.clip(1.0 - np.maximum(1.0 - shade, 0.0) * 0.85, 0.15, 1.0)
1600
  return {
1601
  "shadeMap": encode_relative_map(shade, SHADE_MAP_MIN, SHADE_MAP_MAX),
1602
+ "shadowMap": np.round(shadow * 255.0).astype(np.uint8),
1603
  "showroomShadeMap": build_showroom_shade_map_from_relative(shade, mask),
1604
  "reflectionMap": build_reflection_map(
1605
  img_np,
 
1610
  }
1611
 
1612
 
1613
+ def extract_intrinsic_shading(results: dict) -> np.ndarray | None:
1614
  shading = None
1615
  if "gry_shd" in results:
1616
  shading = results["gry_shd"]
 
1639
  )
1640
  if shading.ndim != 2:
1641
  return None
1642
+ return shading.astype(np.float32)
1643
+
1644
+
1645
+ def build_intrinsic_color_illumination(
1646
+ results: dict,
1647
+ surface_mask: np.ndarray,
1648
+ ) -> np.ndarray | None:
1649
+ """Encode low-frequency RGB illumination chromaticity without source albedo."""
1650
+ shading = _to_numpy(results.get("hr_shd") if results.get("hr_shd") is not None else results.get("lr_shd"))
1651
+ if shading is None or shading.ndim != 3 or shading.shape[2] < 3:
1652
+ return None
1653
+ h, w = surface_mask.shape[:2]
1654
+ if shading.shape[:2] != (h, w):
1655
+ shading = cv2.resize(shading[:, :, :3].astype(np.float32), (w, h), interpolation=cv2.INTER_LINEAR)
1656
+ else:
1657
+ shading = shading[:, :, :3].astype(np.float32)
1658
+ shading = np.maximum(shading, 1e-4)
1659
+ luminance = (
1660
+ shading[:, :, 0] * 0.2126
1661
+ + shading[:, :, 1] * 0.7152
1662
+ + shading[:, :, 2] * 0.0722
1663
+ )
1664
+ chroma = shading / np.maximum(luminance[:, :, None], 1e-4)
1665
+ sigma = max(18.0, min(h, w) / 18.0)
1666
+ channels = [masked_gaussian_blur(chroma[:, :, channel], surface_mask, sigma) for channel in range(3)]
1667
+ broad_chroma = np.stack(channels, axis=2)
1668
+ valid = broad_chroma[surface_mask > 0]
1669
+ if valid.size < 768:
1670
+ return None
1671
+ median = np.median(valid, axis=0)
1672
+ broad_chroma /= np.maximum(median[None, None, :], 1e-4)
1673
+ broad_chroma = np.clip(broad_chroma, COLOR_ILLUMINATION_MIN, COLOR_ILLUMINATION_MAX)
1674
+ broad_chroma[surface_mask == 0] = 1.0
1675
+ encoded = (broad_chroma - COLOR_ILLUMINATION_MIN) / (
1676
+ COLOR_ILLUMINATION_MAX - COLOR_ILLUMINATION_MIN
1677
+ )
1678
+ return np.round(encoded * 255.0).clip(0, 255).astype(np.uint8)
1679
+
1680
+
1681
+ def build_intrinsic_specular_residual(
1682
+ results: dict,
1683
+ surface_mask: np.ndarray,
1684
+ replaced_material_mask: np.ndarray | None,
1685
+ ) -> np.ndarray | None:
1686
+ """Encode positive non-diffuse light while excluding replaced material regions."""
1687
+ residual = _to_numpy(results.get("pos_res"))
1688
+ if residual is None:
1689
+ raw = _to_numpy(results.get("residual"))
1690
+ residual = np.maximum(raw, 0.0) if raw is not None else None
1691
+ if residual is None:
1692
+ return None
1693
+ if residual.ndim == 2:
1694
+ residual = np.repeat(residual[:, :, None], 3, axis=2)
1695
+ if residual.ndim != 3 or residual.shape[2] < 3:
1696
+ return None
1697
+ h, w = surface_mask.shape[:2]
1698
+ residual = residual[:, :, :3].astype(np.float32)
1699
+ if residual.shape[:2] != (h, w):
1700
+ residual = cv2.resize(residual, (w, h), interpolation=cv2.INTER_LINEAR)
1701
+ residual = np.clip(residual, 0.0, SPECULAR_RESIDUAL_MAX)
1702
+ valid_mask = surface_mask > 0
1703
+ if replaced_material_mask is not None and replaced_material_mask.shape[:2] == (h, w):
1704
+ radius = max(3, int(round(min(h, w) / 80.0)))
1705
+ kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (radius * 2 + 1, radius * 2 + 1))
1706
+ valid_mask &= cv2.dilate((replaced_material_mask > 0).astype(np.uint8), kernel) == 0
1707
+ residual[~valid_mask] = 0.0
1708
+ sigma = max(0.8, min(h, w) / 520.0)
1709
+ residual = cv2.GaussianBlur(residual, (0, 0), sigmaX=sigma, sigmaY=sigma)
1710
+ if float(np.max(residual)) < 0.005:
1711
+ return None
1712
+ luminance = (
1713
+ residual[:, :, 0] * 0.2126
1714
+ + residual[:, :, 1] * 0.7152
1715
+ + residual[:, :, 2] * 0.0722
1716
+ )
1717
+ return np.round(luminance / SPECULAR_RESIDUAL_MAX * 255.0).clip(0, 255).astype(np.uint8)
1718
+
1719
+
1720
+ def pack_intrinsic_relighting_map(
1721
+ color_illumination: np.ndarray | None,
1722
+ specular_residual: np.ndarray | None,
1723
+ ) -> np.ndarray | None:
1724
+ if color_illumination is None:
1725
+ return None
1726
+ alpha = (
1727
+ specular_residual
1728
+ if specular_residual is not None
1729
+ else np.zeros(color_illumination.shape[:2], dtype=np.uint8)
1730
+ )
1731
+ return np.dstack((color_illumination[:, :, :3], alpha)).astype(np.uint8)
1732
 
1733
 
1734
  def normalize_relative_shading(
 
1947
  )
1948
  print(f"[LIGHTING] intrinsic shading alignment={alignment}", flush=True)
1949
 
1950
+ color_illumination = build_intrinsic_color_illumination(results, surface_mask)
1951
+ specular_residual = build_intrinsic_specular_residual(
1952
+ results,
1953
+ surface_mask,
1954
+ replaced_material_mask,
1955
+ )
1956
+ return {
1957
+ "shadeMap": encode_relative_map(smoothed, SHADE_MAP_MIN, SHADE_MAP_MAX),
1958
+ "shadowMap": np.round(np.clip(1.0 - np.maximum(1.0 - smoothed, 0.0) * 0.9, 0.12, 1.0) * 255.0).astype(np.uint8),
1959
  "showroomShadeMap": build_showroom_shade_map_from_relative(smoothed, surface_mask),
1960
  "reflectionMap": build_reflection_map(
1961
  img_np,
 
1963
  smoothed,
1964
  replaced_material_mask,
1965
  ),
1966
+ "intrinsicRelightingMap": pack_intrinsic_relighting_map(
1967
+ color_illumination,
1968
+ specular_residual,
1969
+ ),
1970
+ }
1971
  except Exception as exc:
1972
  print(f"Intrinsic shading decomposition failed: {exc}. Falling back to default luminance shading.", flush=True)
1973
  return None
 
4039
  "surfacePlaneIds": ("surface-plane-ids.u8", "uint8"),
4040
  "shadeMap": ("shade.u8", "uint8"),
4041
  "showroomShadeMap": ("showroom-shade.u8", "uint8"),
4042
+ "reflectionMap": ("reflection.u8", "uint8"),
4043
+ "shadowMap": ("shadow.u8", "uint8"),
4044
+ "intrinsicRelightingMap": ("intrinsic-relighting.rgba8", "uint8"),
4045
  }
4046
 
4047
  for segment_index, segment in enumerate(bundle.get("segments", [])):
 
4323
  lighting_maps = None
4324
  shade_map = None
4325
  showroom_shade_map = None
4326
+ reflection_map = None
4327
+ shadow_map = None
4328
+ intrinsic_relighting_map = None
4329
  lighting_source = None
4330
  intrinsic_lighting_used = False
4331
  if ENABLE_SHADING_TRANSFER and ENABLE_INTRINSIC_SHADING:
 
4352
  shade_map = lighting_maps.get("shadeMap")
4353
  showroom_shade_map = lighting_maps.get("showroomShadeMap")
4354
  reflection_map = lighting_maps.get("reflectionMap")
4355
+ shadow_map = lighting_maps.get("shadowMap")
4356
+ intrinsic_relighting_map = lighting_maps.get("intrinsicRelightingMap")
4357
  shade_statistics = lighting_map_statistics(shade_map, surface_mask)
4358
  lighting_elapsed = time.perf_counter() - t0
4359
  print(
 
4485
  base64.b64encode(showroom_shade_map.tobytes()).decode()
4486
  if showroom_shade_map is not None else None
4487
  ),
4488
+ "reflectionMap": (
4489
+ base64.b64encode(reflection_map.tobytes()).decode()
4490
+ if reflection_map is not None else None
4491
+ ),
4492
+ "shadowMap": base64.b64encode(shadow_map.tobytes()).decode() if shadow_map is not None else None,
4493
+ "intrinsicRelightingMap": (
4494
+ base64.b64encode(intrinsic_relighting_map.tobytes()).decode()
4495
+ if intrinsic_relighting_map is not None else None
4496
+ ),
4497
  "metadata": {
4498
  "segmenter": segmenter_metadata_name(),
4499
  "segmentationModel": models["segmentation"]["name"],
 
4531
  "reflectionTransfer": True,
4532
  "edgeFeatherPixels": 2,
4533
  },
4534
+ "reflectionTransferEnabled": reflection_map is not None,
4535
+ "shadowMapEnabled": shadow_map is not None,
4536
+ "intrinsicRelightingMapEnabled": intrinsic_relighting_map is not None,
4537
+ "colorIlluminationMapRange": [COLOR_ILLUMINATION_MIN, COLOR_ILLUMINATION_MAX],
4538
+ "specularResidualMapRange": [0.0, SPECULAR_RESIDUAL_MAX],
4539
  "reflectionMapRange": [REFLECTION_MAP_MIN, REFLECTION_MAP_MAX],
4540
  "geometryConfidence": plane.get("geometryConfidence", 0.0) if plane else 0.0,
4541
  "metricGeometry": bool(plane and plane.get("floorTransform")),
 
4554
  wall_shade_map = wall_lighting_maps.get("shadeMap") if wall_lighting_maps else None
4555
  wall_showroom_shade_map = wall_lighting_maps.get("showroomShadeMap") if wall_lighting_maps else None
4556
  wall_reflection_map = wall_lighting_maps.get("reflectionMap") if wall_lighting_maps else None
4557
+ wall_shadow_map = wall_lighting_maps.get("shadowMap") if wall_lighting_maps else None
4558
  segments.append({
4559
  "id": wall_index + 1,
4560
  "className": "wall",
 
4580
  base64.b64encode(wall_reflection_map.tobytes()).decode()
4581
  if wall_reflection_map is not None else None
4582
  ),
4583
+ "shadowMap": base64.b64encode(wall_shadow_map.tobytes()).decode() if wall_shadow_map is not None else None,
4584
  "metadata": {
4585
  "segmenter": segmenter_metadata_name(),
4586
  "segmentationModel": models["segmentation"]["name"],
 
4590
  "protectedPixels": int(wall_protected_mask.sum()),
4591
  "shadingEnabled": wall_shade_map is not None,
4592
  "reflectionTransferEnabled": wall_reflection_map is not None,
4593
+ "shadowMapEnabled": wall_shadow_map is not None,
4594
  "textureMappingMode": "wall-plane" if wall_plane.get("surfaceTransform") else "wall-contour-homography",
4595
  "geometryConfidence": wall_plane.get("geometryConfidence", 0.0),
4596
  "geometryProvider": wall_plane.get("geometryProvider"),