A machine learning application that detects emotions from facial expressions and plays music matching your mood using the Spotify API.
- Real-time Emotion Detection: Uses computer vision to analyze facial expressions via webcam
- Music Recommendation Engine: Selects music that matches your current emotional state
- Spotify Integration: Plays tracks directly on your Spotify account
- Responsive Web Interface: Clean, intuitive design that updates in real-time
- 😠 Angry
- 🤢 Disgust
- 😨 Fear
- 😊 Happy
- 😢 Sad
- 😲 Surprise
- 😐 Neutral
- Python 3.8+
- Webcam
- Spotify account (Free or Premium)
- Spotify Developer account with registered application
git clone https://github.com/yourusername/emotion-based-music-player.git
cd emotion-based-music-playerpython -m venv venvActivate the virtual environment:
Windows
venv\Scripts\activatemacOS/Linux
source venv/bin/activatepip install -r requirements.txt- Create a Spotify Developer account at developer.spotify.com
- Create a new application in the Spotify Developer Dashboard
- Set the Redirect URI to
http://127.0.0.1:8000/callback - Note your Client ID and Client Secret
- Create a
config.pyfile in the project root:
# Spotify API credentials
SPOTIFY_CLIENT_ID = "your-client-id-here"
SPOTIFY_CLIENT_SECRET = "your-client-secret-here"
SPOTIFY_REDIRECT_URI = "http://127.0.0.1:8000/callback"
# Emotion detection settings
EMOTION_DETECTION_INTERVAL = 5 # Detect emotion every 5 seconds
CAMERA_INDEX = 0 # Default camera (usually the webcam)
# Flask app settings
DEBUG = True
PORT = 8000Run the following script to create a default album art image:
python generate_default_album.py- Start the application:
python app.py- Open your browser and go to:
http://127.0.0.1:8000
- Click "Start Detection" to enable the webcam and begin emotion detection
- Open the Spotify application on your device (to act as a playback device)
- The system will detect your emotion and play appropriate music automatically
- Emotion Detection: The application uses OpenCV for face detection and DeepFace for emotion classification.
- Music Selection: Based on detected emotions, the application:
- First attempts to find matching songs from your personal playlists
- Falls back to your liked songs library if no playlist matches
- Uses Spotify's recommendation API as a final fallback
- Playback: The selected track is played on your active Spotify device
emotion-music-player/
├── app.py # Main application file
├── emotion_detector.py # Emotion detection module
├── music_player.py # Music recommendation and playback
├── config.py # Configuration settings
├── requirements.txt # Dependencies
├── static/ # Static files for web interface
│ ├── css/ # Stylesheets
│ ├── js/ # JavaScript files
│ └── img/ # Images
└── templates/ # HTML templates
└── index.html # Main interface
Edit the emotion_features and emotion_genres dictionaries in music_player.py to customize the audio features and genres associated with each emotion.
Modify the CSS in static/css/style.css to change the appearance of the web interface.
Contributions are welcome! Here's how you can contribute:
- Fork the repository
- Create a feature branch:
git checkout -b feature-name - Commit your changes:
git commit -m 'Add some feature' - Push to the branch:
git push origin feature-name - Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
- DeepFace for facial emotion recognition
- Spotipy for Spotify API integration
- Flask for the web framework
- OpenCV for computer vision capabilities
For questions or feedback, please contact:
- Your Name - Lahiru Bandara
- Project Link: https://github.com/LAHI-RU/emotion-based-music-player
