supervision/tests/detection
Adithi Sreenath c010656161
fix: prevent single object from appearing in multiple polygon zones (#1991)
* fix: prevent single object from appearing in multiple polygon zones

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.

* Add regression test for PolygonZone trigger issue #1987 and remove unused `frame_resolution_wh` attribute
* refactor(polygon_zone): vectorize trigger() and strengthen tests

Replace the O(n×m) Python double-loop in PolygonZone.trigger() with
vectorized NumPy. Semantics are identical: compute a (num_anchors,
num_detections) in_bounds mask, use np.clip solely for safe fancy-index
access, then AND with the polygon mask and reduce with np.all(axis=0).
Also removes the now-unused `from dataclasses import replace` import and
a latent np.all(axis=1) call on a 1D array.

Test improvements:
- Group into TestPolygonZoneInit / TestPolygonZoneTrigger classes
- Replace the trivially-passing regression (sum=0 on both old and new
  code) with adjacent zones + straddling detection that gives sum=2 on
  the old clip_boxes implementation and sum=1 on the fix
- Rename tests to describe behaviour, not issue numbers
- Add test_out_of_bounds_anchor_excluded and
  test_anchor_on_polygon_boundary_included edge cases

* test(polygon_zone): verify current_count updates with expected results during trigger

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: jirka <6035284+Borda@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-10 21:19:09 +01:00
..
tools refactor: reorganize project structure and update module imports (#2112) 2026-02-03 15:24:47 +01:00
utils Add `match` arguments to all `pytest.raises` for more precise error validation in tests (#2145) 2026-02-10 21:22:55 +09:00
__init__.py refactor: reorganize project structure and update module imports (#2112) 2026-02-03 15:24:47 +01:00
test_core.py Fix mask_annotate for int dtypes (#1445) 2026-02-20 10:43:56 +01:00
test_csv.py refactor: reorganize project structure and update module imports (#2112) 2026-02-03 15:24:47 +01:00
test_from_adapters.py refactor: reorganize project structure and update module imports (#2112) 2026-02-03 15:24:47 +01:00
test_from_sam.py Add support for SAM3 PVS output (#2152) 2026-02-19 17:09:31 +01:00
test_json.py refactor: reorganize project structure and update module imports (#2112) 2026-02-03 15:24:47 +01:00
test_line_counter.py refactor docstrings in `scr/supervision/detection` (#2162) 2026-03-10 18:40:05 +01:00
test_polygon_zone_annotator.py refactor: reorganize project structure and update module imports (#2112) 2026-02-03 15:24:47 +01:00
test_polygonzone.py fix: prevent single object from appearing in multiple polygon zones (#1991) 2026-03-10 21:19:09 +01:00
test_vlm.py Add `match` arguments to all `pytest.raises` for more precise error validation in tests (#2145) 2026-02-10 21:22:55 +09:00