File size: 3,610 Bytes
d8021ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
---
title: KYC OCR (India)
emoji: 📄
colorFrom: blue
colorTo: green
sdk: gradio
sdk_version: "5.0.0"
python_version: "3.12"
app_file: app.py
pinned: false
license: mit
short_description: Extract fields from Indian PAN cards and cheques on CPU
---

# 🇮🇳 KYC Document OCR

A lightweight, CPU-friendly pipeline for extracting structured fields from
Indian KYC documents (PAN card, cheque) from images or PDFs.

Built on [RapidOCR](https://github.com/RapidAI/RapidOCR) — ONNX-runtime
deployment of PaddleOCR PP-OCRv4 models. No GPU needed.

## What it does

- **PDF or image input** (PyMuPDF rasterises PDFs at 300 DPI)
- **Document type auto-detection** via keyword + regex scoring
- **Cheque rotation correction** — detects sideways-scanned cheques (text
  spans more vertically than horizontally) and rotates before MICR extraction
- **Format-spec validation** — PAN regex with 4th-character entity-type
  lookup, IFSC regex, date normalisation, MICR positional parse

## Supported documents

| Document | Fields extracted |
|---|---|
| **PAN card** | PAN number, name, father's name (individual) / company name, DOB / date of incorporation, entity type (Individual / Company / HUF / Firm / Trust / etc.) |
| **Cheque** | Bank name, branch address, IFSC code, account number, 9-digit MICR code, cheque number, transaction code |
| **Aadhaar** | _Coming soon — recommended path: read the secure QR code via `pyzbar` rather than OCR_ |
| **Passport** | _Coming soon — recommended path: parse the MRZ via `passporteye`_ |

## How to use the Space

1. Drop a PDF or image into the upload box
2. Click **Extract fields**
3. View the structured output and raw OCR (for debugging)

The included example is a synthetic PAN with fake data so you can try it
without uploading real documents.

## Running locally

```bash
git clone <this-space-url>
cd <repo>
pip install -r requirements.txt
python app.py
```

Then open `http://127.0.0.1:7860`.

## Using `kyc_extract.py` directly as a library

```python
from kyc_extract import process, RapidOCR

engine = RapidOCR()
results = process("path/to/document.pdf", engine)

for page in results:
    print(page["detected_type"])     # 'pan' or 'cheque'
    print(page["fields"])             # structured dict
```

## Privacy

This is a public Space. **Do not upload real personal documents.** For
production KYC pipelines, fork this repo and deploy on your own private
infrastructure (DPDP Act and UIDAI guidelines require it for Aadhaar in
particular).

## Architecture notes

- All extraction is rule-based — no model is hardcoded to specific names,
  banks, PANs, or addresses. The same script handles any PAN or any bank's
  cheque.
- Document classification uses keyword + regex scoring; if confidence is
  low, type is `unknown` and no extractor runs.
- For cheques in portrait-orientation PDFs (common when phones are held
  vertically), the pipeline detects the layout anomaly and re-OCRs the
  rotated image, merging text from both passes so address and IFSC don't
  get lost in the rotation.

## Limitations

- English-only OCR — Hindi label text (`नाम/Name`) comes out garbled but
  the pipeline has shape-based fallbacks so this doesn't break field
  extraction. For full Hindi extraction, add `lang='hi'` to RapidOCR.
- MICR fonts are E-13B and not perfectly read by general OCR. The pipeline
  reconstructs the 9-digit MICR from OCR fragments via positional parsing,
  which works on most cheques but degrades on poor scans.
- Aadhaar and passport extractors are stubs. The proper approach uses QR
  and MRZ respectively, not pure OCR.