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

# 使用 ocr_service 运行基础 OCR

`ocr_service` 示例应用程序启用一项提供光学字符识别 (OCR) 功能的服务。

它从 ROS 系统捕获图像话题，并通过 `ocr_topic` 发布结果。

## `ocr_service` 的管道流程

下图展示了基本消息和数据传输通道，以及相关的客户端/服务器和 ROS 节点。

<img src="https://mintcdn.com/qualcomm-prod/IP0o0G5fE3MFmCJ8/SDKs/QIR-SDK-2.0/media/80-65220-2-qirp-sdk-qsg/ocr-service-pipeline.svg?fit=max&auto=format&n=IP0o0G5fE3MFmCJ8&q=85&s=d47932fbf4e0d6b65b71893568dbe628" alt="管道" style={{ display: 'block', margin: '0 auto', width: '75%' }} width="652" height="563" data-path="SDKs/QIR-SDK-2.0/media/80-65220-2-qirp-sdk-qsg/ocr-service-pipeline.svg" />

<p style={{ textAlign: 'center', fontWeight: 'bold' }}>
  图：`ocr_service` 管道
</p>

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

|     ROS 节点    | 描述                                              |
| :-----------: | :---------------------------------------------- |
|  `OCR_CLIENT` | 向 `ocr_server` 发送 OCR 请求并获取服务器结果。               |
|  `OCR_SERVER` | 从 `ocr_server` 获取 OCR 请求，并根据请求派生 `ocr_rosnode`。 |
| `OCR_ROSNODE` | 根据请求订阅图像话题。                                     |

## `ocr_service `管道中使用的 ROS 话题

<table>
  <thead>
    <tr>
      <th style={{ width: '26%' }}>ROS 话题</th>
      <th style={{ width: '29%' }}>消息类型</th>
      <th style={{ width: '20%' }}>发布/订阅方</th>
      <th style={{ width: '25%' }}>描述</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>`/image_topic_name`</td>
      <td>`<sensor_msgs.msg.Image>`</td>
      <td>订阅来自其他节点的图像</td>
      <td>要处理的图像。</td>
    </tr>

    <tr>
      <td>`/ocr_image_topic_name`</td>
      <td>`<std_msgs.msg.String>`</td>
      <td>由 `ocr-rosnode` 发布</td>
      <td>发布 OCR 结果。</td>
    </tr>
  </tbody>
</table>

## 前提条件

您已按照[设置运行示例应用程序的环境](./set-up-env-for-sample-app)完成了**设置设备**。

## 运行开箱即用的 `ocr_service`

1. 启动三个终端，并在每个终端中运行以下命令，在设备上设置 QIR SDK 和 ROS2 环境。

   `ROS_DOMAIN_ID` 的取值范围：\[0, 232]

   ```cpp theme={null}
   ssh root@[ip-addr]
   (ssh) source /usr/share/qirp-setup.sh
   (ssh) export ROS_DOMAIN_ID=xx
   ```
2. 在终端 1 中，启动 `ocr_server`。
   ```undefined theme={null}
   (ssh) ros2 run ocr_service ocr_server
   ```
3. 在终端 2 中，将图像推送到设备并运行 `ocr_testnode`。
   ```undefined theme={null}
   wget https://raw.githubusercontent.com/qualcomm-qrb-ros/ocr_service/stable/1.0.0/scripts/digital_720p.png -O digital_720p.png
   scp digital_720p.png root@[ip-addr]:/opt
   (ssh) ros2 run ocr_service ocr_testnode --topic imagetest --picture /opt/digital_720p.png
   ```
4. 在终端 3 中，运行 `ocr_client`。

   ```undefined theme={null}
   (ssh) ros2 run ocr_service ocr_client imagetest
   ```

   您将看到以下日志：

   <img src="https://mintcdn.com/qualcomm-prod/IP0o0G5fE3MFmCJ8/SDKs/QIR-SDK-2.0/media/80-65220-2-qirp-sdk-qsg/ocr-results.png?fit=max&auto=format&n=IP0o0G5fE3MFmCJ8&q=85&s=4ccbfef9195258241aea7b6ed8151399" alt="结果" style={{ display: 'block', margin: '0 auto' }} width="1087" height="129" data-path="SDKs/QIR-SDK-2.0/media/80-65220-2-qirp-sdk-qsg/ocr-results.png" />

## 构建并运行 `ocr_service`

1. 在主机上构建 QIR SDK 提供的 `ocr_msg` 和 `ocr_service`。
   ```powershell theme={null}
   cd <qirp_decompressed_workspace>
   source setup.sh
   cd qirp-samples/platform/ocr_service
   colcon build --merge-install --cmake-args ${CMAKE_ARGS}
   ```
2. 将 `ocr_msg` 和 `ocr_service` 推送到设备。
   ```powershell theme={null}
   cd <qirp_decompressed_workspace>
   cd qirp-samples/platform/ocr_service/install
   tar -zcvf ocr_service.tar.gz lib/ share/
   ssh root@[ip-addr]
   scp ocr_service.tar.gz root@[ip-addr]:/opt
   ssh ssh root@[ip-addr]
   (ssh) tar --no-overwrite-dir --no-same-owner -zxf /opt/ocr_service.tar.gz -C /usr/ros/jazzy/
   wget https://raw.githubusercontent.com/qualcomm-qrb-ros/ocr_service/stable/1.0.0/scripts/digital_720p.png -O digital_720p.png
   scp digital_720p.png root@[ip-addr]:/opt
   ```
3. 按照[运行开箱即用的 ocr\_service](#run-out-of-the-box-ocr_service) 中的步骤在设备上运行 `ocr_service`。
