OCR — Pure Rust Optical Character Recognition
A from-scratch OCR system built in Rust, informed by decades of OCR research and modern deep-learning pipelines. The goal is a clean, extensible, pure-Rust toolchain that can rival Tesseract on CPU and approach EasyOCR/PaddleOCR accuracy.
Current State
Compiles and passes 410+ tests. The full pipeline works end-to-end for printed text on clean images, with a modular architecture supporting both classical and learned recognition:
- Image preprocessing: deskew, perspective dewarp, curved-line rectification, super-resolution for tiny/low-DPI text, quality gate (auto-sharpen/contrast), binarization (Otsu, Sauvola), noise reduction, contrast enhancement, auto-rotate (0°/90°/180°/270°)
- Layout analysis: Union-Find CCL, multi-angle oriented detection (±45°), column/line detection, reading-order resolution, table detection, form field extraction
- Text detection:
TextDetectortrait with CCL and lightweight CNN implementations - Recognition: Pattern-matching engine (trainable from synthetic fonts) + CRNN (CNN + BiLSTM + CTC beam search with dictionary/LM rescoring and calibrated confidence) selectable via
--engine - Multi-language: Unicode script detection (Latin, CJK, Arabic, Cyrillic, Greek, Hebrew, Thai, Devanagari), dictionaries for 25+ languages,
--lang auto, per-script CRNN vocabularies - Post-processing: Dictionary-based spell correction, document structure classification (headings, lists, paragraphs), hierarchical Markdown/JSON output
- Output formats: text, JSON, hOCR, TSV, ALTO XML, box files, searchable PDF (feature
pdf-output), Markdown, structured JSON - CLI:
extract,batch(with progress/ETA),layout,list-languages,check,info,validate,train,benchmark,makebox - Training:
CrnnTrainerwith FC-layer backprop on synthetic data + checkpoint saving - Benchmarking: Per-script CER/WER evaluation across all supported scripts
- Font attributes: Bold/italic/monospace detection from stroke analysis (
FontAttributeDetector) - Quantization: INT8 per-tensor symmetric quantization for FC layer (4x memory reduction)
- ONNX import (feature
onnx): Load pre-trained PaddleOCR/EasyOCR weights viaOnnxLoader - Web API (feature
web-api): HTTP server with multipart upload - PDF input (feature
pdf)
What We Learned from Popular OCR Offerings
| Engine | Detection | Recognition | Languages | Strengths |
|---|---|---|---|---|
| Tesseract | Connected components + LSTM line finder | Conv + BiLSTM + CTC (VGSL) | 100+ | Mature, tiny binary, great for scanned docs |
| EasyOCR | CRAFT (CNN) | CRNN (ResNet + BiLSTM + CTC) | 80+ | Simple PyTorch API, good scene text |
| PaddleOCR | DB (differentiable binarization) | SVTR / CRNN | 80+ | Best speed/accuracy trade-off, ultra-light models |
| TrOCR | — | Transformer encoder-decoder | 100+ | Excellent on printed text, no detection stage |
| dots.ocr / VLMs | End-to-end vision-language | 3B-param multimodal | 100+ | Structured output (tables, headings) but needs GPU |
Key Architectural Lessons
- Two-stage pipeline (detection → recognition) dominates for documents. End-to-end VLMs are promising but overkill for pure OCR.
- CTC decoders remove the need for per-character segmentation in sequence models.
- Synthetic training data (rendered fonts + realistic distortions) lets you bootstrap accuracy before collecting real data.
- Image preprocessing matters: deskew, binarization, and contrast enhancement can improve accuracy 10–20%.
- Post-processing dictionaries / language models close the gap between raw recognition and human-level accuracy.
- CPU deployment still matters: Tesseract remains ubiquitous because it runs everywhere without GPU dependencies.
Roadmap
Phase 0 — Baseline (DONE)
- End-to-end pipeline: image → preprocess → layout → recognize → output
- Pattern-matching recognition engine for Latin glyphs
- Layout analysis with CCL, column detection, reading order
- Dictionary-based post-correction
- CLI with extract, batch, layout, list-languages
- 408+ tests passing
Phase 1 — Synthetic Training Infrastructure (DONE)
- Synthetic text-image generator with TTF font rendering and bitmap fallback
- Distortion pipeline (rotation, blur, noise, contrast, shear)
- CER/WER benchmark harness with clean/mild/heavy test sets
- Train pattern-matching templates from synthetic renders (
TemplateTrainer)
Phase 2 — Robust Text Detection (DONE)
-
TextDetectortrait withCclDetectorandCnnDetector - Lightweight 3-layer detection CNN in pure Rust (ndarray)
- Synthetic document generator + IoU-based evaluation harness
- Auto-rotate 0°/90°/180°/270° via projection-variance orientation detection
Phase 3 — Learned Recognition (CRNN) (DONE)
- CNN feature extractor (5 conv layers + maxpool)
- 2-layer bidirectional LSTM (64 hidden)
- CTC decoder (greedy + beam search + dictionary/LM rescoring)
- Calibrated per-character / word confidence (
ConfidenceCalibrator) - Perspective dewarp + curved-line rectification
- Arbitrary-angle text detection (
OrientedCclDetector) - Super-resolution upscaling for tiny / low-DPI text
-
ocr makebox— Tesseract-compatible training.boxexport - CTC loss with forward-backward algorithm
-
CrnnTrainerwith synthetic data + checkpoint saving - Wired into
OcrEnginevia--engine lstm - Model size < 5MB (~2.4MB default config)
Phase 4 — Multi-Language Scale (DONE)
- Unicode script detection and routing (8 scripts)
- Dictionaries for 25+ languages
- Multi-script synthetic data generation (
ScriptLineGenerator) - Per-script CRNN models (
ScriptModelRegistry)
Phase 5 — Advanced Layout & Structure (DONE)
- Table detection with grid line scan and cell reconstruction
- Row/column span inference via pixel-density checks
- Form field extraction (checkboxes, key-value pairs, underline fields)
- Document structure classification (headings, paragraphs, lists, captions)
- Hierarchical Markdown and structured JSON output
- Searchable PDF generation with invisible text overlay
Next Steps (requires training execution)
- Training CLI wired up (
ocr train --engine lstm --epochs 10) - Benchmark CLI wired up (
ocr benchmark --samples 10) - CRNN inference optimized (rayon-parallel convolutions, ndarray FC layer)
- Train CRNN to CER < 5% on clean synthetic test
- Train CRNN to CER < 15% on distorted synthetic test
- Evaluate per-language accuracy on synthetic benchmarks
Installation
# Default build (pattern matching engine)
# Searchable PDF output (`--format pdf`)
# With all features
# Run tests
Usage
# Recognize text from an image
# JSON output with bounding boxes and confidence
# Markdown output with document structure
# Structured JSON with headings, paragraphs, lists
# Enable preprocessing pipeline
# Use CRNN engine
# Batch process a directory
# List supported languages
# Analyze image layout
# Train CRNN on synthetic data
# Benchmark per-script recognition accuracy
# Generate Tesseract-style training .box file from a real image
Library
use Ocr;
use OcrConfig;
let config = default;
let ocr = with_config?;
ocr.initialize.await?;
let result = ocr.recognize_text_from_file.await?;
println!;
Project Structure
src/
├── api/ High-level OCR API (Ocr, TextProcessor)
├── cli/ CLI argument parsing with clap
├── core/ Core OCR engine (config, engine, geometry, layout, output, text)
├── image/ Image preprocessing pipeline (binarization, enhancement, quality)
├── lang/ Language support (CJK, detection, dictionary, N-gram, unicode)
├── layout/ Layout analysis (column/line detection, text ordering, CCL, detection CNN, table/form extractors, classifier)
├── recognition/ Recognition models (pattern matching, CRNN, CTC decoder, LSTM)
├── synthetic/ Synthetic data generation (fonts, distortion, multi-script, document layouts, template training)
├── training/ Model training pipeline (data, losses, metrics, optimizers, CRNN trainer)
└── utils/ Shared utilities (async, error, hash, math, SIMD, time)
License
Apache-2.0