feat: Detections.from_inference supports compressed RLE masks#2178
feat: Detections.from_inference supports compressed RLE masks#2178leeclemnet wants to merge 4 commits intodevelopfrom
Conversation
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## develop #2178 +/- ##
=======================================
Coverage 76% 76%
=======================================
Files 62 62
Lines 7547 7615 +68
=======================================
+ Hits 5714 5780 +66
- Misses 1833 1835 +2 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
Adds compressed COCO RLE mask support across Supervision’s detection inference pipeline and converters, enabling sv.Detections.from_inference() to decode masks provided as compressed RLE strings/bytes (e.g., SAM3 / semantic segmentation outputs) without adding dependencies.
Changes:
- Implement pure-Python COCO compressed RLE string encode/decode and extend
rle_to_mask/mask_to_rleto support compressed formats. - Update inference result processing to accept RLE mask payloads (
rle/rle_mask) and buildDetections.maskaccordingly. - Move RLE utilities into
detection.utils.converters, update exports/docs, and relocate tests accordingly.
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/detection/utils/test_internal.py | Adds inference parsing test cases for compressed RLE masks (incl. non-contiguous + tracker_id). |
| tests/detection/utils/test_converters.py | Adds unit tests for compressed RLE codec and mask↔RLE conversions. |
| tests/dataset/test_utils.py | Removes RLE tests from dataset utils (now covered under converters). |
| src/supervision/detection/utils/internal.py | Extends Roboflow/inference result parsing to decode RLE masks. |
| src/supervision/detection/utils/converters.py | Adds COCO compressed RLE codec + compressed support in converters. |
| src/supervision/dataset/utils.py | Removes old implementations and re-exports converters versions for compatibility. |
| src/supervision/dataset/formats/coco.py | Updates imports to use the new converter locations. |
| src/supervision/init.py | Exposes mask_to_rle / rle_to_mask from converters at top-level API. |
| mkdocs.yml | Removes datasets utils docs page from nav. |
| docs/detection/utils/converters.md | Documents rle_to_mask / mask_to_rle under converters. |
| docs/datasets/utils.md | Removes the old dataset utils docs page. |
| confidence.append(prediction["confidence"]) | ||
| masks.append(mask) | ||
| if "tracker_id" in prediction: |
There was a problem hiding this comment.
mask from rle_to_mask is a boolean array, but masks is annotated as list[npt.NDArray[np.uint8]] in this function. With strict mypy enabled, appending a bool mask here will raise a type error. Consider making the list type boolean (and casting the polygon mask to bool before appending) so both RLE and polygon paths are consistent.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
- Add bounds check in _decode_coco_rle_string() so malformed/truncated compressed RLE strings raise ValueError instead of IndexError - Guard process_roboflow_result against non-dict, missing-key, and malformed-counts RLE payloads — all fall through to box-only detection - Fix stale masks type annotation (uint8 → bool_) in internal.py - Add warn_deprecated() wrappers on rle_to_mask/mask_to_rle re-exports in dataset/utils for consistency with project deprecation pattern - Add negative-path tests: malformed counts, missing size/counts keys, non-dict rle, rle_mask fallback key, truncated compressed strings - Add CHANGELOG entries under develop Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Before submitting
Description
Add RLE mask support to
sv.Detections.from_inferencefor models that return compressed COCO RLE (e.g. SAM3, semantic segmentation), and extendrle_to_mask/mask_to_rlewith compressed string encoding/decoding — implemented as a pure Python COCO RLE codec with no new dependencies.cc @PawelPeczek-Roboflow
Type of Change
Motivation and Context
Workflows currently supports COCO RLE compressed string serialization for segmentation masks, originally for SAM3 instance segmentation and now also for semantic segmentation models where masks may not be contiguous and therefore can't be represented as polygons. We would like
sv.Detections.from_inference()to handle such data and automatically decode it into binary masks as it currently does for polygon mask data.The existing
rle_to_maskutil supports this conversion but not from compressed strings (see #1952).Closes #1952
Changes Made
Testing
Google Colab (optional)
Colab link:
Screenshots/Videos (optional)
Additional Notes