# gaia-dental-studio/dental_smile_design architecture

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

- Interactive diagram: https://gitdiagram.com/gaia-dental-studio/dental_smile_design
- Repository: https://github.com/gaia-dental-studio/dental_smile_design

## Overview

The Dental Smile Design project is a specialized computer vision application that implements a multi-stage pipeline for tooth alignment in facial photographs. Its purpose is to process an input facial image, detect and segment facial features (like the face, mouth, and teeth), and then perform a transformation (or “alignment”) based on 3D structure guidance. The project is structured in clearly delineated stages that mirror the various steps in the processing pipeline.

##### Main Components
- Input Module: Represented by the Data directory which contains input images (e.g., facial photographs).
- Processing Pipeline (Backend Core):
- Stage1 – Preprocessing and Feature Detection:
- DetectFace: Identifies facial landmarks using provided models (e.g., the 68-face landmark predictor).
- DetectMouth: Identifies and processes the mouth region.
- Tooth Segmentation Modules: SegmentTeeth and SegmentToothContour to extract and refine tooth contours.
- Stage2 – First Generation/Transformation:
- Contains Generator.py and Network.py that likely perform an initial transformation (using configurations provided in the config subdirectory and pretrained weights from ckpt).
- Stage3 – Refinement/Final Output Generation:
- Output Module: The Output directory stores the resulting aligned images and intermediate processing outputs.
- Model Restoration and Auxiliary Code:
- The Restore folder that may include code for recovering or reinitializing models.
- Helper files such as helper.py, settings.py, and configuration files such as Code/Config.yaml and those under Code/config.
- External Interfaces:
- API Interface: Provided by api.py (a Flask-based API) and app.py (Streamlit-based UI) to allow users to interact with the model through web interfaces.
- Main Execution scripts such as main.py and main_ForFolder.py, which serve as entry points for running the complete pipeline.

##### Relationships and Data Flow
- Input images (from Data) are fed into the main program (main.py) or the user interface (Streamlit or Flask API).
- The image then flows into Stage1 where feature detection (face, mouth, boundaries) is executed.
- The pre-processed data then moves through Stage2 and subsequently Stage3. Each stage refines the prediction:
- Stage2 processes initial contour to contour or mask-to-mask conversions.
- Stage3 further refines the results from Stage2, applying additional configuration options (e.g., different rendering styles, applying Fourier descriptors).
- The final processed images are saved into the Output directory (organized into prediction and processing subdirectories).

##### Architectural Patterns and Technologies
- Modular design, with each stage (detection, generation, refinement) handled in separate directories and scripts.
- Clearly defined checkpoints (ckpt files) for deep learning models across stages, emphasizing a dependency on pretrained weights and configuration-driven behavior.
- Use of multiple interfaces (command-line via main.py, Flask API via api.py, and Streamlit interface via app.py) demonstrating a layered architecture using backend processing with different presentation/UI layers.

