Spaces:
Configuration error
Configuration error
Upload 5 files
Browse files- README.md +41 -10
- app.py +182 -0
- haarcascade_frontalface_default.xml +0 -0
- refresh.py +16 -0
- requirement.txt +0 -0
README.md
CHANGED
|
@@ -1,10 +1,41 @@
|
|
| 1 |
-
---
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Emotion-based-Music-and-Video-Recommendation-System-
|
| 2 |
+
A webapp which displays music and video on the basis of emotion.
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
Group Members:
|
| 7 |
+
|
| 8 |
+
Gautham Kuckian
|
| 9 |
+
|
| 10 |
+
Prajjwal Vishwakarma
|
| 11 |
+
|
| 12 |
+
Dipanshu Vartak
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
SETUP:
|
| 17 |
+
Copy and enter "pip install -r requirements.txt" in terminal.
|
| 18 |
+
|
| 19 |
+
Also you need to set your youtube and spotify developers accounts for using api and save the keys and token in secret.py.
|
| 20 |
+
|
| 21 |
+
## Output 👨🏻💻⏳
|
| 22 |
+
### 🔗 1) Frontend: This is the frontend of our Project Webapp.
|
| 23 |
+

|
| 24 |
+
|
| 25 |
+
### 🔗 2) Detection Of Face: The below image show the second phase of our Project i.e. Detection of face and recognizing emotion.
|
| 26 |
+

|
| 27 |
+
|
| 28 |
+
### 🖨️ 3) Output of Video: This is the result of the video webpage which works with the help of YouTube API.
|
| 29 |
+

|
| 30 |
+
|
| 31 |
+
### 🔗 4) Detection of Face: The below image show the detection of Happy Mood.
|
| 32 |
+

|
| 33 |
+
|
| 34 |
+
### 🖨️ 5) Output of Music: This is the video recommendation based on user mood. The result of the Music webpage works with the help of Spotify API.
|
| 35 |
+

