You have a photo of an ID card and you need the data inside it — name, date of birth, document number, expiration — as structured JSON. AWS Textract AnalyzeID does this, but it costs $0.025/doc and only supports US documents.
Here's how to do it for any ID card, from any country, in 10 lines of Python — for about $0.013 per document.
The Pipeline
- OCR API extracts raw text from the ID card image
- GPT-4o mini structures the raw text into label-value pairs
No regex, no templates, no per-country configuration.
The Code
import requests, json
from openai import OpenAI
def id_card_to_json(image_path):
# Step 1: OCR
ocr = requests.post(
"https://ocr-wizard.p.rapidapi.com/ocr",
headers={"x-rapidapi-key": "YOUR_KEY", "x-rapidapi-host": "ocr-wizard.p.rapidapi.com"},
files={"image": open(image_path, "rb")},
).json()
# Step 2: Structure with LLM
result = OpenAI().chat.completions.create(
model="gpt-4o-mini",
response_format={"type": "json_object"},
messages=[
{"role": "system", "content": "Extract every label-value pair from this ID document OCR text. Return JSON: {fields: [{label, value}]}"},
{"role": "user", "content": ocr["body"]["fullText"]},
],
)
return json.loads(result.choices[0].message.content)
Example Output
For a US driver license, the OCR returns raw text like:
ID: 012 345 678
DOB: 06-09-85
SEX: F EYES: BR HT: 5-09
CLASS D
ISSUED: 09-30-08 EXPIRES: 10-01-16
GPT-4o mini turns it into:
{
"document_type": "DRIVER LICENSE",
"fields": [
{"label": "ID", "value": "012 345 678"},
{"label": "DOB", "value": "06-09-85"},
{"label": "SEX", "value": "F"},
{"label": "EYES", "value": "BR"},
{"label": "CLASS", "value": "D"},
{"label": "ISSUED", "value": "09-30-08"},
{"label": "EXPIRES", "value": "10-01-16"}
]
}
Tested on 4 Document Types
We tested the same code on US driver licenses (New York, Arizona), an international travel document with bilingual labels, and a US green card.
| Document | Fields Extracted | Accuracy |
|---|---|---|
| US Driver License (New York) | 15 | 100% |
| US Driver License (Arizona) | 12 | 100% |
| International Travel Document | 6 | 83% |
| US Green Card | 10 | 100% |
96% accuracy across all documents with zero custom parsing rules.
Cost: 20x Cheaper than AWS Textract
| Volume | AWS Textract AnalyzeID | OCR API + GPT-4o mini |
|---|---|---|
| 1K docs/mo | $25.00 | $13.07 |
| 10K docs/mo | $250.00 | $13.74 |
| 100K docs/mo | $2,500.00 | $100.49 |
Plus: AWS only supports US documents. This approach works on any country, any document type.
Use Cases
- KYC onboarding — extract customer data from uploaded IDs
- Age verification — parse DOB from any ID card automatically
- Document digitization — batch process physical ID archives
- Travel & hospitality — auto-fill check-in forms from guest IDs
Top comments (0)