Vision Module β Neural Layout & OCR Recognition
Deep learning models for document layout analysis, table structure recognition, and optical character recognition.
What is this? β The vision layer uses neural networks (ONNX-optimized DLA for layout, CRAFT+PaddleOCR for text recognition) to understand document structure and extract text from images.
Core Recognizers (ONNX-Based Inference)
Layout Recognizer
layout_recognizer.py (18KB) β Document layout detection (12-class DLA model).
- 12 semantic classes: Text, Title, Figure, Table, Caption, Header, Footer, Reference, Equation, Background
- ONNX inference with auto-scaling
- Batch processing (default 16)
- Remote DLA server option
- Post-processing: NMS, confidence filtering
- Focus: Layout Recognition Model
OCR Pipeline
ocr.py (28KB) β Two-stage text recognition.
- Stage 1: CRAFT text detection (region proposals)
- Stage 2: PaddleOCR character recognition
- Supports 80+ languages (esp. multilingual support)
- Spatial preservation (bbox per word)
- Confidence scoring per character
- Parallel device limiting (Semaphore)
Table Structure Recognizer
table_structure_recognizer.py (23KB) β Cell grid detection and merging.
- Line detection (Hough transform)
- Cell boundary extraction
- Merged cell detection
- Row/column index mapping
- Heuristic-based post-processing
- Grid reconstruction
Recognizer Base Class
recognizer.py (16KB) β ONNX inference abstraction.
- Model loading (local or HuggingFace)
- Batch inference orchestration
- Input preprocessing (resize, normalize)
- Output postprocessing (NMS, filtering)
- Multi-threaded execution
Post-Processing
postprocess.py (13KB) β NMS and result refinement.
- Non-Maximum Suppression (IoU-based)
- Confidence thresholding
- Label confidence weighting
- Bounding box normalization
- Class-specific filtering
Vision Operators
operators.py (24KB) β Image processing utilities.
- Image resizing and scaling
- Rotation correction (EXIF)
- Perspective transformation
- Feature extraction (text, tables)
- Image normalization
- Histogram equalization
Specialized Recognizers
t_recognizer.py, t_ocr.py, seeit.py
- t_recognizer.py: Tencent recognizer integration
- t_ocr.py: Tencent OCR wrapper
- seeit.py: Visualization & debugging helper
Architecture: Multi-Stage Recognition
Key Algorithms
Layout Recognition (DLA)
Model: Deep Layer Aggregation (DLA) backbone, trained on document images
- Input: Image (auto-scaled to 640Γ640)
- Processing: ResNet50 + FPN (Feature Pyramid Network) for multi-scale features
- Output: Bboxes {x, y, w, h, class_id, confidence}
- Inference: ONNX Runtime (CPU or GPU)
- Batch: Default 16 images at once
- Post-processing: NMS (threshold IoU=0.5), confidence filtering (thr=0.2)
OCR: Two-Stage
Stage 1: CRAFT Text Detection
- Detects word-level regions from image
- Outputs: list of {bbox, confidence}
Stage 2: PaddleOCR Recognition
- Per-region character recognition
- Language model inference
- Outputs: {text, confidence, bbox}
- Supports 80+ languages
Table Structure Recognition
Algorithm:
- Apply Hough transform to detect lines (horizontal + vertical)
- Extract line intersections as potential cell corners
- Cluster intersections into grid
- Infer cell merging (if adjacent cells have same label)
- Output: {cell_bbox, row_idx, col_idx, row_span, col_span}
Integration Flow
From pdf_parser.py perspective:
- Render page image (3x zoom)
- Run LayoutRecognizer β get bboxes with class labels
- Assign extracted text to bboxes (spatial overlap)
- For each bbox by class:
- Text: use assigned text or run OCR
- Table: crop bbox region, run TSR, output grid
- Figure: crop, save image, extract caption
- Header/Footer: mark and optionally skip
- OCR fallback for low-confidence text
- Assemble into semantic blocks
Performance & Trade-offs
| Component | Speed | Accuracy | Notes |
|---|---|---|---|
| LayoutRecognizer (ONNX) | ~50-100 ms/page | High (95%+ for common layouts) | CPU inference possible; batch for speed |
| OCR (CRAFT + PaddleOCR) | ~200-500 ms/page | High (95%+ digital text) | Slower; parallel device limiting |
| TableStructure | ~100-200 ms/table | Medium (heuristic-based) | Line detection can fail on sparse tables |
| NMS Post-processing | <10 ms | Removes duplicates | Configurable IoU threshold |
Configuration & Tuning
Environment Variables:
LAYOUT_RECOGNIZER_TYPEβ "onnx" (default) or "ascend"DEEPDOC_URLβ Remote DLA server URL (fallback)TENSORRT_DLA_SVRβ Alternative DLA server endpointPARALLEL_DEVICESβ Number of parallel inference workersHF_ENDPOINTβ HuggingFace mirror (for model downloads)
Tunable Thresholds:
layout_thrβ Layout confidence threshold (default 0.2)ocr_thrβ OCR confidence threshold (default 0.5)nms_iouβ NMS IoU threshold (default 0.5)batch_sizeβ Inference batch size (default 16)
Model Management
Local Model Loading
Models stored in:
RAGFlow_base_dir/rag/res/deepdoc/
βββ layout.onnx (DLA model)
βββ ...
HuggingFace Fallback
If local model missing, auto-download from:
https://huggingface.co/InfiniFlow/deepdoc
Remote DLA Server
Optional remote TensorRT inference via DEEPDOC_URL or TENSORRT_DLA_SVR environment variable.
Error Handling
- Model load fails (local) β Download from HuggingFace
- HuggingFace unavailable β Try remote DLA server
- Remote DLA unavailable β Fall back to empty layout (output raw text)
- OCR low confidence β Include text with confidence flag
- Table detection fails β Keep as unstructured text
Summary
The vision layer combines PaddlePaddle-trained DLA (layout), CRAFT (text detection), and PaddleOCR (text recognition) into a robust multi-stage pipeline. ONNX inference ensures portability and performance. Fallback strategies and configurable thresholds make the system resilient to edge casesβscanned PDFs, low-quality images, mixed layouts, and multilingual documents.