feat: add image URL loader#2372
Conversation
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## develop #2372 +/- ##
=======================================
Coverage 87% 87%
=======================================
Files 71 71
Lines 10485 10550 +65
=======================================
+ Hits 9098 9160 +62
- Misses 1387 1390 +3 🚀 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 |
Address review feedback from @SkalskiP: - extract _download_to_file into utils/file.py; both download_assets and load_image_from_url now download atomically via a temp file and os.replace - move prepare_url from utils/internal.py to utils/file.py, next to the download primitive - use md5 (usedforsecurity=False) for cache file names, matching the hashing used by the assets flow Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…l-loader # Conflicts: # src/supervision/assets/downloader.py # tests/assets/test_downloader.py # tests/utils/test_image.py
- rename prepare_url to private _normalize_http_url with url arg - share supervision cache root between assets and image URL loader - make URL validation errors descriptive (scheme, host, cause) - add ftp/javascript/data scheme-rejection tests - add sv.load_image_from_url changelog entry Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…l-loader # Conflicts: # docs/changelog.md
…low/supervision into codex/add-image-url-loader
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.