Skip to content

Ajaysomala/Emotion_based_music_recommend_Using__AI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

Emotion-Based Music Player

Real-time facial emotion recognition system that detects mood from webcam input and plays music based on the detected emotion.

This project combines:

  • TensorFlow/Keras for emotion classification
  • OpenCV for webcam capture and face detection
  • python-vlc + Tkinter for music playback UI

The current implementation is stabilized for local Windows usage with dependency compatibility fixes and improved runtime flow.

Features

  • 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.

Project Structure

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

Requirements

  • 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.0
  • numpy==1.18.5
  • opencv-python==4.2.0.32
  • python-vlc==3.0.7110

Setup

  1. Create and activate a virtual environment:
python -m venv .venv
.venv\Scripts\activate
  1. Install dependencies:
pip install -r requirements.txt
  1. Ensure model.h5 is present in the repository root:
EMOTION_BASED_Music_S/model.h5

Run

From project root:

python Tensorflow\emotions.py

Press q in the OpenCV window to close camera detection.

Training (Optional)

If you want to retrain the model:

python Tensorflow\model_train.py

Note: model_train.py currently contains hardcoded dataset paths. Update train_dir and val_dir before running.

Recent Fixes and Stability Improvements

  • Resolved TensorFlow import crash caused by NumPy incompatibility (np.object / numpy.typeDict errors) by pinning:
    • numpy==1.18.5 with tensorflow==2.3.0
  • Added NumPy compatibility aliases in Tensorflow/emotions.py as 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.

Troubleshooting

1) AttributeError: module 'numpy' has no attribute 'object'

Cause: incompatible NumPy for TensorFlow 2.3.

Fix:

pip install --force-reinstall numpy==1.18.5

2) Could not load dynamic library 'cudart64_101.dll'

This is a GPU CUDA warning from TensorFlow.
If you are running CPU-only, this warning is expected and safe to ignore.

3) VLC libvlc.dll issues

  • Ensure VLC is installed.
  • Match architecture (64-bit Python with 64-bit VLC).
  • Reinstall python-vlc if needed.

4) Haar cascade / camera issues

  • Check webcam access permissions in Windows.
  • Ensure no other app is locking the camera.
  • Keep haarcascade_frontalface_default.xml in the project root.

Customizing Playlists

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.

Credits

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages