1- # pyre-unsafe
1+ # pyre-strict
22import logging
33import math
44import time
1010import opensfm .synthetic_data .synthetic_dataset as sd
1111import scipy .signal as signal
1212import scipy .spatial as spatial
13+ from numpy .typing import NDArray
1314from opensfm import (
1415 features as oft ,
1516 geo ,
2526logger : logging .Logger = logging .getLogger (__name__ )
2627
2728
28- def derivative (func : Callable , x : np . ndarray ) -> np . ndarray :
29+ def derivative (func : Callable [[ float ], NDArray ], x : float ) -> NDArray :
2930 eps = 1e-10
3031 d = (func (x + eps ) - func (x )) / eps
3132 d /= np .linalg .norm (d )
3233 return d
3334
3435
35- def samples_generator_random_count (count : int ) -> np . ndarray :
36+ def samples_generator_random_count (count : int ) -> NDArray :
3637 return np .random .rand (count )
3738
3839
3940def samples_generator_interval (
4041 length : float , end : float , interval : float , interval_noise : float
41- ) -> np . ndarray :
42+ ) -> NDArray :
4243 samples = np .linspace (0 , end / length , num = int (end / interval ))
4344 samples += np .random .normal (
4445 0.0 , float (interval_noise ) / float (length ), samples .shape
@@ -47,8 +48,8 @@ def samples_generator_interval(
4748
4849
4950def generate_samples_and_local_frame (
50- samples : np . ndarray , shape : Callable
51- ) -> Tuple [np . ndarray , np . ndarray ]:
51+ samples : NDArray , shape : Callable [[ float ], NDArray ]
52+ ) -> Tuple [NDArray , NDArray ]:
5253 points = []
5354 tangents = []
5455 for i in samples :
@@ -61,8 +62,8 @@ def generate_samples_and_local_frame(
6162
6263
6364def generate_samples_shifted (
64- samples : np . ndarray , shape : Callable , shift : float
65- ) -> np . ndarray :
65+ samples : NDArray , shape : Callable [[ float ], NDArray ] , shift : float
66+ ) -> NDArray :
6667 plane_points = []
6768 for i in samples :
6869 point = shape (i )
@@ -74,8 +75,8 @@ def generate_samples_shifted(
7475
7576
7677def generate_z_plane (
77- samples : np . ndarray , shape : Callable , thickness : float
78- ) -> np . ndarray :
78+ samples : NDArray , shape : Callable [[ float ], NDArray ] , thickness : float
79+ ) -> NDArray :
7980 plane_points = []
8081 for i in samples :
8182 point = shape (i )
@@ -89,8 +90,8 @@ def generate_z_plane(
8990
9091
9192def generate_xy_planes (
92- samples : np . ndarray , shape : Callable , z_size : float , y_size : float
93- ) -> np . ndarray :
93+ samples : NDArray , shape : Callable [[ float ], NDArray ] , z_size : float , y_size : float
94+ ) -> NDArray :
9495 xy1 = generate_samples_shifted (samples , shape , y_size )
9596 xy2 = generate_samples_shifted (samples , shape , - y_size )
9697 xy1 = np .insert (xy1 , 2 , values = np .random .rand (xy1 .shape [0 ]) * z_size , axis = 1 )
@@ -99,16 +100,16 @@ def generate_xy_planes(
99100
100101
101102def generate_street (
102- samples : np . ndarray , shape : Callable , height : float , width : float
103- ) -> Tuple [np . ndarray , np . ndarray ]:
103+ samples : NDArray , shape : Callable [[ float ], NDArray ] , height : float , width : float
104+ ) -> Tuple [NDArray , NDArray ]:
104105 walls = generate_xy_planes (samples , shape , height , width )
105106 floor = generate_z_plane (samples , shape , width )
106107 return walls , floor
107108
108109
109110def generate_cameras (
110- samples : np . ndarray , shape : Callable , height : float
111- ) -> Tuple [np . ndarray , np . ndarray ]:
111+ samples : NDArray , shape : Callable [[ float ], NDArray ] , height : float
112+ ) -> Tuple [NDArray , NDArray ]:
112113 positions , rotations = generate_samples_and_local_frame (samples , shape )
113114 positions = np .insert (positions , 2 , values = height , axis = 1 )
114115 rotations = np .insert (rotations , 2 , values = 0 , axis = 2 )
@@ -117,8 +118,8 @@ def generate_cameras(
117118
118119
119120def line_generator (
120- length : float , center_x : float , center_y : float , transpose : bool , point : np . ndarray
121- ) -> np . ndarray :
121+ length : float , center_x : float , center_y : float , transpose : bool , point : float
122+ ) -> NDArray :
122123 x = point * length
123124 if transpose :
124125 return np .transpose (
@@ -133,13 +134,13 @@ def line_generator(
133134 return np .transpose (np .array ([x + center_x , center_y ]))
134135
135136
136- def ellipse_generator (x_size : float , y_size : float , point : float ) -> np . ndarray :
137+ def ellipse_generator (x_size : float , y_size : float , point : float ) -> NDArray :
137138 y = np .sin (point * 2 * np .pi ) * y_size / 2
138139 x = np .cos (point * 2 * np .pi ) * x_size / 2
139140 return np .transpose (np .array ([x , y ]))
140141
141142
142- def perturb_points (points : np . ndarray , sigmas : List [float ]) -> None :
143+ def perturb_points (points : NDArray , sigmas : List [float ]) -> None :
143144 eps = 1e-10
144145 gaussian = np .array ([max (s , eps ) for s in sigmas ])
145146 for point in points :
@@ -148,7 +149,7 @@ def perturb_points(points: np.ndarray, sigmas: List[float]) -> None:
148149
149150def generate_causal_noise (
150151 dimensions : int , sigma : float , n : int , scale : float
151- ) -> List [np . ndarray ]:
152+ ) -> List [NDArray ]:
152153 dims = [np .arange (- scale , scale ) for _ in range (dimensions )]
153154 mesh = np .meshgrid (* dims )
154155 dist = np .linalg .norm (mesh , axis = 0 )
@@ -254,7 +255,7 @@ def _gps_dop(shot: pymap.Shot) -> float:
254255 return exifs
255256
256257
257- def perturb_rotations (rotations : np . ndarray , angle_sigma : float ) -> None :
258+ def perturb_rotations (rotations : NDArray , angle_sigma : float ) -> None :
258259 for i in range (len (rotations )):
259260 rotation = rotations [i ]
260261 rodrigues = cv2 .Rodrigues (rotation )[0 ].ravel ()
@@ -265,7 +266,7 @@ def perturb_rotations(rotations: np.ndarray, angle_sigma: float) -> None:
265266
266267
267268def add_points_to_reconstruction (
268- points : np . ndarray , color : np . ndarray , reconstruction : types .Reconstruction
269+ points : NDArray , color : NDArray , reconstruction : types .Reconstruction
269270) -> None :
270271 shift = len (reconstruction .points )
271272 for i in range (points .shape [0 ]):
@@ -275,8 +276,8 @@ def add_points_to_reconstruction(
275276
276277def add_shots_to_reconstruction (
277278 shots : List [List [str ]],
278- positions : List [np . ndarray ],
279- rotations : List [np . ndarray ],
279+ positions : List [NDArray ],
280+ rotations : List [NDArray ],
280281 rig_cameras : List [pymap .RigCamera ],
281282 cameras : List [pygeometry .Camera ],
282283 reconstruction : types .Reconstruction ,
@@ -308,13 +309,13 @@ def add_shots_to_reconstruction(
308309
309310
310311def create_reconstruction (
311- points : List [np . ndarray ],
312- colors : List [np . ndarray ],
312+ points : List [NDArray ],
313+ colors : List [NDArray ],
313314 cameras : List [List [pygeometry .Camera ]],
314315 shot_ids : List [List [str ]],
315316 rig_shots : List [List [List [Tuple [str , str ]]]],
316- rig_positions : List [np . ndarray ],
317- rig_rotations : List [np . ndarray ],
317+ rig_positions : List [NDArray ],
318+ rig_rotations : List [NDArray ],
318319 rig_cameras : List [List [pymap .RigCamera ]],
319320 reference : Optional [geo .TopocentricConverter ],
320321) -> Reconstruction :
@@ -355,7 +356,7 @@ def generate_track_data(
355356 projection_noise : float ,
356357 gcp_noise : Tuple [float , float ],
357358 gcps_count : Optional [int ],
358- gcp_shift : Optional [np . ndarray ],
359+ gcp_shift : Optional [NDArray ],
359360 on_disk_features_filename : Optional [str ],
360361) -> Tuple [
361362 sd .SyntheticFeatures , pymap .TracksManager , Dict [str , pymap .GroundControlPoint ]
@@ -490,15 +491,15 @@ def generate_track_data(
490491 return features , tracks_manager , gcps
491492
492493
493- def _is_in_front (point : np . ndarray , center : np . ndarray , z_axis : np . ndarray ) -> bool :
494+ def _is_in_front (point : NDArray , center : NDArray , z_axis : NDArray ) -> bool :
494495 return (
495496 (point [0 ] - center [0 ]) * z_axis [0 ]
496497 + (point [1 ] - center [1 ]) * z_axis [1 ]
497498 + (point [2 ] - center [2 ]) * z_axis [2 ]
498499 ) > 0
499500
500501
501- def _is_inside_camera (projection : np . ndarray , camera : pygeometry .Camera ) -> bool :
502+ def _is_inside_camera (projection : NDArray , camera : pygeometry .Camera ) -> bool :
502503 w , h = float (camera .width ), float (camera .height )
503504 w2 = float (2 * camera .width )
504505 h2 = float (2 * camera .height )
0 commit comments