TrOCR

Use Microsoft's TrOCR for text recognition 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 Hosted API. Run it on a Dedicated Deployment or self-hosted Inference.

Code sample

Install dependencies:

pip install requests opencv-python

Set URL to your Dedicated Deployment URL or a local Inference server. Pass your Roboflow API Key via the API_KEY environment variable.

import base64
import os
import urllib.request

import cv2
import requests

URL = "https://your-deployment.roboflow.cloud"
IMAGE_URL = "https://media.roboflow.com/inference/license_plate_1.jpg"
IMAGE_PATH = "license_plate.jpg"

urllib.request.urlretrieve(IMAGE_URL, IMAGE_PATH)
image = cv2.imread(IMAGE_PATH)
_, buffer = cv2.imencode(".jpg", image)
image_base64 = base64.b64encode(buffer).decode("utf-8")

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

The code above prints the recognized text to the terminal:

Set URL to match your deployment target:

Last updated

Was this helpful?