Updating annotations
This commit is contained in:
@@ -93,6 +93,12 @@ class AnnotationTab(QWidget):
|
||||
self.annotation_tools.clear_annotations_requested.connect(
|
||||
self._on_clear_annotations
|
||||
)
|
||||
self.annotation_tools.process_annotations_requested.connect(
|
||||
self._on_process_annotations
|
||||
)
|
||||
self.annotation_tools.show_annotations_requested.connect(
|
||||
self._on_show_annotations
|
||||
)
|
||||
self.right_splitter.addWidget(self.annotation_tools)
|
||||
|
||||
# Image loading section
|
||||
@@ -237,6 +243,127 @@ class AnnotationTab(QWidget):
|
||||
self.annotation_canvas.clear_annotations()
|
||||
logger.info("Cleared all annotations")
|
||||
|
||||
def _on_process_annotations(self):
|
||||
"""Process annotations and save to database."""
|
||||
# Check if we have an image loaded
|
||||
if not self.current_image or not self.current_image_id:
|
||||
QMessageBox.warning(
|
||||
self, "No Image", "Please load an image before processing annotations."
|
||||
)
|
||||
return
|
||||
|
||||
# Get current class
|
||||
current_class = self.annotation_tools.get_current_class()
|
||||
if not current_class:
|
||||
QMessageBox.warning(
|
||||
self,
|
||||
"No Class Selected",
|
||||
"Please select an object class before processing annotations.",
|
||||
)
|
||||
return
|
||||
|
||||
# Compute bounding box and polyline from annotations
|
||||
bounds = self.annotation_canvas.compute_annotation_bounds()
|
||||
if not bounds:
|
||||
QMessageBox.warning(
|
||||
self,
|
||||
"No Annotations",
|
||||
"Please draw some annotations before processing.",
|
||||
)
|
||||
return
|
||||
|
||||
polyline = self.annotation_canvas.get_annotation_polyline()
|
||||
|
||||
try:
|
||||
# Save annotation to database
|
||||
annotation_id = self.db_manager.add_annotation(
|
||||
image_id=self.current_image_id,
|
||||
class_id=current_class["id"],
|
||||
bbox=bounds,
|
||||
annotator="manual",
|
||||
segmentation_mask=polyline,
|
||||
verified=False,
|
||||
)
|
||||
|
||||
logger.info(
|
||||
f"Saved annotation (ID: {annotation_id}) for class '{current_class['class_name']}' "
|
||||
f"with {len(polyline)} polyline points"
|
||||
)
|
||||
|
||||
QMessageBox.information(
|
||||
self,
|
||||
"Success",
|
||||
f"Annotation saved successfully!\n\n"
|
||||
f"Class: {current_class['class_name']}\n"
|
||||
f"Bounding box: ({bounds[0]:.3f}, {bounds[1]:.3f}) to ({bounds[2]:.3f}, {bounds[3]:.3f})\n"
|
||||
f"Polyline points: {len(polyline)}",
|
||||
)
|
||||
|
||||
# Optionally clear annotations after saving
|
||||
reply = QMessageBox.question(
|
||||
self,
|
||||
"Clear Annotations",
|
||||
"Do you want to clear the annotations to start a new one?",
|
||||
QMessageBox.Yes | QMessageBox.No,
|
||||
QMessageBox.Yes,
|
||||
)
|
||||
|
||||
if reply == QMessageBox.Yes:
|
||||
self.annotation_canvas.clear_annotations()
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to save annotation: {e}")
|
||||
QMessageBox.critical(self, "Error", f"Failed to save annotation:\n{str(e)}")
|
||||
|
||||
def _on_show_annotations(self):
|
||||
"""Load and display saved annotations from database."""
|
||||
# Check if we have an image loaded
|
||||
if not self.current_image or not self.current_image_id:
|
||||
QMessageBox.warning(
|
||||
self, "No Image", "Please load an image to view its annotations."
|
||||
)
|
||||
return
|
||||
|
||||
try:
|
||||
# Clear current annotations
|
||||
self.annotation_canvas.clear_annotations()
|
||||
|
||||
# Retrieve annotations from database
|
||||
annotations = self.db_manager.get_annotations_for_image(
|
||||
self.current_image_id
|
||||
)
|
||||
|
||||
if not annotations:
|
||||
QMessageBox.information(
|
||||
self, "No Annotations", "No saved annotations found for this image."
|
||||
)
|
||||
return
|
||||
|
||||
# Draw each annotation's polyline
|
||||
drawn_count = 0
|
||||
for ann in annotations:
|
||||
if ann.get("segmentation_mask"):
|
||||
polyline = ann["segmentation_mask"]
|
||||
color = ann.get("class_color", "#FF0000")
|
||||
|
||||
# Draw the polyline
|
||||
self.annotation_canvas.draw_saved_polyline(polyline, color, width=3)
|
||||
drawn_count += 1
|
||||
|
||||
logger.info(f"Displayed {drawn_count} saved annotations from database")
|
||||
|
||||
QMessageBox.information(
|
||||
self,
|
||||
"Annotations Loaded",
|
||||
f"Successfully loaded and displayed {drawn_count} annotation(s).",
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to load annotations: {e}")
|
||||
QMessageBox.critical(
|
||||
self, "Error", f"Failed to load annotations:\n{str(e)}"
|
||||
)
|
||||
|
||||
def _restore_state(self):
|
||||
"""Restore splitter positions from settings."""
|
||||
settings = QSettings("microscopy_app", "object_detection")
|
||||
|
||||
Reference in New Issue
Block a user