fix: correct numpy indexing in denormalize_boxes and add ultralytics validation #2012
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.
Description
This PR fixes two separate bugs in the supervision library that cause crashes in production:
Bug 1: denormalize_boxes numpy indexing error
The
denormalize_boxesfunction used incorrect numpy indexing (result[[0, 2]]) which selects rows 0 and 2 instead of all rows at columns 0 and 2. This causes anIndexErrorwhen processing more than 2 bounding boxes. The fix changes it toresult[:, [0, 2]]to properly index all rows at specific columns, matching the pattern already used inclip_boxesandpad_boxes.Bug 2: from_ultralytics missing attribute validation
The
from_ultralyticsmethod crashed withAttributeErrorwhen the results object doesn't have aboxesattribute. The code checkedif boxes is Nonebut didn't verify the attribute exists first usinghasattr(). The fix adds properhasattr()validation and returns an empty detection when the attribute is missing, following the same pattern used forobbvalidation.No new dependencies required for this change.
Type of change
How has this change been tested, please provide a testcase or example of how you tested the change?
For denormalize_boxes:
Added 8 parametrized test cases covering empty arrays, single/multiple boxes, custom normalization factors, and edge cases. The tests verify the function works correctly with 3+ boxes (regression test for #1959).
For from_ultralytics:
Added 3 test cases using mock Ultralytics results to test: missing
boxesattribute entirely,boxes=None(segmentation models), and valid boxes. Tests verify the function handles all cases without crashing.All tests pass: 27 total (16 existing + 11 new).
Any specific deployment considerations
None - these are bug fixes that make the functions work as documented without changing behavior for valid inputs.
Docs
Fixes #1959
Fixes #2000