feat: add image URL loader#2372
Conversation
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## develop #2372 +/- ##
=======================================
Coverage 86% 87%
=======================================
Files 70 70
Lines 10175 10234 +59
=======================================
+ Hits 8799 8853 +54
- Misses 1376 1381 +5 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
Adds a new convenience loader for fetching images over HTTP(S) into OpenCV/NumPy (load_image_from_url) and wires it into the public API, along with unit tests and API docs.
Changes:
- Introduces
load_image_from_url()insupervision.utils.image(URL validation, download viarequests, decode via OpenCV, optional on-disk caching). - Adds
prepare_url()helper insupervision.utils.internalfor HTTP(S) URL normalization/validation. - Exports the new helper from
supervision.__init__, and documents/tests the new behavior.
Quality / Testing / Docs (n/5):
- Code quality: 4/5
- Testing: 5/5
- Docs: 3/5
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/utils/test_image.py | Adds focused unit tests for URL loading, caching behavior, and failure modes. |
| src/supervision/utils/internal.py | Adds shared URL normalization/validation helper (prepare_url). |
| src/supervision/utils/image.py | Implements load_image_from_url (download/decode/cache) and related helpers. |
| src/supervision/init.py | Exposes load_image_from_url from the top-level package. |
| docs/utils/image.md | Adds mkdocs API entry for load_image_from_url. |
…l-loader # Conflicts: # tests/utils/test_image.py
|
In particular, it should use the same hashing strategy, cache files in the same location, and follow similar timeout, retry, and loading behavior. At the moment, these two paths do not share any code, and their behavior is quite different. I’d extract one small primitive into def _download_to_file(url: str, target: Path, *, timeout: float = 30.0, stream: bool = False) -> None:
"""Download `url` to `target` atomically using a temp file and `os.replace`."""Then both |
| warnings.warn(message, category=SupervisionWarnings, stacklevel=2) | ||
|
|
||
|
|
||
| def prepare_url(value: str) -> str: |
There was a problem hiding this comment.
utils/internal.py is focused on deprecation and introspection helpers. A URL normalizer used by a single caller feels out of place there.
Summary
Adds
load_image_from_url()tosupervision.utils.imageand exports it from the top-levelsupervisionpackage. The helper validates and normalizes HTTP(S) URLs, downloads the image withrequests, decodes it through OpenCV, and raises clear errors for invalid URLs, request failures, or undecodable image bytes.Also adds mkdocs API documentation and focused unit tests for successful decoding, invalid image bytes, request failures, and non-HTTP URL rejection.
Validation
uv run --frozen pytest tests/utils/test_image.py tests/utils/test_video.pySKIP=mypy uv run --frozen pre-commit run --files src/supervision/utils/internal.py src/supervision/utils/image.py src/supervision/utils/video.py src/supervision/__init__.py tests/utils/test_image.py tests/utils/test_video.py docs/utils/image.md docs/utils/video.mdgit diff --checkNote: the local mypy hook was skipped because it fails before checking this code due to a NumPy stub syntax issue under the repo's configured Python 3.9 target.