Skip to content

feat: add image URL loader#2372

Open
Erol444 wants to merge 19 commits into
developfrom
codex/add-image-url-loader
Open

feat: add image URL loader#2372
Erol444 wants to merge 19 commits into
developfrom
codex/add-image-url-loader

Conversation

@Erol444

@Erol444 Erol444 commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds load_image_from_url() to supervision.utils.image and exports it from the top-level supervision package. The helper validates and normalizes HTTP(S) URLs, downloads the image with requests, 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.py
  • SKIP=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.md
  • git diff --check

Note: 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.

Comment thread src/supervision/utils/image.py Outdated
Comment thread src/supervision/utils/image.py Outdated
@codecov

codecov Bot commented Jun 29, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.77108% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 87%. Comparing base (9897b07) to head (8197247).

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:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Erol444 Erol444 requested a review from Borda June 29, 2026 11:58
@Erol444 Erol444 marked this pull request as ready for review June 29, 2026 11:58
@Erol444 Erol444 requested a review from SkalskiP as a code owner June 29, 2026 11:58
Comment thread src/supervision/utils/image.py Outdated

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

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() in supervision.utils.image (URL validation, download via requests, decode via OpenCV, optional on-disk caching).
  • Adds prepare_url() helper in supervision.utils.internal for 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.

Comment thread src/supervision/utils/image.py
Comment thread src/supervision/utils/internal.py Outdated
@Borda Borda added the enhancement New feature or request label Jun 29, 2026
@Borda Borda changed the title [codex] add image URL loader feat: add image URL loader Jul 1, 2026
…l-loader

# Conflicts:
#	tests/utils/test_image.py
@SkalskiP

SkalskiP commented Jul 8, 2026

Copy link
Copy Markdown
Collaborator

supervision already has an assets package for downloading files from our curated pool of images and videos. Since this new utility also downloads images, I think we should keep its behavior close to the existing assets flow.

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 utils/file.py:

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 load_image_from_url and download_assets would call it.

Comment thread src/supervision/utils/internal.py Outdated
Erol444 and others added 2 commits July 9, 2026 10:58
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
@Erol444 Erol444 requested a review from SkalskiP July 9, 2026 09:23
Comment thread src/supervision/utils/file.py Outdated
Comment thread src/supervision/utils/image.py
Comment thread src/supervision/utils/file.py
Comment thread tests/utils/test_file.py
Comment thread src/supervision/utils/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>
@Erol444 Erol444 requested a review from SkalskiP July 9, 2026 14:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants