Skip to content

feat(depth): add DepthMap and DepthAnnotator - #2691

Merged
Borda merged 14 commits into
developfrom
feat/depth-1-show
Oct 8, 2026
Merged

Borda merged 14 commits into
developfrom
feat/depth-1-show

Conversation

@cfviotti

@cfviotti cfviotti commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor
A stereo pair from the Spring movie, the depth a stereo matcher computes from it, and the true depth

Top: one moment seen by two cameras 6.5 cm apart (Spring dataset). Bottom left: the depth a stereo matcher computes from the two views, coloured over the left view. Near things are warm, far things are cool, and pixels the matcher could not match stay uncoloured instead of being painted as far away. Bottom right: the dataset's true depth, for comparison.

The same depth map in the six colour tables

The same depth in each colour table. Turbo is the default. Viridis and Cividis stay readable in grayscale and for colour-blind readers.

▶️📓 Notebook Playground Demo (all 4 PRs together)

Description

supervision cannot show a depth map. Depth models (Depth Anything, YOLO26 depth, stereo matchers) all return one, and the usual fix is a hand-written cv2.applyColorMap call: it needs OpenCV, which supervision no longer requires, takes only 8-bit input, and paints pixels the model could not measure as if they were far away.

This PR adds a depth map type and an annotator that colours it. It does not require OpenCV:

import supervision as sv

depth_map = sv.DepthMap(disparity, kind="disparity_px")  # NaN, inf or <= 0 where there is no depth
annotated = sv.DepthAnnotator(opacity=0.6).annotate(image.copy(), depth_map)

Near objects come out warm and far ones cool, and pixels without depth stay uncoloured so the image shows through.

This is part 1 of 4. Later parts, each built only on this one: one colour range across a video, distance in metres per detection, and loaders for model outputs and dataset files.

Motivation and Context

Part of #1313: this covers depth maps. Normal and gradient maps from that issue are not included. Follows the note there to do it without cv2.

