> For the complete documentation index, see [llms.txt](https://docs.roboflow.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.roboflow.com/models/supported-models/trocr.md).

# TrOCR

TrOCR is Microsoft's transformer-based OCR model. It is trained for line-level text recognition, so crop your input to a single text region for best results.

{% hint style="info" %}
TrOCR is not available on the Serverless Cloud API. Run it on a [Dedicated Deployment](https://docs.roboflow.com/deployment/roboflow-cloud/dedicated-deployments) or [self-hosted Inference](https://docs.roboflow.com/deployment/self-hosted/self-hosted).
{% endhint %}

## TrOCR API

{% stepper %}
{% step %}

### Get your API Key

Create a Roboflow account, find your key on the [Roboflow API settings page](https://app.roboflow.com/settings/api) and make it available to your shell:

```bash
export ROBOFLOW_API_KEY="your-key-here"
```

{% endstep %}

{% step %}

### Install the dependencies

These packages fetch the image and call the API:

```bash
pip install -U requests opencv-python supervision
```

{% endstep %}

{% step %}

### Run the model

Set `URL` to your Dedicated Deployment URL or a local Inference server.

```python
import base64
import os
import cv2
import requests
import supervision as sv

URL = "https://your-deployment.roboflow.cloud"
image = sv.load_image_from_url("https://media.roboflow.com/inference/license_plate_1.jpg")
_, buffer = cv2.imencode(".jpg", image)
image_base64 = base64.b64encode(buffer).decode("utf-8")

response = requests.post(
    f"{URL}/ocr/trocr",
    headers={"Authorization": f"Bearer {os.environ['ROBOFLOW_API_KEY']}"},
    json={
        "image": {"type": "base64", "value": image_base64},
    },
)
print(response.json()["result"])
```

{% endstep %}
{% endstepper %}

The code above prints the recognized text to the terminal:

```
TOTAL
```

## TrOCR inference speed

Latency measured with [Roboflow Inference](https://docs.roboflow.com/deployment/self-hosted/self-hosted) on 1x NVIDIA L4, batch size 1, mean after warmup.

<table data-search="false"><thead><tr><th>Model</th><th>Latency (ms)</th></tr></thead><tbody><tr><td><code>trocr</code></td><td>114.4</td></tr></tbody></table>

TrOCR recognizes a single cropped text line, so this is the latency for one line crop, not a full page.

{% hint style="info" %}
Set `URL` to match your deployment target:

* `http://localhost:9001` for a local [Inference](https://docs.roboflow.com/deployment/self-hosted/self-hosted) server.
* Your [Dedicated Deployment](https://docs.roboflow.com/deployment/roboflow-cloud/dedicated-deployments) URL for a private endpoint.
  {% endhint %}

## Run TrOCR with self-hosted Inference

TrOCR is served by [Roboflow Inference](https://docs.roboflow.com/deployment/self-hosted/self-hosted) running on your own hardware. Start a local server, then select the model with the `model` argument on the shared OCR endpoint:

```bash
pip install inference-cli
inference server start  # serves http://localhost:9001
```

```python
from inference_sdk import InferenceHTTPClient

client = InferenceHTTPClient(api_url="http://127.0.0.1:9001")

result = client.ocr_image(inference_input="./serial_number.png", model="trocr")
print(result)
```

{% hint style="warning" %}
TrOCR performs best on cropped, single-line printed text. Crop each text region before sending it: unlike some other OCR models, TrOCR does not handle uncropped or multi-line images well.
{% endhint %}
