Real-time facial emotion recognition system that detects mood from webcam input and plays music based on the detected emotion.
This project combines:
TensorFlow/Kerasfor emotion classificationOpenCVfor webcam capture and face detectionpython-vlc+Tkinterfor music playback UI
The current implementation is stabilized for local Windows usage with dependency compatibility fixes and improved runtime flow.
- Real-time face detection using Haar Cascade.
- CNN-based emotion prediction for 7 classes:
Angry,Disgusted,Fearful,Happy,Neutral,Sad,Surprised
- Emotion stabilization logic to reduce prediction flicker before triggering music.
- Confidence display on video overlay (example:
Happy (92%)). - Dynamic loading of model weights from repository root (
model.h5). - Emotion-wise song folders with an interactive VLC-based player UI.
- Improved control flow to stop camera/player cleanly.
EMOTION_BASED_Music_S/
|-- Tensorflow/
| |-- emotions.py
| |-- model_train.py
| |-- musicplayer.py
| |-- data/
| | |-- train/
| | `-- test/
| `-- songs/
| |-- Angry/
| |-- Disgusted/
| |-- Fearful/
| |-- Happy/
| |-- Neutral/
| |-- Sad/
| `-- Surprised/
|-- model.h5
|-- haarcascade_frontalface_default.xml
|-- requirements.txt
`-- README.md
- Python
3.8.x(recommended for this codebase) - Webcam
- VLC media player installed (64-bit recommended)
- Windows OS (current scripts and paths are Windows-oriented)
Python dependencies are pinned in requirements.txt:
tensorflow==2.3.0numpy==1.18.5opencv-python==4.2.0.32python-vlc==3.0.7110
- Create and activate a virtual environment:
python -m venv .venv
.venv\Scripts\activate- Install dependencies:
pip install -r requirements.txt- Ensure
model.h5is present in the repository root:
EMOTION_BASED_Music_S/model.h5
From project root:
python Tensorflow\emotions.pyPress q in the OpenCV window to close camera detection.
If you want to retrain the model:
python Tensorflow\model_train.pyNote: model_train.py currently contains hardcoded dataset paths. Update train_dir and val_dir before running.
- Resolved TensorFlow import crash caused by NumPy incompatibility (
np.object/numpy.typeDicterrors) by pinning:numpy==1.18.5withtensorflow==2.3.0
- Added NumPy compatibility aliases in
Tensorflow/emotions.pyas a defensive fallback. - Fixed model weight path loading using dynamic
BASE_DIR. - Improved emotion detection stability with emotion counting before selecting a stable emotion.
- Added confidence percentage display in live prediction overlay.
- Reduced noisy runtime logs and optimized prediction call verbosity.
- Improved music player flow to avoid recursive camera relaunch behavior.
Cause: incompatible NumPy for TensorFlow 2.3.
Fix:
pip install --force-reinstall numpy==1.18.5This is a GPU CUDA warning from TensorFlow.
If you are running CPU-only, this warning is expected and safe to ignore.
- Ensure VLC is installed.
- Match architecture (64-bit Python with 64-bit VLC).
- Reinstall
python-vlcif needed.
- Check webcam access permissions in Windows.
- Ensure no other app is locking the camera.
- Keep
haarcascade_frontalface_default.xmlin the project root.
Add songs into the emotion folders under:
Tensorflow/songs/<EmotionName>/
Example:
Tensorflow/songs/Happy/Tensorflow/songs/Sad/
The player automatically loads tracks from the detected emotion folder.
- Base inspiration: https://github.com/atulapra/Emotion-detection
- FER challenge reference:
- Goodfellow et al., "Challenges in Representation Learning: A report on three machine learning contests", arXiv, 2013.