@@ -24,7 +24,7 @@ def __init__(self, model, file_path):
24
24
25
25
def _parse_csv (self ):
26
26
self .df = pd .read_csv (self .file_path )
27
- self ._remove_duplicates ()
27
+ # self._remove_duplicates()
28
28
self ._average_3D_points ()
29
29
30
30
self ._set_camera_list ()
@@ -47,7 +47,6 @@ def _set_observations(self):
47
47
# Create a mapping from camera IDs to indices
48
48
camera_id_to_index = {str (camera_id ): idx for idx , camera_id in enumerate (self .list_cameras )}
49
49
50
-
51
50
# Iterate through the DataFrame to collect observations
52
51
for _ , row in self .df .iterrows ():
53
52
cam0 , pt0 = str (row ['cam0' ]), row ['pt0' ]
@@ -82,9 +81,13 @@ def _average_3D_points(self):
82
81
# Merge the averaged columns back into the original DataFrame
83
82
self .df = self .df .merge (grouped , on = 'ts_local_coords' , how = 'left' )
84
83
84
+ # Create a mapping of ts_local_coords to index in the averaged points
85
+ self .df ['point_index' ] = self .df .groupby ('ts_local_coords' ).ngroup ()
86
+
85
87
# Write the updated DataFrame back to the CSV file
86
88
self .df .to_csv (self .file_path , index = False )
87
89
90
+
88
91
def _remove_duplicates (self ):
89
92
# Drop duplicate rows based on 'ts_local_coords', 'global_x', 'global_y', 'global_z' columns
90
93
logger .debug (f"Original rows: { self .df .shape [0 ]} " )
@@ -182,22 +185,31 @@ def optimize(self, print_result=True):
182
185
self .opt_points = opt_params [12 * n_cams :].reshape (n_pts , 3 )
183
186
184
187
if print_result :
185
- print (f"\n Optimization completed." )
188
+ print (f"\n ************** Optimization completed. ************************** " )
186
189
# Compute initial residuals
187
190
initial_residuals = self .residuals (initial_params )
188
191
initial_residuals_sum = np .sum (initial_residuals ** 2 )
189
192
average_residual = initial_residuals_sum / len (self .bal_problem .observations )
190
- print (f"Before BA, Average residual: { average_residual } " )
193
+ print (f"** Before BA, Average residual of reproj : { np . round ( average_residual , 2 ) } ** " )
191
194
192
195
# Compute Optimize residuals
193
196
opt_residuals = self .residuals (opt_params )
194
197
opt_residuals_sum = np .sum (opt_residuals ** 2 )
195
198
average_residual = opt_residuals_sum / len (self .bal_problem .observations )
196
- print (f"After BA, Average residual: { average_residual } " )
199
+ print (f"** After BA, Average residual of reproj: { np .round (average_residual , 2 )} **" )
200
+ print (f"******************************************************************" )
197
201
198
202
logger .debug (f"Optimized camera parameters: { self .opt_camera_params } " )
199
203
200
204
for i in range (len (self .bal_problem .points )):
201
205
logger .debug (f"\n Point { i } " )
202
206
logger .debug (f"org : { self .bal_problem .points [i ]} " )
203
- logger .debug (f"opt : { self .opt_points [i ]} " )
207
+ logger .debug (f"opt : { self .opt_points [i ]} " )
208
+
209
+ # Map optimized points to the original DataFrame rows
210
+ opt_points_df = pd .DataFrame (self .opt_points , columns = ['opt_x' , 'opt_y' , 'opt_z' ])
211
+ self .bal_problem .df = self .bal_problem .df .join (opt_points_df , on = 'point_index' , rsuffix = '_opt' )
212
+
213
+ # Save the updated DataFrame to the CSV file
214
+ self .bal_problem .df .to_csv (self .bal_problem .file_path , index = False )
215
+ logger .info (f"Optimized points saved to { self .bal_problem .file_path } " )
0 commit comments