Skip to content

About

Real-time multi-face emotion detection using YOLOv8 and OpenCV. Detects facial expressions like happy, sad, angry, and more with color-coded bounding boxes and live webcam support. Built with Python, Ultralytics YOLO, and face_recognition.

Resources

Stars

5 stars

Watchers

0 watching

Forks

Repository files navigation

Real-Time Emotion Detection Using YOLOv8 and OpenCV

A real-time facial emotion and face recognition system using YOLOv8, OpenCV, and face_recognition.
The application detects multiple faces from a webcam feed, identifies known individuals by name, and classifies each face's emotion using a custom-trained YOLOv8 model. Each person is marked with a bounding box colored based on their emotion, and a real-time legend is displayed for clarity.


🧠 Features

  • 🎥 Real-time webcam capture
  • 👥 Multi-face detection using face_recognition
  • 🕵️‍♂️ Face recognition and labeling using pre-encoded images
  • 🧠 Emotion classification using a YOLOv8 model
  • 🎨 Emotion legend displayed in real-time
  • 🟩 Color-coded bounding boxes per emotion (OpenCV BGR format)
  • ⚡ Optimized performance (detection every 30 frames)

🔧 Tech Stack

  • Python 3.8+
  • OpenCV
  • face_recognition
  • dlib
  • NumPy
  • pickle (for face encoding storage)
  • Ultralytics YOLOv8

🚀 Installation

  1. Clone this repo

    git clone https://github.com/yourusername/emotion-detector-yolov8.git
    cd emotion-detector-yolov8
  2. Install the dependencies

    pip install opencv-python
    pip install face_recognition
    pip install numpy
    pip install ultralytics

    ⚠️ If you face issues with face_recognition, install these first:

    pip install cmake
    pip install dlib
    pip install face_recognition
  3. Add your YOLOv8 model Place your trained YOLOv8 model file best.pt in the project root.

  4. Prepare face encodings

    • Create a folder known_faces/ with subfolders for each person (e.g. abasy/)
    • Each subfolder should contain one or more clear face images
    • Run the script create_encodings.py (see structure below) to generate Faces_Encodings.pkl

▶️ Usage

Run the main detection script:

python emotion_detection.py

Press q to quit the webcam.


📓 Model Training (Kaggle)

The YOLOv8 model (best.pt) was trained on a labeled facial emotion dataset using a custom Kaggle notebook.
📎 View the Kaggle notebook here

🏷️ Emotion Labels and Colors (OpenCV BGR)

Emotion BGR Color Description
Angry (0, 0, 255) Red
Disgust (0, 100, 0) Dark Green
Fear (128, 0, 128) Purple
Happy (0, 255, 255) Yellow
Neutral (0, 255, 0) Green
Sad (255, 0, 0) Blue
Surprise (0, 165, 255) Orange

📁 Project Structure

EMOTION-DETECTION/
│
├── known_faces/             # Subfolders per person with images
├── best.pt                  # YOLOv8 emotion model
├── emotion_detection.py     # Main app
├── Faces_Encodings.pkl      # Encoded face database
├── Faces_Encodings.py       # Script to create Faces_Encodings.pkl
└── README.md                # This file

👨‍💻 Author

Developed by Mohamed Alabasy
Feel free to contribute, fork, or reach out for collaboration!

About

Real-time multi-face emotion detection using YOLOv8 and OpenCV. Detects facial expressions like happy, sad, angry, and more with color-coded bounding boxes and live webcam support. Built with Python, Ultralytics YOLO, and face_recognition.

Resources

Stars

5 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages