A comprehensive OCR (Optical Character Recognition) system for extracting information from Egyptian national ID cards, passports, and highlighted PDF documents. This project uses advanced computer vision techniques, YOLO object detection, and multiple OCR engines to accurately extract and process document information.
- Automatic card detection and cropping using YOLO models
- Intelligent text extraction with EasyOCR and Google Cloud Vision API
- Arabic and English text support
- National ID number extraction and decoding
- Name extraction (with compound name handling)
- Address extraction with intelligent filtering
- Fallback mechanisms for robust processing
- MRZ (Machine Readable Zone) detection using PassportEye
- Automatic data parsing including:
- Name and surname
- Sex
- Date of birth
- Passport number
- Expiration date
- Issuing country
- Country code resolution with comprehensive database
- Automatic detection of highlighted text in PDFs (yellow, green, pink)
- Date extraction from highlighted regions
- Number/ID extraction from highlighted content
- Multiple output formats (JSON and Markdown)
- Multi-page PDF support
- FastAPI - Modern web framework for API endpoints
- YOLO (Ultralytics) - Object detection for card/document localization
- EasyOCR - Arabic and English text recognition
- Google Cloud Vision API - Advanced OCR capabilities
- Tesseract OCR - PDF text extraction
- PassportEye - MRZ detection and parsing
- OpenCV - Image processing and computer vision
- PyTorch - Deep learning backend
- Python 3.8 or higher
- Tesseract OCR installed on your system
- Google Cloud Vision API credentials (for ID scanner)
-
Clone the repository
git clone <repository-url> cd ocr_egyptian_id
-
Install dependencies
pip install -r requirements.txt
-
Set up Google Cloud Vision API
- Create a Google Cloud project
- Enable the Vision API
- Download service account credentials
- Save credentials as
test-ocr.jsonin the project root
-
Download YOLO models
- Ensure the following model files are in the project root:
detect_id_card.pt- Card detection modeldetect_odjects.pt- Object detection modeldetect_id.pt- ID-specific detection model
- Ensure the following model files are in the project root:
Start the FastAPI server:
python new_version.pyThe server will run on http://0.0.0.0:8006
GET /healthPOST /scan-id
Content-Type: multipart/form-data
file: <image_file>Response:
{
"status": "success",
"data": {
"firstname": "محمد علي",
"lastname": "أحمد حسن",
"national_id": "29501011234567",
"decoded_info": {
"birth_date": "01/01/1995",
"governorate": "Cairo",
"gender": "Male"
}
}
}POST /id-scanner
Content-Type: multipart/form-data
file: <image_file>Response:
{
"status": "success",
"data": {
"name": "محمد علي أحمد حسن",
"address": "شارع النيل، القاهرة",
"national_id": "٢٩٥٠١٠١١٢٣٤٥٦٧"
}
}POST /extract-passport
Content-Type: multipart/form-data
file: <passport_image>Response:
{
"status": "success",
"data": {
"name": "MOHAMED ALI",
"surname": "AHMED HASSAN",
"sex": "M",
"date_of_birth": "01/01/1995",
"passport_number": "A12345678",
"expiration_date": "01/01/2030",
"issuing_country": "EGYPT"
}
}POST /extract-pdf-highlights?output_format=json
Content-Type: multipart/form-data
file: <pdf_file>Response:
{
"status": "success",
"format": "json",
"highlighted_texts": ["Text 1", "Text 2"],
"total_highlights": 2,
"number_id": "Q5 025701 0085",
"dates": {
"from_date": "2024-01-01",
"to_date": "2024-12-31",
"all_dates": ["2024-01-01", "2024-12-31"]
}
}python id_scanner.py <image_path>python passport.py <passport_image_path>python test_pdf_extraction.pyocr_egyptian_id/
├── scanner.py # YOLO-based ID card scanner
├── id_scanner.py # Google Vision-based ID scanner
├── passport.py # Passport MRZ extraction
├── pdf_highlight_extractor.py # PDF highlight detection and extraction
├── new_version.py # FastAPI server with all endpoints
├── test_pdf_extraction.py # PDF extraction testing script
├── requirements.txt # Python dependencies
├── country_codes.json # Country code database
├── test-ocr.json # Google Cloud credentials
├── detect_id_card.pt # YOLO card detection model
├── detect_odjects.pt # YOLO object detection model
└── detect_id.pt # YOLO ID detection model
- Card detection and cropping
- Perspective transformation
- Name extraction with intelligent positioning
- National ID number detection and decoding
- OCR error correction for Arabic text
- Google Cloud Vision integration
- Arabic text normalization
- Header and noise filtering
- National ID extraction with multiple digit formats
- Address cleaning and validation
- MRZ detection and parsing
- Date parsing and formatting
- Country code resolution
- Data validation and cleaning
- Color-based highlight detection (yellow, green, pink)
- Multi-page PDF processing
- Date and ID extraction
- Markdown and JSON output formats
- EasyOCR: Arabic (
ar) and English (en) - Tesseract: English and Arabic (
eng+ara) - Google Vision: Arabic language hints
Models can be customized in the API endpoints or when initializing the scanner:
scanner = YOLOCardScanner(
card_model_path='detect_id_card.pt',
objects_model_path='detect_odjects.pt',
id_model_path='detect_id.pt'
)The system includes comprehensive error handling:
- Fallback from YOLO to contour-based detection
- Multiple OCR attempts with different preprocessing
- Validation of extracted data
- Detailed error messages in API responses
Logging is configured for debugging and monitoring:
- Application logs:
app.log - Passport processing logs:
passport_debug.log - Console output for real-time monitoring
- GPU Support: EasyOCR and PyTorch can utilize GPU if available
- Image Preprocessing: Automatic image enhancement for better OCR accuracy
- Caching: Temporary files are cleaned up after processing
- Async Processing: FastAPI supports async operations for better performance
- Requires clear, well-lit images for best results
- Arabic text recognition accuracy depends on image quality
- YOLO models need to be trained on representative datasets
- Google Cloud Vision API requires internet connectivity and valid credentials
- Support for more document types
- Batch processing capabilities
- Real-time video stream processing
- Enhanced error correction for OCR results
- Multi-language support expansion
- Docker containerization
- Database integration for storing results