Project · 2023 · completed · Personal
Synthetic MRZ image generation
A configurable pipeline for producing realistic synthetic machine-readable identity-document images for OCR experiments.
Problem
Training OCR systems for identity documents requires large labeled datasets, but real passport and ID images contain sensitive personal information and are difficult to distribute. The objective was to generate controlled Machine Readable Zone examples with valid structure, diverse identities, and realistic capture defects without using real documents.
Approach
The pipeline preprocesses source names, creates randomized name combinations and identity fields, generates ICAO-style TD1 MRZ strings, and renders them with OCR-B fonts over document-like backgrounds. A final degradation stage applies blur, noise, and geometric skew to reproduce common scanning and camera artifacts.
Technical implementation
The mrz package generates checksummed TD1 codes with randomized document numbers, dates, sex markers, and Algerian nationality codes. OpenCV and Pillow compose the text and image backgrounds. Samples are shuffled into clean, Gaussian-noise, Poisson-noise, and blurred subsets, after which a random 20% receive a small affine skew. A command-line pipeline orchestrates preprocessing, name generation, MRZ creation, rendering, and degradation for the requested sample count.
mrz = TD1CodeGenerator(
"ID", "DZA", document_number, birth_date,
sex, expiry_date, "DZA", surname, given_name, ""
)
render_mrz(str(mrz), font="OCRB", background=sample_background())