![]() |
VOOZH | about |
In this article, we will be discussing how can we recommend music based on expressions or say dominant expressions on someone's face. This is a basic project in which we will be using OpenCV, Matplotlib, DeepFace, and Spotify API.
Though there are many other ways to recommend music, this implementation is a basic approach. Let's move forward to the implementation. Install the deepface library in the Python environment.
Import the necessary packages.
The path of the image whose expression detection is to be performed should be copied. The image should then be read using the "imread()" method in cv2, the image is stored in the form of an array. Afterward, utilize Matplotlib's imshow() method to display the image.
Output:
Use deepface to analyze the emotion in an image. Pass the image stored as an array to Deepface's analyze function. It will return a Python dictionary with the percentage of all emotions.
Output:
[{'emotion': {'angry': 2.9941825391265356e-05,
'disgust': 3.6047339119136895e-10,
'fear': 0.00011003920865101386,
'happy': 97.65191646241146,
'sad': 0.0015582609232700413,
'surprise': 0.0032574247843123716,
'neutral': 2.343132812456228},
'dominant_emotion': 'happy',
'region': {'x': 325, 'y': 64, 'w': 128, 'h': 128}}]
Extract the emotion with the highest percentage.
Output:
happy
Using Spotify API to search music according to the emotion with the highest percentage. We use the Spotify API from RapidAPI. You can edit the following parameters in the below code:
You can add your API key by subscribing to Spotify API on the Rapid API website. Replace the <YOUR_API_KEY> with your generated key.
Output:
song name: Happy - From "Despicable Me 2" album name: G I R L song name: Happy Together album name: Happy Together song name: HAPPY album name: HOPE song name: Happy? album name: Lost and Found song name: Happy Pills album name: Happy Pills song name: Happy album name: Ashanti song name: Happy Birthday to You album name: Happy Birthday to You! Songs & Lieder zum Geburtstag, Geburtstagslieder song name: Happy Birthday Song album name: CoComelon Kids Hits, Vol. 3 song name: Happy Birthday album name: Hotter Than July song name: The Happy Song album name: The Happy Song
The response from API shows 10 songs that match the search query. Here is the complete implementation:
Output:
Action: emotion: 100%|██████████| 1/1 [00:00<00:00, 2.30it/s] Action: emotion: 100%|██████████| 1/1 [00:00<00:00, 14.28it/s] Action: emotion: 100%|██████████| 1/1 [00:00<00:00, 15.68it/s] Action: emotion: 100%|██████████| 1/1 [00:00<00:00, 14.28it/s] ['song name: Happy - From "Despicable Me 2" album name:G I R L\n', 'song name: Happy Together album name:Happy Together\n', 'song name: HAPPY album name:HOPE\n', 'song name: Happy? album name:Lost and Found\n', 'song name: Happy Pills album name:Happy Pills\n', 'song name: Happy album name:Ashanti\n', 'song name: Happy Birthday to You album name:Happy Birthday to You! Songs & Lieder zum Geburtstag, Geburtstagslieder \n', 'song name: Happy Birthday Song album name:CoComelon Kids Hits, Vol. 3\n', 'song name: Happy Birthday album name:Hotter Than July\n', 'song name: The Happy Song album name:The Happy Song\n']
In this article, we discussed how to recommend songs from Facial Expressions. There are various methods available to achieve higher accuracy, this is a basic implementation for you to get started.