This project provides an end-to-end flow for document OCR, field mapping, and verification against an applicant form.
Key Features
- OCR via PaddleOCR for images and PDFs, with dynamic preprocessing and PDF rasterization
- Document-type aware field mapping (Aadhaar/Voter → Name, DL/Passport → Address, Birth/SLC → DOB, Handwritten → All)
- Robust normalization and fuzzy verification for names, addresses, phones, DOB, and gender
- Simple frontend to collect applicant details, upload docs, and review confidence
- Inline edit/save of extracted text per document on the review page; saving re-runs map+verify to refresh confidence scores
- Backend: Python, FastAPI, Uvicorn, PaddleOCR, OpenCV, PyMuPDF (fitz), RapidFuzz, Pydantic
- Frontend: HTML, CSS, Vanilla JS, Bootstrap 5
- Testing: Pytest
backend/FastAPI servicecore/ocr.pyOCR utilities: instance cache, preprocess, image/PDF handlingcore/mapper.pyField mapping: regex + label-value fallback, doc-type filteringcore/verifier.pyNormalization and scoring + aggregation and decisionroutes/FastAPI routes for extraction, mapping, verificationapp.pyFastAPI app with CORS and router setup
frontend/Static SPAindex.html,styles.css,app.js
tests/Pytest suite for mapping/verification logic
- Stub client (
backend/core/mosip_client.py) simulates MOSIP pre-registration, upload, and status for offline testing. - Endpoints (all under
/api/v1/mosip):POST /integrate– end-to-end: extract → map/verify (ifmanual_dataprovided) → create pre-reg ID → upload document. Default verification threshold is0.8. Verification uses only overlapping fields between extracted mapped data and manual data; if no overlap, verification is skipped instead of failing.GET /test– quick connectivity stub (returns a canned response).GET /status/{pre_reg_id}– returns stubbed status (pending).POST /batch-submit– process multiple uploaded files; optionalverification_dataJSON array mirrorsmanual_dataper file.
- Frontend buttons: Submit to MOSIP hits
/integrate; Batch Submit posts multiple files to/batch-submitand reports successes/failures.
Use these to validate OCR, mapping, verification, and MOSIP flows.
UI labels (upload panel)
-
Name verification document (e.g., Aadhaar, Voter ID)
-
Address verification document (e.g., Driving License, Passport)
-
Date of birth document (e.g., Birth Certificate, School Leaving Certificate)
-
Large file (> size limit) → expect
FILE_TOO_LARGEstyle error if enforced by deployment stack.
- Health:
curl http://localhost:8000/api/v1/ocr/health - Map & verify:
curl -X POST http://localhost:8000/api/v1/map-and-verify -H "Content-Type: application/json" -d '{"raw_text":"Name: Alice\nDOB: 2000-01-01","user":{"name":"Alice","dob":"2000-01-01"}}' - MOSIP test:
curl http://localhost:8000/api/v1/mosip/test - MOSIP status:
curl http://localhost:8000/api/v1/mosip/status/PRE1234567
Backend dependencies (see backend/requirements.txt):
fastapi,uvicornpaddleocrnumpy,opencv-pythonPyMuPDF(imported asfitz)rapidfuzz(optional but recommended)unidecodepydantic
Create a virtual environment and install dependencies.
python -m venv .venv
.\.venv\Scripts\activate
pip install -r backend\requirements.txt- Start the backend API (FastAPI + Uvicorn):
cd backend
uvicorn app:app --reload --host 127.0.0.1 --port 8000- Serve the frontend (static):
cd frontend
python -m http.server 5500- Open the app at
http://127.0.0.1:5500/index.html - Backend docs:
http://127.0.0.1:8000/docs - MOSIP quick test:
GET http://127.0.0.1:8000/api/v1/mosip/test
Base prefix: /api/v1
- OCR
POST /api/v1/ocr/extract-text→ returns extracted raw text from uploaded file (file)
- Mapping + Verification
POST /api/v1/extract-fields→ maps fields fromraw_text(supportsdocument_type)POST /api/v1/map-and-verify→ maps and verifies againstuser, filtered bydocument_type
- Direct Verification
POST /api/v1/verification/verify→ verifiesocrvsuserpayloads
- MOSIP
POST /api/v1/mosip/integrate→ single-file OCR → map/verify (optional) → pre-reg stub → upload stubPOST /api/v1/mosip/batch-submit→ multi-file submit with optional per-file verification dataGET /api/v1/mosip/test→ connectivity stubGET /api/v1/mosip/status/{pre_reg_id}→ status stub
aadharorvoter: extract and show onlyname; confidence equals name match vs formdlorpassport: extract and show onlyaddress; confidence equals address matchbirthorslc: extract and show onlydob; confidence equals date matchhandwritten: show all fields found; confidence aggregates available fields
- Combines English and Hindi regex patterns for core fields
- Fallback label-value parser for lines like
Name: John Smith,Address: 123 Elm St,Phone number: 555-12345, etc. - Document-type filtering ensures the review displays only relevant fields
- Cleaning:
- Phone → digits only (strip punctuation/spaces)
- Email → lowercase, trim spaces, fix common typos (
qmail→gmail) - Pincode → digits only, up to 6
- Normalization:
- Unicode (NFKD + transliteration via
unidecode) - Names/Addresses → lowercase, punctuation removal, abbreviation expansion (e.g.,
st.→street), stopword removal - Phone → digits-only; NSN (last 10 digits) logic
- DOB → parse common formats to ISO
YYYY-MM-DD
- Unicode (NFKD + transliteration via
- Scoring per field (0..1): name, address, phone, dob, gender
- Aggregation:
- Weighted average over available fields (default weights: name 0.35, dob 0.30, phone 0.15, address 0.15, gender 0.05)
- Decision thresholds:
MATCH (>=0.85),REVIEW (>=0.6), elseMISMATCH
- Applicant form with required fields (all except Middle Name)
- Document uploads per type; processes OCR then mapping+verification
- Review panel shows mapped-only fields per doc type and confidence badge
- Edit/Save UX: only Edit on top; Save appears at bottom when editing applicant data
- Review page extracted-text panel now has Edit/Save to adjust OCR text per doc and recompute confidence
- Run tests:
pytest -q- If PaddleOCR flags differ across versions,
core/ocr.pyhandles new vs old APIs. - CORS is open by default; adjust
backend/app.pyfor production.
Proprietary project. Do not redistribute without permission.