> ## Documentation Index
> Fetch the complete documentation index at: https://dragonwingdocs-staging.qualcomm.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Run basic OCR with ocr_service

The `ocr_service` sample application enables a service that provides the Optical Character Recognition (OCR) function.

It captures the image topic from the ROS system and publishes the result with the `ocr_topic`.

## Pipeline flow for `ocr_service`

This figure shows the basic messages and data transfer channels, with the relevant client/server and ROS node.

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

<p style={{ textAlign: 'center', fontWeight: 'bold' }}>
  Figure : ocr\_service pipeline
</p>

## ROS nodes used in the `ocr_service `pipeline

| ROS node | Description |
| :-: | :- |
| `OCR_CLIENT` | Sends the OCR request to `ocr_server` and gets the server result. |
| `OCR_SERVER` | Gets the OCR request from `ocr_server` , and forks the `ocr_rosnode` according to the request. |
| `OCR_ROSNODE` | Subscribes to the image topic according to the request. |

## ROS topics used in the `ocr_service `pipeline

<table>
  <thead>
    <tr>
      <th style={{ width: '26%' }}>ROS topic</th>
      <th style={{ width: '29%' }}>Message type</th>
      <th style={{ width: '20%' }}>Published/Subscribed by</th>
      <th style={{ width: '25%' }}>Description</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>`/image_topic_name`</td>
      <td>`<sensor_msgs.msg.Image>`</td>
      <td>Subscribe to images from other nodes</td>
      <td>The image to be processed.</td>
    </tr>

    <tr>
      <td>`/ocr_image_topic_name`</td>
      <td>`<std_msgs.msg.String>`</td>
      <td>Published by `ocr-rosnode`</td>
      <td>Publishes the OCR result.</td>
    </tr>
  </tbody>
</table>

## Prerequisites

You have **Set up the device** according to [Set up the environment for running sample applications](./set-up-env-for-sample-app).

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

<Accordion title="Try me">
  <Steps>
    <Step title="Set up QIR SDK and ROS2 environment on the device">
      Start three terminals, and run the following commands in each terminal.

      Value range of `ROS_DOMAIN_ID`: \[0, 232]

      ```bash theme={null}
      ssh root@[ip-addr]
      ```

      ```bash title="SSH Session" theme={null}
      source /usr/share/qirp-setup.sh
      export ROS_DOMAIN_ID=xx
      ```
    </Step>

    <Step title="Launch ocr_server">
      In terminal 1, launch `ocr_server`.

      ```bash title="SSH Session" theme={null}
      ros2 run ocr_service ocr_server
      ```
    </Step>

    <Step title="Push an image and run ocr_testnode">
      In terminal 2, push an image to the device and run the `ocr_testnode`.

      ```bash 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
      ```

      ```bash title="SSH Session" theme={null}
      ros2 run ocr_service ocr_testnode --topic imagetest --picture /opt/digital_720p.png
      ```
    </Step>

    <Step title="Run ocr_client">
      In terminal 3, run the `ocr_client`.

      ```bash title="SSH Session" theme={null}
      ros2 run ocr_service ocr_client imagetest
      ```

      You will see the following log:

      <img src="https://mintcdn.com/qualcomm-staging/blZpKs_b9On5nj-y/archives/2.1/SDKs/QIR-SDK-2.0/media/80-65220-2-qirp-sdk-qsg/ocr-results.png?fit=max&auto=format&n=blZpKs_b9On5nj-y&q=85&s=7d990337a4c00c2e2d95d273061d1d38" alt="result" style={{ display: 'block', margin: '0 auto' }} width="1087" height="129" data-path="archives/2.1/SDKs/QIR-SDK-2.0/media/80-65220-2-qirp-sdk-qsg/ocr-results.png" />
    </Step>
  </Steps>
</Accordion>

## Build and run `ocr_service`

<Accordion title="Try me">
  <Steps>
    <Step title="Build ocr_msg and ocr_service on the host">
      ```bash theme={null}
      cd <qirp_decompressed_workspace>
      source setup.sh
      cd qirp-samples/platform/ocr_service
      colcon build --merge-install --cmake-args ${CMAKE_ARGS}
      ```
    </Step>

    <Step title="Push ocr_msg and ocr_service to the device">
      ```bash theme={null}
      cd <qirp_decompressed_workspace>
      cd qirp-samples/platform/ocr_service/install
      tar -zcvf ocr_service.tar.gz lib/ share/
      scp ocr_service.tar.gz root@[ip-addr]:/opt
      ssh root@[ip-addr]
      ```

      ```bash title="SSH Session" theme={null}
      tar --no-overwrite-dir --no-same-owner -zxf /opt/ocr_service.tar.gz -C /usr/ros/jazzy/
      ```

      ```bash 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
      ```
    </Step>

    <Step title="Run ocr_service">
      Run `ocr_service` on the device with the steps in [Run out-of-the-box ocr\_service](#run-out-of-the-box-ocr_service).
    </Step>
  </Steps>
</Accordion>
