Skip to content

fix(detection): handle empty polygons in filter_polygons_by_area and polygon_to_xyxy - #2761

Merged
Borda merged 5 commits into
roboflow:developfrom
ghanashyam03:fix/empty-polygon-handling
Oct 20, 2026
Merged

Borda merged 5 commits into
roboflow:developfrom
ghanashyam03:fix/empty-polygon-handling

Conversation

@ghanashyam03

Copy link
Copy Markdown
Contributor

Problem

sv.filter_polygons_by_area crashes with ValueError: zero-size array to reduction operation minimum which has no identity when any polygon in the input list has zero vertices (shape (0, 2)). This happens because _to_local_cv2_polygon calls polygon.min(axis=0) unconditionally without checking for empty polygons.
Similarly, sv.polygon_to_xyxy crashes with the same reduction ValueError when given an empty polygon instead of returning an all-zero bounding box matching Supervision's empty geometry convention (e.g. in mask_to_xyxy).

Root cause

  • In _to_local_cv2_polygon, polygon.min(axis=0) lacks a guard for len(polygon) == 0. An empty polygon has area 0.0 in OpenCV (cv2.contourArea(np.empty((0, 2), dtype=np.float32)) == 0.0). Returning (np.empty((0, 2), dtype=np.float32), np.zeros(2, dtype=polygon.dtype)) allows cv2.contourArea to evaluate 0.0, so filter_polygons_by_area filters empty polygons cleanly according to min_area / max_area without crashing.
  • In polygon_to_xyxy, calling np.min(polygon, axis=0) on a zero-size array raises ValueError. Checking len(polygon) == 0 and returning np.zeros(4, dtype=output_dtype) produces an all-zeros box while preserving caller dtype.

Change

  • In src/supervision/detection/utils/polygons.py: added an empty-array check in _to_local_cv2_polygon to return (np.empty((0, 2), dtype=np.float32), np.zeros(2, dtype=polygon.dtype)) when len(polygon) == 0.
  • In src/supervision/detection/utils/converters.py: added an empty-array check in polygon_to_xyxy to return np.zeros(4, dtype=output_dtype) when len(polygon) == 0.
  • Added regression tests in tests/detection/utils/test_polygons.py and tests/detection/utils/test_converters.py covering empty polygons with area constraints and dtype preservation.

Regression coverage

  • tests/detection/utils/test_polygons.py::test_filter_polygons_by_area: parameterized with empty polygon cases (min_area filter, max_area filter, and mixed empty + valid polygons).
  • tests/detection/utils/test_converters.py::test_polygon_to_xyxy: parameterized with empty-polygon-int and empty-polygon-float32 verifying all-zeros bounding box output and dtype preservation.

Validation

  • pytest tests/detection/utils/test_polygons.py tests/detection/utils/test_converters.py — 221 passed
  • pytest tests/detection tests/geometry — 2378 passed, 1 skipped
  • git diff --check — clean

@CLAassistant

CLAassistant commented Oct 17, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@ghanashyam03

Copy link
Copy Markdown
Contributor Author

CLA assistant check Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.You have signed the CLA already but the status is still pending? Let us recheck it.

Done

@Borda Borda added the bug Something isn't working label Oct 19, 2026
@Borda
Borda requested a balanced review from Copilot October 19, 2026 11:55

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The user-visible fixes lack the changelog entry explicitly required by repository guidance.

1 open issue
What changed in this PR

Fixes empty-polygon crashes in Supervision’s detection utilities.

Changes:

  • Treats empty polygons as zero-area inputs when filtering.
  • Returns zero bounding boxes with preserved NumPy dtype.
  • Adds regression cases for filtering, mixed inputs, and dtype preservation.

Code: 5/5; tests: 4/5; documentation: 3/5. Risk: 1/5. Assessment covers behavior, regression coverage, and repository conventions; tests were not rerun.

File Description
tests/​detection/​utils/​test_polygons.py Tests empty and mixed polygons with area constraints.
tests/​detection/​utils/​test_converters.py Tests empty inputs and bounding-box dtype preservation.
src/​supervision/​detection/​utils/​polygons.py Guards empty polygons before coordinate reduction.
src/​supervision/​detection/​utils/​converters.py Returns zero boxes for empty polygons and normalizes nonempty inputs.

Review effort: Balanced

Comment thread src/supervision/detection/utils/converters.py
@Borda Borda changed the title fix(detection): handle empty polygons in filter_polygons_by_area and polygon_to_xyxy fix(detection): handle empty polygons in filter_polygons_by_area and polygon_to_xyxy Oct 20, 2026
@codecov

codecov Bot commented Oct 20, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94%. Comparing base (cebbc5f) to head (7a00b73).

Additional details and impacted files
@@           Coverage Diff           @@
##           develop   #2761   +/-   ##
=======================================
  Coverage       94%     94%           
=======================================
  Files           83      83           
  Lines        13644   13650    +6     
=======================================
+ Hits         12841   12848    +7     
+ Misses         803     802    -1     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Borda
Borda merged commit a67081f into roboflow:develop Oct 20, 2026
36 checks passed
Borda added a commit that referenced this pull request Oct 20, 2026
…d `polygon_to_xyxy` (#2761)

* fix: handle empty polygons in filter_polygons_by_area and polygon_to_xyxy
* fix: preserve array dtype for empty polygons in polygon_to_xyxy

---------

Co-authored-by: Jirka Borovec <6035284+Borda@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants