From 16d7f2330d6e79e771f743bfce5cb88d3515f99a Mon Sep 17 00:00:00 2001 From: cvhub Date: Wed, 27 Mar 2024 10:07:02 +0800 Subject: [PATCH] Enhancement: Add widget for converting obb to hbb support --- anylabeling/views/labeling/label_widget.py | 62 ++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/anylabeling/views/labeling/label_widget.py b/anylabeling/views/labeling/label_widget.py index 9868d568..369079ec 100644 --- a/anylabeling/views/labeling/label_widget.py +++ b/anylabeling/views/labeling/label_widget.py @@ -641,6 +641,14 @@ def __init__( "Perform conversion from horizontal bounding box to oriented bounding box" ), ) + obb_to_hbb = action( + self.tr("&Convert OBB to HBB"), + self.obb_to_hbb, + icon="convert", + tip=self.tr( + "Perform conversion from oriented bounding box to horizontal bounding box" + ), + ) documentation = action( self.tr("&Documentation"), @@ -1164,6 +1172,7 @@ def __init__( modify_label, None, hbb_to_obb, + obb_to_hbb ), ) utils.add_actions( @@ -1725,6 +1734,59 @@ def hbb_to_obb(self): # Hide the progress dialog after processing is done progress_dialog.hide() + def obb_to_hbb(self): + label_file_list = self.get_label_file_list() + + total_files = len(label_file_list) + current_index = 0 + + progress_dialog = QtWidgets.QDialog(self) + progress_dialog.setWindowTitle("Converting...") + progress_dialog_layout = QVBoxLayout(progress_dialog) + progress_bar = QtWidgets.QProgressBar() + progress_dialog_layout.addWidget(progress_bar) + progress_dialog.setLayout(progress_dialog_layout) + + # Show the progress dialog before entering the loop + progress_dialog.show() + + try: + for label_file in label_file_list: + # Update progress label + QtWidgets.QApplication.processEvents() + + with open(label_file, "r", encoding="utf-8") as f: + data = json.load(f) + for i in range(len(data["shapes"])): + if data["shapes"][i]["shape_type"] == "rotation": + del data["shapes"][i]["direction"] + data["shapes"][i]["shape_type"] = "rectangle" + points = np.array(data["shapes"][i]["points"]).astype(np.int32) + x, y, w, h = map(int, list(cv2.boundingRect(points))) + data["shapes"][i]["points"] = [ + [x, y], + [x + w, y], + [x + w, y + w], + [x, y + w], + ] + with open(label_file, "w", encoding="utf-8") as f: + json.dump(data, f, indent=2, ensure_ascii=False) + + # Update progress bar + current_index += 1 + progress_value = int((current_index / total_files) * 100) + progress_bar.setValue(progress_value) + + # Reload the file after processing all label files + self.load_file(self.filename) + return True + except Exception as e: + print(f"Error occurred while updating labels: {e}") + return False + finally: + # Hide the progress dialog after processing is done + progress_dialog.hide() + def save_crop(self, mode="default"): if not self.filename: QtWidgets.QMessageBox.warning(