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 API
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"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",
headers={"Authorization": f"Bearer {os.environ['ROBOFLOW_API_KEY']}"},
json={
"image": {"type": "base64", "value": image_base64},
},
)
print(response.json()["result"])The code above prints the recognized text to the terminal:
TOTALTrOCR inference speed
Latency measured with Roboflow Inference on 1x NVIDIA L4, batch size 1, mean after warmup.
trocr
114.4
TrOCR recognizes a single cropped text line, so this is the latency for one line crop, not a full page.
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:
Last updated
Was this helpful?