A robust and flexible OCR system designed to extract structured data from invoices in multiple languages, with a primary focus on English and Arabic.
This project offers two main components:
- Specialized CLI Tool (
main.py): A tuned pipeline for specific invoice layouts using OpenCV and Tesseract. - Modular OCR Engines (
src/): A collection of classes wrapping modern OCR frameworks (PaddleOCR, EasyOCR) and advanced preprocessing for building custom pipelines.
- Multi-Engine Support: Seamlessly switch between Tesseract, PaddleOCR, and EasyOCR.
- Advanced Preprocessing:
- Automatic skew correction (hough lines transformation).
- Grid line removal for cleaner text extraction.
- Language-specific image enhancement (e.g., morphological operations for Arabic).
- Adaptive thresholding and denoising.
- Structured Extraction:
- Parses invoice line items into structured Excel tables.
- Supports mixed-language content (e.g., Arabic descriptions with English headers).
- Table Recognition: Utilizes PaddleOCR's PP-Structure to detect and extract tables directly.
- Python 3.8+
- Tesseract OCR Engine:
- Mac:
brew install tesseract - Linux:
sudo apt-get install tesseract-ocr - Windows: Download Installer
- Mac:
- Language Data: Ensure you have
engandaratrained data for Tesseract.
pip install -r requirements.txtNote: For PaddleOCR, you may need to install paddlepaddle individually depending on your OS and hardware.
Use this script to process standard invoice images and extract a structured Excel table.
python main.py path/to/invoice.jpg --output outputs/- Input: Path to the invoice image.
- Output: An Excel file (
invoice_table.xlsx) containing the extracted line items.
Quickly check the raw text output for an image to verify Tesseract configuration.
python print_raw.py path/to/image.png <lang>Example: python print_raw.py data/sample.png ara+eng
Build custom OCR pipelines using the classes in src/.
Preprocessing Example:
from src.preprocessing import ImagePreprocessor
preprocessor = ImagePreprocessor(language="arabic")
cleaned_image, skew_angle = preprocessor.preprocess("invoice_scan.jpg")
# cleaned_image is a numpy array ready for OCRPaddleOCR Engine Example:
from src.paddle_ocr_engine import PaddleInvoiceOCR
ocr = PaddleInvoiceOCR(language="english")
lines, scores = ocr.extract_text_lines("invoice_scan.jpg")
for line, score in zip(lines, scores):
print(f"{score:.1f}%: {line}")Table Extraction Example (PaddleOCR):
from src.ocr_engine import extract_table_with_paddle
# Extracts the first table found in the image to a DataFrame
df = extract_table_with_paddle("invoice_scan.jpg")
print(df).
├── main.py # CLI entry point for specific invoice layout
├── print_raw.py # Utility to print raw OCR text
├── requirements.txt # Project dependencies
├── grid_config.json # Coordinate configuration for main.py
├── src/ # Modular Source Code
│ ├── config.py # Global configuration & keywords
│ ├── preprocessing.py # Image enhancement pipeline
│ ├── ocr_engine.py # PaddleOCR Table Structure wrapper
│ ├── paddle_ocr_engine.py # PaddleOCR Text Extraction wrapper
│ ├── easy_ocr_engine.py # EasyOCR wrapper
│ └── ...
├── data/ # Sample data directory
└── outputs/ # Default output directory
Modify this file to update:
- HEADER_KEYWORDS: Keywords used to identify table columns in different languages.
- COLUMN_MAPPINGS: Normalization rules for column names (e.g., mapping "Qty", "Quantity", "الكمية" to "quantity").
- TESSERACT_CONFIG: Default flags for Tesseract (e.g.,
--oem 3 --psm 6).
Contains specific x and y coordinates for the grid-based extraction logic used in main.py. Update these values if your target invoice layout changes.
- Fork the repository.
- Create a feature branch (
git checkout -b feature/amazing-feature). - Commit your changes.
- Push to the branch.
- Open a Pull Request.