Skip to content

Commit

Permalink
ci(pre-commit): autofix
Browse files Browse the repository at this point in the history
  • Loading branch information
pre-commit-ci[bot] committed Nov 19, 2024
1 parent 2539a4b commit cf3e90f
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ def squared_error(p, p1, p2):
squared_distance = np.abs(np.power(np.linalg.norm(p), 2) - np.power(np.dot(p, p2), 2))
return squared_distance

if self._cached_linear_error_rows_rms is not None and \
self._cached_linear_error_cols_rms is not None:
if (
self._cached_linear_error_rows_rms is not None
and self._cached_linear_error_cols_rms is not None
):
return self._cached_linear_error_rows_rms, self._cached_linear_error_cols_rms

error_rows = 0
Expand Down Expand Up @@ -103,8 +105,12 @@ def squared_error(p, p1, p2):
self._cached_linear_error_cols_rms = np.sqrt(error_cols / (self.cols * (self.rows - 2)))
pct_err_cols = pct_err_cols / (self.cols * (self.rows - 2))

return self._cached_linear_error_rows_rms, self._cached_linear_error_cols_rms, \
pct_err_rows, pct_err_cols
return (
self._cached_linear_error_rows_rms,
self._cached_linear_error_cols_rms,
pct_err_rows,
pct_err_cols,
)

def get_flattened_cell_sizes(self):
if self._cached_flattened_cell_sizes is not None:
Expand All @@ -122,13 +128,13 @@ def get_flattened_cell_sizes(self):

self._cached_flattened_cell_sizes = cell_sizes.flatten()
return self._cached_flattened_cell_sizes

def get_aspect_ratio_pattern(self) -> float:
"""Get aspect ratio using the calibration pattern, wich should be squared."""
tilt, pan = self.get_rotation_angles()
acceptance_angle = 10

# dont update if we the detection has big angles, calculation will not be accurate
# dont update if we the detection has big angles, calculation will not be accurate
if np.abs(tilt) > acceptance_angle or np.abs(pan) > acceptance_angle:
return 0.0
# Calculate distances between adjacent corners
Expand All @@ -143,7 +149,6 @@ def get_aspect_ratio_pattern(self) -> float:
vertical_distance = np.linalg.norm(p - pcol)
aspect_ratio = aspect_ratio + (horizontal_distance / vertical_distance)
count += 1
aspect_ratio = aspect_ratio / ((self.rows - 1) * (self.cols - 1))
aspect_ratio = aspect_ratio / ((self.rows - 1) * (self.cols - 1))

return aspect_ratio

Original file line number Diff line number Diff line change
Expand Up @@ -421,9 +421,7 @@ def make_detection_group(self):
self.raw_detection_results_group = QGroupBox("Detection results")
self.raw_detection_results_group.setFlat(True)

self.single_shot_detection_results_group = QGroupBox(
"Single-shot detection results"
)
self.single_shot_detection_results_group = QGroupBox("Single-shot detection results")
self.single_shot_detection_results_group.setFlat(True)

self.raw_detection_label = QLabel("Detected:")
Expand Down Expand Up @@ -567,7 +565,7 @@ def draw_training_heatmap_callback(value):
def draw_evaluation_heatmap_callback(value):
self.image_view.set_draw_evaluation_heatmap(value == Qt.Checked)
self.should_process_image.emit()

def draw_linearity_heatmap_callback(value):
self.image_view.set_draw_linearity_heatmap(value == Qt.Checked)
self.should_process_image.emit()
Expand Down Expand Up @@ -609,9 +607,7 @@ def on_restart_linearity_heatmap_clicked():
self.undistortion_alpha_spinbox.valueChanged.connect(
lambda: self.should_process_image.emit()
)
self.undistortion_alpha_spinbox.valueChanged.connect(
on_restart_linearity_heatmap_clicked
)
self.undistortion_alpha_spinbox.valueChanged.connect(on_restart_linearity_heatmap_clicked)
self.undistortion_alpha_spinbox.setEnabled(False)

