# breezedeus/pix2text architecture

> Architecture diagram of the GitHub repository breezedeus/pix2text, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/breezedeus/pix2text
- Repository: https://github.com/breezedeus/pix2text

## Overview

##### Project Type and Purpose
- Pix2Text is a Python-based open-source toolkit that converts images (and PDFs) into structured Markdown.
- It provides a CLI, HTTP service, and Python API. Under the hood, it stitches together layout analysis, table recognition, OCR engines, and mathematical formula detection/recognition (MFD/MFR).
- Its goal is to be a free alternative to Mathpix, supporting 80+ languages, multiple model backends (Open-source & closed VLMs), and conversion pipelines.

##### File Structure
- Top-level modules:
- pix2text/ — core library
- scripts/ — CLI wrappers, demo scripts, screenshot daemon
- tests/ — unit tests for each component
- setup.py, requirements.txt — packaging & dependencies
- Key subdirectories in pix2text/:
- doc_xl_layout/ — layout analysis models, detectors, parsers
- cli.py, app.py, serve.py — entry points for CLI, Python API, and HTTP service
- ocr_engine.py — orchestration of CnOCR, EasyOCR
- table_ocr.py, table_postprocess.py — table detection & rendering
- latex_ocr.py, text_formula_ocr.py, formula_detector.py — formula MFD & MFR
- vlm_api.py, vlm_table_ocr.py, vlm_text_formula_ocr.py — closed-source VLM interfaces
- render.py — assembling final Markdown output
- utils.py, consts.py — shared utilities and constants

##### Extract Insights from the README
- Pipeline overview (see docs/figs/arch-flow.jpg):
1. Input (image/PDF)
2. Layout analysis → subfields (text, tables, formulas, figures)
3. Dispatch to specialized detectors/engines (OCR, table rec, MFD/MFR)
4. Post-processing & formatting
5. Output Markdown
- Supports multiple backends per subtask (e.g., DocLayout-YOLO, CnOCR/EasyOCR, LiteLLM VLMs).

##### Main Components
a. Frontend Interfaces
- CLI (pix2text cli.py)
- Python API (pix2text.app.py)
- HTTP Server (pix2text.serve.py)
- Jupyter Notebooks (pix2text_v*.ipynb)
b. Ingestion Layer
- Image / PDF Loader
- Preprocessor (image resizing, PDF→images)
c. Layout Analysis
- doc_xl_layout Parser + Detectors (DocLayout-YOLO, ctdet_subfield, shapelyNMS)
d. Subfield Processing
- Text Blocks → OCR Engine (CnOCR / EasyOCR)
- Table Blocks → Table OCR Module + Postprocessor
- Formula Blocks → Formula Detector → LaTeX OCR (MFR)
- VLM Blocks (if configured) → vlm_api
e. Assembly & Rendering
- render.py merges text, tables, formulas into final Markdown AST
- post-processing (cleanup, deduplication)
f. External Dependencies & Services
- Hugging Face models
- LiteLLM VLMs (closed-source)
- External Python packages (shapely, CnOCR, EasyOCR, torchvision)
g. Output
- Markdown file / STDOUT
- PDF conversion endpoint (via HTTP)
- CLI progress logs / JSON

##### Relationships & Data Flow
- User → (CLI / API / HTTP) → Ingestion → Layout Parser
- Layout Parser → Subfield pipelines in parallel
- Each sub-pipeline → its model backend → returns recognized content
- Renderer collects all recognized content → builds Markdown → returns to User
- Optionally, the service logs usage and errors

##### Architectural Patterns & Principles
- Modular pipeline: each subfield type has its own detector and OCR/recognizer
- Factory pattern: detector_factory chooses layout detector
- Strategy pattern: OCR engine selects CnOCR vs. EasyOCR based on language
- Adapter pattern: vlm_api wraps closed-source VLM models into the same interface
- Single Responsibility: render.py only handles Markdown assembly

##### Detail Level & Coverage
- Drill down into doc_xl_layout subdirectories only if necessary for clarity
- Tests and scripts off-to-the-side to indicate coverage & automation but without cluttering the main flow
- Every critical step in docs/figs/arch-flow.jpg is represented

