1- # pyre-unsafe
1+ # pyre-strict
22import glob
33import os
44import os .path
55import shutil
66import sys
7+ from typing import Any , Dict , Generator , List , Tuple
78
89import numpy as np
10+ from numpy .typing import NDArray
911from opensfm import config , io
1012from opensfm .dataset import DataSet
1113
1214
1315class MetaDataSet :
14- def __init__ (self , data_path ) :
16+ def __init__ (self , data_path : str ) -> None :
1517 """
1618 Create meta dataset instance for large scale reconstruction.
1719
1820 :param data_path: Path to directory containing meta dataset
1921 """
20- self .data_path = os .path .abspath (data_path )
22+ self .data_path : str = os .path .abspath (data_path )
2123
2224 config_file = os .path .join (self .data_path , "config.yaml" )
23- self .config = config .load_config (config_file )
25+ self .config : Dict [ str , Any ] = config .load_config (config_file )
2426
2527 self ._image_list_file_name = "image_list_with_gps.tsv"
2628 self ._clusters_file_name = "clusters.npz"
@@ -32,42 +34,42 @@ def __init__(self, data_path):
3234
3335 io .mkdir_p (self ._submodels_path ())
3436
35- def _submodels_path (self ):
37+ def _submodels_path (self ) -> str :
3638 return os .path .join (self .data_path , self .config ["submodels_relpath" ])
3739
38- def _submodel_path (self , i ) :
40+ def _submodel_path (self , i : int ) -> str :
3941 """Path to submodel i folder."""
4042 template = self .config ["submodel_relpath_template" ]
4143 return os .path .join (self .data_path , template % i )
4244
43- def _submodel_images_path (self , i ) :
45+ def _submodel_images_path (self , i : int ) -> str :
4446 """Path to submodel i images folder."""
4547 template = self .config ["submodel_images_relpath_template" ]
4648 return os .path .join (self .data_path , template % i )
4749
48- def _image_groups_path (self ):
50+ def _image_groups_path (self ) -> str :
4951 return os .path .join (self .data_path , "image_groups.txt" )
5052
51- def _image_list_path (self ):
53+ def _image_list_path (self ) -> str :
5254 return os .path .join (self ._submodels_path (), self ._image_list_file_name )
5355
54- def _clusters_path (self ):
56+ def _clusters_path (self ) -> str :
5557 return os .path .join (self ._submodels_path (), self ._clusters_file_name )
5658
57- def _clusters_with_neighbors_path (self ):
59+ def _clusters_with_neighbors_path (self ) -> str :
5860 return os .path .join (
5961 self ._submodels_path (), self ._clusters_with_neighbors_file_name
6062 )
6163
62- def _clusters_with_neighbors_geojson_path (self ):
64+ def _clusters_with_neighbors_geojson_path (self ) -> str :
6365 return os .path .join (
6466 self ._submodels_path (), self ._clusters_with_neighbors_geojson_file_name
6567 )
6668
67- def _clusters_geojson_path (self ):
69+ def _clusters_geojson_path (self ) -> str :
6870 return os .path .join (self ._submodels_path (), self ._clusters_geojson_file_name )
6971
70- def _create_symlink (self , base_path , file_path ) :
72+ def _create_symlink (self , base_path : str , file_path : str ) -> None :
7173 src = os .path .join (self .data_path , file_path )
7274 dst = os .path .join (base_path , file_path )
7375
@@ -104,59 +106,61 @@ def _create_symlink(self, base_path, file_path):
104106 os .path .join (* [".." ] * subfolders , os .path .relpath (src , base_path )), dst
105107 )
106108
107- def image_groups_exists (self ):
109+ def image_groups_exists (self ) -> bool :
108110 return os .path .isfile (self ._image_groups_path ())
109111
110- def load_image_groups (self ):
112+ def load_image_groups (self ) -> Generator [ List [ str ], None , None ] :
111113 with open (self ._image_groups_path ()) as fin :
112114 for line in fin :
113115 yield line .split ()
114116
115- def image_list_exists (self ):
117+ def image_list_exists (self ) -> bool :
116118 return os .path .isfile (self ._image_list_path ())
117119
118- def create_image_list (self , ills ) :
120+ def create_image_list (self , ills : List [ Tuple [ str , float , float ]]) -> None :
119121 with io .open_wt (self ._image_list_path ()) as csvfile :
120122 for image , lat , lon in ills :
121123 csvfile .write ("{}\t {}\t {}\n " .format (image , lat , lon ))
122124
123- def images_with_gps (self ):
125+ def images_with_gps (self ) -> Generator [ Tuple [ str , float , float ], None , None ] :
124126 with io .open_rt (self ._image_list_path ()) as csvfile :
125127 for line in csvfile :
126128 image , lat , lon = line .split ("\t " )
127129 yield image , float (lat ), float (lon )
128130
129- def save_clusters (self , images , positions , labels , centers ):
131+ def save_clusters (
132+ self , images : NDArray , positions : NDArray , labels : NDArray , centers : NDArray
133+ ) -> None :
130134 filepath = self ._clusters_path ()
131135 np .savez_compressed (
132136 filepath , images = images , positions = positions , labels = labels , centers = centers
133137 )
134138
135- def load_clusters (self ):
139+ def load_clusters (self ) -> Tuple [ NDArray , NDArray , NDArray , NDArray ] :
136140 c = np .load (self ._clusters_path ())
137141 images = c ["images" ].ravel ()
138142 labels = c ["labels" ].ravel ()
139143 return images , c ["positions" ], labels , c ["centers" ]
140144
141- def save_clusters_with_neighbors (self , clusters ) :
145+ def save_clusters_with_neighbors (self , clusters : List [ List [ int ]]) -> None :
142146 filepath = self ._clusters_with_neighbors_path ()
143147 np .savez_compressed (filepath , clusters = np .array (clusters , dtype = object ))
144148
145- def load_clusters_with_neighbors (self ):
149+ def load_clusters_with_neighbors (self ) -> NDArray :
146150 c = np .load (self ._clusters_with_neighbors_path (), allow_pickle = True )
147151 return c ["clusters" ]
148152
149- def save_cluster_with_neighbors_geojson (self , geojson ) :
153+ def save_cluster_with_neighbors_geojson (self , geojson : Dict [ str , Any ]) -> None :
150154 filepath = self ._clusters_with_neighbors_geojson_path ()
151155 with io .open_wt (filepath ) as fout :
152156 io .json_dump (geojson , fout )
153157
154- def save_clusters_geojson (self , geojson ) :
158+ def save_clusters_geojson (self , geojson : Dict [ str , Any ]) -> None :
155159 filepath = self ._clusters_geojson_path ()
156160 with io .open_wt (filepath ) as fout :
157161 io .json_dump (geojson , fout )
158162
159- def remove_submodels (self ):
163+ def remove_submodels (self ) -> None :
160164 sm = self ._submodels_path ()
161165 paths = [
162166 os .path .join (sm , o )
@@ -166,7 +170,7 @@ def remove_submodels(self):
166170 for path in paths :
167171 shutil .rmtree (path )
168172
169- def create_submodels (self , clusters ) :
173+ def create_submodels (self , clusters : NDArray ) -> None :
170174 data = DataSet (self .data_path )
171175 for i , cluster in enumerate (clusters ):
172176 # create sub model dirs
@@ -216,7 +220,7 @@ def create_submodels(self, clusters):
216220 for filepath in filepaths :
217221 self ._create_symlink (submodel_path , filepath )
218222
219- def get_submodel_paths (self ):
223+ def get_submodel_paths (self ) -> List [ str ] :
220224 submodel_paths = []
221225 for i in range (999999 ):
222226 submodel_path = self ._submodel_path (i )
0 commit comments