This project combines facial emotion detection with music recommendation. Using a webcam, the system detects a user’s emotion and then suggests music tailored to that emotion from a Spotify dataset.
- Real-Time Emotion Detection: Uses a webcam feed to detect emotions in real-time with DeepFace.
- Music Recommendation: Based on the detected emotion, recommends songs with relevant features (e.g., high valence for happy emotions).
- Spotify Integration: Generates a Spotify search link to easily find and listen to the recommended songs.
This project requires:
- A webcam for real-time video capture.
- A trained face recognizer model (LBPH) and a Haar Cascade classifier.
- A Spotify music dataset to use for recommendations.
- Python 3.x
- Kaggle account to download the Spotify music dataset
-
Clone the repository:
git clone https://github.com/yourusername/face_recognition.git cd face_recognition -
Install the required libraries:
pip install opencv-contrib-python deepface pandas --user
-
Download the Spotify dataset from Kaggle and place it in the project directory:
-
Prepare the face model:
- Ensure that the
haarcascade_frontalface_default.xmlfile is available for face detection. - Run
python face_create_dataset.pyto create dataset consist of your own face. - Then train the dataset model by running
python face_training.py
- Ensure that the
To start the application:
python face_recognition_v2.py- Emotion Detection: The system scans the face for 8 seconds, then identifies the most frequently detected emotion.
- Music Recommendation: Based on the detected emotion, the system suggests five songs and generates Spotify links.
The dataset is a Spotify music dataset containing features like valence, energy, danceability, etc., essential for emotion-based recommendations. The dataset can be downloaded from Kaggle:
Ensure the file is named songs_normalize.csv.
The script uses OpenCV and DeepFace to recognize faces and detect emotions. The program captures video frames, processes them for faces, and then analyzes emotions based on detected faces.
# Set up camera capture
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)The system captures and counts emotions over 10 seconds to determine the most common emotion detected. But you can customize it based on your machine capabilities
from collections import Counter
emotion_counts = Counter()
scan_duration = 10 # secondsBased on the dominant emotion detected, the system filters songs from the dataset with matching features.
# Recommend songs function
def recommend_songs(emotion, num_songs=5):
# Filters for happy, sad, fear, angry, surprised, and neutral emotions
# Each emotion corresponds to specific song features- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a pull request