> ## 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_resnet101 对图像进行分类

> sample_resnet101 是一个基于 Python 的 ROS 节点，使用基于 QNN 的推理执行图像分类。

下图展示了图像分类结果的示例。

<Frame caption="图像分类结果。">
  <img src="https://mintlify.s3.us-west-1.amazonaws.com/qualcomm-prod/zh/SDKs/QIR-SDK-Ubuntu/images/resnet101-output.gif" style={{width: "60%"}} alt="Live camera view of sunglasses with a pink label and a terminal window echoing the classification result on the /resnet101_output topic." />
</Frame>

[ResNet101](https://huggingface.co/qualcomm/ResNet101) 是一个可以对 ImageNet 数据集图像进行分类的机器学习模型。它也可以作为构建针对特定用例的更复杂模型的基础网络。

## 图像分类管线流程

下图展示了图像分类示例应用的管线流程。

<Frame caption="图像分类管线。">
  <img src="https://mintlify.s3.us-west-1.amazonaws.com/qualcomm-prod/zh/SDKs/QIR-SDK-Ubuntu/images/image48.svg" alt="Image classification pipeline running from camera capture and image publishing through preprocessing, inference, and postprocessing to labeled output." />
</Frame>

## 图像分类管线中使用的 ROS 节点

下表列出了图像分类管线中使用的 ROS 节点。

| 节点名                                                                              | 说明                                        |
| -------------------------------------------------------------------------------- | ----------------------------------------- |
| [qrb ros camera](https://github.com/qualcomm-qrb-ros/qrb_ros_camera)             | Qualcomm ROS 2 包，按参数采集图像并将其发布到 ROS topic。 |
| Image publisher                                                                  | 将图像数据发布到 ROS topic——可以是摄像头帧、本地文件或处理后的输出。  |
| Image classification preprocess                                                  | 订阅图像数据，对其进行重塑与缩放，然后重新发布到下游 topic。         |
| [qrb ros nn interface](https://github.com/qualcomm-qrb-ros/qrb_ros_nn_inference) | 加载已训练的 AI 模型，接收预处理后的图像，执行推理，并发布结果。        |
| Image classification postprocess                                                 | 通过将预测索引映射到相应标签，将原始推理输出转换为人类可读的结果。         |

## 图像分类管线中使用的 ROS topic

下表列出了图像分类管线中使用的 ROS topic。

| ROS topic                      | 类型                                          | 说明                       |
| ------------------------------ | ------------------------------------------- | ------------------------ |
| `/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>` | 这是使用模型的 nn interface 结果。 |
| `/resnet101_results`           | `<sensor_msgs.msg.String>`                  | 这是模型输出标签。                |

## 前提条件

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

## 运行开箱即用的图像分类

<Steps>
  <Step title="安装图像分类软件包">
    ```bash Install the packages theme={null}
    sudo apt install ros-jazzy-sample-resnet101
    ```
  </Step>

  <Step title="在设备上设置示例环境">
    ```bash Launch the sample application theme={null}
    source /opt/ros/jazzy/setup.bash
    ros2 launch sample_resnet101 launch_with_image_publisher.py
    or # You can also replace this with a custom image file
    ros2 launch sample_resnet101 launch_with_image_publisher.py image_path:=<your image path>
    or # You can launch with qrb ros camera
    ros2 launch sample_resnet101 launch_with_qrb_ros_camera.py
    ```

    `launch_with_image_publisher.py` 启动脚本使用以下默认参数：

    ```python theme={null}
    DeclareLaunchArgument(
    'image_path',
    default_value=os.path.join(package_path, 'glasses.jpg'),
    description='Path to the 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
    ]
    )
    ```

    a. 它发送 `local glasses.jpg` 文件，并以 10 Hz 的频率输出图像。

    b. 然后，您可以在另一个 shell 终端中使用 `name/resnet101_output` 检查 ROS topic。

    ```bash theme={null}
    ros2 topic echo /resnet101_output
    ```

    ```yaml Output theme={null}
    data: 'sunglass
    ```
  </Step>
</Steps>

## 从源代码构建图像分类

当您需要修改示例代码时，可以从源代码构建图像分类示例。设备端步骤包括安装依赖项、克隆源代码仓库并构建软件包。

### 设备端步骤

<Steps>
  <Step title="安装依赖项">
    ```bash Install the dependencies theme={null}
    sudo apt install ros-jazzy-rclpy \
    ros-jazzy-sensor-msgs \
    ros-jazzy-std-msgs \
    ros-jazzy-cv-bridge \
    ros-jazzy-ament-index-python \
    ros-jazzy-qrb-ros-tensor-list-msgs \
    python3-opencv \
    python3-numpy \
    ros-jazzy-image-publisher \
    ros-jazzy-qrb-ros-nn-inference \
    ros-jazzy-qrb-ros-camera \
    ros-jazzy-image-publisher
    ```
  </Step>

  <Step title="从 qrb_ros_samples 仓库下载源代码">
    ```bash 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="从源代码构建 object_detection 示例应用">
    ```bash Build the sample application theme={null}
    cd ~/qrb_ros_ws/src/qrb_ros_samples/ai_vision/sample_resnet101
    colcon build
    source install/setup.bash
    ```
  </Step>

  <Step title="运行并测试示例应用">
    根据 [运行开箱即用的图像分类](./classify-images-with-sample_resnet101#run-out-of-the-box-image-classification) 的第 2 步运行并测试。
  </Step>
</Steps>
