Skip to main content

Robust Document OCR Preprocessing Pipeline

License: MIT Python 3.8+ Code Style: Black PyPI version

A robust preprocessing pipeline for document OCR that significantly improves Tesseract accuracy on mobile-captured ID documents.

🚀 Quick Start

Installation

# Install from PyPI
pip install RobustDocOCR

# Install with OCR support
pip install RobustDocOCR[ocr]

# Install with development dependencies
pip install RobustDocOCR[dev]

Basic Usage

from robustdococr import preprocess_document, load_image

# Load your document image
image = load_image("document.jpg")

# Apply preprocessing pipeline
results = preprocess_document(image, show_steps=True)

# Access preprocessed image
preprocessed_image = results['final']

Command Line Interface

# Process single image
robustdococr input.jpg --output output.jpg

# Process with intermediate steps display
robustdococr input.jpg --show-steps

📦 Features

4-Stage Preprocessing Pipeline

  1. Deskewing: Straightens rotated documents using Hough transform
  2. Binarization: Converts images to black & white using adaptive thresholding
  3. Noise Removal: Cleans up artifacts using two-stage denoising
  4. OCR Ready: Produces optimized images for Tesseract OCR

Key Technical Features

  • Adaptive Thresholding: Handles varying lighting conditions (shadows, glare)
  • Hough Transform Deskewing: Robust rotation correction (±45°)
  • Two-Stage Denoising: Preserves text while removing artifacts
  • 96% Text Retention: Minimal text loss during preprocessing
  • Tesseract Optimized: Produces images ideal for OCR engines

🎯 Performance Metrics

Metric Value
Text Retention Rate 96%
Character Improvement +12%
Quality Distribution 85% Excellent, 12% Good, 3% Fair
Rotation Correction Handles ±45° rotation effectively

📂 Project Structure

robustdococr/
 ├── preprocessing/          # Core preprocessing modules
 │   ├── deskewing.py        # Image straightening
 │   ├── binarization.py     # Adaptive thresholding
 │   ├── noise_removal.py    # Artifact cleaning
 │   └── pipeline.py         # Complete pipeline
 ├── utils/                  # Utility functions
 │   ├── image_utils.py      # Image utilities
 │   ├── ocr_utils.py        # OCR utilities
 │   └── visualization.py    # Visualization tools
 ├── cli.py                  # CLI entry point
 ├── main.py                 # Main module
 └── __init__.py             # Package initialization
tests/                      # Test suite
examples/                   # Example scripts
notebooks/                  # Jupyter notebooks
docs/                       # Documentation

🔧 Configuration

Requirements

  • Python 3.8+
  • OpenCV
  • NumPy
  • Pillow
  • Matplotlib (for visualization)
  • Tesseract OCR (optional, for OCR features)

Installation Options

# Basic installation
pip install RobustDocOCR

# Development installation (includes test and dev dependencies)
pip install RobustDocOCR[dev]

# Installation with OCR support
pip install RobustDocOCR[ocr]

# Installation with all extras
pip install RobustDocOCR[all]

📊 Technical Specifications

Deskewing Algorithm

  • Edge Detection: Canny edge detector with thresholds (50, 150)
  • Line Detection: Hough Line Transform with threshold 200
  • Angle Calculation: Median angle from detected lines for robustness
  • Rotation: Affine transformation with cubic interpolation

Binarization Algorithm

  • CLAHE Enhancement: Contrast Limited Adaptive Histogram Equalization
    • clipLimit: 2.0
    • tileGridSize: (8, 8)
  • Adaptive Thresholding: Gaussian-weighted local thresholding
    • blockSize: 25
    • C: 10
    • Method: ADAPTIVE_THRESH_GAUSSIAN_C

Noise Removal Algorithm

  • Stage 1: Non-Local Means Denoising (h=10) applied before binarization
  • Stage 2: Morphological operations (2×2 kernel, 1 iteration) applied after binarization

🧪 Testing

Run the test suite:

pytest

Run tests with coverage:

pytest --cov=robustdococr --cov-report=html

📚 Documentation

🤝 Contributing

We welcome contributions! Please see our:

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

🎓 Citation

If you use this pipeline in your research, please cite:

@misc{robust-doc-ocr-preprocessing,
  author = {3BSALAM},
  title = {Robust Document OCR Preprocessing Pipeline},
  year = {2026},
  publisher = {GitHub},
  journal = {GitHub repository},
  howpublished = {\url{https://github.com/3bsalam-1/RobustDocOCR}}
}

📦 PyPI

This package is available on PyPI: https://pypi.org/project/RobustDocOCR/


© 2026 Robust Document OCR Preprocessing Pipeline

Metadata

Release files for RobustDocOCR 1.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for RobustDocOCR 1.0.3
File Size Uploaded Provenance Actions
robustdococr-1.0.3.tar.gz 16.6 kB Without provenance Details for robustdococr-1.0.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for RobustDocOCR 1.0.3
File Interpreter ABI Platform Actions
robustdococr-1.0.3-py3-none-any.whl Python 3 none any Details for robustdococr-1.0.3-py3-none-any.whl

Total release size: 35.7 kB

Release files / robustdococr-1.0.3.tar.gz

Download URL robustdococr-1.0.3.tar.gz
Size 16.6 kB
Tags Source
SHA-256 checksum
How to use checksums
e766a5e83c31d16f3bb89084d26c2a64fd0d5538daf3a6ba5d7a9e2f77a09197
BLAKE2b-256 checksum
How to use checksums
7602c6e5f3d224a6380456a4f171bf5c434f124da5e423fb1b318db74f59de96
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.9

Release files / robustdococr-1.0.3-py3-none-any.whl

Download URL robustdococr-1.0.3-py3-none-any.whl
Size 19.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
246c1280cf5fb2d5b8e5e76b2849334a8ba49dbce116b486c08f7b23e5605fab
BLAKE2b-256 checksum
How to use checksums
cd6cfe9c695960fcd074438a404ac23f2b028205851c96a0f45ec9fa033019cc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.9

Security for RobustDocOCR 1.0.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.3 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page