> 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/deploy/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 Hosted API. Run it on a [Dedicated Deployment](/deploy/dedicated-deployments.md) or [self-hosted Inference](https://inference.roboflow.com/).
{% endhint %}

## Code sample

{% 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 requests opencv-python
```

{% 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 numpy as np
import requests

URL = "https://your-deployment.roboflow.cloud"
content = requests.get("https://media.roboflow.com/inference/license_plate_1.jpg").content
image = cv2.imdecode(np.frombuffer(content, np.uint8), cv2.IMREAD_COLOR)
_, buffer = cv2.imencode(".jpg", image)
image_base64 = base64.b64encode(buffer).decode("utf-8")

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

{% endstep %}
{% endstepper %}

The code above prints the recognized text to the terminal:

```
TOTAL
```

## Inference speed

Latency measured with [Roboflow Inference](https://inference.roboflow.com/) 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://inference.roboflow.com/) server.
* Your [Dedicated Deployment](/deploy/dedicated-deployments.md) URL for a private endpoint.
  {% endhint %}
