How xiaofengshi/chinese-ocr works
Project Type and Purpose
• This is an offline, modular, deep‐learning OCR pipeline.
• Purpose: Given a scene image, detect text regions (CTPN), correct orientation (VGG‐based angle classifier), then perform line‐level, variable‐length text recognition (CRNN using CTC).
• Training and inference scripts exist for TensorFlow/Keras and PyTorch backends.
File Structure (Top‐Level Components)
• angle/: VGG16‐based text‐orientation classifier
• ctpn/: Text‐region detector (CNN+RNN) with utility libs, data prep, and outputs
• crnn/: End‐to‐end text recognizer (CNN+GRU/LSTM+CTC)
• ocr/: Higher‐level wrappers/inference for combining modules
• train/: Dataset generation and training pipelines (keras‐train/ pytorch‐train)
• demo.py/demo.ipynb: Entry point for inference demos
• setup scripts: bash scripts to install env & compile C/C++/CUDA utilities
• config files: _config.yml, ctpn text.yml, various model‐path pointers
Extract Architecture Details from README
• Three sub‐networks (modules) in series:
1. Angle classification (rotate image to upright)
3. CRNN recognition (line‐level text → character string via CTC)
• Training pipelines are separate per module; pre‐trained weights are downloaded externally.
• Data‐prep scripts generate Pascal VOC–style annotations or LMDB for CRNN.
• External datasets: CTW, RCTW, SynthText, COCO‐Text, etc.
Main Components & Interactions
a. Inference Pipeline:
• Input Image → Angle Classifier (angle/predict.py) → Rotated Image
→ Text Detector (ctpn/ctpn/demo.py) → Crop Text Lines
→ Text Recognizer (crnn/test.py or ocr/model_*.py) → Output Text
b. Training Pipelines (side by side):
• Data Preparation (train/create_dataset) → Format for CTPN or CRNN
• CTPN Training (ctpn/ctpn/train_net.py + pascal_voc)
• CRNN Training:
– Keras variant (train/keras-train/train.py)
– PyTorch variant (train/pytorch-train/crnn_main.py)
c. Shared Utilities & Config:
• ctpn/lib: network definitions, ROI layers, proposal logic, nms, utils (Cython/C++)
• crnn/models/utils.py, dataset & keys modules
• setup‐*.sh scripts build native modules, install requirements
Architectural Patterns & Design Principles
• Pipeline pattern: clear stage separation with inputs/outputs
• Modularity: each model in its own package, interchangeable backends (TF/Keras vs PyTorch)
• Layered: low‐level utils (Cython/CLI) → model code → top‐level orchestrators (demo/train)
• Reusability: shared dataset loaders, keys mapping
Elements
• Labels: specify filenames or scripts (e.g., ctpn/ctpn/train_net.py) for clarity
• Config files: note _config.yml or text.yml as input to detection module
Detail Without Overthinking
• Related files into packages but call out key scripts (demo.py, predict.py, crnn.py)
• Clearly separates training vs inference
• Highlights the three main deep‐learning modules
• Shows the flow of data from raw images to final text
• Captures all essential architectural elements, configurations, and external dependencies.