# balaji-kartheek/deepfake_detection architecture

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

- Interactive diagram: https://gitdiagram.com/balaji-kartheek/deepfake_detection
- Repository: https://github.com/balaji-kartheek/deepfake_detection

## Overview

##### Project Type and Purpose
- This is a full-stack DeepFake detection system with three main phases:
a. Offline Data and Model Workflow (Pre-processing + Model Training)
b. Model Registry (Saved Models)
c. Online Inference & Deployment (Flask web app with frontend UI)
- Purpose: ingest user-uploaded videos, detect whether they are real or fake by combining CNN feature extractors + RNN classifier, and display results with face detection overlays.

##### File Structure Analysis
- Top-level directories:
- Dataset (raw video files and Readme)
- Pre-Processing (Jupyter notebook and documentation)
- Model Training (training scripts/data/config)
- Saved Models (trained .h5 artifacts)
- Deploy (Flask app: app.py, templates/, static/, whl packages, requirements.txt)
- Pattern: separate offline ML pipelines vs. online inference service.
- Deployment: Flask “app.py” serves HTML/CSS frontend (templates/upload.html, upload.css) and static assets (uploads folder, model .h5 in Deploy/models).

##### README Insights
- Offline steps: load dataset → extract frames → face subframe extraction → landmark detection → framewise feature extraction.
- Prediction: user uploads video → frames extracted → face detected → features passed to model → classification per frame aggregated → result (REAL/FAKE).
- Technologies: Python, OpenCV, TensorFlow (efficientNet, InceptionV3, GRU), Flask frontend.

a. Main Components
1. Data Source Layer
‑ Raw video dataset (real & fake videos)
2. Offline Pipeline (batch processes)
‑ Frame Extractor
‑ Face Detector & Landmark Extraction
‑ Feature Extractor (pre-trained CNNs)
‑ Sequence Model Trainer (GRU RNN)
3. Model Registry
‑ Saved .h5 models (CNN, CNN+RNN)
4. Deployment Service (Flask API)
‑ Inference Engine (loads model, runs detection)
‑ File Storage (static/uploads)
5. Frontend UI
‑ Upload Page (HTML/CSS)
‑ Result Display (video player + overlay)
6. External Services & Libraries
‑ OpenCV, TensorFlow, Flask, Python packages
b. Relationships and Interactions
- Offline Pipeline reads Dataset → produces Saved Models
- Deploy Service loads Saved Models at startup
- User uploads video via Frontend → Flask API writes to static/uploads → Inference Engine invokes:
1) Frame extraction
2) Face detection/landmarks
3) Feature extraction (CNN)
4) Sequence classification (GRU)
- Inference Engine returns:
- Classification result (REAL vs FAKE)
c. Architectural Patterns and Principles
- Separation of Concerns: offline ML training vs online inference
- Modular pipeline steps (pre-processing, training, inference)
- Model Registry for versioning
- Client-Server pattern: Web UI ↔ REST API
- Stateless inference service (each video independent)
d. Key Technologies
- OpenCV for video/frame processing & face detection
- TensorFlow / Keras for CNN + RNN models
- Flask for lightweight web service
- HTML5/CSS for frontend
- Python libraries: pandas, seaborn, scikit-learn

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Data Source Layer
    data_src["Raw Video Dataset"]:::data
    click data_src "https://github.com/balaji-kartheek/deepfake_detection/tree/main/Dataset/"

    %% Offline Pipeline
    subgraph "Offline Pipeline (Pre-processing & Training)"
        direction TB
        FE["Frame Extractor"]:::compute
        FD["Face Detector & Landmark Extraction"]:::compute
        FEAT["Feature Extractor (CNN)"]:::compute
        SCT["Sequence Model Trainer (GRU RNN)"]:::compute
    end
    click FE "https://github.com/balaji-kartheek/deepfake_detection/blob/main/Pre-Processing/DeepFake_Detection_Pre-Processing.ipynb"
    click FD "https://github.com/balaji-kartheek/deepfake_detection/blob/main/Pre-Processing/DeepFake_Detection_Pre-Processing.ipynb"
    click FEAT "https://github.com/balaji-kartheek/deepfake_detection/blob/main/Pre-Processing/DeepFake_Detection_Pre-Processing.ipynb"
    click SCT "https://github.com/balaji-kartheek/deepfake_detection/tree/main/Model Training/"
    click FD "https://github.com/balaji-kartheek/deepfake_detection/blob/main/Pre-Processing/Phase-1.pdf"

    %% Model Registry
    MR["Saved Models (CNN + RNN)"]:::data
    click MR "https://github.com/balaji-kartheek/deepfake_detection/tree/main/Saved Models/models/CNN_RNN/"

    %% Online Inference & Deployment
    subgraph "Online Inference & Deployment"
        direction TB
        API["Flask API (/upload, /predict)"]:::web
        FS["File Storage (static/uploads)"]:::data
        subgraph "Inference Engine"
            direction TB
            OE1["Frame Extractor"]:::compute
            OE2["Face Detector & Landmark Extraction"]:::compute
            OE3["Feature Extractor (CNN)"]:::compute
            OE4["Sequence Classifier (GRU)"]:::compute
        end
        ML["Inference Model (.h5)"]:::data
        subgraph "Frontend UI"
            direction TB
            UP["Upload Page"]:::web
            CSS["Upload Page CSS"]:::web
            RD["Result Display\n(Overlay face boxes + Real/Fake label)"]:::web
        end
    end
    click API "https://github.com/balaji-kartheek/deepfake_detection/blob/main/Deploy/app.py"
    click FS "https://github.com/balaji-kartheek/deepfake_detection/tree/main/Deploy/static/uploads/"
    click ML "https://github.com/balaji-kartheek/deepfake_detection/blob/main/Deploy/models/inceptionNet_model.h5"
    click UP "https://github.com/balaji-kartheek/deepfake_detection/blob/main/Deploy/templates/upload.html"
    click CSS "https://github.com/balaji-kartheek/deepfake_detection/blob/main/Deploy/templates/upload.css"

    %% External Libraries & Dependencies
    EL["External Libraries & Dependencies"]:::ext
    DEP["requirements.txt"]:::ext
    click EL "https://github.com/balaji-kartheek/deepfake_detection/tree/main/Deploy/Dlib-python whl packages/"
    click DEP "https://github.com/balaji-kartheek/deepfake_detection/blob/main/Deploy/requirments.txt"

    %% User
    U["User"]:::ext

    %% Connections Offline
    data_src -->|"load videos"| FE -->|"extract frames"| FD -->|"detect faces & landmarks"| FEAT -->|"extract features"| SCT -->|"train model"| MR

    %% Connections Online
    U -->|"select video"| UP -->|"POST /upload"| API -->|"save video"| FS
    API -->|"load model"| ML --> OE1 -->|"extract frames"| OE2 -->|"detect faces & landmarks"| OE3 -->|"extract features"| OE4 -->|"classify sequence"| API
    MR -->|"load saved models"| API
    API -->|"return results"| RD --> U
    OE2 -->|"face boxes"| RD

    %% External Libraries usage
    EL --> FE
    EL --> FD
    EL --> FEAT
    EL --> SCT
    EL --> OE1
    EL --> OE2
    EL --> OE3
    EL --> OE4
    EL --> API

    classDef data fill:#bbdefb,stroke:#0d47a1,color:#0d47a1;
    classDef compute fill:#c8e6c9,stroke:#1b5e20,color:#1b5e20;
    classDef web fill:#ffe0b2,stroke:#e65100,color:#e65100;
    classDef ext fill:#e0e0e0,stroke:#424242,color:#424242;
```
