TrOCR

Run Microsoft's TrOCR text recognition API on a Dedicated Deployment or self-hosted Inference

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.

TrOCR is not available on the Serverless Cloud API. Run it on a Dedicated Deployment or self-hosted Inference.

TrOCR API

1

Get your API Key

Create a Roboflow account, find your key on the Roboflow API settings page and make it available to your shell:

export ROBOFLOW_API_KEY="your-key-here"
2

Install the dependencies

These packages fetch the image and call the API:

pip install -U requests opencv-python supervision
3

Run the model

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

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",
    json={
        "api_key": os.environ["ROBOFLOW_API_KEY"],
        "image": {"type": "base64", "value": image_base64},
    },
)
print(response.json()["result"])

The code above prints the recognized text to the terminal:

TOTAL

TrOCR inference speed

Latency measured with Roboflow Inference on 1x NVIDIA L4, batch size 1, mean after warmup.

ModelLatency (ms)
trocr114.4

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

Set URL to match your deployment target:

Run TrOCR with self-hosted Inference

TrOCR is served by Roboflow Inference running on your own hardware. Start a local server, then select the model with the model argument on the shared OCR endpoint:

pip install inference-cli
inference server start  # serves http://localhost:9001
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)

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.