Skip to content

Commit

Permalink
TagLab labels; export dataset visual bug; robustness to IO
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan-Pierce committed Jan 17, 2025
1 parent e20cd9a commit 4cb4566
Show file tree
Hide file tree
Showing 18 changed files with 181 additions and 99 deletions.
13 changes: 8 additions & 5 deletions coralnet_toolbox/IO/QtExportAnnotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from coralnet_toolbox.Annotations.QtPatchAnnotation import PatchAnnotation
from coralnet_toolbox.Annotations.QtPolygonAnnotation import PolygonAnnotation
from coralnet_toolbox.Annotations.QtRectangleAnnotation import RectangleAnnotation

from coralnet_toolbox.QtProgressBar import ProgressBar

warnings.filterwarnings("ignore", category=DeprecationWarning)
Expand Down Expand Up @@ -35,6 +36,7 @@ def export_annotations(self):
options=options)
if file_path:
try:
# Make cursor busy
QApplication.setOverrideCursor(Qt.WaitCursor)

total_annotations = len(list(self.annotation_window.annotations_dict.values()))
Expand Down Expand Up @@ -74,9 +76,6 @@ def export_annotations(self):
json.dump(export_dict, file, indent=4)
file.flush()

progress_bar.stop_progress()
progress_bar.close()

QMessageBox.information(self.annotation_window,
"Annotations Exported",
"Annotations have been successfully exported.")
Expand All @@ -85,5 +84,9 @@ def export_annotations(self):
QMessageBox.warning(self.annotation_window,
"Error Exporting Annotations",
f"An error occurred while exporting annotations: {str(e)}")

QApplication.restoreOverrideCursor()

finally:
# Restore the cursor
QApplication.restoreOverrideCursor()
progress_bar.stop_progress()
progress_bar.close()
17 changes: 11 additions & 6 deletions coralnet_toolbox/IO/QtExportCoralNetAnnotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@ def export_annotations(self):
"CSV Files (*.csv);;All Files (*)",
options=options)
if file_path:


# Make cursor busy
QApplication.setOverrideCursor(Qt.WaitCursor)

total_annotations = len(self.annotation_window.annotations_dict)
progress_bar = ProgressBar(self.annotation_window, title="Exporting CoralNet Annotations")
progress_bar.show()
progress_bar.start_progress(len(self.annotation_window.annotations_dict))
progress_bar.start_progress(total_annotations)

try:
df = []
Expand All @@ -55,7 +58,9 @@ def export_annotations(self):
QMessageBox.warning(self.annotation_window,
"Error Exporting Annotations",
f"An error occurred while exporting annotations: {str(e)}")

progress_bar.stop_progress()
progress_bar.close()
QApplication.restoreOverrideCursor()

finally:
# Restore the cursor
QApplication.restoreOverrideCursor()
progress_bar.stop_progress()
progress_bar.close()
26 changes: 24 additions & 2 deletions coralnet_toolbox/IO/QtExportLabels.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import json
import warnings

from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (QFileDialog, QMessageBox)

from coralnet_toolbox.QtProgressBar import ProgressBar

warnings.filterwarnings("ignore", category=DeprecationWarning)


Expand All @@ -28,8 +31,22 @@ def export_labels(self):
"JSON Files (*.json);;All Files (*)",
options=options)
if file_path:

# Make cursor busy
self.QApplication.setOverrideCursor(Qt.WaitCursor)

# Create a progress bar
total_labels = len(self.label_window.labels)
progress_bar = ProgressBar("Exporting Labels", self.label_window)
progress_bar.show()
progress_bar.start_progress(total_labels)

try:
labels_data = [label.to_dict() for label in self.label_window.labels]
labels_data = []
for i, label in enumerate(self.label_window.labels):
labels_data.append(label.to_dict())
progress_bar.update_progress(i + 1)

with open(file_path, 'w') as file:
json.dump(labels_data, file, indent=4)

Expand All @@ -40,4 +57,9 @@ def export_labels(self):
except Exception as e:
QMessageBox.warning(self.label_window,
"Error Importing Labels",
f"An error occurred while importing labels: {str(e)}")
f"An error occurred while importing labels: {str(e)}")
finally:
# Reset the cursor
self.QApplication.restoreOverrideCursor()
progress_bar.stop_progress()
progress_bar.close()
10 changes: 5 additions & 5 deletions coralnet_toolbox/IO/QtExportTagLabAnnotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,6 @@ def export_annotations(self):
json.dump(taglab_data, file, indent=4)
file.flush()

