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.

10
Core Modules
140KB+
Code
12
Layout Classes
80+
OCR Languages

Core Recognizers (ONNX-Based Inference)

Layout Recognizer

layout_recognizer.py (18KB) β€” Document layout detection (12-class DLA 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

graph TB Input["Input Image"] Input -->|preprocess| Prep["Resize, Normalize
operators.py"] Prep -->|layout| Layout["LayoutRecognizer
12-class DLA"] Layout -->|bboxes + classes| Assign["Region Assignment"] Assign -->|text class| OCRPipe["OCR Pipeline
CRAFT + PaddleOCR"] Assign -->|table class| TSR["Table Structure
Cell Detection"] Assign -->|figure class| FigHandle["Figure Handler
Crop & Save"] Assign -->|other| OtherHandle["Other Handler
Header, Footer, etc."] OCRPipe -->|text boxes| OCROut["OCR Output
text + bbox + conf"] TSR -->|cell grid| TSROut["Table Grid
row/col indices"] FigHandle -->|image| FigOut["Figure Output
path + metadata"] OtherHandle -->|raw bbox| OtherOut["Bbox Output"] OCROut -->|postprocess| PostProc["Post-Processing
NMS + filtering"] TSROut -->|postprocess| PostProc PostProc -->|normalize| Output["Output: Semantic
Regions"] FigOut -->|attach| Output OtherOut -->|attach| Output classDef inputStyle fill:#1a3a52,stroke:#58a6ff,color:#f0f6fc classDef prepStyle fill:#3d3d1a,stroke:#d29922,color:#f0f6fc classDef ocrStyle fill:#2d5a3d,stroke:#3fb950,color:#f0f6fc classDef layoutStyle fill:#5a2d3d,stroke:#f85149,color:#f0f6fc classDef outputStyle fill:#3d5a2d,stroke:#d29922,color:#f0f6fc class Input inputStyle class Prep prepStyle class Layout layoutStyle class OCRPipe,OCROut ocrStyle class PostProc outputStyle class Output outputStyle

Key Algorithms

Layout Recognition (DLA)

Model: Deep Layer Aggregation (DLA) backbone, trained on document images

OCR: Two-Stage

Stage 1: CRAFT Text Detection

Stage 2: PaddleOCR Recognition

Table Structure Recognition

Algorithm:

  1. Apply Hough transform to detect lines (horizontal + vertical)
  2. Extract line intersections as potential cell corners
  3. Cluster intersections into grid
  4. Infer cell merging (if adjacent cells have same label)
  5. Output: {cell_bbox, row_idx, col_idx, row_span, col_span}

Integration Flow

From pdf_parser.py perspective:

  1. Render page image (3x zoom)
  2. Run LayoutRecognizer β†’ get bboxes with class labels
  3. Assign extracted text to bboxes (spatial overlap)
  4. 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
  5. OCR fallback for low-confidence text
  6. Assemble into semantic blocks

Performance & Trade-offs

ComponentSpeedAccuracyNotes
LayoutRecognizer (ONNX)~50-100 ms/pageHigh (95%+ for common layouts)CPU inference possible; batch for speed
OCR (CRAFT + PaddleOCR)~200-500 ms/pageHigh (95%+ digital text)Slower; parallel device limiting
TableStructure~100-200 ms/tableMedium (heuristic-based)Line detection can fail on sparse tables
NMS Post-processing<10 msRemoves duplicatesConfigurable IoU threshold

Configuration & Tuning

Environment Variables:

Tunable Thresholds:

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

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.