Link to this sectionReference for ultralytics/models/yolo/depth/predict.py#
Improvements
This page is sourced from https://github.com/ultralytics/ultralytics/blob/main/ultralytics/models/yolo/depth/predict.py. Have an improvement or example to add? Open a Pull Request — thank you! 🙏
Summary
Link to this section ultralytics.models.yolo.depth.predict.DepthPredictor#
DepthPredictor(self, cfg = DEFAULT_CFG, overrides: dict[str, Any] | None = None, _callbacks: dict | None = None) -> NoneBases: BasePredictor
Predictor for YOLO depth estimation models.
Produces per-pixel depth maps from RGB images.
Args
| Name | Type | Description | Default |
|---|---|---|---|
cfg | DEFAULT_CFG | ||
overrides | `dict[str, Any] | None` | |
_callbacks | `dict | None` |
Methods
| Name | Description |
|---|---|
postprocess | Post-process depth predictions to Results objects. |
Examples
>>> from ultralytics.models.yolo.depth import DepthPredictor
>>> predictor = DepthPredictor(overrides=dict(model="yolo26n-depth.pt"))
>>> results = predictor("image.jpg")Source code in ultralytics/models/yolo/depth/predict.py
class DepthPredictor(BasePredictor):
"""Predictor for YOLO depth estimation models.
Produces per-pixel depth maps from RGB images.
Examples:
>>> from ultralytics.models.yolo.depth import DepthPredictor
>>> predictor = DepthPredictor(overrides=dict(model="yolo26n-depth.pt"))
>>> results = predictor("image.jpg")
"""
def __init__(
self, cfg=DEFAULT_CFG, overrides: dict[str, Any] | None = None, _callbacks: dict | None = None
) -> None:
"""Initialize DepthPredictor."""
super().__init__(cfg, overrides, _callbacks)
self.args.task = "depth"Link to this section ultralytics.models.yolo.depth.predict.DepthPredictor.postprocess#
def postprocess(
self, preds: torch.Tensor | tuple | list, img: torch.Tensor, orig_imgs: list[np.ndarray] | torch.Tensor
) -> list[Results]Post-process depth predictions to Results objects.
Args
| Name | Type | Description | Default |
|---|---|---|---|
preds | `torch.Tensor | tuple | list` |
img | torch.Tensor | required | |
orig_imgs | `list[np.ndarray] | torch.Tensor` |
Source code in ultralytics/models/yolo/depth/predict.py
def postprocess(
self, preds: torch.Tensor | tuple | list, img: torch.Tensor, orig_imgs: list[np.ndarray] | torch.Tensor
) -> list[Results]:
"""Post-process depth predictions to Results objects."""
depth_maps = preds[0] if isinstance(preds, (tuple, list)) else preds # (B, 1, H, W)
if depth_maps.ndim == 3:
depth_maps = depth_maps.unsqueeze(1) # (B, H, W) → (B, 1, H, W)
if not isinstance(orig_imgs, list): # torch.Tensor source (B, 3, H, W)
orig_imgs = ops.convert_torch2numpy_batch(orig_imgs)
results = []
for i, orig_img in enumerate(orig_imgs):
# Crop letterbox padding and rescale to the original image size.
img_path = self.batch[0][i] if isinstance(self.batch[0], list) else self.batch[0]
depth = ops.scale_masks(depth_maps[i : i + 1].float(), orig_img.shape[:2])
results.append(Results(orig_img=orig_img, path=img_path, names=self.model.names, depth=depth.squeeze()))
return results