Transformers
OCR
Extract text from images using TrOCR models.
Extract text from images using TrOCR (Transformer-based OCR) models. Works with handwritten text, printed documents, and screenshots.
For full API reference (extractText(), extractTextMany(), options, result types, and custom providers), see the Core OCR guide.
See it in action
Try OCR Scanner for a working demo.
Recommended Models
| Model | Size | Strength | Use Case |
|---|---|---|---|
Xenova/trocr-small-printed | ~120MB | Printed text | Documents, screenshots (recommended) |
Xenova/trocr-small-handwritten | ~120MB | Handwritten text | Notes, forms, handwriting |
Document Scanner Example
Based on the OCR Scanner showcase app:
import { transformers } from '@localmode/transformers';
import { extractText } from '@localmode/core';
const model = transformers.ocr('Xenova/trocr-small-printed');
async function scanDocument(file: File) {
const dataUrl = await new Promise<string>((resolve) => {
const reader = new FileReader();
reader.onload = () => resolve(reader.result as string);
reader.readAsDataURL(file);
});
const { text } = await extractText({
model,
image: dataUrl,
abortSignal: controller.signal,
});
return text;
}Image Input Formats
The image parameter accepts:
string— Data URL (data:image/jpeg;base64,...)Blob— Image blob from file input
Best Practices
OCR Tips
- Choose the right model — Use handwritten model for handwriting, printed for documents
- Image quality matters — Clear, well-lit images give much better results
- Crop to text area — If the image has a small text region, crop it for better accuracy
- Single line focus — TrOCR works best on single-line or small text regions
For full PDF text extraction, use @localmode/pdfjs instead — it extracts text directly from PDF
structure without OCR. Use OCR only for scanned documents or images of text.