# dungcan2k5/imath_ocrix architecture

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

- Interactive diagram: https://gitdiagram.com/dungcan2k5/imath_ocrix
- Repository: https://github.com/dungcan2k5/imath_ocrix

## Overview

##### Project Type and Purpose
- This is a standalone Python desktop application (“OCR_Project”) whose purpose is to capture regions of the screen (snip), run OCR on plain text or on mathematical formulas (including Vietnamese text support), and present the results to the user.

##### File-Structure Analysis
Top-level files and directories:
- app.py           – main entry point; GUI or CLI orchestration
- snip_screen.py   – screen‐capture service
- img_OCR.py & img_OCR_func.py
- math_OCR.py & math_OCR_func.py
- .py        – shared constants, config for OCR engines
- Imath Ocrix.py   – specialized math OCR orchestrator (possibly using a third‐party engine)
- Img_temp/        – temporary storage for captured image snippets
- __pycache__/     – bytecode cache

##### README Insights
- Description: “Quét văn bản và công thức toán học trong hình ảnh có hỗ trợ tiếng Việt”
- Confirms dual OCR pipelines (text + math) and Vietnamese language requirement.

##### Main Components
– Presents buttons to “capture screen,” “run text OCR,” “run math OCR,” and displays results.
b. Service (snip_screen.py)
– Grabs a user‐selected region, writes to Img_temp/, returns image path.
c. Preprocessing & Routing (define.py + app.py logic)
– Applies any image preprocessing (e.g., binarization), decides which OCR pipeline to invoke.
d. Text OCR Pipeline (img_OCR.py + img_OCR_func.py)
– Uses Tesseract or similar via a wrapper to extract Vietnamese text.
e. Math OCR Pipeline (Imath Ocrix.py + math_OCR.py + math_OCR_func.py)
– Calls a math‐specialized engine (e.g., Mathpix API or custom model) to parse formulas; outputs LaTeX or Unicode.
f. Temp-File Store (Img_temp/)
– Holds intermediate PNG/JPEG files; cleaned up after processing.
g. External Dependencies
– Tesseract OCR engine, any math‐OCR SDK or web API, Python imaging libraries (Pillow/OpenCV).

##### Interactions & Data Flow
1. User clicks “Snip” → UI triggers snip_screen.py → capture → save to Img_temp/ → return image object.
2. UI (app.py) applies define.py settings → chooses pipeline.
3. If Text OCR: pass image to img_OCR.py → calls img_OCR_func.py → invokes Tesseract → raw text.
4. If Math OCR: pass image to math_OCR.py → calls math_OCR_func.py + Imath Ocrix.py → math engine → LaTeX/formula output.
6. Optionally delete temp image or archive in Img_temp/.

##### Architectural Patterns & Principles
- Single Responsibility: each module handles one concern (capture vs text OCR vs math OCR vs UI)
- Modular Pipelines: separate “text” and “math” flows, share only common preprocessing
- Configuration Abstraction: define.py centralizes engine parameters (language, DPI, thresholds)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% UI Layer
    subgraph "UI Layer"
        direction TB
        UI["UI (app.py)"]:::ui
        ButtonAssets["button/"]:::ui
        IconAssets["icon/"]:::ui
    end

    %% Capture Service
    ScreenSnipper["Screen Snipper (snip_screen.py)"]:::core
    %% Temp-File Store
    TempStore["Img_temp/"]:::store
    %% Preprocessing & Routing
    Router["Preprocessing & Routing (define.py)"]:::core

    %% Text OCR Pipeline
    subgraph "Text OCR Pipeline"
        direction TB
        TextOCRModule["img_OCR.py"]:::core
        TextOCRFunc["img_OCR_func.py"]:::core
    end

    %% Math OCR Pipeline
    subgraph "Math OCR Pipeline"
        direction TB
        MathOCRModule["math_OCR.py"]:::core
        MathOCRFunc["math_OCR_func.py"]:::core
        ImathOrpix["Imath Ocrix.py"]:::core
    end

    %% External Dependencies
    Tesseract["Tesseract OCR Engine"]:::external
    MathAPI["Math-OCR API"]:::external

    %% Data Flows
    UI -->|"click Snip"| ScreenSnipper
    ScreenSnipper -->|"image.png"| TempStore
    ScreenSnipper -->|"image.png"| UI
    UI -->|"image.png"| Router
    Router -->|"text path"| TextOCRModule
    Router -->|"math path"| MathOCRModule

    %% Text OCR Flow
    TextOCRModule -->|"call"| TextOCRFunc
    TextOCRFunc -->|"OCR request"| Tesseract
    Tesseract -->|"raw_text"| TextOCRFunc
    TextOCRFunc -->|"raw_text"| TextOCRModule
    TextOCRModule -->|"raw_text"| UI

    %% Math OCR Flow
    MathOCRModule -->|"call"| MathOCRFunc
    MathOCRFunc -->|"orchestrate"| ImathOrpix
    ImathOrpix -->|"LaTeX_output"| MathOCRFunc
    MathOCRFunc -->|"LaTeX_output"| MathOCRModule
    MathOCRModule -->|"LaTeX_output"| UI

    %% Cleanup
    UI -->|"cleanup"| TempStore

    %% Click Events
    click UI "https://github.com/dungcan2k5/imath_ocrix/blob/master/app.py"
    click ButtonAssets "https://github.com/dungcan2k5/imath_ocrix/tree/master/button/"
    click IconAssets "https://github.com/dungcan2k5/imath_ocrix/tree/master/icon/"
    click ScreenSnipper "https://github.com/dungcan2k5/imath_ocrix/blob/master/snip_screen.py"
    click TempStore "https://github.com/dungcan2k5/imath_ocrix/tree/master/Img_temp/"
    click Router "https://github.com/dungcan2k5/imath_ocrix/blob/master/define.py"
    click TextOCRModule "https://github.com/dungcan2k5/imath_ocrix/blob/master/img_OCR.py"
    click TextOCRFunc "https://github.com/dungcan2k5/imath_ocrix/blob/master/img_OCR_func.py"
    click MathOCRModule "https://github.com/dungcan2k5/imath_ocrix/blob/master/math_OCR.py"
    click MathOCRFunc "https://github.com/dungcan2k5/imath_ocrix/blob/master/math_OCR_func.py"
    click ImathOrpix "https://github.com/dungcan2k5/imath_ocrix/blob/master/Imath Ocrix.py"

    %% Styles
    classDef ui fill:#D0E8FF,stroke:#0066CC,color:#000;
    classDef core fill:#DFFFD0,stroke:#33AA00,color:#000;
    classDef store fill:#F0F0F0,stroke:#888888,color:#000;
    classDef external fill:#FFEFD0,stroke:#CC6600,color:#000
```
