Spaces:
Runtime error
Runtime error
from flask import Flask,render_template | |
from flask_socketio import SocketIO,emit | |
import base64 | |
import numpy as np | |
import cv2 | |
import time | |
from deepface import DeepFace | |
app = Flask(__name__) | |
app.config['SECRET_KEY'] = 'secret!' | |
socket = SocketIO(app,async_mode="eventlet") | |
def base64_to_image(base64_string): | |
# Extract the base64 encoded binary data from the input string | |
base64_data = base64_string.split(",")[1] | |
# Decode the base64 data to bytes | |
image_bytes = base64.b64decode(base64_data) | |
# Convert the bytes to numpy array | |
image_array = np.frombuffer(image_bytes, dtype=np.uint8) | |
# Decode the numpy array as an image using OpenCV | |
image = cv2.imdecode(image_array, cv2.IMREAD_COLOR) | |
return image | |
def music_link(emo): | |
if emo == "fear": | |
res = '<iframe style="border-radius:12px" src="https://open.spotify.com/embed/playlist/54i4ygGRUiT04Ro6ZcsXqo?utm_source=generator&theme=0" width="100%" height="352" frameBorder="0" allowfullscreen="" allow="autoplay; clipboard-write; encrypted-media; fullscreen; picture-in-picture" loading="lazy"></iframe>' | |
elif emo == "angry": | |
res = '<iframe style="border-radius:12px" src="https://open.spotify.com/embed/playlist/0dsl5hbFdVT7scb7Vakkwa?utm_source=generator" width="100%" height="352" frameBorder="0" allowfullscreen="" allow="autoplay; clipboard-write; encrypted-media; fullscreen; picture-in-picture" loading="lazy"></iframe>' | |
elif emo == 'neutral': | |
res = '<iframe style="border-radius:12px" src="https://open.spotify.com/embed/playlist/37i9dQZEVXbLZ52XmnySJg?utm_source=generator" width="100%" height="352" frameBorder="0" allowfullscreen="" allow="autoplay; clipboard-write; encrypted-media; fullscreen; picture-in-picture" loading="lazy"></iframe>' | |
elif emo =='sad': | |
res= '<iframe style="border-radius:12px" src="https://open.spotify.com/embed/playlist/37i9dQZF1EVKuMoAJjoTIw?utm_source=generator" width="100%" height="352" frameBorder="0" allowfullscreen="" allow="autoplay; clipboard-write; encrypted-media; fullscreen; picture-in-picture" loading="lazy"></iframe>' | |
elif emo == 'disgust': | |
res = '<iframe style="border-radius:12px" src="https://open.spotify.com/embed/playlist/3a8ssl2IKbhSmEzzIPYvbC?utm_source=generator" width="100%" height="352" frameBorder="0" allowfullscreen="" allow="autoplay; clipboard-write; encrypted-media; fullscreen; picture-in-picture" loading="lazy"></iframe>' | |
elif emo == 'happy': | |
res = '<iframe style="border-radius:12px" src="https://open.spotify.com/embed/playlist/37i9dQZF1EVJSvZp5AOML2?utm_source=generator" width="100%" height="352" frameBorder="0" allowfullscreen="" allow="autoplay; clipboard-write; encrypted-media; fullscreen; picture-in-picture" loading="lazy"></iframe>' | |
elif emo == 'surprise': | |
res = '<iframe style="border-radius:12px" src="https://open.spotify.com/embed/playlist/37i9dQZF1DX5cZuAHLNjGz?utm_source=generator" width="100%" height="352" frameBorder="0" allowfullscreen="" allow="autoplay; clipboard-write; encrypted-media; fullscreen; picture-in-picture" loading="lazy"></iframe>' | |
else: | |
res = '<iframe style="border-radius:12px" src="https://open.spotify.com/embed/playlist/37i9dQZEVXbMDoHDwVN2tF?utm_source=generator" width="100%" height="352" frameBorder="0" allowfullscreen="" allow="autoplay; clipboard-write; encrypted-media; fullscreen; picture-in-picture" loading="lazy"></iframe>' | |
return res | |
def test_connect(): | |
print("Connected") | |
emit("my response", {"data": "Connected"}) | |
def receive_image(image): | |
# Decode the base64-encoded image data | |
image = base64_to_image(image) | |
image = cv2.resize(image, (224, 224), interpolation=cv2.INTER_AREA) | |
# emit("processed_image", image) | |
# Predicts the model | |
cv2.imwrite("./res.jpg",image) | |
objs = DeepFace.analyze(img_path = "./res.jpg", | |
actions = ['emotion']) | |
time.sleep(3) | |
emo = objs[0]['dominant_emotion'] | |
res = music_link(emo) | |
emit("result",{"emo":str(emo),"res":res}) | |
def home(): | |
return render_template("index.html") | |
if __name__ == '__main__': | |
# app.run(debug=True) | |
socket.run(app, debug=True,port=7860,host="0.0.0.0") | |