> ## 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_object_segmentation 分割目标

> sample_object_segmentation 是一个使用 QNN 进行模型推理的 Python 启动文件。它演示了摄像头数据流传输、基于 AI 的推理以及目标分割结果的实时可视化。

Ultralytics YOLOv8 是一个机器学习模型，可预测图像中目标的边界框、分割掩码和类别。

## `sample_object_segmentation` 管道流程

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

<Frame caption="sample_object_segmentation 管道。">
  <img src="https://mintlify.s3.us-west-1.amazonaws.com/qualcomm-prod/zh/SDKs/QIR-SDK-Ubuntu/images/image52.svg" alt="Object segmentation pipeline from camera capture through YOLO preprocessing, inference, postprocessing, and overlay to the segmentation result." />
</Frame>

## `sample_object_segmentation` 管道中使用的 ROS 节点

下表列出了 `sample_object_segmentation` 管道中使用的 ROS 节点。

| 节点名                                                                              | 说明                                             |
| -------------------------------------------------------------------------------- | ---------------------------------------------- |
| [qrb ros camera](https://github.com/qualcomm-qrb-ros/qrb_ros_camera)             | Qualcomm ROS 2 包，使用参数捕获图像并将其发布到 ROS 话题。        |
| [yolo preprocess](https://github.com/qualcomm-qrb-ros/qrb_ros_tensor_process)    | 订阅图像数据，对其进行重塑/缩放，并将其重新发布到下游话题。                 |
| [qrb ros nn interface](https://github.com/qualcomm-qrb-ros/qrb_ros_nn_inference) | 加载已训练的 AI 模型，接收预处理后的图像，执行推理，并发布结果。             |
| [yolo postprocess](https://github.com/qualcomm-qrb-ros/qrb_ros_tensor_process)   | 将推理输出与 yolo 标签文件进行匹配。                          |
| [yolo overlay](https://github.com/qualcomm-qrb-ros/qrb_ros_tensor_process)       | 订阅 yolo postprocess 和图像数据，通过一个 ROS 话题展示目标检测结果。 |

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

下表列出了 `sample_object_segmentation` 管道中使用的 ROS 话题。

| ROS 话题                        | 类型                                           | 发布者                             |
| ----------------------------- | -------------------------------------------- | ------------------------------- |
| `/camera/color/image_raw`     | `< sensor_msgs.msg.Image>`                   | `orbbec_camera`                 |
| `/qrb_inference_input_tensor` | `< qrb_ros_tensor_list_msgs.msg.TensorList>` | `yolo_preprocess_node`          |
| `/yolo_segment_result`        | `<vision_msgs.msg.Detection2DArray>`         | `nn_inference_node`             |
| `/yolo_segment_tensor_output` | `<qrb_ros_tensor_list_msgs.msg.TensorList>`  | `yolo_segment_postprocess_node` |
| `/yolo_segment_overlay`       | `<sensor_msgs.msg.Image>`                    | `yolo_segment_overlay_node`     |

## 前提条件

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

* 在主机上，你已根据 [qrb\_ros\_tensor\_process](https://github.com/qualcomm-qrb-ros/qrb_ros_tensor_process) 的 README 中的步骤下载并构建了 yolo 模型。

  <Note>
    下载 yolo 模型时，`target-runtime` 和 `device` 使用方法如下：
  </Note>

## 生成 YOLOv8 模型

<Steps>
  <Step title="在主机上设置 QAI-Hub。">
    a. 登录 [QAI-Hub](https://app.aihub.qualcomm.com/docs/hub/getting_started.html) 并获取你的 API token。

    b. 在主机上安装 QAI-Hub。

    ```bash theme={null}
     ## It is recommended to use python venv to avoid impacting the host environment.
     python3 -m venv venv_qaihub
     source venv_qaihub/bin/activate

     ## Install QAI-Hub related python packages.
     pip3 install qai-hub

     ## Configure QAI-Hub token, replacing xxx with your own token got from QAI-Hub page.
     qai-hub configure --api_token xxx

     pip3 install "qai-hub-models[yolov8-seg]"
    ```
  </Step>

  <Step title="在主机上导出模型。">
    执行以下命令后，模型文件和 coco8.yaml 将在主机上生成。

    <CodeGroup>
      ```bash IQ-9075     theme={null}
      python -m qai_hub_models.models.yolov8_seg.export --target-runtime "tflite" --chipset "qualcomm-qcs9075" --skip-profiling --skip-inferencing

      # Push model files to the device's default path according to the preceding export log.

      # On the device, create the path if it doesn't exist.
      mkdir /opt/model

      # On the host computer, scp all model files to the device.
      scp <Host_Export_model_Path>/yolov8_seg.tflite ubuntu@<device_ip>:/opt/model

      sudo find / -name "coco8.yaml" -print -quit
      scp <label-file-path>/coco8.yaml ubuntu@<device_ip>:/opt/
      ```

      ```bash VENTUO Q (QCS8275)    theme={null}
      python -m qai_hub_models.models.yolov8_seg.export --target-runtime "tflite" --chipset "qualcomm-qcs8275" --skip-profiling --skip-inferencing

      # Push model files to the device's default path according to the preceding export log.

      # On the device, create the path if it doesn't exist.
      mkdir /opt/model

      # On the host computer, scp all model files to the device.
      scp <Host_Export_model_Path>/yolov8_seg.tflite ubuntu@<device_ip>:/opt/model

      sudo find / -name "coco8.yaml" -print -quit
      scp <label-file-path>/coco8.yaml ubuntu@<device_ip>:/opt/
      ```
    </CodeGroup>
  </Step>
</Steps>

## 开箱即用运行 `sample_object_segmentation`

在设备上，运行示例应用：

```bash Run the sample application theme={null}
source /opt/ros/jazzy/setup.bash
ros2 launch sample_object_segmentation launch_with_qrb_ros_camera.py model:=/opt/model/yolov8_seg.tflite
```

现在，你可以在 `rviz2` 中查看名为 `/yolo_segment_overlay` 的 ROS 话题。

## 从源码构建 `sample_object_segmentation`

<Note>
  在执行以下步骤之前，请确保先运行[前提条件](./segment-objects-with-sample_object_segmentation#prerequisites)。
</Note>

<Steps>
  <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 https://github.com/qualcomm-qrb-ros/qrb_ros_samples.git
    ```
  </Step>

  <Step title="从源码构建 sample_sample_segmentation 示例应用">
    ```bash Build the sample application theme={null}
    cd ~/qrb_ros_ws/src/qrb_ros_samples/ai_vision/sample_object_segmentation
    colcon build
    source install/setup.bash
    ```
  </Step>

  <Step title="运行并测试示例应用">
    根据[开箱即用运行 `sample_object_segmentation`](./segment-objects-with-sample_object_segmentation#run-out-of-the-box-sample_object_segmentation) 的步骤 2–3 运行并测试。
  </Step>
</Steps>
