Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion anylabeling/services/auto_labeling/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def unload(self):

@staticmethod
def load_image_from_filename(filename):
"""Load image from labeling file and return image data and image path."""
"""Load an image for background inference, or return ``None``."""
label_file = os.path.splitext(filename)[0] + ".json"
if QFile.exists(label_file) and LabelFile.is_label_file(label_file):
try:
Expand All @@ -126,9 +126,13 @@ def load_image_from_filename(filename):
image_data = label_file.image_data
else:
image_data = LabelFile.load_image_file(filename)
if not image_data:
logging.error(f"Error reading {filename}")
return None
image = QImage.fromData(image_data)
if image.isNull():
logging.error(f"Error reading {filename}")
return None
return image

def on_next_files_changed(self, next_files):
Expand Down
31 changes: 31 additions & 0 deletions tests/test_model_image_loading.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import tempfile
import unittest
from pathlib import Path

from PyQt6.QtGui import QImage

from anylabeling.services.auto_labeling.model import Model


class TestModelImageLoading(unittest.TestCase):
def test_corrupt_image_returns_none(self):
with tempfile.TemporaryDirectory() as temp_dir:
path = Path(temp_dir) / "corrupt.png"
path.write_bytes(b"not an image")

self.assertIsNone(Model.load_image_from_filename(str(path)))

def test_valid_image_returns_non_null_qimage(self):
with tempfile.TemporaryDirectory() as temp_dir:
path = Path(temp_dir) / "valid.png"
image = QImage(2, 2, QImage.Format.Format_RGB32)
self.assertTrue(image.save(str(path)))

loaded = Model.load_image_from_filename(str(path))

self.assertIsInstance(loaded, QImage)
self.assertFalse(loaded.isNull())


if __name__ == "__main__":
unittest.main()
Loading