# vipstone/faceai architecture

> Architecture diagram of the GitHub repository vipstone/faceai, 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/vipstone/faceai
- Repository: https://github.com/vipstone/faceai

## Overview

##### Main components
- Face Detection Module: This includes both OpenCV-based (detectionOpencv.py, videoOpencv.py) and Dlib-based (detectionDlib.py, videoDlib.py) methods. Note that there’s also a “face_recognition” component.
- Facial Feature Analysis & Recognition: Components include scripts such as faceRecognition.py, faceRecognitionOutline.py, and faceRecognitionMakeup.py for drawing contours, performing makeup, and a general recognition pipeline.
- Classification Modules: Separate functionalities exist for emotion recognition (emotion.py with its referenced CNN models in faceai/classifier/emotion_models) and gender classification (gender.py using its models in faceai/classifier/gender_models). These use deep learning frameworks (keras/tensorflow).
- Video Processing: Both image (still picture functions) and video pipelines are supported (videoDlib.py and videoOpencv.py).
- Image Enhancement/Manipulation: Modules for image repair (inpaint.py), automatic colorization (colorize.py), and composition effects (compose.py) are in place.
- Text Recognition (OCR): The tesseractOcr.py script enables OCR via Tesseract.
- Supporting Utilities: Miscellaneous utilities such as eye tracking (eye.py, eye2.py), faceswap.py, and additional image manipulation utilities in the opencv folder.

##### Relationships and interactions
- Input Layer: Images or videos are supplied as input, which are then processed by one of the detection pipelines (either OpenCV or Dlib) to identify faces.
- Processing Pipeline: Once detection is complete, the identified faces are passed to either the recognition modules or to further feature/expression analysis. Depending on the use case, the image may subsequently be enhanced (colorize, inpaint) or altered (compose, faceswap, makeup simulation).
- Classification & Deep Learning: For classification tasks like emotion or gender detection, the system leverages pre-trained deep learning models stored in the classifier directory. These models interact with the input (after face detection) and output class scores.

##### Architectural Patterns and Design Principles
- Modular Separation: The design naturally separates concerns by having dedicated scripts for each functionality, making it easier to update or replace specific components.
- Reusability and Extensibility: Given the modularity and the presence of clear directories for models and documentation, the system is designed for easy enhancements (e.g., pending implementations like faceswap and eye tracking).

##### Relevant Technologies
- Python 3.6.4 as the programming environment
- OpenCV and Dlib for computer vision processes
- face_recognition library for facial detection and feature extraction
- Keras and TensorFlow for deep learning-driven classification (emotion, gender)
- Tesseract OCR for text recognition functionalities

##### Final advice
- Be very detailed—capture even sub-components such as the dual implementations (OpenCV and Dlib) and point out which functionalities are “pending” (like faceswap and eye movement detection).

## Diagram (Mermaid)

```mermaid
flowchart TD
    A("Image/Video Input"):::input
    B("Core Engine"):::engine

    subgraph "Detection Modules"
        D1("Face Detection (OpenCV)"):::detection
        D2("Face Detection (Dlib)"):::detection
    end

    subgraph "Recognition & Classification"
        F("Facial Feature Recognition"):::recognition
        E("Emotion Classification"):::classification
        G("Gender Classification"):::classification
    end

    subgraph "Enhancement Modules"
        I("Image Enhancement (Inpainting)"):::enhancement
        J("Image Enhancement (Colorization)"):::enhancement
        K("Image Enhancement (Composition/Makeup)"):::enhancement
    end

    L("OCR Module"):::ocr
    U("Utilities (Eye Tracking & Face Swap)"):::utility

    subgraph "External Libraries"
        ext1("OpenCV"):::external
        ext2("Dlib"):::external
        ext3("face_recognition"):::external
        ext4("keras"):::external
        ext5("tensorflow"):::external
        ext6("Tesseract OCR"):::external
    end

    %% Data flow connections
    A --> B
    B --> D1
    B --> D2
    D1 --> F
    D2 --> F
    F --> E
    F --> G
    F --> I
    F --> J
    F --> K
    F --> L
    B --> U

    %% External library connections
    ext1 --> D1
    ext1 --> I
    ext1 --> J
    ext2 --> D2
    ext3 --> F
    ext4 --> E
    ext4 --> G
    ext5 --> E
    ext5 --> G
    ext6 --> L

    %% Click Events for Component Mapping
    click D1 "https://github.com/vipstone/faceai/blob/master/faceai/detectionOpencv.py"
    click D1 "https://github.com/vipstone/faceai/blob/master/faceai/videoOpencv.py"
    click D2 "https://github.com/vipstone/faceai/blob/master/faceai/detectionDlib.py"
    click D2 "https://github.com/vipstone/faceai/blob/master/faceai/videoDlib.py"
    click F "https://github.com/vipstone/faceai/blob/master/faceai/faceRecognition.py"
    click F "https://github.com/vipstone/faceai/blob/master/faceai/faceRecognitionOutline.py"
    click F "https://github.com/vipstone/faceai/blob/master/faceai/faceRecognitionMakeup.py"
    click E "https://github.com/vipstone/faceai/blob/master/faceai/emotion.py"
    click E "https://github.com/vipstone/faceai/blob/master/faceai/classifier/emotion_models/simple_CNN.530-0.65.hdf5"
    click G "https://github.com/vipstone/faceai/blob/master/faceai/gender.py"
    click G "https://github.com/vipstone/faceai/blob/master/faceai/classifier/gender_models/gender_mini_XCEPTION.21-0.95.hdf5"
    click G "https://github.com/vipstone/faceai/blob/master/faceai/classifier/gender_models/simple_CNN.81-0.96.hdf5"
    click I "https://github.com/vipstone/faceai/blob/master/faceai/opencv/inpaint.py"
    click J "https://github.com/vipstone/faceai/blob/master/faceai/colorize.py"
    click J "https://github.com/vipstone/faceai/blob/master/faceai/data/simple_colorize.h5"
    click K "https://github.com/vipstone/faceai/blob/master/faceai/compose.py"
    click L "https://github.com/vipstone/faceai/blob/master/faceai/tesseractOcr.py"
    click U "https://github.com/vipstone/faceai/blob/master/faceai/eye.py"
    click U "https://github.com/vipstone/faceai/blob/master/faceai/eye2.py"
    click U "https://github.com/vipstone/faceai/blob/master/faceai/faceswap.py"

    %% Styles
    classDef input fill:#FFFFFF,stroke:#000000,stroke-width:2px;
    classDef engine fill:#F0F0F0,stroke:#333333,stroke-width:2px;
    classDef detection fill:#bbdefb,stroke:#0d47a1,stroke-width:2px;
    classDef recognition fill:#d1c4e9,stroke:#512da8,stroke-width:2px;
    classDef classification fill:#c8e6c9,stroke:#2e7d32,stroke-width:2px;
    classDef enhancement fill:#ffe0b2,stroke:#ef6c00,stroke-width:2px;
    classDef ocr fill:#fff9c4,stroke:#fbc02d,stroke-width:2px;
    classDef utility fill:#e1bee7,stroke:#6a1b9a,stroke-width:2px;
    classDef external fill:#e0e0e0,stroke:#9e9e9e,stroke-width:2px;

    class A input;
    class B engine;
    class D1,D2 detection;
    class F recognition;
    class E,G classification;
    class I,J,K enhancement;
    class L ocr;
    class U utility;
    class ext1,ext2,ext3,ext4,ext5,ext6 external;
```
