Skip to content

Commit

Permalink
Enhancement: Add widget for converting obb to hbb support
Browse files Browse the repository at this point in the history
  • Loading branch information
CVHub520 committed Mar 27, 2024
1 parent 3707e96 commit 16d7f23
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions anylabeling/views/labeling/label_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -1164,6 +1172,7 @@ def __init__(
modify_label,
None,
hbb_to_obb,
obb_to_hbb
),
)
utils.add_actions(
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 16d7f23

Please sign in to comment.