Depth Estimation¶
supervision.depth.core.DepthMap
¶
A per-pixel depth, disparity or relative depth map for one frame.
:class:~supervision.depth.core.DepthMap is to depth what
:class:~supervision.key_points.core.KeyPoints is to pose: its own container,
with its own :class:~supervision.depth.annotators.DepthAnnotator and model
connectors. It belongs to the whole frame, so it is not a
:class:~supervision.detection.core.Detections field;
:meth:~supervision.depth.core.DepthMap.measure_detections brings the depth
under each object into detections.data.
values are float32 in the kind's unit. For disparity_px and depth_m,
non-finite values and values <= 0 are no depth; for relative_inverse,
non-finite and negative values are no depth, because a normalised map puts its
farthest real pixel at exactly 0. A disparity of 0 or less stays no depth even
when camera.doffs_px > 0 would make a value in (-doffs_px, 0] convert to a
finite distance, so such pixels become NaN in metres instead.
Attributes:
| Name | Type | Description |
|---|---|---|
values |
NDArray[float32]
|
|
kind |
DepthKind
|
What the values measure. |
camera |
DepthCamera | None
|
Optional stereo camera, needed to convert disparity to metric depth. |
Examples:
>>> import numpy as np
>>> import supervision as sv
>>> disparity = np.array([[0.0, 10.0], [20.0, 40.0]], dtype=np.float32)
>>> depth_map = sv.DepthMap(
... disparity,
... kind="disparity_px",
... camera=sv.DepthCamera(fx_px=1000.0, baseline_m=0.1),
... )
>>> depth_map.valid_mask
array([[False, True],
[ True, True]])
>>> depth_map.to_depth().to_float()
array([[ nan, 10. ],
[ 5. , 2.5]], dtype=float32)
Source code in src/supervision/depth/core.py
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 | |
Attributes¶
resolution_wh: tuple[int, int]
property
¶
The map's (width, height) in pixels.
valid_mask: npt.NDArray[np.bool_]
property
¶
A boolean (H, W) array, True where the map holds depth.
Methods:¶
__eq__(other: object) -> bool
¶
Compare every field; values compare NaN equal to NaN.
Source code in src/supervision/depth/core.py
__init__(values: npt.NDArray[np.floating], kind: DepthKind | str, camera: DepthCamera | None = None) -> None
¶
Create a depth map from float values in the kind's unit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
NDArray[floating]
|
|
required |
|
DepthKind | str
|
What the values measure, as a
:class: |
required |
|
DepthCamera | None
|
Optional stereo camera parameters. A |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the values are not a 2D float array, or the camera is
invalid or given for a |
Source code in src/supervision/depth/core.py
__repr__() -> str
¶
Summarise the map without printing its values.
crop(xyxy: npt.ArrayLike) -> DepthMap
¶
Return the part of the map inside a box.
Coordinates are rounded and clipped to the map, as in sv.crop_image. Values
are unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
ArrayLike
|
Box as |
required |
Returns:
| Type | Description |
|---|---|
DepthMap
|
A new :class: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the box is not four finite numbers or does not overlap the map. |
Examples:
>>> import numpy as np
>>> import supervision as sv
>>> depth_map = sv.DepthMap(np.ones((100, 200), np.float32), kind="depth_m")
>>> depth_map.crop((50, 10, 150, 60)).resolution_wh
(100, 50)
Source code in src/supervision/depth/core.py
from_inference(inference_result: Any) -> DepthMap
classmethod
¶
Create a :class:~supervision.depth.core.DepthMap from a depth model served
by Roboflow Inference.
Reads the normalized_depth that Inference's depth models (Depth Anything,
YOLO26 depth) return in every depth_map_format (json nested lists,
png16 or png8 base64 PNGs, or the NumPy array the Inference SDK and
Workflows decode them to). The map is normalised per image with 1 for
the nearest pixel and 0 for the farthest, so larger is nearer. It loads as
relative_inverse float32, as it is and with nothing inverted: Inference
returns larger-is-nearer for every model, Depth Anything V3 included, so do
not invert it again. Values from different images are not comparable. A
float32 array is shared with the map, not copied.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Any
|
One result from Inference, the Inference SDK or a Workflow's depth estimation step. |
required |
Returns:
| Type | Description |
|---|---|
DepthMap
|
A |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the result is a list or carries no |
Examples:
import supervision as sv
from inference_sdk import InferenceHTTPClient
client = InferenceHTTPClient(
api_url="https://serverless.roboflow.com", api_key="<ROBOFLOW_API_KEY>"
)
result = client.depth_estimation(
"<SOURCE_IMAGE_PATH>",
model_id="depth-anything-v3/small",
depth_map_format="png16",
)
depth_map = sv.DepthMap.from_inference(result)
Source code in src/supervision/depth/core.py
from_pfm(path: str | Path, *, kind: DepthKind | str = DepthKind.DISPARITY_PX) -> DepthMap
classmethod
¶
Load a single-channel PFM file, the Middlebury and SceneFlow format.
Rows are flipped to top first. Infinite values (Middlebury's unknown disparity) are no depth. The sign of the header's scale gives the byte order; its magnitude is not applied.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str | Path
|
Path to the |
required |
|
DepthKind | str
|
What the values measure; disparity by default. |
DISPARITY_PX
|
Returns:
| Type | Description |
|---|---|
DepthMap
|
A float32 :class: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the file is not a valid grayscale PFM (bad header, scale
or truncated), or |
Examples:
Source code in src/supervision/depth/core.py
from_png16(path: str | Path, *, scale: float, kind: DepthKind | str) -> DepthMap
classmethod
¶
Load a depth map from a 16-bit grayscale PNG.
Values load as float32 code / scale. Code 0 is no depth (NaN) for every
kind, including relative_inverse; load Inference responses with
:meth:~supervision.depth.core.DepthMap.from_inference. KITTI
disparity uses scale=256; Ultralytics depth datasets store
millimetres (scale=1000) unless the dataset YAML sets depth_scale.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str | Path
|
Path to the PNG. |
required |
|
float
|
Divisor from stored value to the kind's unit. |
required |
|
DepthKind | str
|
What the values measure. |
required |
Returns:
| Type | Description |
|---|---|
DepthMap
|
A float32 :class: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the file is not a single-channel 16-bit PNG, |
Examples:
import supervision as sv
depth_map = sv.DepthMap.from_png16(
"disp_occ_0/000000_10.png", scale=256, kind="disparity_px"
)
Source code in src/supervision/depth/core.py
from_transformers(transformers_results: Any, *, kind: DepthKind | str) -> DepthMap
classmethod
¶
Create a :class:~supervision.depth.core.DepthMap from a Hugging Face depth
estimation result.
Reads predicted_depth from a depth-estimation pipeline result or from a
post_process_depth_estimation(outputs, target_sizes=...) entry. A raw model
output is not supported: it is not resized to the image, and for some models,
such as Depth Pro, post-processing also converts the prediction. The
pipeline's depth image is an 8-bit per-image stretch for display and is
ignored.
Transformers does not say what the model predicts, so kind is required:
relative_inverse for Depth Anything V1 and V2 and DPT relative models,
depth_m for metric models such as Depth Pro, ZoeDepth or Depth Anything
metric. A float32 predicted_depth is shared with the map, not copied.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Any
|
One result holding |
required |
|
DepthKind | str
|
What the model predicts. |
required |
Returns:
| Type | Description |
|---|---|
DepthMap
|
A float32 :class: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the result is a list or has no |
Examples:
import supervision as sv
from transformers import pipeline
estimator = pipeline(
"depth-estimation", model="depth-anything/Depth-Anything-V2-Small-hf"
)
result = estimator("<SOURCE_IMAGE_PATH>")
depth_map = sv.DepthMap.from_transformers(result, kind="relative_inverse")
Source code in src/supervision/depth/core.py
from_ultralytics(ultralytics_results: Any) -> DepthMap
classmethod
¶
Create a :class:~supervision.depth.core.DepthMap from an Ultralytics YOLO26
depth result.
result.depth.data holds metric depth in metres at the original image size,
where values <= 0 are no depth. A float32 depth is shared with the map,
not copied.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Any
|
One |
required |
Returns:
| Type | Description |
|---|---|
DepthMap
|
A |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the result is a list or carries no depth map, or the depth is not a single (H, W) map. |
Examples:
import supervision as sv
from ultralytics import YOLO
model = YOLO("yolo26n-depth.pt")
result = model("<SOURCE_IMAGE_PATH>")[0]
depth_map = sv.DepthMap.from_ultralytics(result)
Source code in src/supervision/depth/core.py
measure_detections(detections: Detections, resolution_wh: tuple[int, int] | None = None) -> Detections
¶
Return the detections with the median depth under each object in data.
For each detection, the median of the valid depth pixels inside its mask
(dense or :class:~supervision.detection.compact_mask.CompactMask) or,
without masks, inside its box (rounded and clipped as in
:func:~supervision.utils.image.crop_image) is stored in a new data
column. The depth is
in metres, under "depth_m", when the map can give them (a depth_m map, or
a disparity map with a camera), and in the map's own kind otherwise, under
"disparity_px" or "relative_inverse"
(supervision.config.DEPTH_M_DATA_FIELD and its siblings), so a disparity
map without a camera never writes "depth_m". A column of the same name
already in data is replaced. Objects whose
region holds no depth get NaN.
Pass resolution_wh when the detections come from an image of another size
than the map, such as a monocular model's output: the map is then stretched
over the image as :class:~supervision.depth.annotators.DepthAnnotator
draws it, each image pixel taking the
map value under its centre, so even a small box gets depth. The values keep
the map's own unit; unlike :meth:~supervision.depth.core.DepthMap.resize,
a disparity map's values are not
rescaled.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Detections
|
:class: |
required |
|
tuple[int, int] | None
|
Size of the image the detections and their masks belong to, when it differs from the map's. Without it they are in map pixels. |
None
|
Returns:
| Type | Description |
|---|---|
Detections
|
A new :class: |
Detections
|
|
Detections
|
|
Detections
|
columns) rather than copying them. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> import numpy as np
>>> import supervision as sv
>>> depth = np.full((100, 100), 20.0, dtype=np.float32)
>>> depth[20:40, 20:40] = 4.0
>>> depth_map = sv.DepthMap(depth, kind="depth_m")
>>> detections = sv.Detections(
... xyxy=np.array([[20, 20, 40, 40], [60, 60, 90, 90]], dtype=float)
... )
>>> detections = depth_map.measure_detections(detections)
>>> detections.data["depth_m"]
array([ 4., 20.], dtype=float32)
>>> labels = [f"{d:.1f} m" for d in detections.data["depth_m"]]
Source code in src/supervision/depth/core.py
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 | |
resize(resolution_wh: tuple[int, int], method: DepthResizeMethod | str = DepthResizeMethod.NEAREST) -> DepthMap
¶
Return the map resized to resolution_wh without blending any pixels.
Blending would mix no-depth pixels into their neighbours and average a foreground edge into the background, inventing depths nobody measured, so both methods only pick existing values:
"nearest"takes the pixel under each target pixel's centre."foreground"keeps, in each group of source pixels a target pixel covers, the valid value nearest the camera (the largest disparity, the smallest depth), so thin near objects survive shrinking and holes never win over depth. An axis that grows samples the nearest pixel.
Disparity is measured in pixels of the map, so a disparity map's values scale with the width ratio, and so do the camera's pixel parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
tuple[int, int]
|
Target |
required |
|
DepthResizeMethod | str
|
A :class: |
NEAREST
|
Returns:
| Type | Description |
|---|---|
DepthMap
|
A new :class: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the resolution is not positive or the method is unknown. |
Examples:
>>> import numpy as np
>>> import supervision as sv
>>> disparity = np.array([[8.0, 0.0, 2.0, 2.0]], dtype=np.float32)
>>> depth_map = sv.DepthMap(disparity, kind="disparity_px")
>>> depth_map.resize((2, 1), method="foreground").to_float()
array([[4., 1.]], dtype=float32)
Source code in src/supervision/depth/core.py
to_depth() -> DepthMap
¶
Return the map as metric depth in metres.
A disparity map converts with fx_px * baseline_m / (disparity + doffs_px);
pixels where that is not a positive distance become no depth. A depth_m
map is returned as it is.
Returns:
| Type | Description |
|---|---|
DepthMap
|
A |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the map is |
Source code in src/supervision/depth/core.py
to_float(no_depth_value: float = np.nan) -> npt.NDArray[np.float32]
¶
Return the values in the kind's unit as a new float32 array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
float
|
The value written where the map has no depth. |
nan
|
Returns:
| Type | Description |
|---|---|
NDArray[float32]
|
A |
Examples:
>>> import numpy as np
>>> import supervision as sv
>>> disparity = np.array([[np.nan, 1.0, 10.0]], dtype=np.float32)
>>> sv.DepthMap(disparity, kind="disparity_px").to_float(0.0)
array([[ 0., 1., 10.]], dtype=float32)
Source code in src/supervision/depth/core.py
value_at(x: float, y: float, resolution_wh: tuple[int, int] | None = None) -> float | None
¶
Return the value in the kind's unit under a point, or None.
The unit is the map's own: metres for depth_m, pixels for disparity_px.
Call :meth:~supervision.depth.core.DepthMap.to_depth first for metres
from a disparity map with a camera. Missing values are None here, but
NaN in :meth:~supervision.depth.core.DepthMap.to_float and in the
columns :meth:~supervision.depth.core.DepthMap.measure_detections writes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
float
|
Column of the point. |
required |
|
float
|
Row of the point. |
required |
|
tuple[int, int] | None
|
Size of the space the point is measured in, normally the
image's, when the map is stretched over an image of another size.
Without it the point is in map pixels. The point is a continuous
coordinate, where pixel |
None
|
Returns:
| Type | Description |
|---|---|
float | None
|
The value, or |
float | None
|
the map. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> import numpy as np
>>> import supervision as sv
>>> disparity = np.array([[np.nan, 10.0]], dtype=np.float32)
>>> depth_map = sv.DepthMap(disparity, kind="disparity_px")
>>> depth_map.value_at(1, 0), depth_map.value_at(0, 0)
(10.0, None)
>>> depth_map.value_at(300, 100, resolution_wh=(400, 200))
10.0
>>> metric = np.array([[1.0, 2.0, 3.0, 4.0]], dtype=np.float32)
>>> depth_map = sv.DepthMap(metric, kind="depth_m")
>>> depth_map.resize((3, 1)).value_at(1, 0)
3.0
>>> depth_map.value_at(1 + 0.5, 0 + 0.5, resolution_wh=(3, 1))
3.0
Source code in src/supervision/depth/core.py
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 | |
supervision.depth.core.DepthCamera
dataclass
¶
Pinhole stereo camera parameters that relate disparity to metric depth.
All pixel values are in pixels of the depth map they belong to, so resizing a
:class:~supervision.depth.core.DepthMap updates them.
Attributes:
| Name | Type | Description |
|---|---|---|
fx_px |
float
|
Focal length in pixels. |
baseline_m |
float
|
Stereo baseline in metres. |
doffs_px |
float
|
Difference between the two views' principal points in pixels
(Middlebury's |
Examples:
>>> import supervision as sv
>>> camera = sv.DepthCamera(fx_px=721.5, baseline_m=0.54)
>>> round(camera.fx_px * camera.baseline_m / 38.96, 2)
10.0
Source code in src/supervision/depth/core.py
Methods:¶
__post_init__() -> None
¶
Store plain floats and reject parameters that cannot convert disparity.
Source code in src/supervision/depth/core.py
supervision.depth.core.DepthKind
¶
Bases: Enum
What the values of :class:~supervision.depth.core.DepthMap measure.
Attributes:
| Name | Type | Description |
|---|---|---|
DISPARITY_PX |
Stereo disparity in pixels of the map. Larger is nearer.
Metric depth is |
|
DEPTH_M |
Metric depth along the optical axis, in metres. Smaller is nearer. |
|
RELATIVE_INVERSE |
Unitless relative depth from a monocular model, with no
metric scale, where larger is nearer and 0 is the farthest valid value.
Only the order of values is meaningful: maps from different models share
no scale or geometry.
Depth Anything V1, V2 and DPT output inverse depth up to an unknown scale
and shift, which fits as it is; Roboflow Inference's maps run from 0 for
the farthest pixel to 1 for the nearest. Invert a relative map that
grows with distance, such as raw Depth Anything V3 output, which is
linear in depth: pass |
Source code in src/supervision/depth/core.py
Methods:¶
from_value(value: DepthKind | str) -> DepthKind
classmethod
¶
Resolve a kind from an enum member or its case-insensitive value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
DepthKind | str
|
A :class: |
required |
Returns:
| Type | Description |
|---|---|
DepthKind
|
The matching :class: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> import supervision as sv
>>> sv.DepthKind.from_value("depth_m")
<DepthKind.DEPTH_M: 'depth_m'>
Source code in src/supervision/depth/core.py
supervision.depth.core.DepthResizeMethod
¶
Bases: Enum
How :meth:~supervision.depth.core.DepthMap.resize picks the value of each
target pixel.
Both methods only pick existing values and never blend them.
Attributes:
| Name | Type | Description |
|---|---|---|
NEAREST |
The pixel under each target pixel's centre. |
|
FOREGROUND |
In each group of source pixels a target pixel covers, the valid value nearest the camera, so thin near objects survive shrinking. |
Source code in src/supervision/depth/core.py
Methods:¶
from_value(value: DepthResizeMethod | str) -> DepthResizeMethod
classmethod
¶
Resolve a resize method from an enum member or its case-insensitive value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
DepthResizeMethod | str
|
A :class: |
required |
Returns:
| Type | Description |
|---|---|
DepthResizeMethod
|
The matching :class: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |