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

# Segment objects with sample_object_segmentation

The `sample_object_segmentation` sample application detects and segments objects in images using ROS 2 nodes. It uses Qualcomm® AI Engine Direct SDK (QNN) for model inference.

The sample application uses the Ultralytics YOLOv8 model to predict bounding boxes, segmentation masks, and classes of objects in an image.

<Note>
  **Note**

  For more information, see [sample\_object\_segmentation](https://github.com/qualcomm-qrb-ros/qrb_ros_samples/tree/main/ai_vision/sample_object_segmentation) on GitHub.
</Note>

<video
  autoPlay
  muted
  loop
  playsInline
  width={400}
  height={401}
  src="https://mintcdn.com/qualcomm-prod/EDJV-hu6qJhZi4pm/SDKs/QIR-SDK-2.0/media/80-65220-2-qirp-sdk-qsg/yolo-segment.mp4?fit=max&auto=format&n=EDJV-hu6qJhZi4pm&q=85&s=f228621984df17aaa41bc0e01b26f273"
  style={{
width: "50%",
display: "block",
margin: "0 auto" }}
  data-path="SDKs/QIR-SDK-2.0/media/80-65220-2-qirp-sdk-qsg/yolo-segment.mp4"
/>

<p style={{ textAlign: 'center', fontWeight: 'bold' }}>
  Figure: Sample effects of `sample_object_segmentation`
</p>

## Pipeline flow for `sample_object_segmentation`

<img src="https://mintcdn.com/qualcomm-prod/EDJV-hu6qJhZi4pm/SDKs/QIR-SDK-2.0/media/80-65220-2-qirp-sdk-qsg/sample_object_segmentation-regenerate.svg?fit=max&auto=format&n=EDJV-hu6qJhZi4pm&q=85&s=08691154134c384551b3567973d5bb42" style={{width: "100%"}} width="1228" height="322" data-path="SDKs/QIR-SDK-2.0/media/80-65220-2-qirp-sdk-qsg/sample_object_segmentation-regenerate.svg" />

<p style={{ textAlign: 'center', fontWeight: 'bold' }}>
  Figure: Pipeline flow for `sample_object_segmentation`
</p>

## ROS nodes used in `sample_object_segmentation`

|                                     **ROS node**                                    |                                              **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.      |
|    [yolo\_preprocess](https://github.com/qualcomm-qrb-ros/qrb_ros_tensor_process)   |       Subscribes to image data, reshapes and resizes it, and republishes it to a downstream topic.       |
| [qrb\_ros\_nn\_inference](https://github.com/qualcomm-qrb-ros/qrb_ros_nn_inference) |    Loads a trained AI model, receives preprocessed images, performs inference, and publishes results.    |
|   [yolo\_postprocess](https://github.com/qualcomm-qrb-ros/qrb_ros_tensor_process)   |                              Matches inference output with YOLO label files.                             |
|     [yolo\_overlay](https://github.com/qualcomm-qrb-ros/qrb_ros_tensor_process)     | Subscribes to YOLO postprocess and image data, and shows the object segmentation results as a ROS topic. |

## ROS topics used in `sample_object_segmentation`

|         **ROS Topic**         |                      **Type**                      |         **Published By**        |
| :---------------------------: | :------------------------------------------------: | :-----------------------------: |
|        `/cam0_stream1`        |               `sensor_msgs.msg.Image`              |          `camera_node`          |
| `/qrb_inference_input_tensor` |      `qrb_ros_tensor_list_msgs.msg.TensorList`     |      `yolo_preprocess_node`     |
|        `/resized_image`       |               `sensor_msgs.msg.Image`              |      `yolo_preprocess_node`     |
| `/yolo_segment_tensor_output` |      `qrb_ros_tensor_list_msgs.msg.TensorList`     |       `nn_inference_node`       |
|     `/yolo_segment_result`    | `qrb_ros_vision_msgs.msg.Detection2DWithMaskArray` | `yolo_segment_postprocess_node` |
|    `/yolo_segment_overlay`    |               `sensor_msgs.msg.Image`              |   `yolo_segment_overlay_node`   |

<Note>
  **Note**

  `yolo_segment_overlay_node` synchronizes two inputs, `/yolo_segment_result` and `/resized_image`, using an exact-time policy. Both must be publishing for `/yolo_segment_overlay` to be produced.
</Note>

## Prerequisites

* You have completed the settings in [Set up the environment for running sample applications](./set-up-env-for-sample-app).
* You have generated the YOLOv8 model using QAI-Hub on the host computer with the following steps.

## Generate YOLOv8 model

1. Set up QAI-Hub on the host computer.
   1. Sign in to [QAI-Hub](https://app.aihub.qualcomm.com/docs/hub/getting_started.html) and get your API token.
   2. Install QAI-Hub.
      ```python 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 the QAI-Hub token, replacing xxx with your own token obtained from the QAI-Hub page.
      qai-hub configure --api_token xxx
      pip3 install "qai-hub-models[yolov8_seg]"
      ```
2. Export the model on the host computer.
   ```python theme={null}
   # For IQ-9075, after executing the following commands, the model file and coco8.yaml are generated in the host computer.
   python -m qai_hub_models.models.yolov8_seg.export --target-runtime "tflite" --chipset "qualcomm-qcs9075-proxy" --skip-profiling --skip-inferencing
   ```
   ```bash title="SSH Session" theme={null}
   # 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
   ```
   ```bash theme={null}
   # On the host computer, scp all model files to the device.
   scp <Host_Export_model_Path>/yolov8_det_float/yolov8_seg.tflite root@<device_ip>:/opt/model
   scp <Host_Export_model_Path>/coco8.yaml root@<device_ip>:/opt/
   ```

For more information, see [qrb\_ros\_tensor\_process](https://github.com/qualcomm-qrb-ros/qrb_ros_tensor_process/tree/main/cv_tensor_process/yolo_v8_process) on GitHub.

## Run out-of-the-box `sample_object_segmentation`

<Accordion title="Try me">
  <Steps>
    <Step title="Run the sample on the development kit">
      ```bash title="SSH Session" theme={null}
      # Set up the runtime environment
      source /usr/share/qirp-setup.sh -m
      # Set the ROS_DOMAIN_ID
      export ROS_DOMAIN_ID=123
      # Launch the sample object segmentation
      ros2 launch sample_object_segmentation launch_with_qrb_ros_camera.py model:=/opt/model/yolov8_seg.tflite
      ```
    </Step>

    <Step title="Check the results on the host computer">
      In a terminal of the host computer, check the ROS topics with the name `/yolo_segment_overlay` in `rqt`.
    </Step>
  </Steps>
</Accordion>

## Build and run `sample_object_segmentation`

The following steps build the `sample_object_segmentation` package using Dragonwing IQ-9075 Evaluation Kit as an example.

<Accordion title="Try me">
  <Steps>
    <Step title="Build and package on the host computer">
      1. Build the sample application project.
         ```bash theme={null}
         cd <decompressed_workspace>/images/iq-9075-evk/qirpsdk_artifacts/iq-9075-evk/
         tar -zxvf qirp-sdk_<qirp_version>.tar.gz
         cd qirp-sdk
         source setup.sh
         # build sample
         cd <qirp_decompressed_path>/qirp-samples/ai_vision/sample_object_segmentation
         colcon build
         ```
      2. Package and push the sample application to the device.
         ```bash theme={null}
         # package and push build result of sample
         cd <qirp_decompressed_path>/qirp-samples/ai_vision/sample_object_segmentation/install/sample_object_segmentation
         tar -czvf sample_object_segmentation.tar.gz lib share
         scp sample_object_segmentation.tar.gz root@[ip-addr]:/opt/
         ```
    </Step>

    <Step title="Install and run on the development kit">
      1. Install the sample application.
         ```bash title="SSH Session" theme={null}
         # Install sample package
         tar --no-overwrite-dir --no-same-owner -zxf /opt/sample_object_segmentation.tar.gz -C /usr/ros/jazzy/
         ```
      2. Run the sample application with the steps in [Run out-of-the-box sample\_object\_segmentation](#run-out-of-the-box-sample_object_segmentation).
    </Step>
  </Steps>
</Accordion>
