Skip to content

Repository files navigation

🎵 Emotion-Driven Music Recommender

An innovative web application that recommends music based on a user's current emotional state, detected either through live facial expressions via webcam or sentiment analysis of text input. This project was developed as a hackathon submission, showcasing real-time AI/ML, web development, and API integration.

✨ Features

  • Real-time Facial Emotion Detection: Utilizes your webcam to detect facial expressions (happy, sad, angry, neutral, surprise, fear, disgust) and display the dominant emotion live on the UI.

  • Live Webcam Feed: Streams your webcam feed directly to the web interface, showing emotion detection in action with bounding boxes and labels.

  • Text-based Emotion Analysis: Allows users to input text (e.g., a diary entry) to determine its sentiment and infer an emotion.

  • Spotify Music Recommendations: Fetches and displays relevant music tracks from Spotify based on the detected emotion, with playable embedded widgets.

  • Dynamic UI Updates: Music recommendations update automatically based on changes in detected webcam emotion or upon text analysis.

📋 Prerequisites

Before you begin, ensure you have the following installed:

  • Python 3.8+ (Recommended)
  • pip (Python package installer, usually comes with Python)
  • Git (for cloning the repository)
  • A Spotify Account: You'll need this to create a Spotify Developer application.

⚙️ Setup Instructions

Follow these steps to get the project up and running on your local machine.

1. Clone the Repository

git clone https://github.com/prasoonmhwr/emotion-driven-music.git 
cd emotion-driven-music

2. Create and Activate a Virtual Environment

It's highly recommended to use a virtual environment to manage dependencies.

python -m venv venv

On Linux/macOS:

source venv/bin/activate

On Windows (Command Prompt):

venv\Scripts\activate.bat

On Windows (PowerShell):

.\venv\Scripts\Activate.ps1

You should see (venv) at the beginning of your terminal prompt, indicating the virtual environment is active.

3. Install Dependencies

Install all required Python packages using pip:

pip install -r requirements.txt

4. Download NLTK Data

The nltk library requires some data to be downloaded. Run this command once:

python oneTimeScript.py

5. Spotify Developer Setup (It is free)

You need to create an application on the Spotify Developer Dashboard to get API credentials.

  1. Go to the Spotify Developer Dashboard
  2. Log in with your Spotify account.
  3. Click on "Create an app".
  4. Provide an App Name (e.g., "Emotion Music Recommender") and a Description. Select "Web API" for the API usage.
  5. After creating the app, note down your Client ID and Client Secret.
  6. Click "Edit Settings" for your app.
  7. Under "Redirect URIs", add:
    • http://localhost:5001/callback
    • http://127.0.0.1:5001/callback
  8. Click "Add" for each, then "Save" at the bottom.

6. Configure Environment Variables (.env file)

Create a new file named .env in the root directory of your project (the same directory as app.py).

Add your Spotify credentials to this file:

SPOTIPY_CLIENT_ID='YOUR_CLIENT_ID_HERE'
SPOTIPY_CLIENT_SECRET='YOUR_CLIENT_SECRET_HERE'
SPOTIPY_REDIRECT_URI='http://localhost:5001/callback'

Replace YOUR_CLIENT_ID_HERE and YOUR_CLIENT_SECRET_HERE with the actual values you copied from Spotify.

▶️ How to Run

  1. Ensure your virtual environment is active.
  2. Navigate to your project's root directory in the terminal.
  3. Run the Flask application:
python app.py
  1. The Flask development server will start, usually on http://127.0.0.1:5001/.
  2. Open your web browser and go to: http://127.0.0.1:5001/

🕹️ Usage

Once the application is running in your browser:

  • Live Webcam Emotion: Your webcam feed should appear, and the detected emotion (with a bounding box) will be displayed. Music recommendations will update automatically every few seconds if your detected emotion changes. You can also click "Refresh Music for Current Mood" to manually get new recommendations.

  • Analyze Text for Emotion: Type how you're feeling or a description of your day into the text area. Click "Analyze & Get Music" to see the inferred emotion and corresponding music recommendations.

  • Play Music: The Spotify recommendations will appear as embedded players. Click the play button on any song to listen to a preview.

Webcam not activating or showing "Error: Could not open webcam."

  • Ensure no other application is currently using your webcam.
  • Check your operating system's privacy settings to ensure your browser and/or Python has permission to access the webcam.
  • Try restarting your computer.

If Everything goes right, this is what you will see

Preview

About

No description, website, or topics provided.

Resources

Stars

12 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages