Extract text from handwritten images with advanced preprocessing, OCR, and text cleaning.
A robust pipeline utilizing OpenCV, Tesseract, and FastAPI for seamless image-to-text conversion.
- OCR Training and Inferencing Demo (Keras)☝️
- Image Preprocessing: Grayscale conversion, denoising, binarization, and skew correction.
- OCR Extraction: Supports multiple languages via Tesseract OCR.
- Text Cleaning: Removes noise, fixes common OCR errors, and structures text.
- FastAPI Integration: REST API endpoint for easy integration into applications.
- Batch Processing: Process all images in a directory automatically.
-
Install Tesseract OCR (v5.3.0+ recommended):
# Linux sudo apt-get install tesseract-ocr libtesseract-dev # macOS brew install tesseract # Windows: Download from [Tesseract installer](https://github.com/UB-Mannheim/tesseract/wiki)
Ensure Tesseract is added to your system's PATH.
-
Clone the repository:
git clone https://github.com/yourusername/handwritten-ocr-pipeline.git cd handwritten-ocr-pipeline -
Install Python dependencies:
pip install -r requirements.txt
your-project-name/
├── images_raw/ # Raw input images
├── images_processed/ # Preprocessed images
├── ocr_output_raw/ # Raw OCR results
├── ocr_output_processed/ # OCR results from processed images
├── cleaned_text/ # Final cleaned text
├── temp_uploads/ # Temporary API uploads
└── src/ # Source code-
Place images in images_raw/ (e.g., image_raw_1.png).
-
Execute the pipeline:
python src/main_pipeline.py
-
Check outputs in: images_processed/: Preprocessed images. cleaned_text/: Final cleaned text.
- Start the API server:
uvicorn src.api:app --reload
- Visit http://localhost:8000/docs to access interactive Swagger documentation
- Upload an image via the /extract-text/ endpoint to receive JSON-formatted text.
- Preprocessing: Adjust parameters in src/preprocess.py (e.g., blur kernel size, binarization method).
- OCR Language: Modify the lang parameter in ocr_extract.py (e.g., lang='fra' for French).
- Text Cleaning: Add custom regex rules in text_clean.py to fix domain-specific errors.
- Tesseract Not Found: Set the Tesseract path explicitly in ocr_extract.py:
pytesseract.pytesseract.tesseract_cmd = r'/usr/bin/tesseract' # Linux/macOS example- Image Skew Issues: Tweak the Radon transform parameters in correct_skew() (requires scikit-image).
Contributions are welcome! Open an issue or submit a PR for:
- Improved skew correction algorithms
- Additional language support
- Enhanced text structuring rules