> ## 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.

# 使用 sample_depth_estimation 估计图像深度值

> 此示例应用允许你输入名为 input_image.jpg 的 RGB 图像或订阅来自 qrb_ro_camera 的 ROS 话题 /cam0_stream1。然后它使用 QNN 执行模型推理，并将结果作为包含每像素深度值的 /depth_map ROS 话题发布。

`sample_depth_estimation` 示例应用接受名为 `input_image.jpg` 的 RGB 图像或来自 `qrb_ros_camera` 的 `/cam0_stream1` ROS 话题。它使用 [Qualcomm AI Engine Direct SDK (QNN)](https://www.qualcomm.com/developer/software/qualcomm-ai-engine-direct-sdk) 运行模型推理，并将结果作为包含每像素深度值的 `/depth_map` ROS 话题发布。

该模型源自 [Depth Anything V2](https://aihub.qualcomm.com/iot/models/depth_anything_v2?searchTerm=depth\&domain=Computer+Vision)，这是一种用于深度估计的深度卷积神经网络模型。

下图展示了一个深度估计结果示例。

<Frame>
  <img src="https://mintlify.s3.us-west-1.amazonaws.com/qualcomm-prod/zh/SDKs/QIR-SDK-Ubuntu/images/image37.gif" alt="Office scene on the left beside its colorized depth map on the right, where warmer colors mark surfaces closer to the camera." />
</Frame>

## `sample_depth_estimation` 管道流程

下图展示了 `sample_depth_estimation` 的管道流程。

<Frame caption="sample_depth_estimation 管道。">
  <img src="https://mintlify.s3.us-west-1.amazonaws.com/qualcomm-prod/zh/SDKs/QIR-SDK-Ubuntu/images/image39.svg" alt="Depth estimation pipeline from camera or image publisher input through preprocessing, inference, and postprocessing to the depth map topic." />
</Frame>

## `sample_depth_estimation` 中使用的 ROS 节点

下表列出了 `sample_depth_estimation` 中使用的 ROS 节点。

| 节点名                                                                              | 说明                                                   |
| -------------------------------------------------------------------------------- | ---------------------------------------------------- |
| [qrb ros camera](https://github.com/qualcomm-qrb-ros/qrb_ros_camera)             | Qualcomm ROS 2 包，使用参数捕获图像并将其发布到 ROS 话题。              |
| Image publisher                                                                  | 将图像数据发布到 ROS 话题——可以是摄像头帧、本地文件或处理后的输出。                |
| Sample depth estimation                                                          | 订阅输入图像进行预处理，然后对 qrb ros nn interface 节点发布的输出张量执行后处理。 |
| [qrb ros nn interface](https://github.com/qualcomm-qrb-ros/qrb_ros_nn_inference) | 加载已训练的 AI 模型，接收预处理后的图像，执行推理，并发布结果。                   |

## `sample_depth_estimation` 中使用的 ROS 话题

下表列出了 `sample_depth_estimation` 中使用的 ROS 话题。

| ROS 话题                         | 类型                                          | 说明               |
| ------------------------------ | ------------------------------------------- | ---------------- |
| `/image_raw`                   | `<sensor_msgs.msg.Image>`                   | 发布的图像信息。         |
| `/qrb_inference_input_tensor`  | `<qrb_ros_tensor_list_msgs.msg.TensorList>` | 预处理后的消息。         |
| `/qrb_inference_output_tensor` | `<qrb_ros_tensor_list_msgs.msg.TensorList>` | 神经网络接口使用模型得到的结果。 |
| `/depth_map`                   | `<sensor_msgs.msg.Image>`                   | 深度图结果。           |

## 前提条件

你已根据[安装 QIR SDK](./install-the-qir-sdk)完成设备设置，并在设备上安装了 ROS2 Jazzy 和 Qualcomm Intelligent Robotics (QIR) SDK。

## 从源码构建 `sample_depth_estimation`

当你需要修改示例代码时，可以从源码构建 `sample_depth_estimation`。设备端步骤包括安装依赖、克隆源码仓库和构建包。

### 设备步骤

<Steps>
  <Step title="下载 Depth-Anything-V2 模型">
    ```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
    ```
  </Step>

  <Step title="安装依赖">
    ```bash Install the dependencies theme={null}
    sudo apt install -y ros-dev-tools
    sudo apt install -y ros-jazzy-qrb-ros-camera
    ```
  </Step>

  <Step title="从 qrb_ros_samples 仓库下载源码">
    ```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
    ```
  </Step>

  <Step title="从源码构建示例应用">
    ```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
    ```
  </Step>

  <Step title="使用 source 命令设置环境并启动示例应用">
    ```bash Launch the sample application theme={null}
    source install/setup.bash
    ros2 launch sample_depth_estimation launch_with_image_publisher.py
    ```
  </Step>

  <Step title="你可以用自定义图像文件或模型路径替换">
    ```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>
    ```
  </Step>

  <Step title="如果连接了 GMSL 摄像头，你也可以使用 qrb_ros_camera 启动">
    ```bash Launch with qrb_ros_camera theme={null}
    source install/setup.bash
    ros2 launch sample_depth_estimation launch_with_qrb_ros_camera.py
    ```
  </Step>

  <Step title="使用该启动脚本时，它使用默认参数，即以 10 Hz 的发布频率发送本地 input_image.jpg 文件">
    ```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
            ]
        )
    ```
  </Step>

  <Step title="然后你可以在 rqt 中通过话题名 /depth_map 查看 ROS 话题">
    要安装 `rqt`，请参阅 [ROS 2 Jazzy documentation](https://docs.ros.org/en/jazzy/Tutorials/Beginner-CLI-Tools/Introducing-Turtlesim/Introducing-Turtlesim.html)。

    a. 运行 `rqt` 并依次选择以下按钮：`Plugins --> Visualization --> Image View`。

    b. 选择 `/sample_container/depth_map` 查看结果。
  </Step>
</Steps>
