# jayllfpt/table2html architecture

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

- Interactive diagram: https://gitdiagram.com/jayllfpt/table2html
- Repository: https://github.com/jayllfpt/table2html

## Overview

##### Main Components
- Input/Image Interface: This is where images (in OpenCV format, i.e., numpy.ndarray) are received as input.
- API/Orchestration Layer: The Table2HTML class (found in the package’s __init__.py and main.py) orchestrates the entire workflow. It initializes using specific configuration dictionaries (table_config, row_config, column_config) which point to model files.
- Model Files Repository: Under the “table2html/models” folder, the pre-trained models (e.g., table_detection.pt, row_detection.pt, column_detection.pt) reside. These are central to detecting the various elements within the image.
- Structure Detection and OCR Components: Also in “table2html/source”, the modules structure_detector.py, ocr_engine.py, and table_processor.py work together to identify individual cells, determine rows/columns, extract text, and build the HTML representation.
- Testing and CI/CD: The “tests” directory (with test_table2html.py) and the .github/workflows directory indicate automated testing and possibly continuous integration workflows.

##### Relationships and Interactions
- The flow begins when an image is provided by the user. The API layer (Table2HTML) first calls the Table Detection Component to locate table boundaries.
- Once a table is detected, the image may be cropped (with a configurable padding) and passed to the Structure Detection Component. Here, the row and column detectors parse the table layout.
- The OCR Engine (if applicable) extracts text from each detected cell.
- Finally, the processed data (coordinates for table and cells, along with textual content) is combined and transformed into an HTML representation.
- The pre-trained model files serve as static resources and are loaded by their respective modules at runtime to perform detection.

##### Architectural Patterns & Design Principles
- Modularity: The project is organized into distinct submodules (e.g., detection, structure parsing, visualization), making it easier to maintain or extend.
- Separation of Concerns: There is a clear separation between image processing, detection (via deep learning models), post-processing (converting detections to HTML), and testing.
- Reusability: Components like the OCR engine and visualization functions can be reused or swapped as needed.
- Configuration-Driven: The use of configuration dictionaries (for thresholds and model paths) facilitates flexibility in tuning performance and model behavior.

##### Relevant Technologies & Libraries
- Python (as the primary language).
- OpenCV for image manipulation and I/O.
- Deep Learning frameworks (implicitly PyTorch since “.pt” files are used, though not explicitly mentioned, it is typical for such models).
- Optionally, any OCR technology integrated via the ocr_engine module.
- GitHub Actions (CI/CD workflows under .github/workflows).

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Input Layer"
        I["Input Image (numpy.ndarray)"]:::input
    end

    subgraph "API/Orchestration Layer"
        B["Table2HTML API"]:::api
    end

    subgraph "Detection Pipeline"
        TD["Table Detector (table_detector.py)"]:::detection
        SD1["Structure Detector (structure_detector.py)"]:::detection
        SD2["Table Processor (table_processor.py)"]:::detection
        OCR["OCR Engine (ocr_engine.py)"]:::ocr
        HTML["HTML Generator"]:::output
    end

    subgraph "Utilities"
        VIS["Visualization Utility (utils.py)"]:::utility
    end

    subgraph "Resources"
        MODELS["Model Files Repository"]:::resource
    end

    subgraph "Testing & CI/CD"
        TEST["Test Suite (test_table2html.py)"]:::testing
        CI["CI Workflow (.github/workflows/workflow.yml)"]:::testing
    end

    I -->|"providesImage"| B
    B -->|"orchestrates"| TD
    TD -->|"detectsTable"| SD1
    SD1 -->|"parsesStructure"| SD2
    SD2 -->|"extractsText"| OCR
    OCR -->|"generatesHTML"| HTML
    SD1 ---|"visualizes"| VIS

    B -.->|"loadsModels"| MODELS
    TD -.->|"usesModel"| MODELS
    SD1 -.->|"usesModel"| MODELS
    SD2 -.->|"usesModel"| MODELS
    OCR -.->|"usesModel"| MODELS

    B -->|"triggersTests"| TEST
    B -->|"triggerCI"| CI

    click I "https://github.com/jayllfpt/table2html/blob/main/table2html/main.py"
    click B "https://github.com/jayllfpt/table2html/blob/main/table2html/__init__.py"
    click TD "https://github.com/jayllfpt/table2html/blob/main/table2html/source/table_detector.py"
    click SD1 "https://github.com/jayllfpt/table2html/blob/main/table2html/source/structure_detector.py"
    click SD2 "https://github.com/jayllfpt/table2html/blob/main/table2html/source/table_processor.py"
    click OCR "https://github.com/jayllfpt/table2html/blob/main/table2html/source/ocr_engine.py"
    click VIS "https://github.com/jayllfpt/table2html/blob/main/table2html/source/utils.py"
    click MODELS "https://github.com/jayllfpt/table2html/tree/main/table2html/models/"
    click TEST "https://github.com/jayllfpt/table2html/blob/main/tests/test_table2html.py"
    click CI "https://github.com/jayllfpt/table2html/blob/main/.github/workflows/workflow.yml"

    classDef input fill:#aaf,stroke:#000,stroke-width:2px;
    classDef api fill:#afa,stroke:#000,stroke-width:2px;
    classDef detection fill:#ff9,stroke:#000,stroke-width:2px;
    classDef ocr fill:#f9f,stroke:#000,stroke-width:2px;
    classDef output fill:#ccf,stroke:#000,stroke-width:2px;
    classDef utility fill:#fcf,stroke:#000,stroke-width:2px;
    classDef resource fill:#faa,stroke:#000,stroke-width:2px;
    classDef testing fill:#cff,stroke:#000,stroke-width:2px;
```