# Close the progress bar
progress_bar.stop_progress()
progress_bar.close()

QMessageBox.information(self.annotation_window,
"Annotations Exported",
"Annotations have been successfully exported.")
Expand All @@ -193,4 +189,8 @@ def export_annotations(self):
"Error Exporting Annotations",
f"An error occurred while exporting annotations: {str(e)}")

QApplication.restoreOverrideCursor()
finally:
# Restore the cursor
QApplication.restoreOverrideCursor()
progress_bar.stop_progress()
progress_bar.close()
10 changes: 6 additions & 4 deletions coralnet_toolbox/IO/QtExportViscoreAnnotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ def export_annotations(self):
QMessageBox.warning(self.annotation_window,
"Error Exporting Viscore Annotations",
f"An error occurred while exporting annotations: {str(e)}")

progress_bar.stop_progress()
progress_bar.close()
QApplication.restoreOverrideCursor()

finally:
# Restore the cursor
QApplication.restoreOverrideCursor()
progress_bar.stop_progress()
progress_bar.close()
11 changes: 6 additions & 5 deletions coralnet_toolbox/IO/QtImportAnnotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def import_annotations(self):
options=options)
if file_paths:
try:
# Make cursor busy
QApplication.setOverrideCursor(Qt.WaitCursor)

all_data = {}
Expand Down Expand Up @@ -114,10 +115,6 @@ def import_annotations(self):
# Load the annotations for current image
self.annotation_window.load_annotations()

# Stop the progress bar
progress_bar.stop_progress()
progress_bar.close()

QMessageBox.information(self.annotation_window,
"Annotations Imported",
"Annotations have been successfully imported.")
Expand All @@ -127,4 +124,8 @@ def import_annotations(self):
"Error Importing Annotations",
f"An error occurred while importing annotations: {str(e)}")

QApplication.restoreOverrideCursor()
finally:
# Restore the cursor
QApplication.restoreOverrideCursor()
progress_bar.stop_progress()
progress_bar.close()
12 changes: 6 additions & 6 deletions coralnet_toolbox/IO/QtImportCoralNetAnnotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,6 @@ def import_annotations(self):
# Load the annotations for current image
self.annotation_window.load_annotations()

# Stop the progress bar
progress_bar.stop_progress()
progress_bar.close()

QMessageBox.information(self.annotation_window,
"Annotations Imported",
"Annotations have been successfully imported.")
Expand All @@ -172,5 +168,9 @@ def import_annotations(self):
QMessageBox.warning(self.annotation_window,
"Error Importing Annotations",
f"An error occurred while importing annotations: {str(e)}")

QApplication.restoreOverrideCursor()

finally:
# Restore the cursor to the default cursor
QApplication.restoreOverrideCursor()
progress_bar.stop_progress()
progress_bar.close()
54 changes: 31 additions & 23 deletions coralnet_toolbox/IO/QtImportImages.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,38 @@ def import_images(self):
"",
"Image Files (*.png *.jpg *.jpeg *.tif* *.bmp)")
if file_names:
# Set the cursor to waiting (busy) cursor
QApplication.setOverrideCursor(Qt.WaitCursor)
try:
# Set the cursor to waiting (busy) cursor
QApplication.setOverrideCursor(Qt.WaitCursor)

progress_bar = ProgressBar(self.image_window, title="Importing Images")
progress_bar.show()
progress_bar.start_progress(len(file_names))
progress_bar.set_value(1)
progress_bar = ProgressBar(self.image_window, title="Importing Images")
progress_bar.show()
progress_bar.start_progress(len(file_names))

for i, file_name in enumerate(file_names):
if file_name not in set(self.image_window.image_paths):
self.image_window.add_image(file_name)
progress_bar.update_progress()
for i, file_name in enumerate(file_names):
if file_name not in set(self.image_window.image_paths):
try:
self.image_window.add_image(file_name)
except Exception as e:
print(f"Warning: Could not import image {file_name}. Error: {e}")

# Update the progress bar
progress_bar.update_progress()

progress_bar.stop_progress()
progress_bar.close()
# Update filtered images
self.image_window.filter_images()
# Show the last image
self.image_window.load_image_by_path(self.image_window.image_paths[-1])

# Update filtered images
self.image_window.filter_images()
# Show the last image
self.image_window.load_image_by_path(self.image_window.image_paths[-1])

