README with results and usage
Browse files
README.md
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# doc-fraud-ocr-validate
|
| 2 |
+
|
| 3 |
+
OCR + field-validation pipeline for document fraud detection: TrOCR-based
|
| 4 |
+
printed-text field extraction evaluated on real scanned documents, plus a
|
| 5 |
+
pure-Python rule layer that flags structurally invalid document fields
|
| 6 |
+
(number format, date logic, ICAO MRZ check digits).
|
| 7 |
+
|
| 8 |
+
## Results
|
| 9 |
+
|
| 10 |
+
### Rule layer (`evaluate_validators.py`, 2000 seeded cases per rule, seed 13)
|
| 11 |
+
|
| 12 |
+
Labels are fixed at generation time (by construction), never by the code
|
| 13 |
+
under test, so a shared bug cannot hide.
|
| 14 |
+
|
| 15 |
+
| Rule | Precision | Recall | F1 |
|
| 16 |
+
|---|---|---|---|
|
| 17 |
+
| ssn | 1.0 | 1.0 | 1.0 |
|
| 18 |
+
| date_past | 1.0 | 1.0 | 1.0 |
|
| 19 |
+
| name | 1.0 | 1.0 | 1.0 |
|
| 20 |
+
| mrz_line2 | 1.0 | 1.0 | 1.0 |
|
| 21 |
+
|
| 22 |
+
The MRZ validator is additionally verified against the ICAO Doc 9303 Part 4
|
| 23 |
+
specimen line `L898902C36UTO7408122F1204159ZE184226B<<<<<10` (composite
|
| 24 |
+
check digit covers lower-line positions 1-10, 14-20, 22-43; the sex character
|
| 25 |
+
is excluded) and the worked example `L898902C3 -> 6`.
|
| 26 |
+
|
| 27 |
+
### OCR extraction (`evaluate_ocr.py`)
|
| 28 |
+
|
| 29 |
+
`microsoft/trocr-base-printed` on 1000 lines sampled (seed 7) from the
|
| 30 |
+
`test` split of `priyank-m/SROIE_2019_text_recognition` (real scanned
|
| 31 |
+
receipts):
|
| 32 |
+
|
| 33 |
+
| Metric | Value |
|
| 34 |
+
|---|---|
|
| 35 |
+
| CER, raw | 0.0079 |
|
| 36 |
+
| CER, normalized (case/punctuation stripped) | **0.0033** |
|
| 37 |
+
| Exact match, raw | 94.6% |
|
| 38 |
+
| Exact match, normalized | **98.6%** |
|
| 39 |
+
|
| 40 |
+
Worst-case errors are single-character confusions (digits, N/M), not
|
| 41 |
+
catastrophic failures. Run on a T4 in ~4 min of pure inference (1000 lines,
|
| 42 |
+
batch 16). Live metrics: https://huggingface.co/spaces/Offlin33er/doc-fraud-ocr-trackio
|
| 43 |
+
|
| 44 |
+
## Usage
|
| 45 |
+
|
| 46 |
+
```bash
|
| 47 |
+
pip install torch transformers==5.17.0 datasets==5.0.1 tokenizers huggingface_hub pillow
|
| 48 |
+
|
| 49 |
+
# rule layer (CPU, no deps beyond stdlib)
|
| 50 |
+
python evaluate_validators.py --n 5000
|
| 51 |
+
|
| 52 |
+
# OCR evaluation (GPU recommended)
|
| 53 |
+
python evaluate_ocr.py --n 1000 --batch-size 16
|
| 54 |
+
```
|
| 55 |
+
|
| 56 |
+
`validators.py` is dependency-free; `validate_document(fields)` returns a
|
| 57 |
+
list of failed checks for an extracted-field dict, e.g.:
|
| 58 |
+
|
| 59 |
+
```python
|
| 60 |
+
from validators import validate_document
|
| 61 |
+
validate_document({
|
| 62 |
+
"document_number": "123-45-6789",
|
| 63 |
+
"name": "ANNA MARIA ERIKSSON",
|
| 64 |
+
"date_of_birth": "1974-08-12",
|
| 65 |
+
"issue_date": "2023-04-15",
|
| 66 |
+
"expiry_date": "2022-04-14",
|
| 67 |
+
})
|
| 68 |
+
# -> ["expiry_before_issue"]
|
| 69 |
+
```
|
| 70 |
+
|
| 71 |
+
## Notes and limitations
|
| 72 |
+
|
| 73 |
+
- SSN checks are structural only (public SSA randomization rules: area
|
| 74 |
+
001-899 excluding 666, group 01-99, serial 0001-9999). A structurally
|
| 75 |
+
valid number is not an issued number.
|
| 76 |
+
- TrOCR is a line-level recognizer: feeding it full pages requires a line
|
| 77 |
+
segmentation step upstream.
|
| 78 |
+
- The MRZ synthetic evaluation is label-by-construction; correctness of the
|
| 79 |
+
check-digit formula itself is anchored to the ICAO worked example above.
|
| 80 |
+
- `trocr_compat.py` is a compatibility shim: transformers 5.x cannot
|
| 81 |
+
auto-convert TrOCR's legacy RoBERTa tokenizer files, so the module builds
|
| 82 |
+
an equivalent `tokenizer.json` into the HF cache on first use (idempotent).
|
| 83 |
+
|
| 84 |
+
## Attribution
|
| 85 |
+
|
| 86 |
+
- TrOCR: Li et al., "TrOCR: Transformer-based Optical Character Recognition
|
| 87 |
+
with Pre-trained Models" (arXiv:2109.10282) - model `microsoft/trocr-base-printed`.
|
| 88 |
+
- SROIE: Huang et al., ICDAR 2019 competition dataset, mirrored as
|
| 89 |
+
`priyank-m/SROIE_2019_text_recognition`.
|
| 90 |
+
- MRZ check digits: ICAO Doc 9303 Part 4 (TD3 specification and specimen).
|