@@ -37,21 +37,23 @@ def __init__(self, map_path, config=default_config):
3737 head_logging ('XRLocalization' )
3838 self .config = config
3939 database_path = os .path .join (map_path , 'database.bin' )
40-
40+
4141 if os .path .exists (database_path ):
4242 self .database = ImageDatabase (database_path )
4343 self .database .create ()
4444 else :
45- pairs = [name for name in os .listdir (map_path )
46- if name .startswith ('pairs-query' )]
45+ pairs = [
46+ name for name in os .listdir (map_path )
47+ if name .startswith ('pairs-query' )
48+ ]
4749 if len (pairs ) == 0 :
4850 raise ValueError (
4951 'Not found database under map: {}' .format (map_path ))
5052 else :
5153 self .pairs = PairsDatabase (os .path .join (map_path , pairs [0 ]))
5254
5355 self .reconstruction = Reconstruction (map_path )
54-
56+
5557 if hasattr (self , 'database' ):
5658 self .gextractor = Extractor (self .config ['global_feature' ])
5759 self .lextractor = Extractor (self .config ['local_feature' ])
@@ -61,8 +63,9 @@ def __init__(self, map_path, config=default_config):
6163 config_logging (self .config )
6264
6365 if self .config ['mode' ] == '2D2D' and self .config ['matcher' ] == 'gam' :
64- raise ValueError ('Loc mode {} is not compatible with matcher {}' .format (
65- self .config ['mode' ], self .config ['matcher' ]))
66+ raise ValueError (
67+ 'Loc mode {} is not compatible with matcher {}' .format (
68+ self .config ['mode' ], self .config ['matcher' ]))
6669
6770 head_logging ('Init Success' )
6871
@@ -91,15 +94,19 @@ def geo_localize(self, data):
9194 if hasattr (self , 'database' ):
9295 image_feature = self .gextractor .extract (data )
9396 image_ids = self .database .retrieve (image_feature ,
94- self .config ['retrieval_num' ])
97+ self .config ['retrieval_num' ])
9598 return image_ids
9699 elif hasattr (self , 'pairs' ) and isinstance (data , str ):
97- image_names = self .pairs .image_retrieve (data , self .config ['retrieval_num' ])
98- image_ids = [self .reconstruction .name_to_id (name ) for name in image_names ]
100+ image_names = self .pairs .image_retrieve (
101+ data , self .config ['retrieval_num' ])
102+ image_ids = [
103+ self .reconstruction .name_to_id (name ) for name in image_names
104+ ]
99105 return np .array ([id for id in image_ids if id != - 1 ])
100106
101107 def feature_match_2d3d (self , query_points , query_point_descriptors ,
102- train_points , train_point_descriptors , width , height ):
108+ train_points , train_point_descriptors , width ,
109+ height ):
103110 """Feature matching phase."""
104111 query_feat = {
105112 'shape' : np .array ([height , width ]),
@@ -114,58 +121,57 @@ def feature_match_2d3d(self, query_points, query_point_descriptors,
114121 return pred ['matches' ], pred ['scores' ]
115122
116123 def establish_correspondences_2d2d (self , query_feat , image_ids ):
117- '''Establish 2D-3D correspondences depend on 2D2D matching
118- '''
124+ """Establish 2D-3D correspondences depend on 2D2D matching."""
119125 logging .info ('Scene size: {0}' .format (len (image_ids )))
120- match_indices = np .ones (len (query_feat ['points' ]), dtype = int )* - 1
126+ match_indices = np .ones (len (query_feat ['points' ]), dtype = int ) * - 1
121127 match_priors = np .zeros (len (query_feat ['points' ]))
122128 for image_id in image_ids :
123129 ref_image = self .reconstruction .image_at (image_id )
124130 ref_feat = {
125- 'points' : ref_image .xys ,
126- 'descs' : self .reconstruction .point3d_features (ref_image .point3D_ids ),
127- 'scores' : np .ones (len (ref_image .xys )),
128- 'shape' : np .array ([600 , 600 ]) # TODO
131+ 'points' :
132+ ref_image .xys ,
133+ 'descs' :
134+ self .reconstruction .point3d_features (ref_image .point3D_ids ),
135+ 'scores' :
136+ np .ones (len (ref_image .xys )),
137+ 'shape' :
138+ np .array ([600 , 600 ]) # TODO
129139 }
130140 pred = self .matcher .match (query_feat , ref_feat )
131141 matches , scores = pred ['matches' ], pred ['scores' ]
132-
142+
133143 reserve_matches = matches [:, scores > match_priors [matches [0 ]]]
134144 reserve_scores = scores [scores > match_priors [matches [0 ]]]
135145 if len (reserve_scores ) > 0 :
136- match_indices [reserve_matches [0 ]] = ref_image .point3D_ids [reserve_matches [1 ]]
146+ match_indices [reserve_matches [0 ]] = ref_image .point3D_ids [
147+ reserve_matches [1 ]]
137148 match_priors [reserve_matches [0 ]] = reserve_scores
138149
139150 if len (match_priors [match_indices != - 1 ]) > 400 :
140151 break
141152
142153 point3d_ids = match_indices [match_indices != - 1 ]
143- points3d = self .reconstruction .point3d_coordinates (
144- point3d_ids )
154+ points3d = self .reconstruction .point3d_coordinates (point3d_ids )
145155 points2d = query_feat ['points' ][match_indices != - 1 ]
146156 priors = match_priors [match_indices != - 1 ]
147157 logging .info ('Match number: {0}' .format (len (priors )))
148158 return points2d , points3d , priors
149159
150-
151160 def establish_correspondences_2d3d (self , feat , image_ids ):
152- '''Establish 2D-3D correspondences depend on 2D3D matching
153- '''
161+ """Establish 2D-3D correspondences depend on 2D3D matching."""
154162 point3d_ids = self .reconstruction .visible_points (image_ids )
155- point3ds = self .reconstruction .point3d_coordinates (
156- point3d_ids )
157- point3d_descs = self .reconstruction .point3d_features (
158- point3d_ids )
159- logging .info ('3d points size: {0}' .format (
160- point3d_descs .shape [1 ]))
163+ point3ds = self .reconstruction .point3d_coordinates (point3d_ids )
164+ point3d_descs = self .reconstruction .point3d_features (point3d_ids )
165+ logging .info ('3d points size: {0}' .format (point3d_descs .shape [1 ]))
161166
162167 # Matching
163- matches , priors = self .feature_match_2d3d (feat ['points' ],
164- feat ['descs' ],
165- point3ds ,
166- point3d_descs ,
167- feat ['shape' ][1 ], # width
168- feat ['shape' ][0 ])
168+ matches , priors = self .feature_match_2d3d (
169+ feat ['points' ],
170+ feat ['descs' ],
171+ point3ds ,
172+ point3d_descs ,
173+ feat ['shape' ][1 ], # width
174+ feat ['shape' ][0 ])
169175 logging .info ('Match number: {0}' .format (matches .shape [1 ]))
170176 points2d = feat ['points' ][matches [0 ]]
171177 points3d = point3ds [matches [1 ]]
@@ -202,19 +208,22 @@ def refine_localize(self, image, camera, ref_image_ids):
202208 logging .info ('Coarse location number: {0}' .format (len (scenes )))
203209
204210 best_ret = {
205- 'ninlier' : 0 , 'qvec' : np .array ([1 , 0 , 0 , 0 ]),
206- 'tvec' : np .array ([0 , 0 , 0 ]), 'mask' : None
211+ 'ninlier' : 0 ,
212+ 'qvec' : np .array ([1 , 0 , 0 , 0 ]),
213+ 'tvec' : np .array ([0 , 0 , 0 ]),
214+ 'mask' : None
207215 }
208216 for i , image_ids in enumerate (scenes [:self .config ['max_scene_num' ]]):
209217 # Establish 2D-3D correspondences
210218 if self .config ['mode' ] == '2D3D' :
211- points2d , points3d , priors = self . establish_correspondences_2d3d (
212- feat , image_ids )
219+ points2d , points3d , priors = \
220+ self . establish_correspondences_2d3d ( feat , image_ids )
213221 elif self .config ['mode' ] == '2D2D' :
214- points2d , points3d , priors = self . establish_correspondences_2d2d (
215- feat , image_ids )
222+ points2d , points3d , priors = \
223+ self . establish_correspondences_2d2d ( feat , image_ids )
216224
217- if len (priors ) < 3 : continue
225+ if len (priors ) < 3 :
226+ continue
218227
219228 # Pose estimation
220229 ret = self .prior_guided_pose_estimation (points2d , points3d , priors ,
@@ -226,4 +235,4 @@ def refine_localize(self, image, camera, ref_image_ids):
226235 best_ret = ret
227236 if best_ret ['ninlier' ] > self .config ['max_inlier' ]:
228237 break
229- return best_ret
238+ return best_ret
0 commit comments