perf(cv2): simplify fallback operations - #2441
Merged
Merged
Conversation
- Remove unused compatibility operations and use focused Pillow and NumPy paths to reduce maintained fallback code. - Preserve numerical decisions and hot-path performance with exact regression coverage and bounded algorithms. --- Co-authored-by: Codex <codex@openai.com>
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## develop #2441 +/- ##
=======================================
Coverage 86% 87%
=======================================
Files 84 84
Lines 11786 11785 -1
=======================================
+ Hits 10170 10196 +26
+ Misses 1616 1589 -27 🚀 New features to boost your workflow:
|
Contributor
There was a problem hiding this comment.
Pull request overview
This PR streamlines Supervision’s private _cv2 compatibility layer by removing unused fallback surface area and tightening the cv2-free implementations, while improving fidelity/performance for several core image-processing primitives (contours, connected components, color conversion, resize/text rendering). It also updates library consumers and tests to validate the new, narrower contracts.
Changes:
- Reduced
_cv2API surface (removed unused transform/distance APIs) and introduced a minimalfind_contours()geometry helper used by mask-to-polygon conversion. - Improved numeric fidelity/performance in cv2-free paths: fixed-point
BGR2GRAYforuint8, vectorized connected-component stats, refinedapproxPolyDP, and optimized Pillow-based text/resize behavior. - Updated docs/changelog and expanded regression tests around contours, resize, text clipping, and edge-distance behavior.
Reviewed changes
Copilot reviewed 24 out of 24 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/detection/test_line_counter.py | Adds regression test ensuring rotated line-zone labels use the Pillow rotation path and preserve alpha behavior. |
| tests/cv2/test_transform.py | Removes tests for fallback APIs that were deleted from the _cv2 surface (affine + distance transform), keeping blur coverage. |
| tests/cv2/test_text.py | Adds coverage for clipping text when the baseline origin is partially off-canvas. |
| tests/cv2/test_image.py | Adds tighter resize regression tests for uint8 linear interpolation and float interpolation threshold behavior. |
| tests/cv2/test_geometry.py | Adds randomized regression tests for approxPolyDP and float32 drift bounds for polygon intersection. |
| tests/cv2/test_cv2.py | Updates expected _cv2 facade symbol set (replaces findContours with find_contours, removes unused symbols). |
| tests/cv2/test_contours.py | Updates contour expectations to geometry-only contract, adds facade test for find_contours, and validates chamfer distances/edge-distance behavior. |
| tests/cv2/test_constants.py | Updates constants expectations to reflect removed compatibility exports. |
| tests/cv2/test_components.py | Adds high-component-image regression test for vectorized connected-component stats matching OpenCV. |
| tests/cv2/test_color.py | Adds regression test for uint8 fixed-point BGR2GRAY rounding matching OpenCV. |
| src/supervision/detection/utils/masks.py | Introduces fixed-point 3×3 L2 chamfer transform and updates edge-distance filtering to avoid distanceTransform. |
| src/supervision/detection/utils/converters.py | Switches mask→polygon extraction to use the new cv2.find_contours() helper. |
| src/supervision/detection/line_zone.py | Replaces cv2 affine rotation with Pillow-based rotation for label images. |
| src/supervision/_cv2/constants.py | Removes unused _DIST_L2 constant from the private constants set. |
| src/supervision/_cv2/_video.py | Uses PyAV’s stream-template API to replace the removed local stream-copy helper. |
| src/supervision/_cv2/_transform.py | Removes unused affine and distance-transform fallbacks, leaving _blur. |
| src/supervision/_cv2/_text.py | Reworks Pillow rasterization to build a glyph-sized mask and clip safely at image boundaries. |
| src/supervision/_cv2/_image.py | Delegates uint8 bilinear resize to Pillow and keeps numeric half-pixel sampling for non-uint8 paths. |
| src/supervision/_cv2/_geometry.py | Replaces approxPolyDP simplification with an OpenCV-matching stack traversal + cleanup step. |
| src/supervision/_cv2/_contours.py | Simplifies contour extraction to return geometry only (no hierarchy construction). |
| src/supervision/_cv2/_components.py | Vectorizes centroid/statistics computation and uses ndimage.find_objects for component bounds. |
| src/supervision/_cv2/_color.py | Implements exact OpenCV-style fixed-point BGR2GRAY for uint8 images. |
| src/supervision/_cv2/init.py | Removes unused cv2-compat symbols and adds find_contours() as the minimal geometry facade function. |
| docs/changelog.md | Documents the fallback API reduction and the new precision/performance behaviors. |
- Preserve INTER_LINEAR uint8 reductions within one LSB while retaining the resize performance budget and numeric RGBA handling. - Restore repeated-endpoint contour anchors and bound cross-platform chamfer coefficient drift in regression tests. --- Co-authored-by: Codex <codex@openai.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.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 focuses on removing unused cv2 compatibility symbols and streamlining the cv2-free fallback implementation, while also improving performance and precision in key image processing operations. The main changes include removing unused API surface from the
_cv2module, adding a minimalfind_contourshelper, vectorizing connected-component statistics, and updating color conversion to exactly match OpenCV for uint8 images.API surface reduction and fallback simplification:
Removed unused raw compatibility symbols from
src/supervision/_cv2/__init__.py, includingdistanceTransform,getRotationMatrix2D, andwarpAffine, as well as related constants and their exports. These are no longer needed by production consumers, which now use more targeted operations. [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12]Added a new
find_contoursfunction tosrc/supervision/_cv2/__init__.pythat provides only the mask-to-polygon conversion geometry required by production, further narrowing the fallback API. [1] [2]Performance and precision improvements:
Updated the cv2-free fallback for connected-component statistics in
src/supervision/_cv2/_components.pyto use vectorized NumPy operations andscipy.ndimage.find_objects, significantly reducing overhead and improving performance.Changed the BGR-to-gray conversion in
src/supervision/_cv2/_color.pyto use OpenCV's fixed-point uint8 arithmetic, ensuring exact matches for all possible input colors.Changelog and documentation:
Minor refactoring:
src/supervision/_cv2/_contours.py.