A high-performance, production-ready microservice for extracting structured data from official documents using Google Gemini Vision AI and FastAPI.
Documentation β’ Installation β’ Usage β’ Features β’ Contributing
OCR Extraction is a robust microservice designed to transform unstructured document images (Passports, Visas, Emirates IDs) into structured, validated JSON data. Built with FastAPI and Google Gemini Vision, it leverages the power of Multimodal LLMs to handle complex OCR tasks that traditional methods struggle with, such as:
- π Multi-Format Support: Handles JPG, PNG, and PDF files automatically.
- π§ Intelligent Extraction: Uses context-aware AI to correct orientation and disambiguate characters (e.g., '1' vs 'I').
- π‘οΈ Production Ready: Includes rate limiting, load balancing, and JWT authentication.
- β‘ High Performance: Async architecture with multiple Gemini API key distribution.
| Feature | Description | Output |
|---|---|---|
| π Passport OCR | Extracts full details including MRZ from global passports. | Structured JSON with validated dates. |
| π³ ID Card OCR | specialized extraction for Emirates IDs and standard ID cards. | Name, ID Number, Expiry, Nationality. |
| π PDF Processing | Auto-converts PDF documents to high-res images for analysis. | Seamless handling of multi-page PDFs. |
| π‘οΈ MRZ Logic | Custom logic to parse and validate Machine Readable Zones (MRZ). | Cleaned, pattern-validated strings. |
- β‘ Async Architecture: Built on FastAPI for high-concurrency performance.
- π Load Balancing: Round-robin distribution across multiple Google Gemini API keys to handle rate limits.
- π Rate Limiting: Integrated
SlowAPIto prevent abuse (default: 5 req/min per IP). - π Security: JWT Authentication middleware and Nginx reverse proxy configurations.
- π³ Containerized: Full Docker and Docker Compose support for easy deployment.
graph TD;
Client-->Nginx;
Nginx-->FastAPI;
FastAPI-->Auth_Layer;
Auth_Layer-->Rate_Limiter;
Rate_Limiter-->Gemini_Vision_API;
Gemini_Vision_API-->Structured_JSON;
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Client / User β
β POST /extract_passport_details β
β Authorization: Bearer <token> β
ββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Nginx Reverse Proxy β
β β’ Entry Point (Port 8000) β
β β’ File Size Validation (<200MB) β
β β’ Load Balancing (Least Conn) β
ββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β FastAPI Application β
β ββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββ β
β β Middleware β Core Logic β β
β β β’ SlowAPI (Limit) β β’ File Validation (PIL/Fitz) β β
β β β’ JWT Auth β β’ Model Pool Management β β
β ββββββββββββ¬ββββββββββββ΄βββββββββββββββββ¬ββββββββββββββββ β
β β β β
β βΌ βΌ β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Google Gemini Vision API β β
β β β’ Multi-modal LLM Processing β β
β β β’ Context-aware OCR & Extraction β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
- π Python 3.9 or higher
- π Google Gemini API Key(s)
- π³ Docker (Optional, for containerized run)
-
Clone the repository
git clone https://github.com/Venumurala91/OCR_Extraction.git cd OCR_Extraction -
Create virtual environment
python -m venv venv # Windows venv\Scripts\activate # Linux/Mac source venv/bin/activate
-
Install dependencies
pip install -r requirements.txt
-
Configure Environment Create a
.envfile in the root directory:SECRET_TOKEN=your_secret_auth_token GOOGLE_API_KEY=your_gemini_api_key # Add multiple keys if available for load balancing GOOGLE_API_KEY_2=your_second_key
-
Run the application
python fastapi_app.py
Visit documentation at
http://localhost:8000/docs
-
Build the image
docker-compose build
-
Run containers
docker-compose up -d
-
Access API The API will be available at
http://localhost:8000.
- Authentication: Obtain your
SECRET_TOKENfrom the admin or.envfile. - Make a Request: Send a POST request with the image file.
import requests
url = "http://localhost:8000/extract_passport_details"
headers = {
"Authorization": "your_secret_token"
}
files = {
"image": open("path/to/passport.jpg", "rb")
}
response = requests.post(url, headers=headers, files=files)
print(response.json())| Endpoint | Method | Description |
|---|---|---|
/extract_passport_details |
POST |
Extract data from Passport images/PDFs |
/extract_visa_details |
POST |
Extract data from Visa documents |
/extract_emirates_id_details |
POST |
Extract data from Emirates ID cards |
/health |
GET |
Server health check status |
OCR_Extraction/
βββ π fastapi_app.py # Main application entry point & routes
βββ βοΈ api_functions.py # Core logic, Gemini interaction, & Validation
βββ π³ Dockerfile # Docker build configuration
βββ π³ docker-compose.yml # Container orchestration
βββ π§ nginx.conf # Nginx reverse proxy configuration
βββ π requirements.txt # Python dependencies
βββ π README.md # Project documentation
βββ π .env # Environment variables (GitIgnored)
| Issue | Solution | π§ |
|---|---|---|
| 401 Unauthorized | Check your Authorization header. |
ensure it matches SECRET_TOKEN. |
| 429 Too Many Requests | Rate limit exceeded. | Wait 1 minute or increase limit in config. |
| Invalid Image Format | Unsupported file type. | Use .jpg, .png, or .pdf. |
| Empty Response | Poor image quality. | Ensure image is clear and not blurry. |
To see detailed logs, ensure your environment is not suppressing output. The application uses standard Python logging.
- Framework: FastAPI
- AI Model: Google Gemini 1.5 Flash / Pro
- Image Processing: Pillow (PIL), PyMuPDF (Fitz)
- Server: Uvicorn
- Proxy: Nginx
- Containerization: Docker
This project is licensed under the MIT License - see the LICENSE file for details.
- Google DeepMind for the Gemini Vision API.
- Tiangolo for the amazing FastAPI framework.