# Restore the cursor to the default cursor
QApplication.restoreOverrideCursor()

QMessageBox.information(self.image_window,
"Image(s) Imported",
"Image(s) have been successfully imported.")
QMessageBox.information(self.image_window,
"Image(s) Imported",
"Image(s) have been successfully imported.")
except Exception as e:
QMessageBox.warning(self.image_window,
"Error Importing Image(s)",
f"An error occurred while importing image(s): {str(e)}")
finally:
# Restore the cursor to the default cursor
QApplication.restoreOverrideCursor()
progress_bar.stop_progress()
progress_bar.close()
18 changes: 17 additions & 1 deletion coralnet_toolbox/IO/QtImportLabels.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

from coralnet_toolbox.QtLabelWindow import Label

from coralnet_toolbox.QtProgressBar import ProgressBar


# ----------------------------------------------------------------------------------------------------------------------
# Classes
Expand All @@ -34,6 +36,12 @@ def import_labels(self):
try:
with open(file_path, 'r') as file:
labels_data = json.load(file)

# Create a progress bar
total_labels = len(labels_data)
progress_bar = ProgressBar("Importing Labels", self.label_window)
progress_bar.show()
progress_bar.start_progress(total_labels)

for label_data in labels_data:
label = Label.from_dict(label_data)
Expand All @@ -42,6 +50,8 @@ def import_labels(self):
label.long_label_code,
label.color,
label.id)
# Update the progress bar
progress_bar.update_progress()

# Set the Review label as active
self.label_window.set_active_label(self.label_window.get_label_by_long_code("Review"))
Expand All @@ -53,4 +63,10 @@ def import_labels(self):
except Exception as e:
QMessageBox.warning(self.label_window,
"Error Importing Labels",
f"An error occurred while importing Labels: {str(e)}")
f"An error occurred while importing Labels: {str(e)}")

finally:
# Stop the progress bar
progress_bar.stop_progress()
progress_bar.close()
QApplication.restoreOverrideCursor()
42 changes: 26 additions & 16 deletions coralnet_toolbox/IO/QtImportTagLabLabels.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import json
import warnings

from PyQt5.QtWidgets import QFileDialog, QMessageBox
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QColor
from PyQt5.QtWidgets import QFileDialog, QMessageBox, QApplication

from coralnet_toolbox.QtLabelWindow import Label

Expand Down Expand Up @@ -43,31 +45,33 @@ def import_taglab_labels(self):
"Invalid JSON Format",
"The selected JSON file does not contain 'Labels' or 'labels' key.")
return

# Make cursor busy
QApplication.setOverrideCursor(Qt.WaitCursor)

# Create a progress bar
total_labels = len(data['labels'])
progress_bar = ProgressBar("Importing TagLab Labels", self.label_window)
progress_bar = ProgressBar(self.label_window, "Importing TagLab Labels")
progress_bar.show()
progress_bar.start_progress(total_labels)

for label_id, label_info in data['labels'].items():
short_label_code = label_info['name'].strip()
long_label_code = label_info['name'].strip()
color = label_info['fill']
for label_info in data['labels']:
try:
short_label_code = label_info['name'].strip()
long_label_code = label_info['name'].strip()
color = label_info['fill']

# Create a QtColor object from the color string
color = QColor(color[0], color[1], color[2])

label = Label(short_label_code, long_label_code, color, label_id)
if not self.label_window.label_exists(label.short_label_code, label.long_label_code):
self.label_window.add_label(label.short_label_code,
label.long_label_code,
label.color,
label.id)
# Add label if it does not exist
self.label_window.add_label_if_not_exists(short_label_code, long_label_code, color)

except Exception as e:
print(f"Warning: Could not import label {label_info['name']}: {str(e)}")

# Update the progress bar
progress_bar.update_progress()

# Close the progress bar
progress_bar.close()
progress_bar.stop_progress()

QMessageBox.information(self.label_window,
"Labels Imported",
Expand All @@ -77,3 +81,9 @@ def import_taglab_labels(self):
QMessageBox.warning(self.label_window,
"Error Importing Labels",
f"An error occurred while importing TagLab labels: {str(e)}")

finally:
# Stop the progress bar
progress_bar.stop_progress()
progress_bar.close()
QApplication.restoreOverrideCursor()
Loading

0 comments on commit 4cb4566

Please sign in to comment.