> ## 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 估计图像深度值

`sample_depth_estimation` 示例应用使用 ROS 2 节点从 RGB 图像估计每像素深度图。它使用 Qualcomm® AI Engine Direct SDK (QNN) 进行设备端模型推理。

`sample_depth_estimation` 接受一张名为 `input_image.jpg` 的 RGB 图像作为输入，或订阅来自 `qrb_ros_camera` 节点的 `/sample_container/cam0_stream1` topic。它将深度估计结果作为每像素深度图发布到 `/sample_container/depth_map` topic。

<Note>
  **注意**

  * 有关更多信息，请参见 [sample\_depth\_estimation](https://github.com/qualcomm-qrb-ros/qrb_ros_samples/tree/stable-sample_depth_estimation/1.0.0) GitHub 仓库。
  * 此模型来自 [Depth Anything V2](https://aihub.qualcomm.com/iot/models/depth_anything_v2?searchTerm=depth\&domain=Computer+Vision)，其中深度指的是从摄像头到图像中每个点的距离，使用深度卷积神经网络估计。
</Note>

<video
  autoPlay
  muted
  loop
  playsInline
  width={384}
  height={144}
  src="https://mintlify.s3.us-west-1.amazonaws.com/qualcomm-prod/zh/SDKs/QIR-SDK-2.0/media/80-65220-2-qirp-sdk-qsg/sample-depth-estimation-output.mp4"
  style={{
width: "80%",
display: "block",
margin: "0 auto"
}}
/>

<p style={{ textAlign: 'center', fontWeight: 'bold' }}>
  图：`sample_depth_estimat` 的示例效果
</p>

## `sample_depth_estimation` 的流水线流程

<img
  src="https://mintlify.s3.us-west-1.amazonaws.com/qualcomm-prod/zh/SDKs/QIR-SDK-2.0/media/80-65220-2-qirp-sdk-qsg/sample-depth-estimation-pipeline-regenerate.svg"
  style={{
width: "100%",
display: "block",
margin: "0 auto" }}
/>

<p style={{ textAlign: 'center', fontWeight: 'bold' }}>
  图：`sample_depth_estimation` 的流水线流程
</p>

## `sample_depth_estimation` 流水线中使用的 ROS 节点

| ROS 节点                    | 说明                                                                                                                  |
| :------------------------ | :------------------------------------------------------------------------------------------------------------------ |
| `sample_depth_estimation` | 一个基于 Python 的 ROS 2 Jazzy 包，用于处理每像素深度值。该 ROS 节点订阅一个图像 topic，在预处理和后处理后发布深度估计结果 topic。                                |
| `qrb_ros_camera`          | Qualcomm ROS 2 包，使用参数捕获图像并将其发布到 ROS topic。                                                                          |
| `qrb_ros_nn_inference`    | 该 ROS 节点加载训练好的 AI 模型，接收预处理后的图像，执行推理并发布结果。                                                                           |
| `image_publisher`         | ROS 2 Jazzy 包，使用本地路径发布图像 ROS topic。有关更多信息，请参见 [image\_publisher](https://github.com/ros-perception/image_pipeline)。 |

## `sample_depth_estimation` 流水线中使用的 ROS topic

<table>
  <thead>
    <tr>
      <th align="center">ROS topic</th>
      <th align="center">类型</th>
      <th align="center">说明</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td align="left"><code>/image\_raw</code></td>
      <td align="left"><code>sensor\_msgs.msg.Image</code></td>
      <td align="left">发布的图像信息，来自 <code>image\_publisher</code>。以绝对名称订阅，因此不带命名空间。</td>
    </tr>

    <tr>
      <td align="left"><code>/sample\_container/cam0\_stream1</code></td>
      <td align="left"><code>sensor\_msgs.msg.Image</code></td>
      <td align="left">摄像头图像流，来自 <code>qrb\_ros\_camera</code>。</td>
    </tr>

    <tr>
      <td align="left"><code>/sample\_container/qrb\_inference\_input\_tensor</code></td>
      <td align="left"><code>qrb\_ros\_tensor\_list\_msgs.msg.TensorList</code></td>
      <td align="left">预处理消息。</td>
    </tr>

    <tr>
      <td align="left"><code>/sample\_container/qrb\_inference\_output\_tensor</code></td>
      <td align="left"><code>qrb\_ros\_tensor\_list\_msgs.msg.TensorList</code></td>
      <td align="left">后处理消息。</td>
    </tr>

    <tr>
      <td align="left"><code>/sample\_container/depth\_map</code></td>
      <td align="left"><code>sensor\_msgs.msg.Image</code></td>
      <td align="left">包含以颜色图表示的每像素深度值。</td>
    </tr>
  </tbody>
</table>

<Note>
  **注意**

  launch 文件将所有节点放置于 `sample_container` 命名空间下，因此该示例的 topic 都以 `/sample_container/` 为前缀。只有 `/image_raw` 以绝对名称订阅，因此保持全局。
</Note>

## 前提条件

你已完成 [设置运行示例应用的环境](./set-up-env-for-sample-app) 中的以下设置。

* 设置设备
* 设置主机 docker

## 开箱即用运行 `sample_depth_estimation`

<Accordion title="试一试">
  <Steps>
    <Step title="在开发套件上运行示例">
      ```bash title="SSH Session" theme={null}
      source /usr/share/qirp-setup.sh -m
      export ROS_DOMAIN_ID=55

      # Launch the sample depth estimation with image publisher.
      # You can replace 'image_path' with your desired image.
      ros2 launch sample_depth_estimation launch_with_image_publisher.py \
          image_path:=/usr/ros/jazzy/share/sample_depth_estimation/resource/input_image.jpg

      # Launch the sample depth estimation node with qrb_ros_camera.
      ros2 launch sample_depth_estimation launch_with_qrb_ros_camera.py
      ```
    </Step>

    <Step title="在主机计算机上查看深度估计结果">
      1. 启动一个终端并运行以下命令来检查深度估计结果。
         ```bash title="SSH Session" theme={null}
         export ROS_DOMAIN_ID=55
         ros2 topic echo /sample_container/depth_map
         ```
      2. 启动 `rqt` 以在主机 docker 上查看深度估计结果，有关更多信息，请参见 [rqt](https://wiki.ros.org/rqt)。
         ```bash title="SSH Session" theme={null}
         # YOUR_HOST_IP is the IP address of the Host where you want to view the sample output.
         export DISPLAY=YOUR_HOST_IP:0
         export ROS_DOMAIN_ID=55
         rqt
         ```
      3. 依次选择以下按钮。**Plugins** --> **Visualization** --> **Image View**
      4. 在 rqt gui 上手动选择 topic `/sample_container/depth_map`，然后图片将成功显示在 `rqt` 上。
    </Step>
  </Steps>
</Accordion>

## 构建并运行 `sample_depth_estimation`

以下步骤以 Dragonwing IQ-9075 Evaluation Kit 为例构建 `sample_depth_estimation` 包。

<Accordion title="试一试">
  <Steps>
    <Step title="在主机计算机上构建并打包">
      1. 构建示例应用项目。
         ```bash title="SSH Session" theme={null}
         cd <decompressed_workspace>/images/iq-9075-evk/qirpsdk_artifacts/iq-9075-evk/
         tar -zxvf qirp-sdk_<qirp_version>.tar.gz
         cd <qir_decompressed_path>/qirp-sdk
         source setup.sh
         # build sample
         cd <qirp_decompressed_path>/qirp-samples/ai_vision/sample_depth_estimation
         colcon build
         ```
      2. 打包并将示例应用推送到设备。
         ```bash title="SSH Session" theme={null}
         # package and push build result of sample
         cd ./install/sample_depth_estimation
         tar -czvf sample_depth_estimation.tar.gz lib share
         scp sample_depth_estimation.tar.gz root@[ip-addr]:/opt/
         ```
    </Step>

    <Step title="在开发套件上安装并运行">
      1. 安装示例应用。
         ```bash title="SSH Session" theme={null}
         # Install sample package
         tar --no-overwrite-dir --no-same-owner -zxf /opt/sample_depth_estimation.tar.gz -C /usr/ros/jazzy/
         ```
      2. 按照 [开箱即用运行 sample\_depth\_estimation](#run-out-of-the-box-sample_depth_estimation) 中的步骤运行示例应用。
    </Step>
  </Steps>
</Accordion>
