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.
- 🎥 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)
- Python 3.8+
- OpenCV
- face_recognition
- dlib
- NumPy
- pickle (for face encoding storage)
- Ultralytics YOLOv8
-
Clone this repo
git clone https://github.com/yourusername/emotion-detector-yolov8.git cd emotion-detector-yolov8 -
Install the dependencies
pip install opencv-python pip install face_recognition pip install numpy pip install ultralytics
⚠️ If you face issues withface_recognition, install these first:pip install cmake pip install dlib pip install face_recognition
-
Add your YOLOv8 model Place your trained YOLOv8 model file
best.ptin the project root. -
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 generateFaces_Encodings.pkl
- Create a folder
Run the main detection script:
python emotion_detection.pyPress q to quit the webcam.
The YOLOv8 model (best.pt) was trained on a labeled facial emotion dataset using a custom Kaggle notebook.
📎 View the Kaggle notebook here
| 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 |
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
Developed by Mohamed Alabasy
Feel free to contribute, fork, or reach out for collaboration!