How dangxg/h-cat-r-synthetic-handwriten-dataset-generator works
Purpose and entry points
This is a Python CLI pipeline that produces synthetic Catalan handwriting OCR datasets. The primary entry point is build_dataset.py; supporting CLI stages acquire and validate font/text inputs: download_fonts.py, verify_and_clean_fonts.py, and verify_and_clean_books.py.
Input acquisition and curation
Font acquisition downloads handwriting/manuscript fonts into category-organized font directories via download_fonts.py. verify_and_clean_fonts.py is a quality gate: it checks required Catalan characters, digits, and punctuation both in font cmap tables and by actual Pillow rendering, optionally removing invalid fonts. Text curation is handled by verify_and_clean_books.py, which validates/cleans Catalan book text before generation.
Dataset construction
build_dataset.py reads curated texts and eligible fonts, creates text units as either five-word lines or individual words, and renders them as fixed-height, variable-width RGB PNGs. It supports style selection, font-category filtering, per-category font limits, source/output-directory overrides, and configurable train/validation proportions.
Parallel rendering boundary
build_dataset.py is designed for multiprocess image generation. The main process precomputes all render tasks and assigns unique image filenames before workers start. Workers render/write only their assigned PNGs and return metadata; the parent process serializes metadata and aggregate statistics. Work is dynamically balanced with unordered pool iteration and platform-specific multiprocessing behavior (spawn on Windows; fork on Linux/macOS).
Output contract
Generation writes a HuggingFace imagefolder-compatible directory with train, validation, and test splits. Each split contains PNGs plus metadata.jsonl; root-level dataset_info.json records split counts, feature schema, generation mode/style, and font totals. Metadata links every image to its transcription, font identity/category/style, source book, and mode. This contract is produced by build_dataset.py.
Runtime and external dependencies
The runtime is Python 3.8+ with Pillow for raster rendering, fontTools for font inspection, and tqdm for progress reporting. HuggingFace datasets is an optional downstream loader; generated data targets OCR training workflows such as HuggingFace Transformers TrOCR. External content sources are Wikisource Catalan validated books and downloadable third-party fonts.
Diagnostic utility
test_font_rendering.py independently renders either one font or an entire font directory, providing a focused visual/functional diagnostic separate from destructive font cleanup.
Main entry points: build_dataset.py Dataset builder · download_fonts.py Font downloader · verify_and_clean_fonts.py Font quality gate · verify_and_clean_books.py Book text curator
Components 17
Input acquisition & curation
- Third-party font sources · Downloadable handwriting and manuscript fonts.
- Font downloader
download_fonts.py - Category font directories
- Font quality gate · Checks cmap coverage and Pillow rendering; may remove invalid fonts.
verify_and_clean_fonts.py - Catalan Wikisource books
- Book text curator · Validates and cleans Catalan text.
verify_and_clean_books.py - Curated book texts
- Font rendering diagnostic · Visually tests one font or a font directory without cleanup.
test_font_rendering.py
Dataset generation
- Dataset builder · Selects fonts and texts and controls split/style configuration.
build_dataset.py - Text units · Five-word lines or individual words.
- Render task planner · Preassigns unique filenames and render tasks before workers start.
- Multiprocess render workers · Dynamically render assigned fixed-height RGB PNGs and return metadata.
- Metadata & statistics writer · Serializes worker metadata and aggregate generation statistics.
Imagefolder output
- Train / validation / test PNGs · Fixed-height, variable-width RGB PNG images.
- Split metadata.jsonl · Links images to transcription, font, category, style, source book, and mode.
- dataset_info.json · Records split counts, schema, generation mode/style, and font totals.
Other components
- HF OCR training consumers · Optional datasets loader and TrOCR-oriented workflows.
Connections 19
- Third-party font sources → Font downloader: downloads
- Font downloader → Category font directories: stores by category
- Category font directories → Font quality gate: inspects
- Font quality gate → Category font directories: keeps eligible fonts
- Category font directories → Font rendering diagnostic: diagnoses
- Catalan Wikisource books → Book text curator: provides text
- Book text curator → Curated book texts: writes cleaned text
- Category font directories → Dataset builder: eligible fonts
- Curated book texts → Dataset builder: source texts
- Dataset builder → Text units: creates
- Dataset builder → Render task planner: configures
- Text units → Render task planner: plans
- Render task planner → Multiprocess render workers: unordered task dispatch
- Multiprocess render workers → Train / validation / test PNGs: writes PNGs
- Multiprocess render workers → Metadata & statistics writer: returns metadata
- Metadata & statistics writer → Split metadata.jsonl: writes per split
- Metadata & statistics writer → dataset_info.json: writes summary
- Train / validation / test PNGs → HF OCR training consumers: imagefolder dataset
- Split metadata.jsonl → HF OCR training consumers: transcriptions