Depth Annotators¶
import supervision as sv
image = ...
depth_map = sv.DepthMap(...)
depth_annotator = sv.DepthAnnotator(opacity=0.65)
annotated_image = depth_annotator.annotate(
scene=image.copy(),
depth_map=depth_map,
)

OpenCV SGBM disparity over the frame (left) and the ground truth (right). Spring dataset by Mehl et al. (doi:10.18419/darus-3376) and Spring open movie by Blender Foundation, CC BY 4.0; downscaled.
supervision.depth.annotators.DepthAnnotator
¶
Colours a :class:~supervision.depth.core.DepthMap over an image, near objects
warm and far ones cool.
Pixels without depth are left unpainted, so the scene shows through.
import supervision as sv
from inference import get_model
from PIL import Image
image = Image.open("<SOURCE_IMAGE_PATH>")
model = get_model(model_id="depth-anything-v3/small")
depth_map = sv.DepthMap.from_inference(model.infer(image)[0])
depth_annotator = sv.DepthAnnotator(display_range="auto", opacity=0.6)
annotated_image = depth_annotator.annotate(image.copy(), depth_map)
Source code in src/supervision/depth/annotators.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | |
Methods:¶
__init__(colormap: DepthColormap | str = DepthColormap.TURBO, display_range: str | tuple[float, float] | DepthClipRange = 'auto', opacity: float = 1.0, scale: DepthScale | str = DepthScale.INVERSE) -> None
¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
DepthColormap | str
|
Colour table: |
TURBO
|
|
str | tuple[float, float] | DepthClipRange
|
The values the colour table spans; values outside clamp
to its ends.
|
'auto'
|
|
float
|
Opacity of the colours over the scene, from 0 to 1. Values
outside are clamped: |
1.0
|
|
DepthScale | str
|
|
INVERSE
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/supervision/depth/annotators.py
annotate(scene: ImageType, depth_map: DepthMap) -> ImageType
¶
Colour the depth map over the scene.
A map whose size differs from the scene's is stretched over the whole scene, each scene pixel showing the map pixel under its centre, so a smaller map with the scene's aspect ratio lines up. A map with another aspect ratio is stretched along each axis on its own; if the model saw a letterboxed or cropped frame, undo that on the map before annotating.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
ImageType
|
The image to draw on, a 3-channel |
required |
|
DepthMap
|
The depth map to colour. |
required |
Returns:
| Type | Description |
|---|---|
ImageType
|
The annotated image, matching the type of |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
Examples:
>>> import numpy as np
>>> import supervision as sv
>>> disparity = np.array([[0.0, 1.0], [2.0, 3.0]], dtype=np.float32)
>>> depth_map = sv.DepthMap(disparity, kind="disparity_px")
>>> annotator = sv.DepthAnnotator(display_range=(1.0, 3.0))
>>> image = np.zeros((2, 2, 3), dtype=np.uint8)
>>> annotator.annotate(image, depth_map)[..., ::-1].tolist()
[[[0, 0, 0], [48, 18, 59]], [[162, 252, 60], [122, 4, 3]]]
Source code in src/supervision/depth/annotators.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | |
supervision.depth.annotators.DepthClipRange
dataclass
¶
One colour range for a whole clip of depth maps.
Colouring each frame with its own range makes a still wall change colour as things enter and leave the frame. A clip range, computed in a first pass over the clip, keeps the colour scale fixed on every frame. Colours then stay put only where the depth values are steady, as in ground truth or calibrated stereo; a model's own frame-to-frame wobble becomes more visible.
A clip range applies to maps of the kind it was computed from; on maps of another
kind its values would be read in the wrong unit. A disparity_px clip range also
applies to maps of the resolution it was computed at, because disparity scales
with the map's width.
Attributes:
| Name | Type | Description |
|---|---|---|
display_range |
tuple[float, float]
|
|
Examples:
>>> import numpy as np
>>> import supervision as sv
>>> frames = [
... sv.DepthMap(np.full((4, 4), d, dtype=np.float32), kind="disparity_px")
... for d in (10.0, 20.0, 30.0)
... ]
>>> sv.DepthClipRange.from_depth_maps(frames)
DepthClipRange(display_range=(10.0, 30.0))
Source code in src/supervision/depth/annotators.py
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | |
Methods:¶
__post_init__() -> None
¶
Store plain floats and reject a reversed or unusable range.
Source code in src/supervision/depth/annotators.py
from_depth_maps(depth_maps: Iterable[DepthMap], low_percentile: float = _DEFAULT_LOW_PERCENTILE, high_percentile: float = _DEFAULT_HIGH_PERCENTILE) -> DepthClipRange
classmethod
¶
Compute a clip's percentile range in one pass.
Each map contributes up to about 65,536 valid values, thinned at random when it has more. Frames with at least that many valid values weigh equally, and sparser frames weigh by their count of valid values. Whenever the clip's samples pass 4,194,304, each one is kept with probability 1/2 and later maps are kept at half the previous rate, so memory stays bounded however long the clip is and early and late frames are sampled alike. Once thinning starts, the percentiles are estimates from the samples, so 0 and 100 only approximate the clip's minimum and maximum. The random draws are seeded, so the result is reproducible for a given NumPy version. Pass a generator to read the clip once without holding it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Iterable[DepthMap]
|
The clip's maps, all of one kind. |
required |
|
float
|
Percentile of the clip's valid values at the range's low
end, from 0 to 100. Defaults to 2, as for |
_DEFAULT_LOW_PERCENTILE
|
|
float
|
Percentile at the range's high end, from 0 to 100.
Defaults to 98, as for |
_DEFAULT_HIGH_PERCENTILE
|
Returns:
| Type | Description |
|---|---|
DepthClipRange
|
The clip's :class: |
DepthClipRange
|
maps' own unit. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the percentiles are not
|
Source code in src/supervision/depth/annotators.py
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | |
supervision.depth.colormaps.DepthColormap
¶
Bases: Enum
Colour tables a depth map can be painted with.
The near end of the depth range is always the warm or bright end of the table. Turbo separates the most depth steps and is the default; Viridis and Cividis keep their order in grayscale and for colour-blind viewers; Inferno and Magma start near black, so pixels without depth should stay unpainted with them.
Attributes:
| Name | Type | Description |
|---|---|---|
TURBO |
Google's Turbo rainbow, built for depth and disparity. |
|
VIRIDIS |
Perceptually uniform, blue to yellow. |
|
CIVIDIS |
Perceptually uniform and optimised for colour-vision deficiency. |
|
INFERNO |
Perceptually uniform, black to pale yellow. |
|
MAGMA |
Perceptually uniform, black to pale pink. |
|
GRAYSCALE |
Black far, white near. |
Source code in src/supervision/depth/colormaps.py
Methods:¶
from_value(value: DepthColormap | str) -> DepthColormap
classmethod
¶
Resolve a colormap from an enum member or its case-insensitive name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
DepthColormap | str
|
A :class: |
required |
Returns:
| Type | Description |
|---|---|
DepthColormap
|
The matching :class: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> import supervision as sv
>>> sv.DepthColormap.from_value("Viridis")
<DepthColormap.VIRIDIS: 'viridis'>
Source code in src/supervision/depth/colormaps.py
supervision.depth.core.DepthScale
¶
Bases: Enum
The scale a depth map is coloured on: inverse depth or metric depth.
An explicit display_range stays in the map's own unit whatever the scale.
Attributes:
| Name | Type | Description |
|---|---|---|
INVERSE |
Inverse depth, where larger is nearer: a disparity or relative map as it is, and a metric map as 1 / depth (disparity with a camera). It spends colour on near detail, the way stereo measures it. |
|
METRIC |
Metric depth in metres, linear in distance. Needs a |
Source code in src/supervision/depth/core.py
Methods:¶
from_value(value: DepthScale | str) -> DepthScale
classmethod
¶
Resolve a scale from an enum member or its case-insensitive value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
DepthScale | str
|
A :class: |
required |
Returns:
| Type | Description |
|---|---|
DepthScale
|
The matching :class: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |