Conversation
Co-authored-by: Codex <codex@openai.com>
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## develop #2432 +/- ##
========================================
Coverage 86% 86%
========================================
Files 78 81 +3
Lines 11020 11376 +356
========================================
+ Hits 9507 9837 +330
- Misses 1513 1539 +26 🚀 New features to boost your workflow:
|
Contributor
There was a problem hiding this comment.
Pull request overview
This PR expands supervision._cv2 with pure-Python (and SciPy-backed) fallbacks for several OpenCV geometry/connected-components operations, improving portability when native OpenCV is unavailable.
Changes:
- Add fallback implementations for connected components, contour tracing, and polygon geometry in
src/supervision/_cv2/. - Wire the new fallbacks into the
_cv2facade exports and remove the unsupportedRETR_CCOMPconstant usage. - Add tests comparing fallback behavior against OpenCV reference implementations and exercising key downstream consumers.
Assessment (n/5):
- Code quality: 3/5
- Testing: 3/5
- Documentation: 4/5
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/cv2/test_geometry.py | New tests validating fallback polygon geometry against OpenCV. |
| tests/cv2/test_contours.py | New tests validating fallback contour tracing + consumer integration. |
| tests/cv2/test_constants.py | Update facade-binding tests for the newly supported operations. |
| tests/cv2/test_components.py | New tests validating connected-components fallbacks vs OpenCV. |
| src/supervision/detection/utils/masks.py | Switch contains_holes to topology-based fallback + stabilize tie-breaking in segment filtering. |
| src/supervision/_cv2/constants.py | Remove _RETR_CCOMP constant (now unsupported in fallback surface). |
| src/supervision/_cv2/_geometry.py | New fallback implementations for area, polygon simplification, fill, and convex intersection. |
| src/supervision/_cv2/_contours.py | New Suzuki–Abe-style contour fallback + hierarchy reconstruction. |
| src/supervision/_cv2/_components.py | New SciPy-based connected-components + hole detection helper. |
| src/supervision/_cv2/init.py | Export new fallbacks from the _cv2 compatibility facade. |
[resolve group] PR #2432 — items 1 2 Item #1 (@Copilot): contour.mean(axis=0) can fall outside concave contours, corrupting the parent/child hierarchy in _parents. Now uses a scanline-midpoint interior point. Item #2 (@Copilot): remapped_parents used ordered_indices.index() in a comprehension (O(n^2)). Now precomputes an index_map for O(1) lookups. Challenge: both evidence=VALID suggestion=VALID resolution=as-suggested --- Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
…back [resolve group] PR #2432 — items 3 5 Item #3 (@Copilot): np.sort(..., axis=0) sorted x/y columns independently, letting mismatched point sets appear equal. Now sorts points lexicographically (row-wise) via np.lexsort. Item #5 (@Copilot): filter_segments_by_distance calls connectedComponentsWithStats, which was not monkeypatched to the fallback, so the test silently used native OpenCV. Added the missing monkeypatch alongside the existing ones. Challenge: both evidence=VALID suggestion=VALID resolution=as-suggested --- Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
[resolve group] PR #2432 — items 4 Item #4 (@Copilot): _contains_holes was imported into the fallback facade but never used or re-exported there, which trips unused-import lint checks. Removed from the import list. Challenge: evidence=VALID suggestion=VALID resolution=codex-direct --- Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com> Co-authored-by: OpenAI Codex <codex@openai.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This pull request introduces several new fallback implementations for OpenCV-like image processing functions in the
supervision._cv2module. The main goal is to provide Python-based fallbacks for key connected components, contour, and polygon geometry operations when the native OpenCV backend is unavailable. The changes involve adding new modules, updating the main__init__.pyto export these functions, and removing references to unavailable constants and methods.The most important changes are:
New Fallback Implementations
_components.pywith Python/scipy-based implementations forconnectedComponents,connectedComponentsWithStats, and a utility_contains_holesfor mask topology analysis. ([src/supervision/_cv2/_components.pyR1-R102](https://github.com/roboflow/supervision/pull/2432/files#diff-27518adcc08235320d81aa9ecdad8953f335345c8bd20615b93020b00224f61aR1-R102))_contours.pywith a fallback Suzuki-Abe contour tracing algorithm, including_find_contoursforfindContoursand related helpers. ([src/supervision/_cv2/_contours.pyR1-R261](https://github.com/roboflow/supervision/pull/2432/files#diff-ddbb2f5922d838abeaa4293661ccd24c3eac3a99de45f8e7ac965e12ed04734aR1-R261))_geometry.pyproviding polygon geometry methods:_approx_poly_dp(Douglas-Peucker simplification),_contour_area,_fill_poly, and_intersect_convex_convex. ([src/supervision/_cv2/_geometry.pyR1-R208](https://github.com/roboflow/supervision/pull/2432/files#diff-b5a2be7aec829d71675e502a4f6d79423f020c74f19dd4764dbcbc73d6547ba6R1-R208))API and Constant Updates
src/supervision/_cv2/__init__.pyto export the new fallback implementations forapproxPolyDP,connectedComponents,connectedComponentsWithStats,contourArea,fillPoly,findContours, andintersectConvexConvex, replacing previous_unavailablestubs. ([[1]](https://github.com/roboflow/supervision/pull/2432/files#diff-86e4f932e0a4f2028060b775cb747808c89741df6a612664a14ad5a523310876R7-R18),[[2]](https://github.com/roboflow/supervision/pull/2432/files#diff-86e4f932e0a4f2028060b775cb747808c89741df6a612664a14ad5a523310876L141-R176))_RETR_CCOMPconstant and all references toRETR_CCOMPin the API, consolidating support onRETR_TREEonly. ([[1]](https://github.com/roboflow/supervision/pull/2432/files#diff-86e4f932e0a4f2028060b775cb747808c89741df6a612664a14ad5a523310876L45),[[2]](https://github.com/roboflow/supervision/pull/2432/files#diff-86e4f932e0a4f2028060b775cb747808c89741df6a612664a14ad5a523310876L79),[[3]](https://github.com/roboflow/supervision/pull/2432/files#diff-86e4f932e0a4f2028060b775cb747808c89741df6a612664a14ad5a523310876L202),[[4]](https://github.com/roboflow/supervision/pull/2432/files#diff-666cc43876a66ce9eb1a3308902462ecc588776d4aa07b185b60540c6a2534faL24))Internal Usage
masks.pyto import and use the new_contains_holesutility from_components.py. ([src/supervision/detection/utils/masks.pyR7](https://github.com/roboflow/supervision/pull/2432/files#diff-76873461bd415c05ef936307b0c76ea0b62559253a2f7a02e16211f795be9642R7))These changes significantly enhance the robustness and portability of the
supervision._cv2module by providing pure-Python/scipy fallbacks for essential image processing routines.