# sarmadusmani89/emotion-prediction architecture

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

- Interactive diagram: https://gitdiagram.com/sarmadusmani89/emotion-prediction
- Repository: https://github.com/sarmadusmani89/emotion-prediction

## Overview

##### Project type and purpose
- From the file tree and README, this is a small full-stack web application (likely built with Flask) that performs real-time emotion detection on video streams (webcam or sample videos) using a pre-trained PyTorch model. It serves a web front-end, processes video frames server-side, runs inference, and sends processed frames/alerts back to the browser.

##### File structure
- Top-level files:
- main.py – entry point, defines web server routes
- emotion_recognizer.py – encapsulates video capture and model-inference logic
- attention_autoencoder_emotion_model.pth – pre-trained PyTorch model
- templates/index.html – front-end HTML view
- static/alert.mp3 – audio alert asset
- videos/… – sample video files
- Pattern: a single backend service (MVC style)
- “templates” = Views
- “main.py” routes = Controllers
- “emotion_recognizer.py” + .pth = Model
- No external microservices or databases; uses in-memory video capture and model loading.

##### README for additional insights
- Confirms “real-time” processing of children’s emotions
- Alerts on negative emotions via an audio file

##### Components
a. Main components:
1) Client (Browser UI)
2) Web Server (Flask app in main.py)
3) Video Capture & Inference Module (emotion_recognizer.py using OpenCV + PyTorch)
4) Model Artifacts (attention_autoencoder_emotion_model.pth)
5) Static Assets (alert.mp3)
6) Templates (index.html)
7) Sample Videos (videos/*.mp4) – optional input source
b. Relationships & interactions:
- Browser → HTTP GET “/” → Server serves index.html
- Browser → HTTP GET “/video_feed” → Server streams JPEG frames (yielded by OpenCV capture + inference overlay)
- Server loads .pth model into memory at startup
- For each frame: capture → predict emotion → if negative, flag alert → encode frame → send to browser
- Browser displays MJPEG stream in a <img> or <video> tag; on receiving “alert” flag, plays alert.mp3 from /static
c. Architectural patterns:
- MVC: templates (V), routes in main.py (C), emotion_recognizer + .pth (M)
- Streaming API pattern for video frames
d. Key technologies:
- Python + Flask
- OpenCV for frame capture/processing
- PyTorch for inference
- HTML5 for UI, static MP3 for alert sound

- “Browser UI” (green),
- “Flask Server” (blue),
- “VideoCapture+Inference” (orange),
- “Model File (.pth)” (gray),
- “Static Assets” (audio) & “Templates” (HTML) (purple)
- GET / → index.html
- GET /video_feed → MJPEG stream
- Frame → predict_emotion() → frame+metadata

##### Next steps
- Lay out the Browser on the left.
- .pth file and sample videos as data sources feeding into Inference Module.
- Static/alert.mp3 and templates/index.html as assets served by the Flask App.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    Browser["Browser UI"]:::frontend

    %% Flask Server with subcomponents
    subgraph "Flask Server"
        RH["Route Handlers"]:::backend
        IM["VideoCapture + Inference Module"]:::inference
    end

    %% Data Artifacts and Assets
    Model["Model File (.pth)"]:::artifact
    AlertAudio["Audio Alert (alert.mp3)"]:::assets
    IndexHTML["Template (index.html)"]:::assets
    SampleVideos["Sample Videos Directory"]:::datasource

    %% Data Flow
    Browser -->|"GET /"| RH
    RH -->|"200 OK index.html"| Browser
    RH -->|"GET /video_feed"| RH
    Browser -->|"load UI"| IndexHTML
    Browser -->|"GET /video_feed"| RH
    RH -->|"stream_frame()"| IM
    IM -->|"capture frame (Webcam) or sample video"| SampleVideos
    IM -->|"load model at startup"| Model
    IM -->|"predict_emotion()"| IM
    IM -->|"encode JPEG"| RH
    RH -->|"stream MJPEG"| Browser
    IM -->|"flag negative emotion"| RH
    Browser -->|"play_alert()"| AlertAudio

    %% Click Events
    click RH "https://github.com/sarmadusmani89/emotion-prediction/blob/main/main.py"
    click IM "https://github.com/sarmadusmani89/emotion-prediction/blob/main/emotion_recognizer.py"
    click Model "https://github.com/sarmadusmani89/emotion-prediction/blob/main/attention_autoencoder_emotion_model.pth"
    click AlertAudio "https://github.com/sarmadusmani89/emotion-prediction/blob/main/static/alert.mp3"
    click IndexHTML "https://github.com/sarmadusmani89/emotion-prediction/blob/main/templates/index.html"
    click SampleVideos "https://github.com/sarmadusmani89/emotion-prediction/tree/main/videos/"

    %% Styles
    classDef frontend fill:#a2fca2,stroke:#333,stroke-width:1px
    classDef backend fill:#a2d2fc,stroke:#333,stroke-width:1px
    classDef inference fill:#fcf4a2,stroke:#333,stroke-width:1px
    classDef artifact fill:#cfcfcf,stroke:#333,stroke-width:1px
    classDef assets fill:#e0a2fc,stroke:#333,stroke-width:1px
    classDef datasource fill:#a2fced,stroke:#333,stroke-width:1px
```
