Download services/direction/analysis.py from modventures/floor-visualizer-gpu: direct link, hf CLI and curl.
- Browser
- Download file 3.36 kB
-
https://huggingface.co/spaces/modventures/floor-visualizer-gpu/resolve/main/services/direction/analysis.py
- Command line
-
hf download hf://spaces/modventures/floor-visualizer-gpu/services/direction/analysis.py
-
curl -L -o analysis.py https://huggingface.co/spaces/modventures/floor-visualizer-gpu/resolve/main/services/direction/analysis.py
3.36 kB
| """Floor direction geometry helpers.""" | |
| import numpy as np | |
| from services.geometry.camera import default_camera_intrinsics | |
| from services.geometry.multi_plane import geometry_point_maps | |
| def floor_direction_geometry_inputs( | |
| surface_mask: np.ndarray, | |
| geometry: dict | None, | |
| plane: dict | None, | |
| surface_mapping: dict | None, | |
| *, | |
| use_surface_uv: bool, | |
| geometry_maps: dict | None = None, | |
| ) -> dict: | |
| direction_maps = surface_mapping | |
| if direction_maps is None: | |
| raw_maps = geometry_maps if geometry_maps is not None else geometry_point_maps(surface_mask, geometry) | |
| if raw_maps is not None: | |
| direction_maps = { | |
| "normalMap": raw_maps["normals"], | |
| "validMask": raw_maps["validMask"], | |
| "intrinsics": raw_maps["intrinsics"], | |
| "planes": [], | |
| } | |
| normal_map = direction_maps.get("normalMap") if direction_maps else None | |
| valid_mask = direction_maps.get("validMask") if direction_maps else None | |
| intrinsics = direction_maps.get("intrinsics") if direction_maps else None | |
| surface_planes = direction_maps.get("planes") or [] if direction_maps else [] | |
| dominant_floor_plane = None | |
| if surface_planes: | |
| floor_planes = [item for item in surface_planes if item.get("isFloor")] | |
| candidates = floor_planes or surface_planes | |
| dominant_floor_plane = max( | |
| candidates, | |
| key=lambda item: int(item.get("assignedCount") or item.get("inlierCount") or 0), | |
| ) | |
| floor_normal = None | |
| render_u_axis = None | |
| render_v_axis = None | |
| if use_surface_uv and dominant_floor_plane is not None: | |
| floor_normal = dominant_floor_plane.get("normal") | |
| render_u_axis = dominant_floor_plane.get("uAxis") | |
| render_v_axis = dominant_floor_plane.get("vAxis") | |
| elif plane and plane.get("planeNormal"): | |
| floor_normal = plane.get("planeNormal") | |
| elif dominant_floor_plane is not None: | |
| floor_normal = dominant_floor_plane.get("normal") | |
| if floor_normal is None and normal_map is not None and normal_map.shape[:2] == surface_mask.shape: | |
| lengths = np.linalg.norm(normal_map, axis=2) | |
| valid_floor = ( | |
| (surface_mask > 0) | |
| & np.isfinite(normal_map).all(axis=2) | |
| & (lengths > 1e-4) | |
| ) | |
| if valid_mask is not None and valid_mask.shape == surface_mask.shape: | |
| valid_floor &= valid_mask.astype(bool) | |
| floor_normals = normal_map[valid_floor] | |
| if len(floor_normals) >= 200: | |
| floor_normals = floor_normals / np.linalg.norm(floor_normals, axis=1, keepdims=True) | |
| reference = np.median(floor_normals, axis=0) | |
| if reference[1] < 0: | |
| reference = -reference | |
| floor_normal = reference.tolist() | |
| if intrinsics is None and plane and plane.get("cameraIntrinsics"): | |
| intrinsics = np.asarray(plane["cameraIntrinsics"], dtype=np.float32).reshape(3, 3) | |
| if intrinsics is None: | |
| height, width = surface_mask.shape[:2] | |
| intrinsics = default_camera_intrinsics(width, height) | |
| return { | |
| "normalsMap": normal_map, | |
| "validMask": valid_mask, | |
| "intrinsics": intrinsics, | |
| "floorNormal": floor_normal, | |
| "renderUAxis": render_u_axis, | |
| "renderVAxis": render_v_axis, | |
| } | |