Make OpenCV an optional dependency to support contrib variants#2139
Make OpenCV an optional dependency to support contrib variants#2139
Conversation
Co-authored-by: Borda <6035284+Borda@users.noreply.github.com>
Co-authored-by: Borda <6035284+Borda@users.noreply.github.com>
Borda
left a comment
There was a problem hiding this comment.
@copilot, you need to reflect the base dep. drop and move to extra in CI so to also install it there and in the code path all cv2 imports with try/except and raise relevant message what is missing and what to add, in case it is not critical to use it in the module, move the imports to local in class or function
Also, reconsider if some of the cv2 functions can be replaced with other base dependencies, for example, read image can be used from matplotlib.pyplot.imread, etc.
There was a problem hiding this comment.
Pull request overview
This PR addresses issue #2114 by making OpenCV an optional install-time dependency to support different OpenCV variants (standard, contrib, headless) and prevent conflicts. Previously, supervision depended on opencv-python, which would override opencv-contrib-python when both were installed, preventing access to contrib modules like CSRT tracker.
Changes:
- Removed
opencv-pythonfrom core dependencies inpyproject.toml - Added 4 optional extras for different OpenCV variants:
headless,desktop,desktop-contrib,headless-contrib - Updated installation documentation in README.md, docs/index.md, and docs/changelog.md to explain the new installation options and document the breaking change
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 7 comments.
| File | Description |
|---|---|
| pyproject.toml | Removed opencv-python from core dependencies and added 4 optional extras for different OpenCV variants |
| README.md | Added installation instructions for choosing OpenCV variants and clarified that OpenCV is required |
| docs/index.md | Added comprehensive installation instructions across pip, poetry, uv, and rye for different OpenCV variants |
| docs/changelog.md | Documented the breaking change with migration instructions for version 0.28.0rc0 |
| ```bash | ||
| uv pip install supervision | ||
| # With headless OpenCV (recommended for servers) | ||
| uv pip install supervision[headless] | ||
| ``` | ||
|
|
||
| For uv projects: | ||
|
|
||
| ```bash | ||
| uv add supervision | ||
| uv add supervision --extra headless | ||
| ``` |
There was a problem hiding this comment.
The uv installation instructions are incomplete compared to the pip section. The pip section shows all four OpenCV variants with descriptive comments, but the uv section only shows the headless variant. For consistency and to help users make informed choices, add the other variants:
# For desktop applications (includes GUI support)
uv pip install supervision[desktop]# For desktop with extra modules (e.g., CSRT tracker)
uv pip install supervision[desktop-contrib]# For servers with extra modules (no GUI)
uv pip install supervision[headless-contrib]Also add: "If you already have OpenCV installed: uv pip install supervision"
There was a problem hiding this comment.
Done in commit 2b67b3d. Added all 4 OpenCV variants to the uv installation section with descriptive comments, plus instructions for when OpenCV is already installed.
| ```bash | ||
| rye add supervision | ||
| rye add supervision --features headless | ||
| ``` |
There was a problem hiding this comment.
The rye installation instructions are incomplete compared to other installation methods. Only the headless variant is shown. For consistency and to help users make informed choices, add examples for the other variants:
# For desktop applications (includes GUI support)
rye add supervision --features desktop# For desktop with extra modules (e.g., CSRT tracker)
rye add supervision --features desktop-contrib# For servers with extra modules (no GUI)
rye add supervision --features headless-contribThere was a problem hiding this comment.
Done in commit 2b67b3d. Added all 4 OpenCV variants to the rye installation section with descriptive comments matching the other package managers.
docs/changelog.md
Outdated
| !!! breaking "Breaking Change" | ||
| **OpenCV is now an optional dependency.** To ensure compatibility with different OpenCV variants (standard, contrib, headless), `opencv-python` has been removed from the core dependencies. Users must now explicitly choose their preferred OpenCV package: | ||
|
|
||
| - `pip install supervision[headless]` - Installs `opencv-python-headless` (recommended for servers) | ||
| - `pip install supervision[desktop]` - Installs `opencv-python` (includes GUI support) | ||
| - `pip install supervision[desktop-contrib]` - Installs `opencv-contrib-python` (extra modules + GUI) | ||
| - `pip install supervision[headless-contrib]` - Installs `opencv-contrib-python-headless` (extra modules, no GUI) | ||
|
|
||
| If you already have OpenCV installed, you can install supervision without extras: `pip install supervision` | ||
|
|
||
| This change resolves conflicts where `opencv-python` would override `opencv-contrib-python`, preventing access to extra modules like CSRT tracker. |
There was a problem hiding this comment.
The breaking change notice should clarify the runtime behavior more explicitly. Currently it states "OpenCV is now an optional dependency" which could be interpreted as "supervision works without OpenCV."
Consider rephrasing to be more explicit about the requirement:
"OpenCV is required at runtime but is now an optional install-time dependency. To ensure compatibility with different OpenCV variants (standard, contrib, headless), opencv-python has been removed from the core dependencies. Users must now have OpenCV installed either by:
- Explicitly choosing an OpenCV extra when installing supervision (recommended)
- Having a compatible OpenCV package already installed
Without OpenCV installed, import supervision will fail with an ImportError."
This makes it clear that OpenCV is still required to use the library, just not automatically installed.
There was a problem hiding this comment.
Done in commit 2b67b3d. Clarified that "OpenCV is required at runtime but is now an optional install-time dependency" and added a note explaining that functions will raise ImportError without OpenCV installed. This makes it clear OpenCV is still required to use the library.
| Supervision requires OpenCV. Choose the variant that best fits your needs: | ||
|
|
||
| ```bash | ||
| # Recommended for servers (no GUI) | ||
| pip install supervision[headless] | ||
| ``` | ||
|
|
||
| ```bash | ||
| # For desktop applications (includes GUI support) | ||
| pip install supervision[desktop] | ||
| ``` | ||
|
|
||
| ```bash | ||
| # For desktop with extra modules (e.g., CSRT tracker) | ||
| pip install supervision[desktop-contrib] | ||
| ``` | ||
|
|
||
| ```bash | ||
| # For servers with extra modules (no GUI) | ||
| pip install supervision[headless-contrib] | ||
| ``` | ||
|
|
||
| If you already have OpenCV installed: | ||
|
|
||
| ```bash | ||
| pip install supervision | ||
| ``` |
There was a problem hiding this comment.
Consider adding a warning in the installation documentation that users should only install ONE OpenCV variant at a time. The different opencv-python packages are mutually exclusive and installing multiple extras that include different OpenCV variants will cause conflicts.
For example, add a note like:
"
There was a problem hiding this comment.
Done in commit 2b67b3d. Added warning in the pip installation section:
opencv-python packages (standard, contrib, headless, etc.) are mutually exclusive and cannot coexist in the same environment.
…alls - Wrapped import cv2 with try/except in 16 files across detection, dataset, key_points, draw, and utils modules - Added ensure_cv2_installed() import and calls to all __init__ methods, public functions, and classmethods that use cv2 - Follows pattern established in utils/internal.py, conversion.py, video.py, and image.py - Files updated: line_zone.py, polygon_zone.py, polygons.py, masks.py, converters.py, internal.py, dataset/core.py, dataset/utils.py, pascal_voc.py, key_points/annotators.py, draw/utils.py, utils/notebook.py - All files compile successfully with py_compile Co-authored-by: Borda <6035284+Borda@users.noreply.github.com>
Co-authored-by: Borda <6035284+Borda@users.noreply.github.com>
Done in commits b4b3422 and b7aba15: ✅ CI workflows updated - Added ✅ All cv2 imports wrapped - 16 files updated with try/except blocks and ✅ Module-level cv2 constants fixed - Replaced The package now:
Regarding replacing cv2 functions with base dependencies: Most cv2 usage is for core functionality (image processing, video I/O, drawing) that doesn't have direct equivalents in matplotlib. The cv2.imread in examples/docstrings don't need changes as they're just documentation. |
Codecov Report❌ Patch coverage is ❌ Your patch check has failed because the patch coverage (64%) is below the target coverage (95%). You can increase the patch coverage or adjust the target coverage. Additional details and impacted files@@ Coverage Diff @@
## develop #2139 +/- ##
========================================
- Coverage 72% 72% -0%
========================================
Files 61 61
Lines 7249 7359 +110
========================================
+ Hits 5246 5309 +63
- Misses 2003 2050 +47 🚀 New features to boost your workflow:
|
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
|
@copilot, overall, the nesure method seems overengineering, what about just local imports? |
…ntime behavior Co-authored-by: Borda <6035284+Borda@users.noreply.github.com>
The Should I simplify to local imports and remove the helper function, accepting less helpful error messages? Or keep the current approach for better UX? Current error: vs. simplified error: |
Summary
✅ All tasks completed successfully!
This PR makes OpenCV an optional dependency for supervision, resolving the issue where
opencv-pythonwould overrideopencv-contrib-python.Implementation Checklist
opencv-python>=4.5.5.64from main dependenciesChanges Made
1. Core dependency management
2. Import wrapping (16 files)
All cv2 imports wrapped with try/except:
3. Error handling
ensure_cv2_installed()function in utils/internal.py with installation instructions4. Documentation
Testing
✅ Verified supervision can be imported without OpenCV installed
✅ Verified helpful ImportError raised when cv2 functions are called
✅ Verified full functionality works with opencv-python-headless installed
✅ Tested basic operations: pillow_to_cv2, cv2_to_pillow, Detections, BoxAnnotator
Installation Examples
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.