Skip to content

refactor(cv2): add optional backend facade - #2430

Merged
Borda merged 9 commits into
developfrom
cv2/1
Jul 15, 2026
Merged

refactor(cv2): add optional backend facade#2430
Borda merged 9 commits into
developfrom
cv2/1

Conversation

@Borda

@Borda Borda commented Jul 15, 2026

Copy link
Copy Markdown
Member

This pull request introduces a new private OpenCV compatibility layer (src/supervision/_cv2/__init__.py) and refactors the codebase to use it. The main goal is to centralize OpenCV imports and provide a fallback mechanism when OpenCV is not installed, improving robustness and portability. All direct cv2 imports throughout the codebase are replaced with imports from this new compatibility module.

Key changes:

OpenCV Compatibility Layer

  • Added a new module src/supervision/_cv2/__init__.py that acts as a compatibility layer for OpenCV. It exposes OpenCV constants and functions if cv2 is available, and provides clear error messages or fallbacks if not.

Refactoring Imports

Consistency in Examples

These changes make the codebase more robust to environments where OpenCV may not be installed and simplify future maintenance by centralizing OpenCV-related logic.

Co-authored-by: Codex <codex@openai.com>
@codecov

codecov Bot commented Jul 15, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 46.08696% with 62 lines in your changes missing coverage. Please review.
✅ Project coverage is 86%. Comparing base (16814ac) to head (8117950).

Additional details and impacted files
@@           Coverage Diff           @@
##           develop   #2430   +/-   ##
=======================================
- Coverage       87%     86%   -0%     
=======================================
  Files           72      73    +1     
  Lines        10679   10774   +95     
=======================================
+ Hits          9264    9297   +33     
- Misses        1415    1477   +62     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

Pull request overview

This PR introduces a private OpenCV compatibility facade (supervision._cv2) and refactors Supervision’s internal OpenCV usage to route through that single boundary. This centralizes OpenCV symbol imports and enables clearer failure behavior (or limited constant availability) in environments where cv2 cannot be imported.

Changes:

  • Added src/supervision/_cv2/__init__.py as a curated OpenCV surface with a fallback mode that raises BackendUnavailableError for operations without an available backend.
  • Replaced direct import cv2 usage across multiple modules/tests/doc examples with from supervision import _cv2 as cv2.
  • Added a dedicated test suite (tests/cv2/) to validate the facade’s exported surface, constant alignment, and fallback behavior.

Reviewed changes

Copilot reviewed 29 out of 29 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
tests/utils/test_video.py Switch test OpenCV import to the _cv2 facade.
tests/utils/test_image.py Switch test OpenCV import to the _cv2 facade.
tests/cv2/test_cv2.py Add tests asserting _cv2 exports/behavior and fallback import scenarios.
tests/cv2/init.py Introduce package marker/docstring for cv2-facade tests.
tests/annotators/test_core.py Switch test OpenCV import to the _cv2 facade.
src/supervision/utils/video.py Route video utilities’ OpenCV usage through _cv2.
src/supervision/utils/notebook.py Route notebook rendering conversions through _cv2.
src/supervision/utils/image.py Route image utilities’ OpenCV usage through _cv2.
src/supervision/utils/conversion.py Route conversion helpers’ OpenCV usage through _cv2.
src/supervision/metrics/detection.py Update lazy OpenCV import sites to use _cv2.
src/supervision/key_points/core.py Update doc examples to import OpenCV via _cv2.
src/supervision/key_points/annotators.py Route keypoint annotators’ OpenCV usage through _cv2.
src/supervision/draw/utils.py Route drawing helpers’ OpenCV usage through _cv2.
src/supervision/detection/utils/polygons.py Route polygon helpers’ OpenCV usage through _cv2.
src/supervision/detection/utils/masks.py Route mask helpers’ OpenCV usage through _cv2.
src/supervision/detection/utils/iou_and_nms.py Route IoU/NMS helpers’ OpenCV usage through _cv2.
src/supervision/detection/utils/internal.py Route internal detection helpers’ OpenCV usage through _cv2.
src/supervision/detection/utils/converters.py Route converter helpers’ OpenCV usage through _cv2.
src/supervision/detection/tools/polygon_zone.py Route PolygonZone OpenCV usage through _cv2.
src/supervision/detection/tools/inference_slicer.py Update doc examples to import OpenCV via _cv2.
src/supervision/detection/line_zone.py Route LineZone OpenCV usage through _cv2.
src/supervision/detection/core.py Update doc examples to import OpenCV via _cv2.
src/supervision/detection/compact_mask.py Route compact mask resize path through _cv2.
src/supervision/dataset/utils.py Route dataset utilities’ OpenCV usage through _cv2.
src/supervision/dataset/formats/pascal_voc.py Route Pascal VOC format OpenCV usage through _cv2.
src/supervision/dataset/core.py Route dataset core OpenCV usage through _cv2.
src/supervision/classification/core.py Update doc examples to import OpenCV via _cv2.
src/supervision/annotators/core.py Route annotators’ OpenCV usage (and examples) through _cv2.
src/supervision/_cv2/init.py New OpenCV facade module with fallback constants + explicit failure for missing backend.

Comment thread tests/cv2/test_cv2.py Outdated
Comment thread tests/cv2/test_cv2.py Outdated
Comment thread tests/cv2/test_cv2.py Outdated
Borda and others added 8 commits July 15, 2026 19:53
[resolve group] PR #2430 — item 1

---
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
Co-authored-by: OpenAI Codex <codex@openai.com>
[resolve group] PR #2430 — items 2 3

---
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
Co-authored-by: OpenAI Codex <codex@openai.com>
---
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
Co-authored-by: Codex <codex@openai.com>
@Borda
Borda merged commit 8ecd9a6 into develop Jul 15, 2026
26 checks passed
@Borda
Borda deleted the cv2/1 branch July 15, 2026 18:53
@Borda Borda mentioned this pull request Jul 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants