Machine Learning Algorithms for OCR: A Technical Guide
The ML algorithms behind modern OCR - CNNs, LSTMs, CTC and attention decoding, transformers, and TrOCR, explained with real system examples.

Modern OCR isn't one algorithm; it's a pipeline of distinct machine learning components, each solving a specific sub-problem, trained end-to-end on data rather than relying on hand-designed templates or rules. This is a guide to what those components actually are, why each one exists, how production systems have evolved, and where the field is heading.
Who This Is For
- ML engineers and researchers building or fine-tuning custom OCR models rather than using an off-the-shelf API.
- Developers evaluating OCR systems who want to understand what's actually happening under the hood, not just accuracy percentages.
- Anyone comparing OCR engines and wanting to understand why some handle handwriting or unusual fonts better than others.
Detection and Recognition Are Two Distinct Problems
Worth separating these clearly before anything else, since production systems architect them as genuinely different sub-problems with different algorithms: text detection finds where text sits on a page, locating regions, handling curved or irregular text, and distinguishing text from background. Text recognition then reads what the text in each detected region actually says. A production system commonly uses one algorithm optimized for the first problem and a different one for the second, rather than a single unified model for both.
The Foundational Architecture: CNN + Sequence Model + Decoder
The architecture that defined most production OCR from roughly 2015 to 2022 — and still underlies many systems today — combines three specific components, commonly called a CRNN (Convolutional Recurrent Neural Network):
- A convolutional neural network (CNN) extracts visual features from the image — edges, shapes, and patterns — reusing the same pattern-detection filters across the whole image rather than treating every pixel independently. This is what lets the model generalize to fonts, sizes, and degradation levels it's never explicitly seen, provided it was trained on enough varied examples.
- A recurrent network, typically an LSTM (long short-term memory), processes the CNN's output as a sequence, modeling the relationship between characters. This is what lets the model use context, recognizing that a partial character shape following "t-h" is much more likely to be "e" than "z," the same way predictive text uses preceding words. LSTMs specifically address a known weakness in simpler recurrent networks (the vanishing gradient problem) that made it hard for earlier architectures to retain context over longer sequences.
- A decoder converts the sequence model's output into an actual character string — and this is where two genuinely different strategies exist, covered in detail below.
This exact architecture — CNN plus LSTM plus a decoder is what powers Tesseract 4 and 5, and was standard across most commercial OCR engines released in this period. Its core advantage over older, template-matching approaches is genuine generalization: the network learns character recognition from training data rather than depending on hand-designed rules for every font and layout variation.
Two Decoding Strategies: CTC vs. Attention-Based
Worth distinguishing these clearly rather than treating attention as simply an add-on; they're genuinely different approaches to the same problem (converting a sequence model's output into characters), and production and research systems choose between them, or combine both.
CTC (Connectionist Temporal Classification) decoding solves a genuinely tricky alignment problem: the visual feature sequence and the character sequence don't have a fixed one-to-one correspondence, since character widths vary and a single character can span multiple feature positions. CTC handles this alignment without requiring pre-segmented training data — a large part of why CRNN-style architectures could be trained end-to-end rather than needing manually labeled character boundaries. Its main advantage is training simplicity and speed, at the cost of assuming each output position is conditionally independent given the input.
Attention-based decoding takes a different approach: at each decoding step, the model learns to "attend" to the most relevant parts of the input sequence, rather than processing it in one fixed pass. This generally handles longer-range dependencies and irregular text layouts better than CTC, at the cost of typically slower inference.
Some systems combine both rather than choosing one: the MMOCR framework, as one documented example, pairs a ResNet31 backbone with an attention-based decoder running alongside a separate LSTM-based decoder (called SAR) in parallel, using both decoding strategies together rather than betting on a single approach.
Where Attention and Transformers Come In
Two further developments improved on the base CRNN architecture:
- Multi-head attention runs an attention mechanism in parallel multiple times, letting the model separately evaluate different kinds of dependencies (short-range versus long-range context) and combine the results, a refinement that improves prediction accuracy over a plain LSTM sequence model.
- Transformers address a specific, practical limitation of LSTMs directly: LSTMs process sequences strictly in order, meaning each position depends on computing all previous positions first, which limits parallelization and slows training and inference on longer sequences. Transformers don't require processing input in strict order, which meaningfully speeds up training compared to a pure LSTM-based approach.
This isn't just theoretical — it shows up in real, dated production system upgrades. PaddleOCR's flagship pipeline originally used a CRNN recognizer (CNN feature extraction, bidirectional LSTM sequence modeling, CTC decoding), the same family of architecture described above. Its 2022 release (PP-OCRv3) and 2025 release (PP-OCRv5) upgraded the recognition stage to a transformer-based recognizer called SVTR (Spatial Visual Transformer), replacing the LSTM component specifically while keeping a separate, segmentation-based detection algorithm (DBNet) for the detection stage, a concrete example of the detection/recognition split and the LSTM-to-transformer migration both happening in one real, actively maintained system.
TrOCR, from Microsoft Research, is one of the most widely cited transformer-based OCR models and represents a further architectural departure from the CRNN lineage above: rather than using a CNN for feature extraction, TrOCR uses a pretrained vision transformer as its image encoder, paired with a pretrained language model as its text decoder — an end-to-end transformer architecture with no CNN or CTC component at all. This is worth knowing as a distinct lineage from the CNN+LSTM+CTC (CRNN) and CNN+transformer (SVTR-style) architectures covered above, not a variant of either.
The Real Accuracy Gap: Printed vs. Handwritten Text
Worth stating plainly, since it's a consistent, measurable pattern across architectures: printed text recognition typically reaches 98–99.5% character accuracy with modern approaches, while handwriting recognition, a genuinely harder problem — runs meaningfully lower, commonly in the 85–95% range depending on handwriting quality. Transformer-based models trained specifically on handwriting datasets (IAM and RIMES are the commonly used benchmark datasets in this space) significantly outperform other approaches on this specific sub-problem, which is part of why handwriting-specific model variants exist rather than a single architecture handling both cases equally well.
Language-Specific Challenges Beyond Latin Script
Worth knowing this isn't a solved, uniform problem across languages: cursive scripts like Arabic present genuinely harder segmentation challenges than Latin script, since letters change shape depending on whether they appear in initial, medial, final, or isolated position within a word, and diacritical marks alter meaning in ways that add further complexity.
Research addressing this specifically uses transfer learning from pretrained CNN backbones (VGG16 and ResNet50 are commonly used) combined with transformer-based encoders, with reported classification accuracy in the 92–98% range depending on the specific approach and dataset, a reminder that architecture choices aren't one-size-fits-all across scripts, and a system tuned primarily on Latin-script training data won't automatically generalize well to cursive or diacritic-heavy languages.
Ensemble and Multi-Model Approaches
A more recent development worth naming: rather than relying on a single model's output, some systems run multiple models and use their agreement (or disagreement) as a signal — a form of ensemble learning applied to OCR specifically. DeepRead's extraction pipeline, as one current example, uses multi-model consensus rather than a single deterministic pass, which is architecturally in this same family as the multi-head attention approach described above — running multiple evaluations and combining them, rather than trusting one model's single output uncritically.
This connects directly to confidence scoring as a practical mechanism: models that disagree on a given value are a genuine signal that the value deserves human review, distinct from a single model simply stating high confidence in a wrong answer.
Where This Is Heading: LLMs and Multimodal Approaches
Multimodal large language models represent a further architectural shift beyond CRNN-family approaches — interpreting visual and textual context together rather than running detection and recognition as separate, sequential stages.
Conclusion
Modern OCR is built on a specific, evolved lineage of machine learning components: CNNs for visual feature extraction, LSTMs or transformers for sequence modeling, and CTC or attention-based decoding to solve the alignment problem between visual features and character output, with detection and recognition architected as genuinely separate sub-problems, not one unified model.
The field's real, ongoing shift is from LSTM-based to transformer-based sequence modeling (visible directly in production systems like PaddleOCR's dated version history), fully transformer-native architectures like TrOCR that abandon the CNN entirely, and growing use of ensemble and multi-model consensus approaches for both accuracy and confidence signaling. The accuracy gap between printed and handwritten text, and between Latin and non-Latin scripts, remains real and worth testing for directly rather than assuming a single reported accuracy number transfers across document types.
FAQ
What's the difference between OCR text detection and text recognition?
Detection finds where text is located on a page — the regions and boundaries. Recognition reads what the text in each detected region actually says. Production systems typically use different, specialized algorithms for each stage rather than one unified model.
What is a CRNN, and why was it the standard OCR architecture for so long?
CRNN (Convolutional Recurrent Neural Network) combines a CNN for visual feature extraction with an RNN (typically LSTM) for sequence modeling and a CTC or attention-based decoder for character output. It powered most production OCR systems from roughly 2015 to 2022, including Tesseract 4 and 5, because it could be trained end-to-end on data rather than requiring hand-designed rules per font or layout.
What's the difference between CTC and attention-based decoding?
CTC aligns variable-length feature sequences to character output without needing pre-segmented training data, favoring training simplicity and speed. Attention-based decoding lets the model focus on the most relevant input at each decoding step, generally handling longer-range dependencies and irregular layouts better, at the cost of slower inference. Some systems use both together rather than choosing one.
How is TrOCR different from a traditional CRNN-based OCR model?
TrOCR uses a pretrained vision transformer as its image encoder and a pretrained language model as its text decoder — an end-to-end transformer architecture with no CNN feature extractor or CTC decoder at all, a genuinely different lineage from CRNN-style models rather than a variant of one.
Why is handwriting recognition still less accurate than printed text recognition?
Printed text typically reaches 98–99.5% character accuracy with modern approaches, while handwriting recognition runs in the 85–95% range depending on handwriting quality — a consistent, measurable gap across architectures, since handwriting variability is a genuinely harder pattern-recognition problem than consistent printed fonts.
More articles

How AI Automation Minimizes Human Error in Document Handling
How AI automation reduces human error across document handling - misfiling, routing, retention, and review, with real case study evidence.

Document Scanning OCR for Expense Tracking: How the Pipeline Actually Works
How document scanning and OCR actually automate expense tracking, the pipeline, IRS compliance, fraud detection, and a DIY build option.