constructelligence commited on
Commit
2bcd27a
·
verified ·
1 Parent(s): 326de32

Upload test_depth_fusion.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. test_depth_fusion.py +130 -0
test_depth_fusion.py ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """Tests for depth_fusion.py on depth rendered from known geometry. Run: python3 test_depth_fusion.py
3
+
4
+ Scenes are the camera-projected rooms of test_wall_measure.py (feet in the world),
5
+ with a depth map ray-cast against the true wall, ceiling, floor and side-wall
6
+ planes, then corrupted the way a phone LiDAR / ToF sensor is: 5 mm noise, 3%
7
+ dropouts, and glass that returns the scene behind it.
8
+ """
9
+ import numpy as np
10
+
11
+ import test_wall_measure as tw
12
+ from depth_fusion import measure_with_depth
13
+ from wall_measure import PAINTED, SKIRTING, WINDOW
14
+
15
+ FT = 0.3048
16
+ K = (tw.F, tw.F, tw.W / 2, tw.H / 2)
17
+
18
+
19
+ def render_depth(cam, planes, noise_m=0.005, dropout=0.03, seed=0):
20
+ """Depth (metres, OpenCV z) of the nearest of ``planes`` = [(normal, offset_ft, bounds)] per pixel."""
21
+ r, c = cam
22
+ yy, xx = np.mgrid[0:tw.H, 0:tw.W] + 0.0
23
+ d_cam = np.stack([(xx - tw.W / 2) / tw.F, -(yy - tw.H / 2) / tw.F, np.ones_like(xx)], -1) # test camera: y up
24
+ d_world = d_cam @ r # R^T d for row vectors
25
+ best = np.full((tw.H, tw.W), np.inf)
26
+ for normal, offset, bounds in planes:
27
+ normal = np.asarray(normal, float)
28
+ denom = d_world @ normal
29
+ with np.errstate(divide="ignore", invalid="ignore"):
30
+ t = -(c @ normal + offset) / denom
31
+ hit = c + d_world * t[..., None]
32
+ ok = (t > 0) & np.isfinite(t)
33
+ for axis, (lo, hi) in bounds.items():
34
+ ok &= (hit[..., axis] >= lo - 1e-6) & (hit[..., axis] <= hi + 1e-6)
35
+ best = np.where(ok & (t < best), t, best)
36
+ depth = best * FT # z component of d_cam is 1
37
+ rng = np.random.default_rng(seed)
38
+ depth = depth + rng.normal(0, noise_m, depth.shape)
39
+ depth[rng.random(depth.shape) < dropout] = 0.0
40
+ return depth
41
+
42
+
43
+ ROOM = [((0, 0, 1), 0.0, {0: (-30, 30), 1: (0, 8)}), # back wall z = 0
44
+ ((0, 1, 0), -8.0, {0: (-30, 30), 2: (-30, 0)}), # ceiling y = 8
45
+ ((0, 1, 0), 0.0, {0: (-30, 30), 2: (-30, 0)})] # floor y = 0
46
+
47
+
48
+ def test_single_wall_to_the_centimetre():
49
+ cam = tw.camera(yaw_deg=-25, pitch_deg=-5, x_offset=1.0, distance=12.0, eye_height=5.0)
50
+ labels = tw.scene(width=12.0, cam=cam)
51
+ # The wall in the labels is 12 ft wide; the room's back plane is wider, so cap it in depth too.
52
+ planes = [((0, 0, 1), 0.0, {0: (0, 12), 1: (0, 8)})] + ROOM[1:]
53
+ depth = render_depth(cam, planes)
54
+ depth[labels == WINDOW] += 2.5 # glass: the sensor sees through it
55
+ m = measure_with_depth(labels, depth, K)
56
+ assert m["status"] == "measured" and len(m["faces"]) == 1, m.get("reason")
57
+ face = m["faces"][0]
58
+ assert face["stop_line_trust"] == {"floor": "depth_planes", "top": "depth_planes", "bottom": "depth_planes"}
59
+ assert abs(face["width_mm"] - 12 * 304.8) < 20, face["width_mm"] # 12 ft within 2 cm
60
+ assert abs(face["height_mm"] - 8 * 304.8) < 10, face["height_mm"] # 8 ft within 1 cm
61
+ assert abs(face["skirting_height_in"] - 4.0) < 0.5, face["skirting_height_in"]
62
+ assert abs(face["area_ft2"]["openings"] - 12.0) < 0.3, face["area_ft2"]
63
+ assert face["plane"]["rms_mm"] < 8 and face["scale_source"] == "depth"
64
+ # Camera-frame stop line: the floor line lies 5 ft below the camera (eye height), pitched.
65
+ floor = np.array(face["stop_lines_camera_mm"]["floor"])
66
+ assert floor.shape == (2, 3)
67
+
68
+
69
+ def test_inside_corner_from_two_planes():
70
+ cam = tw.camera(yaw_deg=-38, x_offset=3.0, distance=9.0, eye_height=4.5)
71
+ back = tw.project(tw.rect(0, 10, 0, 8), cam)
72
+ side = tw.project([(10, 8, 0), (10, 8, -6), (10, 0, -6), (10, 0, 0)], cam)
73
+ labels = np.zeros((tw.H, tw.W), np.uint8)
74
+ tw.fill(labels, back, PAINTED)
75
+ tw.fill(labels, side, PAINTED)
76
+ planes = [((0, 0, 1), 0.0, {0: (-30, 10), 1: (0, 8)}),
77
+ ((1, 0, 0), -10.0, {1: (0, 8), 2: (-6, 0)}),
78
+ ((0, 1, 0), -8.0, {0: (-30, 10), 2: (-30, 0)}),
79
+ ((0, 1, 0), 0.0, {0: (-30, 10), 2: (-30, 0)})]
80
+ depth = render_depth(cam, planes, seed=1)
81
+ m = measure_with_depth(labels, depth, K)
82
+ assert len(m["faces"]) == 2, [f["plane"] for f in m["faces"]]
83
+ back_face, side_face = m["faces"]
84
+ assert back_face["ends"]["right"] == "inside_corner" and side_face["ends"]["left"] == "inside_corner"
85
+ # The corner is the true room corner: both faces stop at the same 3D line.
86
+ a = np.array(back_face["stop_lines_camera_mm"]["right"])
87
+ b = np.array(side_face["stop_lines_camera_mm"]["left"])
88
+ assert np.abs(a - b).max() < 25, (a, b)
89
+ assert abs(side_face["width_mm"] - 6 * 304.8) < 40, side_face["width_mm"] # side wall 6 ft visible
90
+ assert abs(back_face["height_mm"] - 8 * 304.8) < 15
91
+
92
+
93
+ def test_no_ceiling_in_view_falls_back_to_wall_extent():
94
+ cam = tw.camera(yaw_deg=-10, x_offset=6.0, distance=12.0, eye_height=4.0)
95
+ labels = tw.scene(width=12.0, cam=cam, window=None)
96
+ planes = [((0, 0, 1), 0.0, {0: (0, 12), 1: (0, 8)}), ROOM[2]] # floor only
97
+ depth = render_depth(cam, planes, seed=2)
98
+ face = measure_with_depth(labels, depth, K)["faces"][0]
99
+ assert face["stop_line_trust"]["top"] == "depth_extent" and face["stop_line_trust"]["floor"] == "depth_planes"
100
+ assert abs(face["height_mm"] - 8 * 304.8) < 40, face["height_mm"]
101
+
102
+
103
+ def test_too_little_depth_fails_cleanly():
104
+ labels = tw.scene()
105
+ m = measure_with_depth(labels, np.zeros((tw.H, tw.W)), K)
106
+ assert m["status"] == "depth_failed" and m["faces"] == []
107
+
108
+
109
+ def test_low_resolution_depth_is_aligned():
110
+ # Phone LiDAR is 256x192; the RGB frame is larger. Same field of view.
111
+ cam = tw.camera(yaw_deg=-15, x_offset=4.0, distance=12.0, eye_height=4.5)
112
+ labels = tw.scene(width=12.0, cam=cam)
113
+ planes = [((0, 0, 1), 0.0, {0: (0, 12), 1: (0, 8)})] + ROOM[1:]
114
+ depth = render_depth(cam, planes, seed=3)
115
+ small = depth[2::4, 2::4] # 300 x 225, pixel centres preserved
116
+ face = measure_with_depth(labels, small, K)["faces"][0]
117
+ assert abs(face["width_mm"] - 12 * 304.8) < 40, face["width_mm"]
118
+ assert abs(face["height_mm"] - 8 * 304.8) < 20, face["height_mm"]
119
+
120
+
121
+ def main():
122
+ tests = [value for name, value in sorted(globals().items()) if name.startswith("test_")]
123
+ for test in tests:
124
+ test()
125
+ print(f"ok {test.__name__}")
126
+ print(f"{len(tests)} tests passed")
127
+
128
+
129
+ if __name__ == "__main__":
130
+ main()