Skip to content

Commit

Permalink
Enhancement: Add widget for converting polygon to hbb support
Browse files Browse the repository at this point in the history
  • Loading branch information
CVHub520 committed Jun 21, 2024
1 parent a7cb4c5 commit 00d3fff
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions anylabeling/views/labeling/label_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,14 @@ def __init__(
"Perform conversion from oriented bounding box to horizontal bounding box"
),
)
polygon_to_hbb = action(
self.tr("&Convert Polygon to HBB"),
self.polygon_to_hbb,
icon="convert",
tip=self.tr(
"Perform conversion from polygon to horizontal bounding box"
),
)

documentation = action(
self.tr("&Documentation"),
Expand Down Expand Up @@ -1204,6 +1212,7 @@ def __init__(
None,
hbb_to_obb,
obb_to_hbb,
polygon_to_hbb,
),
)
utils.add_actions(
Expand Down Expand Up @@ -1825,6 +1834,61 @@ def obb_to_hbb(self):
# Hide the progress dialog after processing is done
progress_dialog.hide()

def polygon_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"] == "polygon":
data["shapes"][i]["shape_type"] = "rectangle"
points = np.array(data["shapes"][i]["points"])
xmin = int(np.min(points[:, 0]))
ymin = int(np.min(points[:, 1]))
xmax = int(np.max(points[:, 0]))
ymax = int(np.max(points[:, 1]))
data["shapes"][i]["points"] = [
[xmin, ymin],
[xmax, ymin],
[xmax, ymax],
[xmin, ymax],
]
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 00d3fff

Please sign in to comment.