LocalMode
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.

ModelSizeStrengthUse Case
Xenova/trocr-small-printed~120MBPrinted textDocuments, screenshots (recommended)
Xenova/trocr-small-handwritten~120MBHandwritten textNotes, 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

  1. Choose the right model — Use handwritten model for handwriting, printed for documents
  2. Image quality matters — Clear, well-lit images give much better results
  3. Crop to text area — If the image has a small text region, crop it for better accuracy
  4. 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.

Showcase Apps

AppDescriptionLinks
OCR ScannerExtract text from images and scanned documentsDemo · Source

Next Steps

On this page