|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
|
app.py
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from asyncio.windows_events import NULL
|
| 2 |
+
from datetime import datetime
|
| 3 |
+
from shutil import register_unpack_format
|
| 4 |
+
from flask import Flask,render_template,request,Response
|
| 5 |
+
import cv2
|
| 6 |
+
import requests
|
| 7 |
+
import os,sys
|
| 8 |
+
from refresh import Refresh
|
| 9 |
+
from contextlib import redirect_stderr, redirect_stdout
|
| 10 |
+
import re
|
| 11 |
+
from urllib.request import urlopen
|
| 12 |
+
from flask import Flask,render_template,Response,redirect,url_for
|
| 13 |
+
import cv2
|
| 14 |
+
from keras.models import load_model
|
| 15 |
+
from time import sleep
|
| 16 |
+
from keras.preprocessing.image import img_to_array
|
| 17 |
+
from keras.preprocessing import image
|
| 18 |
+
import cv2
|
| 19 |
+
import numpy as np
|
| 20 |
+
import time
|
| 21 |
+
import webbrowser
|
| 22 |
+
import secret
|
| 23 |
+
|
| 24 |
+
app=Flask(__name__)
|
| 25 |
+
camera=cv2.VideoCapture(0)
|
| 26 |
+
face_classifier = cv2.CascadeClassifier('C:/Users/Gautham/OneDrive/Desktop/EBMAMRS/EBMAMRS/haarcascade_frontalface_default.xml')
|
| 27 |
+
classifier =load_model('C:/Users/Gautham/OneDrive/Desktop/EBMAMRS/EBMAMRS/fer.h5')
|
| 28 |
+
emotion_labels = ['Angry','Fear','Happy','Neutral', 'Sad', 'Surprise']
|
| 29 |
+
|
| 30 |
+
def generate_frames():
|
| 31 |
+
camera=cv2.VideoCapture(0)
|
| 32 |
+
while True:
|
| 33 |
+
success,frame=camera.read()
|
| 34 |
+
if not success:
|
| 35 |
+
break
|
| 36 |
+
else:
|
| 37 |
+
capture_duration = 5
|
| 38 |
+
start_time = time.time()
|
| 39 |
+
while( int(time.time() - start_time) < capture_duration ):
|
| 40 |
+
_, frame = camera.read()
|
| 41 |
+
frame=cv2.flip(frame,1)
|
| 42 |
+
labels = []
|
| 43 |
+
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
|
| 44 |
+
faces = face_classifier.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5, minSize=(30, 30), flags=cv2.CASCADE_SCALE_IMAGE)
|
| 45 |
+
|
| 46 |
+
for (x,y,w,h) in faces:
|
| 47 |
+
cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,255),2)
|
| 48 |
+
roi_gray = gray[y:y+h,x:x+w]
|
| 49 |
+
roi_gray = cv2.resize(roi_gray,(48,48),interpolation=cv2.INTER_AREA)
|
| 50 |
+
|
| 51 |
+
if np.sum([roi_gray])!=0:
|
| 52 |
+
roi = roi_gray.astype('float')/255.0
|
| 53 |
+
roi = img_to_array(roi)
|
| 54 |
+
roi = np.expand_dims(roi,axis=0)
|
| 55 |
+
prediction = classifier.predict(roi)[0]
|
| 56 |
+
global label
|
| 57 |
+
label=emotion_labels[prediction.argmax()]
|
| 58 |
+
label_position = (x,y)
|
| 59 |
+
cv2.putText(frame,label,label_position,cv2.FONT_HERSHEY_SIMPLEX,1,(0,255,0),2)
|
| 60 |
+
print(label)
|
| 61 |
+
ret,buffer=cv2.imencode('.jpg',frame)
|
| 62 |
+
frame=buffer.tobytes()
|
| 63 |
+
cv2.waitKey(0)
|
| 64 |
+
yield(b'--frame\r\n'b'Content-Type: image/jpeg\r\n\r\n'+ frame + b'\r\n\r\n')
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
@app.route('/')
|
| 68 |
+
def main():
|
| 69 |
+
return render_template('main.html')
|
| 70 |
+
|
| 71 |
+
@app.route('/index')
|
| 72 |
+
def index():
|
| 73 |
+
return render_template('index.html')
|
| 74 |
+
|
| 75 |
+
@app.route('/video')
|
| 76 |
+
def video():
|
| 77 |
+
return Response(generate_frames(),mimetype='multipart/x-mixed-replace; boundary=frame')
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
######### Code for displaying Videos or Music ##################
|
| 81 |
+
@app.route('/result',methods = ["POST", "GET"])
|
| 82 |
+
|
| 83 |
+
def playlistmanager():
|
| 84 |
+
option = request.form['btnradio']
|
| 85 |
+
print("option is "+option)
|
| 86 |
+
try:
|
| 87 |
+
label
|
| 88 |
+
except NameError:
|
| 89 |
+
return Response("Emotion was not detected properly")
|
| 90 |
+
else:
|
| 91 |
+
input=[]
|
| 92 |
+
input=label
|
| 93 |
+
print(input)
|
| 94 |
+
###playlist selection###
|
| 95 |
+
if input=='Happy':
|
| 96 |
+
if option=="video":
|
| 97 |
+
playlist_id=secret.happy
|
| 98 |
+
else:
|
| 99 |
+
playlist_id=secret.mhappy
|
| 100 |
+
|
| 101 |
+
elif input=='Sad':
|
| 102 |
+
if option=="video":
|
| 103 |
+
playlist_id=secret.sad
|
| 104 |
+
else:
|
| 105 |
+
playlist_id=secret.msad
|
| 106 |
+
elif input=='Neutral':
|
| 107 |
+
if option=="video":
|
| 108 |
+
playlist_id=secret.neutral
|
| 109 |
+
else:
|
| 110 |
+
playlist_id=secret.mneutral
|
| 111 |
+
elif input=='Angry':
|
| 112 |
+
if option=="video":
|
| 113 |
+
playlist_id=secret.angry
|
| 114 |
+
else:
|
| 115 |
+
playlist_id=secret.mangry
|
| 116 |
+
elif input=='Surprise':
|
| 117 |
+
if option=="video":
|
| 118 |
+
playlist_id=secret.surprise
|
| 119 |
+
else:
|
| 120 |
+
playlist_id=secret.msurprise
|
| 121 |
+
elif input=='Fear':
|
| 122 |
+
if option=="video":
|
| 123 |
+
playlist_id=secret.fear
|
| 124 |
+
else:
|
| 125 |
+
playlist_id=secret.mfear
|
| 126 |
+
else:
|
| 127 |
+
return Response("Enter valid keyword")
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
if option =="video":
|
| 131 |
+
######### Youtube Code ###########
|
| 132 |
+
search_url='https://www.googleapis.com/youtube/v3/playlistItems'
|
| 133 |
+
search_params={
|
| 134 |
+
'key':secret.youtube_key,
|
| 135 |
+
# 'q':input + 'short movie',
|
| 136 |
+
'playlistId':playlist_id,
|
| 137 |
+
'part':'contentDetails',
|
| 138 |
+
'maxResults':10,
|
| 139 |
+
}
|
| 140 |
+
|
| 141 |
+
r=requests.get(search_url,params=search_params)
|
| 142 |
+
results=r.json()['items']
|
| 143 |
+
# print(results)
|
| 144 |
+
video_ids=[]
|
| 145 |
+
for result in results:
|
| 146 |
+
video_ids.append(result['contentDetails']['videoId'])
|
| 147 |
+
# print(video_ids)
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
return render_template('results.html',input=input,video_ids=video_ids)
|
| 151 |
+
|
| 152 |
+
else:
|
| 153 |
+
########## Spotify Code ##########
|
| 154 |
+
user_id=secret.user_id
|
| 155 |
+
print("refreshing token")
|
| 156 |
+
refreshCaller=Refresh()
|
| 157 |
+
spotify_token=refreshCaller.refresh()
|
| 158 |
+
print(spotify_token)
|
| 159 |
+
query="https://api.spotify.com/v1/playlists/{}/tracks".format(playlist_id)
|
| 160 |
+
response=requests.get(query,
|
| 161 |
+
headers={"Content-Type": "application/json",
|
| 162 |
+
"Authorization": "Bearer {}".format(spotify_token)})
|
| 163 |
+
response_json=response.json()['items']
|
| 164 |
+
# print(response_json)
|
| 165 |
+
music_ids=[]
|
| 166 |
+
for music in response_json:
|
| 167 |
+
music_ids.append(music['track']['id'])
|
| 168 |
+
# print(music_ids)
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
return render_template('music.html',input=input,music_ids=music_ids)
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
|
| 177 |
+
|
| 178 |
+
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
if __name__ == '__main__':
|
| 182 |
+
app.run(debug=True)
|
haarcascade_frontalface_default.xml
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
refresh.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import secret
|
| 3 |
+
|
| 4 |
+
class Refresh:
|
| 5 |
+
|
| 6 |
+
def __init__(self):
|
| 7 |
+
self.refresh_token=secret.refresh_token
|
| 8 |
+
self.base_64=secret.base_64
|
| 9 |
+
|
| 10 |
+
def refresh(self):
|
| 11 |
+
query="https://accounts.spotify.com/api/token"
|
| 12 |
+
response = requests.post(query,
|
| 13 |
+
data={"grant_type":"refresh_token","refresh_token":secret.refresh_token},
|
| 14 |
+
headers={"Authorization":"Basic "+ secret.base_64})
|
| 15 |
+
return(response.json()["access_token"])
|
| 16 |
+
|
requirement.txt
ADDED
|
Binary file (2.16 kB). View file
|
|
|