# jyotiska222/emotion-detection-model architecture

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

- Interactive diagram: https://gitdiagram.com/jyotiska222/emotion-detection-model
- Repository: https://github.com/jyotiska222/emotion-detection-model

## Overview

##### Project type and purpose
- This is a machine-learning / computer-vision project that trains a CNN on the FER-2013 dataset and then performs real-time emotion detection via a webcam feed.
- It’s an open-source tool for emotion recognition, consisting of a training pipeline, evaluation pipeline, saved model storage, and an inference (real-time) component.

##### File structure
- Top-level:
- dataset/ – raw images organized by emotion labels (train & test)
- saved_model/ – serialized Keras model (emotion_model.h5)
- src/ – Python scripts
- requirements.txt, README.md
- src/ contains three distinct scripts:
- train_model.py – loads dataset, defines and trains CNN, writes model to saved_model/
- test_model.py – loads saved_model/emotion_model.h5 and test images, reports metrics
- realtime_emotion.py – uses OpenCV to capture webcam frames, loads the model, preprocesses faces, predicts and renders label overlays

##### README for additional insights
- Confirms the use of TensorFlow/Keras, OpenCV, NumPy, Pandas, Matplotlib.
- Describes training → testing → real-time inference workflows.

a. Main components
- Data Storage
– FER-2013 dataset (dataset/train, dataset/test)
- Training Module
– train_model.py, CNN architecture, TensorFlow/Keras
- Model Repository
– saved_model/emotion_model.h5
- Evaluation Module
– test_model.py
- Inference Module
– realtime_emotion.py, OpenCV, webcam
- External Interfaces
– User’s webcam, display window

b. Relationships and interactions
1. Data Storage → Training Module (batch ingestion of training images)
2. Training Module → Model Repository (save trained .h5)
3. Data Storage → Evaluation Module & Model Repository → Evaluation Module (load test images & model, compute metrics)
4. Model Repository & External Interface (webcam) → Inference Module (load model, capture frames, predict, render)

c. Architectural patterns / design principles
- Separation of Concerns: distinct scripts for training, testing, inference
- Data-Model-Interface pipeline: raw data → model artifact → runtime inference
- Modularization: each major step is encapsulated in its own Python script
- Reproducibility: uses requirements.txt and consistent directory layout

d. Key technologies, frameworks, libraries
- TensorFlow / Keras – CNN definition, training, serialization
- OpenCV – face detection / capture, frame processing, display
- NumPy & Pandas – data handling and preprocessing
- Matplotlib – optional visualizations in train/test scripts

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Data Storage
    TrainDS["dataset/train/"]:::storage
    TestDS["dataset/test/"]:::storage

    %% Model Repository
    ModelRepo["emotion_model.h5"]:::storage

    %% External Interfaces
    Webcam["Webcam"]:::external
    Display["Display Window"]:::external

    %% Offline Processes
    subgraph "Offline Processes"
        subgraph "Training Module"
            Preprocess["Preprocess Images<br/>(face detection, grayscale, resize)"]:::offline
            TrainCNN["Train CNN Model<br/>(TensorFlow/Keras)"]:::offline
            TrainModel["train_model.py"]:::offline
        end
        subgraph "Evaluation Module"
            Evaluate["Evaluate on Test Set<br/>(metrics computation)"]:::offline
            TestModel["test_model.py"]:::offline
        end
    end

    %% Online Inference
    subgraph "Online Inference"
        subgraph "Inference Module"
            RealTime["realtime_emotion.py"]:::runtime
            Capture["Capture Frames<br/>(OpenCV)"]:::runtime
            Preprocess2["Preprocess Frame<br/>(face detect, resize)"]:::runtime
            Predict["Predict Emotion<br/>(model inference)"]:::runtime
            Render["Render Overlay<br/>(label on frame)"]:::runtime
        end
    end

    %% Data Flows
    TrainDS -->|ingest training images| Preprocess
    Preprocess --> TrainCNN
    TrainCNN --> TrainModel
    TrainModel -->|save .h5 model| ModelRepo

    TestDS -->|load test images| Evaluate
    ModelRepo -->|load model| Evaluate
    Evaluate --> TestModel

    ModelRepo -->|load model| RealTime
    Webcam --> Capture
    RealTime --> Capture
    Capture --> Preprocess2
    Preprocess2 --> Predict
    Predict --> Render
    Render --> Display

    %% Click Events
    click TrainDS "https://github.com/jyotiska222/emotion-detection-model/tree/main/dataset/train/"
    click TestDS "https://github.com/jyotiska222/emotion-detection-model/tree/main/dataset/test/"
    click TrainModel "https://github.com/jyotiska222/emotion-detection-model/blob/main/src/train_model.py"
    click TestModel "https://github.com/jyotiska222/emotion-detection-model/blob/main/src/test_model.py"
    click ModelRepo "https://github.com/jyotiska222/emotion-detection-model/blob/main/saved_model/emotion_model.h5"
    click RealTime "https://github.com/jyotiska222/emotion-detection-model/blob/main/src/realtime_emotion.py"

    %% Styles
    classDef storage fill:#D3D3D3,stroke:#333,stroke-width:1px
    classDef offline fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef runtime fill:#90EE90,stroke:#333,stroke-width:1px
    classDef external fill:#FFA500,stroke:#333,stroke-width:1px
```
