Skip to content

RAJAT-MITRA/Emotion_AI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

13 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿง  EmotionAI

Real-time facial expression analysis powered by DeepFace CNNs.
Upload a portrait or stream your webcam โ€” get instant emotion scores, face-bounding boxes, and contextual insights.


โœจ Features

Feature Details
๐Ÿ“ค Image Upload Analyze any JPG/PNG/WebP portrait. Bounding box + emotion scores returned instantly.
๐Ÿ“ท Live Webcam Frame-by-frame real-time analysis every ~800 ms via your browser camera.
๐Ÿ˜„ 4-Class Emotion Detection Joy ยท Sadness ยท Anger ยท Neutral with percentage confidence scores.
๐ŸŽฏ Face Bounding Box Detected face is highlighted with a dynamic overlay that scales proportionally.
๐Ÿ“Š Confidence Ring Animated radial progress ring for dominant emotion confidence.
๐Ÿ’ก Contextual Insights Personality-driven tips per detected emotion state.
โšก Hot Reload Dev Vite HMR frontend + Uvicorn backend, both run concurrently during development.

๐Ÿ–ผ๏ธ UI Preview

The interface is a 3-column dark dashboard:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  ๐ŸŸ  EmotionAI                      โ— DeepFace ยท Live โ”‚  โ† Top Bar
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚  INPUT MODE  โ”‚                       โ”‚  NEURAL OUTPUTโ”‚
โ”‚              โ”‚   Canvas / Viewport   โ”‚               โ”‚
โ”‚  ๐Ÿ“ค Upload   โ”‚   (drop zone or       โ”‚  Emotion      โ”‚
โ”‚  ๐Ÿ“ท Webcam   โ”‚    webcam feed)       โ”‚  Analysis     โ”‚
โ”‚              โ”‚                       โ”‚               โ”‚
โ”‚ EMOTION      โ”‚                       โ”‚  Confidence   โ”‚
โ”‚ PALETTE      โ”‚                       โ”‚  Ring + Bars  โ”‚
โ”‚  โ€ข Joy       โ”‚                       โ”‚  + Insight    โ”‚
โ”‚  โ€ข Sadness   โ”‚                       โ”‚               โ”‚
โ”‚  โ€ข Anger     โ”‚                       โ”‚               โ”‚
โ”‚  โ€ข Neutral   โ”‚                       โ”‚               โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Design System:

  • Fonts: Syne (headings) + Space Grotesk (body)
  • Palette: Warm orange accent (#FF6B35), emotion-coded colors (Joy = amber, Sadness = blue, Anger = red, Neutral = slate)
  • Animations: Framer Motion transitions, floating emoji idle states, springy bar reveals

๐Ÿ—๏ธ Architecture

EmotionAI/
โ”‚
โ”œโ”€โ”€ emotion.py              โ† Python FastAPI backend (port 8000)
โ”œโ”€โ”€ requirements.txt        โ† Python dependencies
โ”‚
โ””โ”€โ”€ frontend/               โ† React + Vite frontend (port 5173)
    โ”œโ”€โ”€ index.html
    โ”œโ”€โ”€ vite.config.js
    โ”œโ”€โ”€ package.json
    โ””โ”€โ”€ src/
        โ”œโ”€โ”€ main.jsx        โ† React entry point
        โ”œโ”€โ”€ index.css       โ† Global reset + Google Fonts import
        โ”œโ”€โ”€ App.jsx         โ† Main app component (all UI + logic)
        โ””โ”€โ”€ App.css         โ† Full design system & layout styles

Data Flow

Browser (React)
    โ”‚
    โ”‚  POST /analyze         (image upload โ†’ multipart/form-data)
    โ”‚  POST /analyze-frame   (webcam frame โ†’ base64 JSON)
    โ–ผ
FastAPI Backend (emotion.py)
    โ”‚
    โ”‚  cv2.imdecode()        โ†’ numpy array
    โ”‚  DeepFace.analyze()    โ†’ raw emotion dict + face region
    โ”‚  safe_float/safe_int() โ†’ serialization guard
    โ–ผ
JSON Response
    {
      "emotions": [
        { "label": "Joy",     "score": 87.4 },
        { "label": "Neutral", "score": 8.1  },
        { "label": "Sadness", "score": 3.1  },
        { "label": "Anger",   "score": 1.4  }
      ],
      "region": { "x": 110, "y": 45, "w": 220, "h": 280 },
      "image_width": 640,
      "image_height": 480
    }

๐Ÿ› ๏ธ Tech Stack

Backend

Package Purpose
FastAPI REST API framework
Uvicorn ASGI server
DeepFace CNN-based facial emotion analysis
OpenCV (cv2) Image decoding from bytes/base64
NumPy Array handling for image data
TensorFlow / Keras DeepFace model runtime

Frontend

Package Purpose
React 19 UI component framework
Vite 8 Dev server + bundler with HMR
Framer Motion Smooth animations & transitions
react-webcam Webcam access & frame capture
lucide-react Icon set

๐Ÿš€ Getting Started

Prerequisites

  • Python 3.10+ with pip
  • Node.js 18+ with npm
  • A webcam (optional, for live mode)

1. Clone the repo

git clone https://github.com/your-username/EmotionAI.git
cd EmotionAI

2. Set up the Python backend

# Install all Python dependencies
pip install -r requirements.txt

โš ๏ธ Note: This installs TensorFlow + DeepFace which are large (~2 GB). First run will also auto-download the DeepFace model weights.

# Start the backend server
python emotion.py

Backend will be live at โ†’ http://localhost:8000


3. Set up the React frontend

cd frontend

# Install Node dependencies
npm install --legacy-peer-deps

# Start the dev server
npm run dev

Frontend will be live at โ†’ http://localhost:5173


4. Open the app

Navigate to http://localhost:5173 in your browser.


๐Ÿ“ก API Reference

Base URL: http://localhost:8000

POST /analyze

Analyze an uploaded image file.

Request: multipart/form-data

Field Type Description
file UploadFile Image file (JPG, PNG, WebP)

Response:

{
  "emotions": [
    { "label": "Joy",     "score": 87.4 },
    { "label": "Neutral", "score": 8.1  },
    { "label": "Sadness", "score": 3.1  },
    { "label": "Anger",   "score": 1.4  }
  ],
  "region": { "x": 110, "y": 45, "w": 220, "h": 280 },
  "image_width": 640,
  "image_height": 480
}

POST /analyze-frame

Analyze a base64-encoded webcam frame (used for live mode).

Request: application/json

{
  "image": "data:image/jpeg;base64,/9j/4AAQSkZJRgAB..."
}

Response: Same structure as /analyze.


Error response (non-500):

{ "error": "Awaiting clear webcam frame..." }

The backend always returns HTTP 200 with an error key rather than throwing 500s โ€” this keeps the React frontend resilient during webcam startup.


โš™๏ธ How DeepFace Analysis Works

Inside emotion.py, the process_frame() function:

  1. Guards against empty frames โ€” returns a soft error if the image is null or zero-size (common during webcam init).
  2. Calls DeepFace.analyze() with:
    • actions=["emotion"] โ€” only emotion analysis, skipping age/gender/race for speed.
    • enforce_detection=False โ€” allows analysis even if no face is confidently detected.
    • silent=True โ€” suppresses verbose TF logs.
  3. Extracts & remaps raw emotion keys (happy, sad, angry, neutral) into labeled objects.
  4. Casts all NumPy types to plain Python float/int via safe_float() / safe_int() to avoid JSON serialization errors.
  5. Sorts emotions by score descending so the dominant emotion is always emotions[0].
  6. Returns face region in absolute pixel coordinates โ€” the frontend converts these to percentages for responsive overlay rendering.

๐ŸŽฎ Using the App

Upload Mode

  1. Click "Upload Image" in the sidebar (or it's selected by default).
  2. Click "Choose File" or click anywhere in the drop zone.
  3. Select a portrait photo โ€” DeepFace processes it and the results appear within 1โ€“3 seconds.
  4. A golden bounding box appears around the detected face with the dominant emotion label.
  5. The right panel shows the dominant emotion card with a confidence ring + full spectrum bars.
  6. Click โœ• to clear and try another image.

Live Webcam Mode

  1. Click "Live Webcam" in the sidebar.
  2. Allow browser camera permissions if prompted.
  3. Click "โ–ถ Start Scan" โ€” the app begins analyzing your face every 800 ms.
  4. Watch the emotion bars animate in real-time as your expression changes.
  5. Click "โน Stop Scan" to pause analysis.

๐Ÿงฉ Frontend Component Map

App.jsx
โ”‚
โ”œโ”€โ”€ <ConfidenceRing />          โ€” SVG radial ring showing % confidence
โ”‚
โ”œโ”€โ”€ Shell Layout
โ”‚   โ”œโ”€โ”€ <header.topbar>         โ€” Brand logo + DeepFace live badge
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ <aside.sidebar>         โ€” Mode selector + emotion palette legend
โ”‚   โ”‚
โ”‚   โ””โ”€โ”€ <div.main>
โ”‚       โ”‚
โ”‚       โ”œโ”€โ”€ <div.canvas-area>   โ€” Left: viewport + header + status chip
โ”‚       โ”‚   โ”œโ”€โ”€ <div.dropzone>  โ€” Upload idle state
โ”‚       โ”‚   โ”œโ”€โ”€ <img.preview>   โ€” Uploaded image + bounding box
โ”‚       โ”‚   โ””โ”€โ”€ <Webcam>        โ€” Live webcam feed + scan button
โ”‚       โ”‚
โ”‚       โ””โ”€โ”€ <div.analysis-panel>  โ€” Right: results panel
โ”‚           โ”œโ”€โ”€ <div.dominant-card>  โ€” Top emotion + ring
โ”‚           โ”œโ”€โ”€ <div.spectrum-section> โ€” Animated bar rows
โ”‚           โ””โ”€โ”€ <div.tip-card>   โ€” Contextual insight message

๐Ÿ› Known Quirks & Fixes Applied

Issue Fix Applied
deepface not installed Run pip install -r requirements.txt
lucide-react missing in frontend Install with npm install lucide-react --legacy-peer-deps
Vite PostCSS crash (<<<<<<< HEAD) Resolved git merge conflict markers in root package.json and package-lock.json
NumPy serialization โ†’ HTTP 500 safe_float() / safe_int() helpers wrap all DeepFace output values
Empty webcam frames on startup process_frame() returns {"error": "..."} instead of crashing

๐Ÿ“ฆ Production Build

Frontend

cd frontend
npm run build
# Output in frontend/dist/

Backend (with Gunicorn)

pip install gunicorn
gunicorn emotion:app -w 2 -k uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000

๐Ÿ“„ License

MIT License โ€” see LICENSE for details.


Built with โค๏ธ using DeepFace, FastAPI & React

About

This is a project for BCT held at JIS college of engineering on Feb26

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors