# Depth Anything V2

> Estimate depth from a single image for any scene

- Category: Multimodal Generation / Vision & Understanding
- Language: Python
- Repository: https://github.com/DepthAnything/Depth-Anything-V2
- Website: https://depth-anything-v2.github.io
- Canonical page: https://ai-tldr.dev/tools/depth-anything-v2/

## In short

Depth Anything V2 is a model that takes a single RGB image and predicts a depth map, an estimate of how far each pixel is from the camera. It sits in the Vision & Understanding category of the Multimodal Generation tools and is open-source, written in Python. To get started: install.

## Overview

Depth Anything V2 is a model that takes a single RGB image and predicts a depth map, an estimate of how far each pixel is from the camera. It targets relative depth across any scene, so you can feed it indoor, outdoor, or general photos without a calibrated setup.

It is aimed at computer-vision developers and researchers who need depth without a stereo rig, LiDAR, or special hardware. The project ships four pre-trained sizes (Small at 24.8M params up to Giant at 1.3B), so you can trade accuracy against speed and memory for your hardware.

In the multimodal computer-vision category, it sits among monocular depth estimators and improves on V1 in fine detail and robustness. You can use it from the project's own PyTorch code or load it through Hugging Face Transformers, and there are command-line scripts for both images and videos.

## What it does

- Single-image (monocular) depth estimation that works across indoor, outdoor, and general scenes
- Four pre-trained model scales (Small, Base, Large, and an upcoming Giant) to balance accuracy, speed, and memory
- Runs on CUDA, Apple MPS, or CPU; device is selected automatically in the sample code
- Loadable two ways: the project's own PyTorch API or the Hugging Face Transformers depth-estimation pipeline
- Command-line scripts for batch image folders (run.py) and for videos (run_video.py)
- Adjustable input size (default 518) to trade speed for more fine-grained results

## Getting started

Clone the repo, install the Python dependencies, download a checkpoint, then run inference on an image in a few lines of PyTorch.

1. Install

   Clone the repository and install its requirements with pip.

```bash
git clone https://github.com/DepthAnything/Depth-Anything-V2
cd Depth-Anything-V2
pip install -r requirements.txt
```

2. Download a checkpoint

   Download a model checkpoint (Small, Base, or Large) from Hugging Face and put it under the checkpoints directory.

3. Run inference in Python

   Load the encoder you downloaded and call infer_image to get an HxW depth map as a NumPy array.

```python
import cv2
import torch

from depth_anything_v2.dpt import DepthAnythingV2

DEVICE = 'cuda' if torch.cuda.is_available() else 'mps' if torch.backends.mps.is_available() else 'cpu'

model_configs = {
    'vits': {'encoder': 'vits', 'features': 64, 'out_channels': [48, 96, 192, 384]},
    'vitb': {'encoder': 'vitb', 'features': 128, 'out_channels': [96, 192, 384, 768]},
    'vitl': {'encoder': 'vitl', 'features': 256, 'out_channels': [256, 512, 1024, 1024]},
    'vitg': {'encoder': 'vitg', 'features': 384, 'out_channels': [1536, 1536, 1536, 1536]}
}

encoder = 'vitl' # or 'vits', 'vitb', 'vitg'

model = DepthAnythingV2(**model_configs[encoder])
model.load_state_dict(torch.load(f'checkpoints/depth_anything_v2_{encoder}.pth', map_location='cpu'))
model = model.to(DEVICE).eval()

raw_img = cv2.imread('your/image/path')
depth = model.infer_image(raw_img) # HxW raw depth map in numpy
```

4. Or use Hugging Face Transformers

   If you do not want to clone the repo, load the model through the Transformers depth-estimation pipeline (predictions may differ slightly due to a resizing difference).

```python
from transformers import pipeline
from PIL import Image

pipe = pipeline(task="depth-estimation", model="depth-anything/Depth-Anything-V2-Small-hf")
image = Image.open('your/image/path')
depth = pipe(image)["depth"]
```

## When to use it

- Generate a depth map from a single photo when you have no stereo camera or LiDAR
- Add depth cues to 3D reconstruction, AR, or background-blur (bokeh) pipelines
- Batch-process a folder of images or run depth on a video with the included CLI scripts
- Compare model sizes to fit a depth estimator onto constrained hardware (CPU, MPS, or GPU)
