Fix lossy COCO round-trip: preserve area and iscrowd in as_coco#2185
Merged
Conversation
2 tasks
Agent-Logs-Url: https://github.com/roboflow/supervision/sessions/d56933e0-3900-4d5a-b00e-ff660f437cd6 Co-authored-by: Borda <6035284+Borda@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix as_coco function not saving the annotations as expected
Fix lossy COCO round-trip: preserve Mar 30, 2026
area and iscrowd in as_coco
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## develop #2185 +/- ##
=======================================
- Coverage 77% 77% -0%
=======================================
Files 62 62
Lines 7592 7595 +3
=======================================
+ Hits 5810 5812 +2
- Misses 1782 1783 +1 🚀 New features to boost your workflow:
|
…o-annotation-saving
- Replace bare int()/float() calls on data.get() with np.asarray(...).item() so mypy accepts all possible data dict value types (NDArray | list[Any] | int) --- Co-authored-by: Claude Code <noreply@anthropic.com>
--- Co-authored-by: Claude Code <noreply@anthropic.com>
--- Co-authored-by: Claude Code <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes COCO export round-tripping for DetectionDataset.from_coco() -> as_coco() -> from_coco() by preserving COCO annotation metadata (area, iscrowd) stored in Detections.data instead of recomputing/overwriting it during export.
Changes:
- Update
detections_to_coco_annotationsto readarea/iscrowdfromdetections.data(with sensible fallbacks) and ensureiscrowdis serialized as anint(0/1). - Add tests covering preservation of
area, preservation ofiscrowdwhen no mask is present, and fallback behavior when metadata is absent.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
src/supervision/dataset/formats/coco.py |
Preserves area/iscrowd from Detections.data during COCO annotation export and normalizes iscrowd to int. |
tests/dataset/formats/test_coco.py |
Adds regression tests validating the new area/iscrowd export behavior and fallback logic. |
…o-annotation-saving
Borda
approved these changes
Mar 30, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
DetectionDataset.from_coco→as_coco→from_cocowas not idempotent. The==comparison after a save/reload cycle returnedFalsebecausedetections_to_coco_annotationsdiscarded annotation metadata fromdetections.data.Root causes in
detections_to_coco_annotationsareaalways overwritten with bbox area —box_width * box_heightwas used unconditionally, ignoringdetections.data["area"]which holds the original COCO annotation area (typically polygon area, not bbox area).iscrowdstored as Pythonbool—contains_holes(...) or contains_multiple_segments(...)returns abool; COCO expectsint(0/1). Serialized astrue/falsein JSON.iscrowdreset to0when no mask — crowd annotations (iscrowd=1) loaded from COCO were silently demoted to0on save if no mask was present.Fix
When
detections.datahas noarea/iscrowd(e.g., detections not loaded from COCO), behavior is unchanged — falls back to bbox area and0respectively.✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.