Skip to content

fix(dataset): raise when an image cannot be written during export - #2772

Merged
Borda merged 4 commits into
roboflow:developfrom
tongwentao:fix/dataset-image-write-errors
Oct 22, 2026
Merged

Borda merged 4 commits into
roboflow:developfrom
tongwentao:fix/dataset-image-write-errors

Conversation

@tongwentao

@tongwentao tongwentao commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Description

In-memory dataset exports ignore a False result from cv2.imwrite. An export can therefore return normally even though an image was never saved. Both the shared detection-image exporter and classification folder exporter now raise OSError containing the destination path, matching ImageSink.

Minimal reproduction (the destination image path already exists as a directory):

from pathlib import Path
from tempfile import TemporaryDirectory

import numpy as np
import supervision as sv

with TemporaryDirectory() as directory:
    output = Path(directory)
    (output / "image.png").mkdir()
    dataset = sv.DetectionDataset(
        classes=[],
        images={"image.png": np.zeros((4, 4, 3), dtype=np.uint8)},
        annotations={"image.png": sv.Detections.empty()},
    )
    dataset.as_yolo(images_directory_path=str(output))

Before: returns None, with no image file written. After: raises OSError: Failed to save image to path: .../image.png.

Motivation and Context

Report incomplete exports at the point of failure, so users can correct the destination instead of discovering missing training images later.

Changes Made

  • Check image-write results in save_dataset_images and ClassificationDataset.as_folder_structure, and document the exception.
  • Add real filesystem failure cases and successful pixel-preservation tests for both exporters; add an Unreleased changelog entry.

Testing

Revalidated after merging current upstream develop (893a9b1a) and preserving the fix under Unreleased.

  • New regression cases against unmodified develop: two failed with DID NOT RAISE OSError; both successful-write controls passed.

  • MPLBACKEND=Agg .venv/bin/pytest src/ tests/ --cov=supervision --cov-report=term: 6260 passed, 1 skipped, 44 warnings, 94% coverage. This includes the integration test running the ordinary suite with OpenCV imports blocked.

  • .venv/bin/pre-commit run --all-files: all configured pre-commit hooks passed, including Ruff, docformatter, mypy and codespell.

  • Tested on Python 3.12 with OpenCV headless 4.14.0.94.

  • Added/updated tests, and the full suite passes locally

  • Updated docs (docstrings / mkdocs entry) for new or changed public API

  • Added a changelog entry in docs/changelog.md under Unreleased

Signed-off-by: tongwentao <twtzs@foxmail.com>
@tongwentao
tongwentao requested a review from SkalskiP as a code owner October 20, 2026 06:41
Signed-off-by: tongwentao <twtzs@foxmail.com>
@Borda Borda added the bug Something isn't working label Oct 20, 2026
@Borda
Borda requested a balanced review from Copilot October 20, 2026 07:11

Copilot AI 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.

🟢 Approval recommended

Both narrowly scoped failure checks have focused regression coverage and matching documentation, with no unresolved findings.

No open issues 🎉

What changed in this PR

🟢 Approve — This change prevents silent image-write failures in Supervision’s in-memory dataset exports.

Changes:

  • Raise OSError with the destination path when either exporter receives a failed write result.
  • Add filesystem-failure and successful pixel-preservation tests.
  • Document the exception and update the changelog.

No actionable issues identified. Behavior, error handling, tests, and documentation are covered by the supplied review evidence; tests were not rerun here.

Quality: code 5/5 (minimal checks), tests 5/5 (failure and success coverage), documentation 5/5 (matching updates). Risk: 1/5.

File Description
tests/​dataset/​test_utils.py Tests detection image-write failures and pixel preservation.
tests/​dataset/​test_core.py Tests classification image-write failures and pixel preservation.
src/​supervision/​dataset/​utils.py Checks detection image writes and documents errors.
src/​supervision/​dataset/​core.py Checks classification image writes and documents errors.
docs/​changelog.md Announces the export failure handling.

Review effort: Balanced

@CLAassistant

CLAassistant commented Oct 21, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@codecov

codecov Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94%. Comparing base (e82da41) to head (c23d3e7).

Additional details and impacted files
@@           Coverage Diff           @@
##           develop   #2772   +/-   ##
=======================================
  Coverage       94%     94%           
=======================================
  Files           83      83           
  Lines        13659   13661    +2     
=======================================
+ Hits         12863   12867    +4     
+ Misses         796     794    -2     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Borda
Borda merged commit fb7a66f into roboflow:develop Oct 22, 2026
36 checks passed
Borda added a commit to moyunliuyin/supervision that referenced this pull request Oct 22, 2026
Changes:
- Merge fetched develop at b4225e5 into fix/pascal-voc-devkit-extents, retaining the Pascal VOC conversion change and its tests alongside the target dataset export, JSON sink, compact-mask, metrics, and test updates.
- Resolve docs/changelog.md by retaining the PR Pascal VOC note and target notes for roboflow#2772, roboflow#2771, and roboflow#2767.

Impact:
- Brings the PR branch up to the fetched target implementation while preserving the PR behavior and release note.
- Adds the target 32 other changed paths to the branch; no remote publication is included.

Verification:
- Focused Pascal VOC and COCO format tests: 179 passed, 5 warnings.
- Staged merge diff check passed; no unmerged paths remained; reviewed merge tree 49df6c7.

Residual limits:
- Full remediation gates remain to be run after finding intake.
- The test run emitted one pure-NumPy fallback warning and four deprecation warnings.

---

Co-authored-by: Codex <codex@openai.com>
Borda pushed a commit that referenced this pull request Oct 24, 2026
)

* fix(dataset): raise when an image cannot be written during export
* docs(changelog): link image export fix to PR 2772


---------

Signed-off-by: tongwentao <twtzs@foxmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants