modventures commited on
Commit
20e8cd7
·
verified ·
1 Parent(s): 1ceb521

Dev Deploy: ba13dc9

Browse files
Files changed (1) hide show
  1. app.py +97 -0
app.py CHANGED
@@ -148,6 +148,33 @@ ENABLE_MULTI_PLANE_SURFACES = str(config_value(
148
  "enable_multi_plane_surfaces",
149
  "1",
150
  )).lower() in {"1", "true", "yes", "on"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
151
  INTRINSIC_MODEL_VERSION = str(config_value(
152
  "INTRINSIC_MODEL_VERSION",
153
  "models",
@@ -1857,6 +1884,67 @@ def active_model_metadata(geometry: dict | None, depth: np.ndarray | None, shade
1857
  }
1858
 
1859
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1860
  def build_segmentation_bundle(contents: bytes):
1861
  t_start = time.perf_counter()
1862
 
@@ -1910,6 +1998,13 @@ def build_segmentation_bundle(contents: bytes):
1910
  t0 = time.perf_counter()
1911
  surface_mapping = build_surface_uv_mapping(surface_mask, surface_indices, geometry)
1912
  use_surface_uv = bool(surface_mapping and surface_mapping.get("useSurfaceUv"))
 
 
 
 
 
 
 
1913
  mapping_metadata = (
1914
  surface_mapping["metadata"]
1915
  if surface_mapping is not None
@@ -1990,6 +2085,8 @@ def build_segmentation_bundle(contents: bytes):
1990
  "metricGeometry": bool(plane and plane.get("floorTransform")),
1991
  "geometryProvider": plane.get("geometryProvider", geometry.get("provider")) if plane else geometry.get("provider"),
1992
  "geometryModel": models["geometry"]["name"],
 
 
1993
  },
1994
  })
1995
 
 
148
  "enable_multi_plane_surfaces",
149
  "1",
150
  )).lower() in {"1", "true", "yes", "on"}
151
+ # Camera-height prior: correct MoGe-2's absolute-scale ambiguity (systematic
152
+ # "everything renders too big") by rescaling only the FE-facing UV outputs so
153
+ # the measured camera->floor distance matches a typical photo height.
154
+ ENABLE_CAMERA_HEIGHT_PRIOR = str(config_value(
155
+ "ENABLE_CAMERA_HEIGHT_PRIOR",
156
+ "runtime",
157
+ "enable_camera_height_prior",
158
+ "1",
159
+ )).lower() in {"1", "true", "yes", "on"}
160
+ CAMERA_HEIGHT_METERS = float(config_value(
161
+ "CAMERA_HEIGHT_METERS",
162
+ "runtime",
163
+ "camera_height_meters",
164
+ "1.45",
165
+ ))
166
+ CAMERA_HEIGHT_PRIOR_MIN_SCALE = float(config_value(
167
+ "CAMERA_HEIGHT_PRIOR_MIN_SCALE",
168
+ "runtime",
169
+ "camera_height_prior_min_scale",
170
+ "0.5",
171
+ ))
172
+ CAMERA_HEIGHT_PRIOR_MAX_SCALE = float(config_value(
173
+ "CAMERA_HEIGHT_PRIOR_MAX_SCALE",
174
+ "runtime",
175
+ "camera_height_prior_max_scale",
176
+ "2.5",
177
+ ))
178
  INTRINSIC_MODEL_VERSION = str(config_value(
179
  "INTRINSIC_MODEL_VERSION",
180
  "models",
 
1884
  }
1885
 
1886
 
1887
+ def apply_camera_height_prior(plane: dict | None, surface_mapping: dict | None):
1888
+ """Correct MoGe-2's absolute-scale ambiguity via a camera-height prior.
1889
+
1890
+ MoGe yields a confident floor *plane* but its global metric scale can drift,
1891
+ which makes tiles render uniformly too large (or small). The camera sits at
1892
+ the origin in MoGe point space, so the perpendicular camera->floor distance
1893
+ is ``|planeOrigin . planeNormal|``. We rescale the FE-facing UV outputs by
1894
+ ``CAMERA_HEIGHT_METERS / measured_height`` so that distance matches a typical
1895
+ photo height.
1896
+
1897
+ Only the FE-facing outputs are touched -- per-pixel ``surfaceUv`` (preferred
1898
+ render path) and the ``floorTransform`` homography (fallback path). All
1899
+ internal plane-fit / surface-RANSAC thresholds stay in native MoGe units so
1900
+ segmentation and plane fitting are unaffected. Returns the prior metadata
1901
+ (or ``None`` if no correction was applied).
1902
+ """
1903
+ if not ENABLE_CAMERA_HEIGHT_PRIOR or not plane:
1904
+ return None
1905
+ origin = plane.get("planeOrigin")
1906
+ normal = plane.get("planeNormal")
1907
+ if origin is None or normal is None:
1908
+ return None
1909
+
1910
+ origin = np.asarray(origin, dtype=np.float64)
1911
+ normal = np.asarray(normal, dtype=np.float64)
1912
+ nlen = float(np.linalg.norm(normal))
1913
+ if nlen < 1e-6:
1914
+ return None
1915
+ normal /= nlen
1916
+ measured_height = abs(float(origin @ normal))
1917
+ if not np.isfinite(measured_height) or measured_height < 1e-3:
1918
+ return None
1919
+
1920
+ scale = CAMERA_HEIGHT_METERS / measured_height
1921
+ scale = float(np.clip(scale, CAMERA_HEIGHT_PRIOR_MIN_SCALE, CAMERA_HEIGHT_PRIOR_MAX_SCALE))
1922
+ prior_meta = {
1923
+ "measuredHeight": round(measured_height, 4),
1924
+ "assumedHeight": CAMERA_HEIGHT_METERS,
1925
+ "scale": round(scale, 4),
1926
+ }
1927
+ if abs(scale - 1.0) < 1e-3:
1928
+ return prior_meta
1929
+
1930
+ # Scale the per-pixel surface UV (the preferred render path).
1931
+ if surface_mapping is not None and surface_mapping.get("uv") is not None:
1932
+ surface_mapping["uv"] = (surface_mapping["uv"].astype(np.float32) * scale).astype(np.float32)
1933
+
1934
+ # Scale the floorTransform homography output (fallback render path).
1935
+ # The FE evaluates uv = (H . p) / (w . p); multiplying the two numerator
1936
+ # rows (indices 0-5) by ``scale`` scales the output uv uniformly.
1937
+ ft = plane.get("floorTransform")
1938
+ if ft is not None and len(ft) == 9:
1939
+ scaled = [float(v) for v in ft]
1940
+ for idx in (0, 1, 2, 3, 4, 5):
1941
+ scaled[idx] *= scale
1942
+ plane["floorTransform"] = scaled
1943
+
1944
+ plane["cameraHeightPrior"] = prior_meta
1945
+ return prior_meta
1946
+
1947
+
1948
  def build_segmentation_bundle(contents: bytes):
1949
  t_start = time.perf_counter()
1950
 
 
1998
  t0 = time.perf_counter()
1999
  surface_mapping = build_surface_uv_mapping(surface_mask, surface_indices, geometry)
2000
  use_surface_uv = bool(surface_mapping and surface_mapping.get("useSurfaceUv"))
2001
+ camera_height_prior = apply_camera_height_prior(plane, surface_mapping)
2002
+ if camera_height_prior is not None:
2003
+ print(
2004
+ f"[TIMING] Camera-height prior: measured {camera_height_prior['measuredHeight']}m "
2005
+ f"-> scale {camera_height_prior['scale']}x (assumed {camera_height_prior['assumedHeight']}m)",
2006
+ flush=True,
2007
+ )
2008
  mapping_metadata = (
2009
  surface_mapping["metadata"]
2010
  if surface_mapping is not None
 
2085
  "metricGeometry": bool(plane and plane.get("floorTransform")),
2086
  "geometryProvider": plane.get("geometryProvider", geometry.get("provider")) if plane else geometry.get("provider"),
2087
  "geometryModel": models["geometry"]["name"],
2088
+ "cameraHeightPriorEnabled": ENABLE_CAMERA_HEIGHT_PRIOR,
2089
+ "cameraHeightPrior": camera_height_prior,
2090
  },
2091
  })
2092