- How Pix2Text ingests and preprocesses input
- The modular decomposition into layout, OCR, table, and formula pipelines
- The orchestration of models and external services
- The rendering of final Markdown outputs
- The user interfaces and integration points for CLI, API, and HTTP

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Frontend Layer
    subgraph "Frontend Interfaces"
        direction TB
        CLI(["CLI"]):::frontend
        API(["Python API"]):::frontend
        HTTP(["HTTP Server"]):::frontend
        NB1(["Jupyter Notebook v1_0"]):::frontend
        NB2(["Jupyter Notebook v1_1"]):::frontend
    end

    User(["User"]):::frontend --> CLI
    User --> API
    User --> HTTP
    User --> NB1
    User --> NB2

    %% Ingestion Layer
    subgraph "Ingestion Layer"
        direction TB
        Loader(["Image/PDF Loader"]):::ingestion
        Preprocessor(["Preprocessor"]):::ingestion
        ImgUtils(["Image Utils"]):::ingestion
    end

    CLI --> Loader
    API --> Loader
    HTTP --> Loader
    Loader --> Preprocessor
    Preprocessor --> ImgUtils

    %% Layout Analysis
    subgraph "Layout Analysis"
        direction TB
        Parser(["Layout Parser"]):::layout
        Detectors(["Detectors\n(DocLayout-YOLO, ctdet_subfield)"]):::layout
    end

    ImgUtils --> Parser
    Preprocessor --> Parser
    Parser --> Detectors

    %% Subfield Processing
    subgraph "Subfield Processing"
        direction TB
        OCR(["OCR Engine"]):::ocr
        Table(["Table OCR"]):::ocr
        subgraph "Formula Processing"
            direction TB
            FormulaDetector(["Formula Detector"]):::ocr
            TextFormulaOCR(["Text Formula OCR"]):::ocr
            LatexOCR(["LaTeX OCR"]):::ocr
        end
        subgraph "VLM Integration"
            direction TB
            VLMAPI(["VLM API"]):::ocr
            VLMTable(["VLM Table OCR"]):::ocr
        end
    end

    Detectors --> OCR
    Detectors --> Table
    Detectors --> FormulaDetector
    FormulaDetector --> TextFormulaOCR
    TextFormulaOCR --> LatexOCR
    Detectors --> VLMAPI
    Detectors --> VLMTable

    OCR --> RecognizedText(["Text Blocks"]):::output
    Table --> RecognizedTable(["Table Blocks"]):::output
    LatexOCR --> RecognizedFormula(["Formula Blocks"]):::output
    VLMAPI --> RecognizedVLM(["VLM Blocks"]):::output
    VLMTable --> RecognizedVLM

    %% Assembly & Rendering
    subgraph "Assembly & Rendering"
        direction TB
        Render(["Render Markdown"]):::rendering
    end

    RecognizedText --> Render
    RecognizedTable --> Render
    RecognizedFormula --> Render
    RecognizedVLM --> Render

    Render --> OutputMD(["Markdown Output"]):::output
    Render --> Logs(["CLI Logs / JSON"]):::output
    HTTP --> PDFEP(["PDF Endpoint"]):::output

    %% External Services
    subgraph "External Dependencies"
        direction TB
        HF(["Hugging Face Models"]):::external
        LiteLLM(["LiteLLM VLMs"]):::external
        ExtPKG(["External Packages\n(CnOCR, EasyOCR, torchvision)"]):::external
    end

    Detectors -.-> HF
    OCR -.-> ExtPKG
    Table -.-> ExtPKG
    LatexOCR -.-> HF
    VLMAPI -.-> LiteLLM
    VLMTable -.-> LiteLLM

    %% Click Events
    click CLI "https://github.com/breezedeus/pix2text/blob/main/pix2text/cli.py"
    click API "https://github.com/breezedeus/pix2text/blob/main/pix2text/app.py"
    click HTTP "https://github.com/breezedeus/pix2text/blob/main/pix2text/serve.py"
    click NB1 "https://github.com/breezedeus/pix2text/blob/main/pix2text_v1_0.ipynb"
    click NB2 "https://github.com/breezedeus/pix2text/blob/main/pix2text_v1_1.ipynb"
    click Loader "https://github.com/breezedeus/pix2text/blob/main/pix2text/pix_to_text.py"
    click Preprocessor "https://github.com/breezedeus/pix2text/blob/main/pix2text/utils.py"
    click ImgUtils "https://github.com/breezedeus/pix2text/blob/main/pix2text/doc_xl_layout/utils/image.py"
    click Parser "https://github.com/breezedeus/pix2text/blob/main/pix2text/doc_xl_layout/doc_xl_layout_parser.py"
    click Detectors "https://github.com/breezedeus/pix2text/tree/main/pix2text/doc_xl_layout/detectors/"
    click OCR "https://github.com/breezedeus/pix2text/blob/main/pix2text/ocr_engine.py"
    click Table "https://github.com/breezedeus/pix2text/blob/main/pix2text/table_ocr.py"
    click FormulaDetector "https://github.com/breezedeus/pix2text/blob/main/pix2text/formula_detector.py"
    click TextFormulaOCR "https://github.com/breezedeus/pix2text/blob/main/pix2text/text_formula_ocr.py"
    click LatexOCR "https://github.com/breezedeus/pix2text/blob/main/pix2text/latex_ocr.py"
    click VLMAPI "https://github.com/breezedeus/pix2text/blob/main/pix2text/vlm_api.py"
    click VLMTable "https://github.com/breezedeus/pix2text/blob/main/pix2text/vlm_table_ocr.py"
    click Render "https://github.com/breezedeus/pix2text/blob/main/pix2text/render.py"

    %% Styles
    classDef frontend fill:#E3F2FD,stroke:#42A5F5,color:#0D47A1
    classDef ingestion fill:#E0F7FA,stroke:#00ACC1,color:#006064
    classDef layout fill:#FFE0B2,stroke:#FB8C00,color:#E65100
    classDef ocr fill:#C8E6C9,stroke:#43A047,color:#1B5E20
    classDef rendering fill:#E1BEE7,stroke:#8E24AA,color:#4A148C
    classDef external fill:#F5F5F5,stroke:#9E9E9E,color:#424242
    classDef output fill:#F3E5F5,stroke:#AB47BC,color:#6A1B9A
```
