Optical Character Recognition (OCR)

Data Engineering & Processing beginner Building with AI 11 min read

Overview

Optical Character Recognition (OCR) is the technology that converts images of text — scanned documents, photographs of signs, PDF pages rendered as images — into machine-readable text. In the context of AI data pipelines, OCR is the critical bridge between the physical world of printed and handwritten documents and the digital world of text that can be embedded, indexed, and retrieved.

Many enterprise data sources contain documents that lack an extractable text layer: scanned contracts from the 1990s, photographed whiteboards, faxed medical records, image-based PDFs exported from legacy systems. Without OCR, these documents are invisible to any text-based AI system — they cannot be chunked, embedded, or searched. OCR unlocks this “dark data” and makes it available for downstream processing.

The field has progressed enormously since its origins in the 1950s. Modern OCR systems achieve average accuracy rates of 96.5% across diverse document types, and AI-powered solutions combining computer vision with language models can handle complex layouts, tables, handwriting, and multilingual content that would have been impossible just a few years ago.

How It Works

The OCR Pipeline

OCR is not a single algorithm but a multi-stage pipeline. Each stage transforms the input progressively from raw pixels to structured text:

  1. Image preprocessing — Clean and enhance the raw image to maximize recognition accuracy.
  2. Layout analysis — Detect text regions, columns, tables, and figures on the page.
  3. Line and word segmentation — Break detected text regions into individual lines, then words.
  4. Character recognition — Classify each character (or subword unit) using a trained model.
  5. Post-processing — Apply language models, dictionaries, and contextual rules to correct recognition errors.
Key Concept: Preprocessing Is Critical

The quality of OCR output depends heavily on image preprocessing. Key preprocessing steps include: grayscale conversion (reducing color to intensity), binarization (converting to black-and-white using adaptive thresholding), deskewing (correcting rotated text), noise reduction (Gaussian blur to remove artifacts), and contrast enhancement. Skipping preprocessing can reduce accuracy by 10-20%.

Traditional OCR: Tesseract

Tesseract is the most widely used open-source OCR engine, originally developed by Hewlett-Packard in the 1980s and later maintained by Google. It processes images through these stages:

# Basic Tesseract OCR with preprocessing in Python
import pytesseract
import cv2

# Load and preprocess image
img = cv2.imread("scanned_doc.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (3, 3), 0)
thresh = cv2.adaptiveThreshold(
    gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
    cv2.THRESH_BINARY, 11, 2
)

# Run OCR
text = pytesseract.image_to_string(thresh, lang="eng")

# Get structured output with bounding boxes
data = pytesseract.image_to_data(thresh, output_type=pytesseract.Output.DICT)
for i, word in enumerate(data["text"]):
    conf = int(data["conf"][i])
    if conf > 60:  # Filter low-confidence detections
        print(f"Word: '{word}' | Confidence: {conf}%")

AI-Powered OCR

Modern AI-based OCR systems go beyond traditional character-by-character recognition by combining computer vision, sequence modeling, and language understanding:

Key Concept: Traditional vs. AI-Based OCR

Traditional OCR (Tesseract) requires careful preprocessing, struggles with complex layouts and handwriting, and needs manual tuning per document type. AI-based OCR (Mistral OCR, Document AI, PaddleOCR) works well out-of-the-box, handles diverse layouts, and combines vision with language understanding for higher accuracy. The tradeoff: traditional OCR is free and runs locally; AI-based solutions may require cloud APIs and per-page pricing (~$1-2 per 1,000 pages).

Handling Difficult Documents

Certain document types push OCR systems to their limits:

Practical Applications

Unlocking Legacy Document Archives

Enterprise organizations often have decades of critical information locked in scanned PDFs — technical manuals, legal agreements, engineering specifications. OCR is the first step in making this content available to modern search and RAG systems. A typical pipeline scans the document, runs OCR to extract text, then feeds the text into the standard parsing → chunking → embedding pipeline.

Invoice and Receipt Processing

Financial automation systems use OCR to extract structured data from invoices: vendor name, line items, amounts, tax totals, and dates. Layout-aware OCR models are particularly valuable here because the spatial position of text (e.g., the number to the right of “Total:”) carries semantic meaning that plain text extraction loses.

Medical Record Digitization

Healthcare systems process millions of handwritten prescriptions, clinical notes, and lab reports. OCR combined with domain-specific language models can extract structured medical information from these documents, enabling clinical decision support and regulatory compliance.

OCR in the Document Parsing Pipeline

OCR is typically embedded within a broader document parsing pipeline rather than used standalone. When a parser encounters a PDF page without an embedded text layer (or with an unreliable one), it delegates to an OCR engine. The OCR output is then fed back into the parser’s layout analysis and element classification stages. This integration is why tools like Docling and Unstructured bundle OCR capabilities directly.

# Example: OCR integration in Unstructured's hi-res pipeline
from unstructured.partition.pdf import partition_pdf

# hi_res strategy automatically uses OCR for image-based pages
elements = partition_pdf(
    filename="scanned_report.pdf",
    strategy="hi_res",
    ocr_languages=["eng"],
    infer_table_structure=True,
)

for el in elements:
    print(f"{el.category}: {el.text[:100]}...")

Key Takeaways

Further Reading