> ## Documentation Index
> Fetch the complete documentation index at: https://dragonwingdocs.qualcomm.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Estimate image depth values with sample_depth_estimation

> This sample application allows you to input an RGB image named input_image.jpg or subscribe to the ROS topic /cam0_stream1 from qrb_ro_camera. It then uses QNN to perform model inference and publishes the result as the depth map ROS topic containing per-pixel depth values.

The `sample_depth_estimation` sample application takes an RGB image named `input_image.jpg` or the `/cam0_stream1` ROS topic from `qrb_ros_camera`. It runs model inference with the [Qualcomm AI Engine Direct SDK (QNN)](https://www.qualcomm.com/developer/software/qualcomm-ai-engine-direct-sdk) and publishes the result as the depth map ROS topic containing per-pixel depth values.

The model has its source from [Depth Anything V2](https://aihub.qualcomm.com/iot/models/depth_anything_v2?searchTerm=depth\&domain=Computer+Vision), a deep convolutional neural network model for depth estimation.

The following figure shows an example depth estimation result.

![Office scene on the left beside its colorized depth map on the right, where warmer colors mark surfaces closer to the camera.](https://mintcdn.com/qualcomm-prod/eHVY1H1lLEVhgq58/SDKs/QIR-SDK-Ubuntu/images/image37.gif?s=29542e84a56d294663095e7afae7ea6f)

## `sample_depth_estimation` pipeline flow

The following figure shows the pipeline flow for `sample_depth_estimation`.

![Depth estimation pipeline from camera or image publisher input through preprocessing, inference, and postprocessing to the depth map topic.](https://mintcdn.com/qualcomm-prod/eHVY1H1lLEVhgq58/SDKs/QIR-SDK-Ubuntu/images/image39.svg?fit=max\&auto=format\&n=eHVY1H1lLEVhgq58\&q=85\&s=e14ef3b08cf8d1a6b2e13b5ab3cf7e7d)

sample\_depth\_estimation pipeline.

## ROS nodes used in `sample_depth_estimation`

The following table lists the ROS nodes used in `sample_depth_estimation`.

| Node name | Description |
| - | - |
| [qrb ros camera](https://github.com/qualcomm-qrb-ros/qrb_ros_camera) | Qualcomm ROS 2 package that captures images with parameters and publishes them to ROS topics. |
| Image publisher | Publishes image data to a ROS topic—can be camera frames, local files, or processed outputs. |
| Sample depth estimation | Subscribes to input images for preprocessing, then performs postprocessing on the output tensor published by the qrb ros nn interface node. |
| [qrb ros nn interface](https://github.com/qualcomm-qrb-ros/qrb_ros_nn_inference) | Loads a trained AI model, receives preprocessed images, performs inference, and publishes results. |

## ROS topics used in `sample_depth_estimation`

The following table lists the ROS topics used in `sample_depth_estimation`.

| ROS topic | Type | Description |
| - | - | - |
| `/image_raw` | `<sensor_msgs.msg.Image>` | Published image information. |
| `/qrb_inference_input_tensor` | `<qrb_ros_tensor_list_msgs.msg.TensorList>` | Preprocessed message. |
| `/qrb_inference_output_tensor` | `<qrb_ros_tensor_list_msgs.msg.TensorList>` | Neural network interface result with model. |
| `/sample_container/depth_map` | `<sensor_msgs.msg.Image>` | Depth map result. |

## Prerequisites

You have set up the device, installed ROS2 Jazzy and Qualcomm Intelligent Robotics (QIR) SDK on the device according to [Install the QIR SDK](https://dragonwingdocs.qualcomm.com/SDKs/QIR-SDK-Ubuntu/install-the-qir-sdk.md).

## Build from the source of `sample_depth_estimation`

Build `sample_depth_estimation` from source when you need to change the sample code. The device steps install the dependencies, clone the source repository, and build the package.

### Device steps

**1. Download the Depth-Anything-V2 model**

```bash Download the model theme={null}
sudo mkdir -p /opt/model && cd /opt/model
sudo wget https://huggingface.co/qualcomm/Depth-Anything-V2/resolve/19ce3645e11de17eed7e869eebcc07dd352834f3/Depth-Anything-V2.bin?download=true -O Depth-Anything-V2.bin
```

**2. Install dependencies**

```bash Install the dependencies theme={null}
sudo apt install -y ros-dev-tools
sudo apt install -y ros-jazzy-qrb-ros-camera
```

**3. Download the source code from the qrb\_ros\_samples repository**

```bash Download the source code theme={null}
mkdir -p ~/qrb_ros_ws/src && cd ~/qrb_ros_ws/src
git clone -b jazzy-rel https://github.com/qualcomm-qrb-ros/qrb_ros_samples.git
```

**4. Build the sample application from source code**

```bash Build the sample application theme={null}
cd ~/qrb_ros_ws/src/qrb_ros_samples/ai_vision/sample_depth_estimation
rosdep install -i --from-path ./ --rosdistro jazzy -y
colcon build
```

**5. Set up the environment with the source command and launch the sample application**

```bash Launch the sample application theme={null}
source install/setup.bash
ros2 launch sample_depth_estimation launch_with_image_publisher.py
```

**6. You can replace this with a custom image file or model path**

```bash Launch with a custom image and model theme={null}
source install/setup.bash
ros2 launch sample_depth_estimation launch_with_image_publisher.py image_path:=<your local image path> model_path:=<your local model path>
```

**7. You can also launch with qrb\_ros\_camera if you connect to the GMSL camera**

```bash Launch with qrb_ros_camera theme={null}
source install/setup.bash
ros2 launch sample_depth_estimation launch_with_qrb_ros_camera.py
```

**8. When using this launch script, it uses the default parameters, which send the local input\_image.jpg file with a publishing rate of 10 Hz**

```python theme={null}
image_path_arg = DeclareLaunchArgument(
         'image_path',
         default_value=os.path.join(package_path, "resource", "input_image.jpg"),
         description='Path to the input image file'
    )
    # Node for image_publisher
    image_publisher_node = Node(
        package='image_publisher',
        executable='image_publisher_node',
        namespace=namespace,
        name='image_publisher_node',
        output='screen',
        parameters=[
            {'filename': image_path},
            {'rate': 10.0},  # Set the publishing rate to 10 Hz
        ]
    )
```

**9. You can then check ROS topics with the topic name /sample\_container/depth\_map in rqt**

To install `rqt`, see the [ROS 2 Jazzy documentation](https://docs.ros.org/en/jazzy/Tutorials/Beginner-CLI-Tools/Introducing-Turtlesim/Introducing-Turtlesim.html).

a. Run `rqt` and select the following buttons in sequence: `Plugins --> Visualization --> Image View`.

b. Select `/sample_container/depth_map` to see the results.
