supervision
supervision copied to clipboard
fix: prevent single object from appearing in multiple polygon zones (#1987)
Description
Fixes PolygonZone allowing objects to appear in multiple zones simultaneously.
Resolves #1987
When checking if a detection is inside a polygon zone, the previous implementation would clip the bounding box to fit within each ROI's dimensions before calculating anchor points. This caused the same detection to produce different anchor points for different ROIs, allowing it to be counted as present in multiple zones.
Example:
- Detection box: [441, 258, 718, 713]
- ROI1 bounds: [0, 167, 457, 427]
- ROI2 bounds: [458, 67, 1011, 309]
Before fix:
- Clipped to ROI1: [441, 258, 457, 427] → anchor at (449, 427) → detected in ROI1 ✗
- Clipped to ROI2: [458, 258, 718, 309] → anchor at (588, 309) → detected in ROI2 ✗
- Result: Object counted in BOTH zones
After fix:
- Original box center: (580, 485)
- Check against ROI1: center not in ROI1 → False ✓
- Check against ROI2: center in ROI2 → True ✓
- Result: Object counted in only ONE zone
Bug visualization
Before Fix
After Fix
Type of change
- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] This change requires a documentation update
How has this change been tested?
-
Automated tests: All 6 tests in
test/detection/test_polygonzone.pypass:-
test_polygon_zone_trigger -
test_polygon_zone_initialization
-
-
Manual verification: Ran
bug_recreation.pyandfix_test.pyon a sample image to confirm:- Original detection spanning multiple ROIs → previously counted in multiple zones
- After fix → correctly counted in only one zone
Any specific deployment considerations
- Only the
trigger()method ofPolygonZonewas modified. - No API or interface changes.
- No secrets or additional dependencies.
Docs
- [x] Docs updated?
- Updated docstring of
trigger()explaining anchor calculation and bug fix.
- Updated docstring of