indicators_alpha_label = QLabel("Indicators alpha:")
Expand Down Expand Up @@ -696,9 +692,13 @@ def start(
self.setEnabled(True)

if self.operation_mode == OperationMode.CALIBRATION:
self.setWindowTitle(f"Camera intrinsics calibrator ({self.data_source.get_camera_name()})")
self.setWindowTitle(
f"Camera intrinsics calibrator ({self.data_source.get_camera_name()})"
)
if self.operation_mode == OperationMode.EVALUATION:
self.setWindowTitle(f"Camera intrinsics Evaluation Mode ({self.data_source.get_camera_name()})")
self.setWindowTitle(
f"Camera intrinsics Evaluation Mode ({self.data_source.get_camera_name()})"
)

logging.info("Init")
logging.info(f"\tmode : {mode}")
Expand Down Expand Up @@ -727,11 +727,13 @@ def start(
self.evaluation_sample_slider.setEnabled(False)
self.image_view_type_combobox.clear()
# Order of of how items are added to the combobox matters,
# default index is 0, so rectified image is added first to be default view
self.image_view_type_combobox.addItem(ImageViewMode.SOURCE_RECTIFIED.value, \
ImageViewMode.SOURCE_RECTIFIED)
self.image_view_type_combobox.addItem(ImageViewMode.SOURCE_UNRECTIFIED.value, \
ImageViewMode.SOURCE_UNRECTIFIED)
# default index is 0, so rectified image is added first to be default view
self.image_view_type_combobox.addItem(
ImageViewMode.SOURCE_RECTIFIED.value, ImageViewMode.SOURCE_RECTIFIED
)
self.image_view_type_combobox.addItem(
ImageViewMode.SOURCE_UNRECTIFIED.value, ImageViewMode.SOURCE_UNRECTIFIED
)

self.detector.moveToThread(self.detector_thread)
self.detector.detection_results_signal.connect(self.process_detection_results)
Expand Down Expand Up @@ -850,19 +852,21 @@ def process_evaluation_results(
def on_consumed(self):
self.data_source.consumed()

def save_parameters(self,filename):
def save_parameters(self, filename):
data_coll_params = self.data_collector.parameters_value()
board_params = self.board_parameters.parameters_value()
detector_params = self.detector.parameters_value()
calibrator_type = self.calibrator_type_combobox.currentData()
print(calibrator_type.value["name"], flush=True)
calib_params = self.calibrator_dict[calibrator_type].parameters_value()
with open(filename, 'w') as file:
yaml.dump({"board_parameters" : board_params}, file, default_flow_style=False)
yaml.dump({"calibrator_type" : calibrator_type.value["name"]}, file, default_flow_style=False)
with open(filename, "w") as file:
yaml.dump({"board_parameters": board_params}, file, default_flow_style=False)
yaml.dump(
{"calibrator_type": calibrator_type.value["name"]}, file, default_flow_style=False
)
yaml.dump({"calibration_parameters": calib_params}, file, default_flow_style=False)
yaml.dump({"data_collector" : data_coll_params}, file, default_flow_style=False)
yaml.dump({"detector_params" : detector_params}, file, default_flow_style=False)
yaml.dump({"data_collector": data_coll_params}, file, default_flow_style=False)
yaml.dump({"detector_params": detector_params}, file, default_flow_style=False)

def on_save_clicked(self):
output_folder = QFileDialog.getExistingDirectory(
Expand Down Expand Up @@ -896,17 +900,25 @@ def on_save_clicked(self):

for index, image in enumerate(self.data_collector.get_training_images()):
cv2.imwrite(os.path.join(training_folder, f"{index:04d}.jpg"), image) # noqa E231
np.savetxt(os.path.join(training_folder,f"{index:04d}_training_img_points.txt"), \
self.data_collector.get_training_detection(index).get_flattened_image_points())
np.savetxt(os.path.join(training_folder,f"{index:04d}_training_obj_points.txt"), \
self.data_collector.get_training_detection(index).get_flattened_object_points())
np.savetxt(
os.path.join(training_folder, f"{index:04d}_training_img_points.txt"),
self.data_collector.get_training_detection(index).get_flattened_image_points(),
)
np.savetxt(
os.path.join(training_folder, f"{index:04d}_training_obj_points.txt"),
self.data_collector.get_training_detection(index).get_flattened_object_points(),
)

for index, image in enumerate(self.data_collector.get_evaluation_images()):
cv2.imwrite(os.path.join(evaluation_folder, f"{index:04d}.jpg"), image) # noqa E231
np.savetxt(os.path.join(evaluation_folder,f"{index:04d}_eval_img_points.txt"), \
self.data_collector.get_evaluation_detection(index).get_flattened_image_points())
np.savetxt(os.path.join(evaluation_folder,f"{index:04d}_eval_obj_points.txt"), \
self.data_collector.get_evaluation_detection(index).get_flattened_object_points())
np.savetxt(
os.path.join(evaluation_folder, f"{index:04d}_eval_img_points.txt"),
self.data_collector.get_evaluation_detection(index).get_flattened_image_points(),
)
np.savetxt(
os.path.join(evaluation_folder, f"{index:04d}_eval_obj_points.txt"),
self.data_collector.get_evaluation_detection(index).get_flattened_object_points(),
)

def process_detection_results(self, img: np.array, detection: BoardDetection, img_stamp: float):
"""Process the results from an object detection."""
Expand Down Expand Up @@ -942,16 +954,21 @@ def process_detection_results(self, img: np.array, detection: BoardDetection, im
self.single_shot_reprojection_error_avg_label.setText("Reprojection error (avg):")
self.single_shot_reprojection_error_rms_label.setText("Reprojection error (rms):")
board_speed = None
self.image_view.set_draw_indicators(board_speed,
self.data_collector.max_allowed_pixel_speed.value,
self.data_collector.get_skew_percentage(),
self.data_collector.get_size_percentage(),
0, 0, # rows cols linear error
0, 0, # rows cols percentage linear error
0.0, # aspect ratio
0, 0,
self.indicators_alpha_spinbox.value(),
False)
self.image_view.set_draw_indicators(
board_speed,
self.data_collector.max_allowed_pixel_speed.value,
self.data_collector.get_skew_percentage(),
self.data_collector.get_size_percentage(),
0,
0, # rows cols linear error
0,
0, # rows cols percentage linear error
0.0, # aspect ratio
0,
0,
self.indicators_alpha_spinbox.value(),
False,
)

else:
camera_model = (
Expand All @@ -969,9 +986,11 @@ def process_detection_results(self, img: np.array, detection: BoardDetection, im
)
else:
filter_result = CollectionStatus.NOT_EVALUATED

if self.operation_mode == OperationMode.EVALUATION and \
self.image_view_type_combobox.currentData() == ImageViewMode.SOURCE_RECTIFIED:

if (
self.operation_mode == OperationMode.EVALUATION
and self.image_view_type_combobox.currentData() == ImageViewMode.SOURCE_RECTIFIED
):
self.data_collector.process_detection_eval_mode(
image=img,
detection=detection,
Expand Down Expand Up @@ -1025,7 +1044,9 @@ def process_detection_results(self, img: np.array, detection: BoardDetection, im
rough_angles = detection.get_rotation_angles(camera_model)

self.raw_detection_label.setText("Detected: True")
err_rms_rows, err_rms_cols, pct_err_rows, pct_err_cols = detection.get_linear_error_rms()
err_rms_rows, err_rms_cols, pct_err_rows, pct_err_cols = (
detection.get_linear_error_rms()
)
self.raw_linear_error_rows_rms_label.setText(
f"Linear error rows rms: {err_rms_rows:.2f} px" # noqa E231
)
Expand Down Expand Up @@ -1067,19 +1088,26 @@ def process_detection_results(self, img: np.array, detection: BoardDetection, im
f"Evaluation occupancy: {100.0*self.data_collector.get_evaluation_occupancy_rate():.2f}" # noqa E231
)

board_speed = 0 if self.last_detection is None else detection.get_speed(self.last_detection)
board_speed = (
0 if self.last_detection is None else detection.get_speed(self.last_detection)
)
self.last_detection = detection
pan, tilt = detection.get_rotation_angles()
self.image_view.set_draw_indicators(board_speed,
self.data_collector.max_allowed_pixel_speed.value,
self.data_collector.get_skew_percentage(),
self.data_collector.get_size_percentage(),
err_rms_rows, err_rms_cols,
pct_err_rows, pct_err_cols,
detection.get_aspect_ratio_pattern(),
pan, tilt,
self.indicators_alpha_spinbox.value(),
self.draw_indicators_checkbox.isChecked())
self.image_view.set_draw_indicators(
board_speed,
self.data_collector.max_allowed_pixel_speed.value,
self.data_collector.get_skew_percentage(),
self.data_collector.get_size_percentage(),
err_rms_rows,
err_rms_cols,
pct_err_rows,
pct_err_cols,
detection.get_aspect_ratio_pattern(),
pan,
tilt,
self.indicators_alpha_spinbox.value(),
self.draw_indicators_checkbox.isChecked(),
)

# Draw training / evaluation points
self.image_view.set_draw_training_points(self.draw_training_points_checkbox.isChecked())
Expand All @@ -1089,9 +1117,7 @@ def process_detection_results(self, img: np.array, detection: BoardDetection, im
self.draw_evaluation_heatmap_checkbox.isChecked()
)

self.image_view.set_draw_linearity_heatmap(
self.draw_linearity_heatmap_checkbox.isChecked()
)
self.image_view.set_draw_linearity_heatmap(self.draw_linearity_heatmap_checkbox.isChecked())

if self.draw_training_points_checkbox.isChecked():
self.image_view.set_training_points(
Expand All @@ -1114,9 +1140,7 @@ def process_detection_results(self, img: np.array, detection: BoardDetection, im
)

if self.draw_linearity_heatmap_checkbox.isChecked():
self.image_view.set_linearity_heatmap(
self.data_collector.get_linearity_heatmap()
)
self.image_view.set_linearity_heatmap(self.data_collector.get_linearity_heatmap())

if self.operation_mode == OperationMode.CALIBRATION:
if (
Expand Down
Loading

0 comments on commit cf3e90f

Please sign in to comment.