Skip to content

MdNaqui4/Multilingual-OCR

Repository files navigation

Multilingual Invoice OCR System

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:

  1. Specialized CLI Tool (main.py): A tuned pipeline for specific invoice layouts using OpenCV and Tesseract.
  2. Modular OCR Engines (src/): A collection of classes wrapping modern OCR frameworks (PaddleOCR, EasyOCR) and advanced preprocessing for building custom pipelines.

🚀 Features

  • 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.

🛠️ Installation

Prerequisites

  • Python 3.8+
  • Tesseract OCR Engine:
    • Mac: brew install tesseract
    • Linux: sudo apt-get install tesseract-ocr
    • Windows: Download Installer
  • Language Data: Ensure you have eng and ara trained data for Tesseract.

Install Dependencies

pip install -r requirements.txt

Note: For PaddleOCR, you may need to install paddlepaddle individually depending on your OS and hardware.

📖 Usage

1. Specialized Invoice Processor (main.py)

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.

2. Raw OCR Verification (print_raw.py)

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

3. Modular Library (src/)

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 OCR

PaddleOCR 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)

📂 Project Structure

.
├── 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

⚙️ Configuration

src/config.py

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).

grid_config.json

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.

🤝 Contributing

  1. Fork the repository.
  2. Create a feature branch (git checkout -b feature/amazing-feature).
  3. Commit your changes.
  4. Push to the branch.
  5. Open a Pull Request.

📄 License

MIT License

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages