Skip to content

Add opt-in on_error hook for unreadable images during folder scan - #88

Merged
JonasWurst merged 5 commits into
mainfrom
jonas-lig-10211-labelformat-add-opt-in-on_error-hook-for-unreadable-images
Jul 16, 2026
Merged

Add opt-in on_error hook for unreadable images during folder scan#88
JonasWurst merged 5 commits into
mainfrom
jonas-lig-10211-labelformat-add-opt-in-on_error-hook-for-unreadable-images

Conversation

@JonasWurst

@JonasWurst JonasWurst commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Context

This is the upstream labelformat change that unblocks the studio-side work.

Problem

Folder-scanning formats (yolov8, kitti, pascalvoc, lightly, maskpair) open every image via get_image_dimensionsPIL.Image.open to read dimensions. A broken/undecodable file raised mid-iteration, and since a generator cannot resume after raising, one bad file killed get_images() and get_labels() entirely, propagating a raw PIL.UnidentifiedImageError/OSError.

Changes

  1. Normalize the failure. get_image_dimensions (utils.py) now wraps the fallback PIL.Image.open and raises a typed ImageDimensionError carrying the offending path instead of a raw PIL/OS error.
  2. Add an opt-in error hook. get_images_from_folder gains on_error: Callable[[Path, ImageDimensionError], None] | None = None. Default None → re-raise (no behavior change). When set: invoke on_error(path, error) and skip the file so the generator survives.
  3. Expose the hook on the folder-based readers. Settable on_error attribute on yolov8, kitti, lightly, maskpair; on_error parameter on pascalvoc.from_dirs (its scan happens at construction). maskpair.get_labels was kept aligned so dropped images don't break label lookup.

Default behavior stays raise; tolerance is strictly opt-in.

Testing

  • New unit tests for the typed error + hook in utils, and per-format tests for yolov8, pascalvoc, and maskpair (including get_labels alignment).
  • Full suite passes (164 passed, 1 skipped), mypy clean, black clean.

🤖 Generated with Claude Code

Folder-scanning formats open every image to read dimensions, so one
broken/undecodable file killed get_images() and get_labels() with a raw
PIL error mid-iteration.

- Normalize the failure: get_image_dimensions now wraps the PIL fallback
  and raises a typed ImageDimensionError carrying the offending path.
- Add an opt-in on_error hook to get_images_from_folder. Default None
  re-raises (no behavior change); when set, the file is skipped so the
  generator survives.
- Expose on_error on the folder-scanning readers (yolov8, kitti, lightly
  as a settable attribute; pascalvoc.from_dirs as a param; maskpair as a
  settable attribute, with get_labels kept aligned to readable images).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Comment thread tests/unit/formats/test_yolov8.py Outdated
Comment thread tests/unit/formats/semantic_segmentation/test_pascalvoc.py Outdated
Comment thread tests/integration/test_maskpair_integration.py Outdated
Comment thread tests/integration/test_maskpair_integration.py Outdated
Move the on_error test-dataset builders out of the test classes into
module-level helpers at the bottom of each file (no self), and hoist the
repeated local imports to the top of the files.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@JonasWurst

Copy link
Copy Markdown
Contributor Author

/review

@MalteEbner MalteEbner left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but test naming does not follow our coding standards.

Comment thread tests/integration/test_maskpair_integration.py Outdated
Comment thread tests/unit/formats/test_yolov8.py Outdated
JonasWurst and others added 2 commits July 16, 2026 11:56
Rename the on_error tests in yolov8 and maskpair to the
test_<method>__<scenario> convention used by neighboring tests
(e.g. test_get_images__reraises_by_default).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
- One test class per source class: dissolve the feature-grouping
  on_error test classes. yolov8's get_images tests move into
  Test_YOLOv8BaseInput after get_categories; maskpair's on_error tests
  merge into TestMaskPairIntegration.
- Order tests by source order: move the get_image_dimensions corrupt
  test above the get_images_from_folder tests in test_utils.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@JonasWurst
JonasWurst merged commit 07b00a0 into main Jul 16, 2026
2 checks passed
@JonasWurst
JonasWurst deleted the jonas-lig-10211-labelformat-add-opt-in-on_error-hook-for-unreadable-images branch July 16, 2026 10:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants