# buiquangmanhhp1999/extract-information-from-identity-card architecture

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

- Interactive diagram: https://gitdiagram.com/buiquangmanhhp1999/extract-information-from-identity-card
- Repository: https://github.com/buiquangmanhhp1999/extract-information-from-identity-card

## Overview

##### Project Type and Purpose
- This is a backend service (no user-facing GUI) that exposes an HTTP API for extracting structured data from scanned identity-card images.
- It chains three machine-learning stages—corner detection, text detection, and OCR via a custom VietOCR model—and then outputs JSON.

##### Top-Level Components
A. API Layer (main.py + serve/serve_model.py or src/serve_model.py)
- Listens for HTTP POSTs with image payloads
- Parses requests, forwards images to the pipeline, returns JSON
B. Configuration Manager (src/config.py)
- Centralizes paths to model files and pipeline parameters
C. Corner Detection Module (src/detector/config_corner_detection + detector.py)
- Uses a TFLite model to find card corners
- Utils (image_utils.py) perform perspective warp/alignment
D. Text Detection Module (src/detector/config_text_detection + detector.py)
- Uses a separate TFLite model to locate text regions
E. Text Recognition Module (src/vietocr)
- Configuration files (base.yml, vgg-transformer.yml) select model architecture
- Model definitions (cnn.py, transformer.py, vgg.py) load weights
- Predictor (tool/predictor.py) runs OCR on each cropped region
- Optional translation (tool/translate.py) if needed
F. Merge & Post-processing (src/merged_model.py)
- Combines OCR results into structured fields (id, name, birth, address, home)
G. External Storage (samples folder for example images, downloaded weights in vietocr/config_text_recognition)

##### Data Flow & Interactions
1. Client HTTP POST → API Layer
2. API loads config → forwards image to Corner Detection
3. Corner Detection → aligned image
6. OCR outputs strings → Merge & Post-processing
7. Final JSON → API Layer → HTTP Response

##### Architectural Patterns & Principles
- Layered architecture: clear separation between API, ML detection, and OCR
- Modularity: each model lives in its own directory with configs, weights, and utilities
- Single responsibility: detector modules only detect, OCR module only recognizes, merge module only assembles results

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Global Entities
    Client["Client"]:::external

    %% API Layer
    API["HTTP API\nmain.py\nserve/serve_model.py\nsrc/serve_model.py"]:::api

    %% Configuration Manager
    Config["Config Manager\nsrc/config.py"]:::config

    %% Corner Detection Module
    subgraph "Corner Detection Module"
        CDModelCfg["config_corner_detection/\nmodel.tflite\npipeline.config\nlabel_map.pbtxt"]:::config
        CDdet["detector.py"]:::ml
        CDutil["image_utils.py"]:::utils
    end

    %% Text Detection Module
    subgraph "Text Detection Module"
        TDModelCfg["config_text_detection/\nmodel.tflite\npipeline.config\nlabel_map.pbtxt"]:::config
        TDdet["detector.py"]:::ml
    end

    %% VietOCR Engine
    subgraph "VietOCR Engine"
        VOCfg["config_text_recognition/\nbase.yml\nvgg-transformer.yml"]:::config
        VOmodel["cnn.py\ntransformer.py\nvgg.py\nvocab.py"]:::ml
        VOSrc["text_recognition.py\ntransformerocr.py"]:::ml
        VOTool["tool/predictor.py\ntool/translate.py"]:::utils
    end

    %% Merge & Post-processing
    Merger["Merge & Post-processing\nsrc/merged_model.py"]:::ml

    %% External Storage
    Samples["samples/"]:::external
    Weights["Downloaded Weights"]:::external

    %% Data Flow
    Client -->|"HTTP POST\nImage"| API
    API -->|"Load Config"| Config
    API -->|"Image"| CDdet
    Config --> CDdet
    CDdet -->|"Aligned Image"| TDdet
    Config --> TDdet
    TDdet -->|"BBoxes"| VOTool
    VOCfg --> VOmodel
    VOmodel --> VOSrc
    VOSrc --> VOTool
    VOTool -->|"Strings"| Merger
    Merger -->|"JSON"| API
    Samples -.->|"Example Images"| API
    Weights -.->|"Model Weights"| VOCfg

    %% Click Events
    click API "https://github.com/buiquangmanhhp1999/extract-information-from-identity-card/blob/master/main.py"
    click API "https://github.com/buiquangmanhhp1999/extract-information-from-identity-card/blob/master/serve/serve_model.py"
    click API "https://github.com/buiquangmanhhp1999/extract-information-from-identity-card/blob/master/src/serve_model.py"
    click Config "https://github.com/buiquangmanhhp1999/extract-information-from-identity-card/blob/master/src/config.py"
    click CDModelCfg "https://github.com/buiquangmanhhp1999/extract-information-from-identity-card/blob/master/src/detector/config_corner_detection/model.tflite"
    click CDModelCfg "https://github.com/buiquangmanhhp1999/extract-information-from-identity-card/blob/master/src/detector/config_corner_detection/pipeline.config"
    click CDModelCfg "https://github.com/buiquangmanhhp1999/extract-information-from-identity-card/blob/master/src/detector/config_corner_detection/label_map.pbtxt"
    click CDdet "https://github.com/buiquangmanhhp1999/extract-information-from-identity-card/blob/master/src/detector/detector.py"
    click CDutil "https://github.com/buiquangmanhhp1999/extract-information-from-identity-card/blob/master/src/detector/utils/image_utils.py"
    click TDModelCfg "https://github.com/buiquangmanhhp1999/extract-information-from-identity-card/blob/master/src/detector/config_text_detection/model.tflite"
    click TDModelCfg "https://github.com/buiquangmanhhp1999/extract-information-from-identity-card/blob/master/src/detector/config_text_detection/pipeline.config"
    click TDModelCfg "https://github.com/buiquangmanhhp1999/extract-information-from-identity-card/blob/master/src/detector/config_text_detection/label_map.pbtxt"
    click TDdet "https://github.com/buiquangmanhhp1999/extract-information-from-identity-card/blob/master/src/detector/detector.py"
    click VOCfg "https://github.com/buiquangmanhhp1999/extract-information-from-identity-card/blob/master/src/vietocr/config_text_recognition/base.yml"
    click VOCfg "https://github.com/buiquangmanhhp1999/extract-information-from-identity-card/blob/master/src/vietocr/config_text_recognition/vgg-transformer.yml"
    click VOmodel "https://github.com/buiquangmanhhp1999/extract-information-from-identity-card/blob/master/src/vietocr/model/cnn.py"
    click VOmodel "https://github.com/buiquangmanhhp1999/extract-information-from-identity-card/blob/master/src/vietocr/model/transformer.py"
    click VOmodel "https://github.com/buiquangmanhhp1999/extract-information-from-identity-card/blob/master/src/vietocr/model/vgg.py"
    click VOmodel "https://github.com/buiquangmanhhp1999/extract-information-from-identity-card/blob/master/src/vietocr/model/vocab.py"
    click VOSrc "https://github.com/buiquangmanhhp1999/extract-information-from-identity-card/blob/master/src/vietocr/text_recognition.py"
    click VOSrc "https://github.com/buiquangmanhhp1999/extract-information-from-identity-card/blob/master/src/vietocr/transformerocr.py"
    click VOTool "https://github.com/buiquangmanhhp1999/extract-information-from-identity-card/blob/master/src/vietocr/tool/predictor.py"
    click VOTool "https://github.com/buiquangmanhhp1999/extract-information-from-identity-card/blob/master/src/vietocr/tool/translate.py"
    click Merger "https://github.com/buiquangmanhhp1999/extract-information-from-identity-card/blob/master/src/merged_model.py"
    click Samples "https://github.com/buiquangmanhhp1999/extract-information-from-identity-card/tree/master/samples/"

    %% Styles
    classDef api fill:#D0E6FF,stroke:#0288D1,stroke-width:2px
    classDef ml fill:#DFFFD0,stroke:#558B2F,stroke-width:2px
    classDef config fill:#FFF9C4,stroke:#FBC02D,stroke-width:2px
    classDef utils fill:#EEEEEE,stroke:#616161,stroke-width:1px
    classDef external fill:#F0F0F0,stroke:#BDBDBD,stroke-width:1px
```