##### Specific Elements to Be Included
- Clear, descriptive labels for each major component (e.g., “Data Input”, “Stage1: Feature Detection”, “Stage2: Initial Generation”, “Stage3: Refinement”, “API/Streamlit Interfaces”).
- Annotations or notes indicating where model checkpoints and configuration files are used.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Interfaces and Entry Points
    API["Flask API"]:::external
    UI["Streamlit UI"]:::external
    subgraph "Main Execution Scripts"
        MainScript["Main Script (main.py)"]:::auxiliary
        MainForFolder["Main For Folder (main_ForFolder.py)"]:::auxiliary
    end

    %% Input Module
    DataInput["Data Input"]:::input

    %% Stage1: Preprocessing and Feature Detection
    subgraph "Stage1: Feature Detection"
        DetectFace["Detect Face"]:::processing
        DetectMouth["Detect Mouth"]:::processing
        DetectContour["Segment Teeth (Contour)"]:::processing
        SegmentToothContour["Segment Tooth Contour"]:::processing
    end

    %% Stage2: Initial Generation/Transformation
    subgraph "Stage2: Initial Generation"
        Stage2Generator["Generator (Stage2)"]:::processing
        Stage2Network["Network (Stage2)"]:::processing
        Stage2Config["Config (Stage2)"]:::processing
        Stage2CKPT["Pretrained Weights (Stage2)"]:::processing
        Stage2Mask2Mask["Mask2Mask Transformation"]:::processing
    end

    %% Stage3: Refinement/Final Output Generation
    subgraph "Stage3: Refinement Generation"
        Stage3Generator["Generator (Stage3)"]:::processing
        Stage3Network["Network (Stage3)"]:::processing
        Stage3Config["Config (Stage3)"]:::processing
        Stage3CKPT["Pretrained Weights (Stage3)"]:::processing
        Stage3Mask2Teeth["Mask2Teeth Refinement"]:::processing
    end

    %% Output Module
    OutputModule["Output Module"]:::output

    %% Auxiliary Components
    Restore["Model Restore"]:::auxiliary
    HelperSettings["Helper & Settings"]:::auxiliary
    GlobalConfig["Global Config"]:::auxiliary

    %% Connections for External Interfaces
    API -->|"triggers"| MainScript
    UI  -->|"triggers"| MainScript
    MainForFolder -->|"triggers"| DataInput
    MainScript -->|"triggers"| DataInput

    %% Data flow through processing pipeline
    DataInput -->|"feeds"| DetectFace
    DataInput -->|"feeds"| DetectMouth
    DataInput -->|"feeds"| DetectContour
    DataInput -->|"feeds"| SegmentToothContour

    DetectFace -->|"outputs"| Stage2Generator
    DetectMouth  -->|"outputs"| Stage2Network
    DetectContour -->|"outputs"| Stage2Config
    SegmentToothContour -->|"outputs"| Stage2CKPT

    %% Combining Stage1 outputs to Stage2 processing
    DetectFace --- DetectMouth
    DetectContour --- SegmentToothContour

    %% Stage2 internal flow
    Stage2Generator --> Stage2Network
    Stage2Network --> Stage2Config
    Stage2Config --> Stage2CKPT
    Stage2CKPT --> Stage2Mask2Mask

    %% Stage2 to Stage3 flow
    Stage2Mask2Mask --> Stage3Generator

    %% Stage3 internal flow
    Stage3Generator --> Stage3Network
    Stage3Network --> Stage3Config
    Stage3Config --> Stage3CKPT
    Stage3CKPT --> Stage3Mask2Teeth

    %% Final output
    Stage3Mask2Teeth -->|"saves result"| OutputModule

    %% Auxiliary configuration dependencies
    GlobalConfig --- Stage2Config
    GlobalConfig --- Stage3Config
    HelperSettings --- Stage2Config
    HelperSettings --- Stage3Config

    %% Model restoration influence
    Restore --- Stage2Generator
    Restore --- Stage3Generator

    %% Click Events
    click DataInput "https://github.com/gaia-dental-studio/dental_smile_design/tree/main/Data"
    click DetectFace "https://github.com/gaia-dental-studio/dental_smile_design/blob/main/Code/Stage1/DetectFace/DetectFace.py"
    click DetectMouth "https://github.com/gaia-dental-studio/dental_smile_design/blob/main/Code/Stage1/DetectMouth/DetectMouth.py"
    click DetectContour "https://github.com/gaia-dental-studio/dental_smile_design/blob/main/Code/Stage1/SegmentTeeth/DetectContour.py"
    click SegmentToothContour "https://github.com/gaia-dental-studio/dental_smile_design/blob/main/Code/Stage1/SegmentToothContour/SegmentToothContour.py"
    click Stage2Generator "https://github.com/gaia-dental-studio/dental_smile_design/blob/main/Code/Stage2/Generator.py"
    click Stage2Network "https://github.com/gaia-dental-studio/dental_smile_design/blob/main/Code/Stage2/Network.py"
    click Stage2Config "https://github.com/gaia-dental-studio/dental_smile_design/tree/main/Code/Stage2/config"
    click Stage2CKPT "https://github.com/gaia-dental-studio/dental_smile_design/tree/main/Code/Stage2/ckpt"
    click Stage2Mask2Mask "https://github.com/gaia-dental-studio/dental_smile_design/blob/main/Code/Stage2_Mask2Mask.py"
    click Stage3Generator "https://github.com/gaia-dental-studio/dental_smile_design/blob/main/Code/Stage3/Generator.py"
    click Stage3Network "https://github.com/gaia-dental-studio/dental_smile_design/blob/main/Code/Stage3/Network.py"
    click Stage3Config "https://github.com/gaia-dental-studio/dental_smile_design/tree/main/Code/Stage3/config"
    click Stage3CKPT "https://github.com/gaia-dental-studio/dental_smile_design/tree/main/Code/Stage3/ckpt"
    click Stage3Mask2Teeth "https://github.com/gaia-dental-studio/dental_smile_design/blob/main/Code/Stage3_Mask2Teeth.py"
    click OutputModule "https://github.com/gaia-dental-studio/dental_smile_design/tree/main/Output"
    click Restore "https://github.com/gaia-dental-studio/dental_smile_design/blob/main/Code/Restore/Restore.py"
    click HelperSettings "https://github.com/gaia-dental-studio/dental_smile_design/blob/main/Code/helper.py"
    click GlobalConfig "https://github.com/gaia-dental-studio/dental_smile_design/blob/main/Code/Config.yaml"
    click API "https://github.com/gaia-dental-studio/dental_smile_design/blob/main/Code/api.py"
    click UI "https://github.com/gaia-dental-studio/dental_smile_design/blob/main/Code/app.py"
    click MainScript "https://github.com/gaia-dental-studio/dental_smile_design/blob/main/Code/main.py"
    click MainForFolder "https://github.com/gaia-dental-studio/dental_smile_design/blob/main/Code/main_ForFolder.py"

    %% Styles
    classDef input fill:#9fdff3,stroke:#333,stroke-width:2px;
    classDef output fill:#d9ead3,stroke:#333,stroke-width:2px;
    classDef processing fill:#c8f7c5,stroke:#333,stroke-width:2px;
    classDef external fill:#ffe599,stroke:#333,stroke-width:2px;
    classDef auxiliary fill:#f4cccc,stroke:#333,stroke-width:2px;
```
