# chauminhnguyen/ocr architecture

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

- Interactive diagram: https://gitdiagram.com/chauminhnguyen/ocr
- Repository: https://github.com/chauminhnguyen/ocr

## Overview

##### Main Components
- Input & Preprocessing:
– Include modules/scripts such as pipeline.py, demo.py, crop_images.py and any image processing utilities (e.g., imgproc.py, file_utils.py).
- Text Detection Module:
– Represent the “CRAFT” component which includes its own code (e.g., craft.py, craft_utils.py) for text detection.
– Note that the CRAFT directory also contains a “basenet” submodule (with vgg16_bn.py) and model-related files.
- Text Recognition Modules:
– Illustrate “MORAN_v2” as one of the recognition engines (with its models and tools directories, and scripts like demo.py, inference.py, and main.py).
– Include “deep-text-recognition-benchmark” (which implements StarNet) with its own set of modules (e.g., modules for feature_extraction, prediction, sequence_modeling, transformation) and accompanying scripts.
– Optionally note external/alternative modules (VietOCR and EasyOCR) that are mentioned in the README, even if they are not fully included in the file structure, showing that the project supports multiple recognizers.
- Models & Pre-trained Assets:
- Utilities & Supporting Scripts:
– Capture utility functions (file_utils.py, demo files, and others) and any testing scripts (test.py in several directories).

##### Interactions & Data Flow
- The Input passes through preprocessing routines.
- Point out that the outputs from these recognizers are then combined or used to form the final OCR results.

##### Architectural Patterns and Design Principles
- Modularity: Separate directories for detection and recognition illustrate a modular design.
- Extensibility: The architecture supports multiple recognition components using a plug-and-play method.
- Data Flow: Emphasize a clear sequential pipeline—from input and detection to recognition.
- External Dependencies: Clearly mark where external pre-trained models are integrated.
- Utility & orchestration scripts (e.g., pipeline.py, demo.py) indicate the overall control flow.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Input & Preprocessing"
        PP1["pipeline.py"]:::preprocess
        PP2["demo.py"]:::preprocess
        PP3["crop_images.py"]:::preprocess
        PP4["imgproc.py"]:::preprocess
        PP5["file_utils.py"]:::preprocess
    end

    I_OUT["Preprocessed Images"]:::intermediate
    PP5 --> I_OUT

    subgraph "Text Detection (CRAFT)"
        C1["craft.py"]:::detection
        C2["craft_utils.py"]:::detection
        C3["vgg16_bn.py"]:::detection
        C4["Pre-trained Models"]:::storage
    end

    I_OUT -->|"preprocessed"| C1
    C4 -->|"detection models"| I_DET[Detected Text Regions]:::intermediate

    subgraph "Text Recognition Modules"
        subgraph "MORAN_v2"
            M1["main.py"]:::recognition
            M2["inference.py"]:::recognition
            M3["demo.py"]:::recognition
            M4["Models"]:::storage
            M5["Tools"]:::tools
        end
        subgraph "StarNet (deep-text-recognition-benchmark)"
            D1["model.py"]:::recognition
            D2["Modules"]:::tools
            D3["Pre-trained Models"]:::storage
        end
        E1["External OCR Modules (VietOCR, EasyOCR)"]:::external
    end

    I_DET -->|"region_crops"| M1
    I_DET -->|"region_crops"| D1
    I_DET -->|"region_crops"| E1

    FO["Final OCR Output"]:::final
    M3 -->|"recognized_text"| FO
    D2 -->|"recognized_text"| FO
    E1 -->|"recognized_text"| FO

    subgraph "Utilities & Supporting Scripts"
        U1["CRAFT/test.py"]:::utility
        U2["MORAN_v2/test.py"]:::utility
        U3["deep-text-recognition-benchmark/test.py"]:::utility
        U4["utils.py (deep)"]:::utility
        U5["utils.py (MORAN)"]:::utility
    end

    FO --- U1
    FO --- U2
    FO --- U3
    FO --- U4
    FO --- U5

    click PP1 "https://github.com/chauminhnguyen/ocr/blob/main/pipeline.py"
    click PP2 "https://github.com/chauminhnguyen/ocr/blob/main/demo.py"
    click PP3 "https://github.com/chauminhnguyen/ocr/blob/main/crop_images.py"
    click PP4 "https://github.com/chauminhnguyen/ocr/blob/main/CRAFT/imgproc.py"
    click PP5 "https://github.com/chauminhnguyen/ocr/blob/main/CRAFT/file_utils.py"
    click C1 "https://github.com/chauminhnguyen/ocr/blob/main/CRAFT/craft.py"
    click C2 "https://github.com/chauminhnguyen/ocr/blob/main/CRAFT/craft_utils.py"
    click C3 "https://github.com/chauminhnguyen/ocr/blob/main/CRAFT/basenet/vgg16_bn.py"
    click C4 "https://github.com/chauminhnguyen/ocr/tree/main/CRAFT/model"
    click M1 "https://github.com/chauminhnguyen/ocr/blob/main/MORAN_v2/main.py"
    click M2 "https://github.com/chauminhnguyen/ocr/blob/main/MORAN_v2/inference.py"
    click M3 "https://github.com/chauminhnguyen/ocr/blob/main/MORAN_v2/demo.py"
    click M4 "https://github.com/chauminhnguyen/ocr/tree/main/MORAN_v2/models"
    click M5 "https://github.com/chauminhnguyen/ocr/tree/main/MORAN_v2/tools"
    click D1 "https://github.com/chauminhnguyen/ocr/blob/main/deep-text-recognition-benchmark/model.py"
    click D2 "https://github.com/chauminhnguyen/ocr/tree/main/deep-text-recognition-benchmark/modules"
    click D3 "https://github.com/chauminhnguyen/ocr/tree/main/deep-text-recognition-benchmark/models"
    click U1 "https://github.com/chauminhnguyen/ocr/blob/main/CRAFT/test.py"
    click U2 "https://github.com/chauminhnguyen/ocr/blob/main/MORAN_v2/test.py"
    click U3 "https://github.com/chauminhnguyen/ocr/blob/main/deep-text-recognition-benchmark/test.py"
    click U4 "https://github.com/chauminhnguyen/ocr/blob/main/deep-text-recognition-benchmark/utils.py"
    click U5 "https://github.com/chauminhnguyen/ocr/blob/main/MORAN_v2/tools/utils.py"

    classDef preprocess fill:#ccffcc,stroke:#006600,stroke-width:2px;
    classDef detection fill:#ffcccc,stroke:#cc0000,stroke-width:2px;
    classDef recognition fill:#e6ccff,stroke:#9933ff,stroke-width:2px;
    classDef external fill:#ffffcc,stroke:#cccc00,stroke-width:2px;
    classDef storage fill:#ccffff,stroke:#00cccc,stroke-width:2px;
    classDef tools fill:#f0e68c,stroke:#b8860b,stroke-width:2px;
    classDef utility fill:#d3d3d3,stroke:#808080,stroke-width:2px;
    classDef intermediate fill:#ffe6cc,stroke:#ff6600,stroke-width:2px;
    classDef final fill:#cceeff,stroke:#0066cc,stroke-width:2px;
```