Changes Made

  • sv.DepthMap(values, kind): one frame of depth as float32. sv.DepthKind says what the values measure: disparity_px, depth_m or relative_inverse (a monocular model's normalised output). valid_mask marks the pixels that hold depth.
  • sv.DepthAnnotator(colormap, display_range, opacity):
    • colormap: Turbo (default), Viridis, Cividis, Inferno, Magma or grayscale (sv.DepthColormap), with near always at the warm or bright end.
    • display_range: "auto" (the map's 2nd to 98th percentile) or a fixed (low, high).
    • A metric map is coloured as inverse depth, which gives near detail most of the colours.
    • NumPy (BGR) and Pillow scenes, like every annotator. The map is stretched to the scene's size.
  • Colour tables are read from matplotlib's colormaps on first use, so no tables are copied into supervision. Colours are blended between entries so smooth surfaces show no bands.
  • Docs: reference pages under docs/depth/, a how-to (docs/how_to/visualize_depth.md) and a changelog entry.
  • Out of scope here: video colour range, metres and distance, model loaders (parts 2 to 4).

Testing

pytest tests/depth --doctest-modules src/supervision/depth   39 passed
pytest                                                       4832 passed, 79 skipped
pre-commit run                                               all hooks pass, mypy included
mkdocs build                                                 no new warnings
  • Added/updated tests, and the full suite passes locally
  • Updated docs (docstrings / mkdocs entry) for new or changed public API
  • Added a changelog entry in docs/changelog.md under Unreleased
  • A map with depth only on odd rows and columns (sparse LiDAR-style) still gets its real colour range
  • The images above are this annotator on frames of the Spring dataset (CC BY 4.0, credited in the how-to)

Additional Notes

  • The docs example image is hosted on media.roboflow.com with the other annotator examples.
  • opencv-python is not needed. Stretching the map to the scene uses NumPy indexing.

Colour any depth array over an image. Wrap a stereo disparity, metric
depth or relative depth array in sv.DepthMap with the kind of value it
holds, then draw it with sv.DepthAnnotator: near objects come out warm,
far ones cool, and pixels without depth stay unpainted so the image
shows through. It works on NumPy and Pillow images without OpenCV.

- sv.DepthKind names what the values measure. NaN, infinities and
  values at or below 0 (below 0 for relative maps) are no depth, and
  valid_mask marks the rest.
- sv.DepthAnnotator takes a colour table (sv.DepthColormap: Turbo,
  Viridis, Cividis, Inferno, Magma or grayscale), a display_range of
  "auto" (the map's own 2nd to 98th percentile) or a fixed (low, high)
  tuple, and an opacity. A metric map is coloured as inverse depth, so
  near detail gets most of the colours.
- Reference pages, a how-to guide and a changelog entry.

Closes #1313

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@codecov

codecov Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93%. Comparing base (ea95d6d) to head (6c17662).

Additional details and impacted files
@@           Coverage Diff            @@
##           develop   #2691    +/-   ##
========================================
  Coverage       93%     93%            
========================================
  Files           78      81     +3     
  Lines        11996   12228   +232     
========================================
+ Hits         11117   11349   +232     
  Misses         879     879            
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Borda
Borda requested a balanced review from Copilot October 7, 2026 17:30
@Borda Borda added the enhancement New feature or request label Oct 7, 2026
@cfviotti
cfviotti marked this pull request as ready for review October 7, 2026 17:35
@cfviotti
cfviotti requested a review from SkalskiP as a code owner October 7, 2026 17:35

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.

🟡 Changes recommended

Narrow valid display ranges are normalized incorrectly, and public examples recommend a private API.

3 open findings
What changed in this PR

Adds first-class depth-map storage and OpenCV-free visualization.

Changes:

  • Introduces DepthMap, depth kinds, colormaps, and DepthAnnotator.
  • Exports the APIs and adds comprehensive tests.
  • Adds reference docs, a how-to, navigation, and changelog entry.

Review scores: Code quality 3/5 · Testing 4/5 · Documentation 3/5

File Description
src/​supervision/​depth/​core.py Defines depth-map types and range calculation.
src/​supervision/​depth/​colormaps.py Adds vendored color tables and interpolation.
src/​supervision/​depth/​annotators.py Implements depth coloring, resizing, and blending.
src/​supervision/​depth/​__init__.py Initializes the depth package.
src/​supervision/​__init__.py Exports the new public APIs.
tests/​depth/​test_core.py Tests depth-map behavior.
tests/​depth/​test_colormaps.py Verifies color tables and mapping.
tests/​depth/​test_annotators.py Tests annotation, ranges, resizing, and Pillow support.
tests/​depth/​__init__.py Initializes the test package.
docs/​depth/​core.md Adds core API reference.
docs/​depth/​annotators.md Adds annotator reference and examples.
docs/​how_to/​visualize_depth.md Adds a depth visualization guide.
mkdocs.yml Adds depth pages to navigation.
docs/​changelog.md Records the new feature.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/supervision/depth/annotators.py Resolved
Comment thread docs/how_to/visualize_depth.md Resolved
Comment thread src/supervision/depth/annotators.py Resolved
cfviotti and others added 6 commits October 7, 2026 14:43
The how-to and the DepthAnnotator example read the image through
supervision._cv2, which the OpenCV migration guide marks private and not
application API. They now open it with Pillow, so the examples stay
OpenCV-free like the feature itself.

A fixed display_range narrower than 1e-20 no longer gets stretched by a
floor on its width; every range is already validated as low < high.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The six colour tables now come from matplotlib, already a dependency,
instead of 140 lines of vendored hex. Colours are byte-identical to the
old tables on matplotlib 3.6.3 and 3.10.3.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
No other annotator validates opacity, so DepthAnnotator no longer does
either. Also fixes a test docstring that mentioned cameras, which this
PR does not add.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The changelog says the annotator does not require OpenCV rather than
that it runs without it, and uses the ([#N](...)) link style. The
DepthKind docstring says relative_inverse only promises larger is nearer.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Borda added a commit that referenced this pull request Oct 8, 2026
[resolve No.5] Review by foundry:sw-engineer (PR #2691):
"**[high] `display_range=(low, high)` is read in the coloured unit (1/m f..."
Challenge: evidence=VALID suggestion=VALID resolution=as-suggested

---
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
Borda added a commit that referenced this pull request Oct 8, 2026
[resolve No.6] Review by foundry:sw-engineer (PR #2691):
"**[medium] Flat or single-valid-pixel map colour depends on `kind` and f..."
Challenge: evidence=VALID suggestion=VALID resolution=as-suggested

---
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
Borda added a commit that referenced this pull request Oct 8, 2026
[resolve No.7] Review by foundry:sw-engineer (PR #2691):
"**[medium] Explicit `display_range` validation gaps: wrong arity or `Non..."
Challenge: evidence=VALID suggestion=VALID resolution=as-suggested

---
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
Borda added a commit that referenced this pull request Oct 8, 2026
[resolve No.22] Review by foundry:solution-architect (PR #2691):
"**[low] Colour-range policy lives on the container and is consumed throu..."
Challenge: evidence=VALID suggestion=VALID resolution=as-suggested

---
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
Borda added a commit that referenced this pull request Oct 8, 2026
[resolve No.18] Review by foundry:perf-optimizer (PR #2691):
"**[low] `valid_mask` recomputed three times per annotate, plus extra flo..."
Challenge: evidence=VALID suggestion=REJECT resolution=self-resolved

---
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
Borda added a commit that referenced this pull request Oct 8, 2026
[resolve No.20] Review by foundry:solution-architect (PR #2691):
"**[medium] `DepthKind.RELATIVE_INVERSE` name and contract disagree; no k..."
Challenge: evidence=VALID suggestion=VALID resolution=as-suggested

---
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
Borda added a commit that referenced this pull request Oct 8, 2026
[resolve No.21] Review by foundry:solution-architect (PR #2691):
"**[low] `DepthMap` invariants unprotected and float32 input aliased** — ..."
Challenge: evidence=VALID suggestion=REJECT resolution=self-resolved

---
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
Borda added a commit that referenced this pull request Oct 8, 2026
[resolve No.15] Review by foundry:perf-optimizer (PR #2691):
"**[medium] Resize via 2-D broadcast fancy index is the slow path** — `sr..."
Challenge: evidence=VALID suggestion=VALID resolution=as-suggested

---
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
Borda added a commit that referenced this pull request Oct 8, 2026
[resolve No.16] Review by foundry:perf-optimizer (PR #2691):
"**[medium] Masked copy with a broadcast `(H,W,1)` mask** — `src/supervis..."
Challenge: evidence=VALID suggestion=VALID resolution=as-suggested

---
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
Borda added a commit that referenced this pull request Oct 8, 2026
[resolve No.9] Review by foundry:sw-engineer (PR #2691):
"**[medium] `_blend` re-implements the weighted sum available as `supervi..."
Challenge: evidence=VALID suggestion=VALID resolution=as-suggested

---
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
Borda added a commit that referenced this pull request Oct 8, 2026
[resolve No.8] Review by foundry:sw-engineer (PR #2691):
"**[medium] `opacity` unvalidated: out-of-range values silently no-op or ..."
Challenge: evidence=VALID suggestion=REJECT resolution=self-resolved

---
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
Borda added a commit that referenced this pull request Oct 8, 2026
[resolve No.23] Review by foundry:solution-architect (PR #2691):
"**[low] Map and scene aspect-ratio mismatch stretched silently** — `src/..."
Challenge: evidence=VALID suggestion=REJECT resolution=self-resolved

---
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
Borda added a commit that referenced this pull request Oct 8, 2026
[resolve No.4] Review by codecov[bot] (PR #2691):
"## [Codecov](https://app.codecov.io/gh/roboflow/supervision/pull/2691?dr..."
Challenge: evidence=VALID suggestion=REJECT resolution=self-resolved

---
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
Borda added a commit that referenced this pull request Oct 8, 2026
[resolve No.9] Review by foundry:sw-engineer (PR #2691):
"add a one-line docstring note on why _index_map samples pixel centres in..."
Challenge: evidence=VALID suggestion=VALID resolution=as-suggested

---
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
@Borda Borda changed the title feat(depth): add sv.DepthMap and sv.DepthAnnotator feat(depth): add DepthMap and DepthAnnotator Oct 8, 2026
Borda and others added 4 commits October 8, 2026 23:37
- fix(depth): read display_range in the map's own unit
- fix(depth): paint flat maps at the far end for all kinds
- fix(depth): validate display_range length and float32 span
- docs(depth): document the opacity clamp

[resolve group] PR #2691 — items 5 6 7 8

---
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
- refactor(depth): move auto range policy into annotators
- docs(depth): fix RELATIVE_INVERSE contract, add guidance
- docs(depth): document DepthMap aliasing and mask rules
- docs(depth): note per-axis stretch of other ratios
- docs(depth): explain centre sampling in _index_map

[resolve group] PR #2691 — items 22 20 21 23 9

---
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
- perf(depth): compute the valid mask once per annotate
- perf(depth): resample the map with 1-D takes
- perf(depth): copy blended pixels with a full mask
- refactor(depth): blend through _cv2.addWeighted

[resolve group] PR #2691 — items 18 15 16 9

---
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
- test(depth): cover TypeError, drop dead branch
- test(depth): cover kinds under auto and relative
- test(depth): cover annotator boundary inputs
- test(depth): widen DepthMap and enum tests
- test(depth): pin colours with literal oracles
- test(depth): tidy test names and docstrings

[resolve group] PR #2691 — items 4 10 11 12 13 14

---
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
@Borda
Borda force-pushed the feat/depth-1-show branch from 711cc55 to 20af081 Compare October 8, 2026 21:45
- `DepthAnnotator.annotate` raises `ValueError` for a scene that is not uint8, so OpenCV and the NumPy fallback blend the same input and `opacity >= 1` no longer writes 0-255 into a float scene
- `display_range` that is not a pair of numbers (`5`, `None`, `"12"`, non-numeric bounds) raises `ValueError` naming the parameter instead of `TypeError`
- the reciprocal in `_Conversion.apply` ignores float32 overflow, so a depth near zero inverts to infinity without a `RuntimeWarning`
- tests cover opacity outside 0 to 1, partial opacity over a map without holes, holes under a stretched map, list/int/array ranges and a metric range whose inverse overflows float32

---
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
@Borda
Borda merged commit 33e06f8 into develop Oct 8, 2026
39 checks passed
@Borda
Borda deleted the feat/depth-1-show branch October 8, 2026 22:28
cfviotti added a commit that referenced this pull request Oct 9, 2026
Brings in the squash-merged #2691 with Jirka's follow-ups. Adaptations:
- DepthMap._percentile_range stays removed (it now lives in annotators.py
  as _percentile_range); the loaders never called it.
- visualize_depth.md: the relative-depth inversion example imports numpy
  itself, since the loading section now imports numpy only in the Stereo tab.
- from_transformers docstring: relative_inverse is suggested for Depth
  Anything V1 and V2 (not V3, whose map grows with distance per DepthKind).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
cfviotti added a commit that referenced this pull request Oct 9, 2026
Brings in the squash-merged #2691 with the maintainer's edits. Adaptations:
- DepthClipRange moves from depth/core.py to depth/annotators.py, next to
  the range helpers it reuses (_check_percentiles, _values_at_ranks), which
  #2691 moved there; sv.DepthClipRange is unchanged. Its docs entry and
  tests follow it to depth/annotators.md and tests/depth/test_annotators.py.
- A clip range is read in the maps' own unit and converted like a tuple
  through _convert_display_range, so an unusable one raises ValueError at
  annotate instead of falling back to the percentile range.
- A flat clip (equal ends) is accepted and paints every pixel the far-end
  colour, as "auto" does on a flat map; the old one-step widening is gone.
- DepthClipRange also rejects a span that does not fit in float32.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
cfviotti added a commit that referenced this pull request Oct 9, 2026
Brings in #2691 as squash-merged with the maintainer's edits. Adaptations:
- display_range stays in the map's own unit for every quantity: with
  quantity="depth" on a disparity map with a camera it is read in pixels and
  converted to metres; a range with low + doffs_px <= 0 is rejected.
- The auto range comes from annotators._percentile_range with the quantity's
  conversion; DepthMap._percentile_range(quantity=...) is gone.
- A flat range paints the far end for quantity="depth" too: the near_is_low
  flip runs after the maintainer's flat-range early return.
- DepthMap.resize("nearest") samples with 1-D takes per axis.
- Docs and docstrings describe display_range in the map's unit.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
cfviotti added a commit that referenced this pull request Oct 9, 2026
Brings in #2691 and #2692 (DepthClipRange) as merged, with the
maintainer's edits.

Adaptations:
- changelog: loaders bullet (#2694) sits above the new #2692 and #2697
  bullets.
- visualize_depth.md front matter: description covers loading and
  colouring over images and video.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

3 participants