Rohit001 commited on
Commit
1a11d2f
1 Parent(s): 03f2d9d

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +75 -0
  2. requirement.txt +4 -0
app.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask,render_template
2
+ from flask_socketio import SocketIO,emit
3
+ import base64
4
+ import numpy as np
5
+ import cv2
6
+ import time
7
+ from deepface import DeepFace
8
+
9
+
10
+ app = Flask(__name__)
11
+ app.config['SECRET_KEY'] = 'secret!'
12
+ socket = SocketIO(app,async_mode="eventlet")
13
+
14
+
15
+ def base64_to_image(base64_string):
16
+ # Extract the base64 encoded binary data from the input string
17
+ base64_data = base64_string.split(",")[1]
18
+ # Decode the base64 data to bytes
19
+ image_bytes = base64.b64decode(base64_data)
20
+ # Convert the bytes to numpy array
21
+ image_array = np.frombuffer(image_bytes, dtype=np.uint8)
22
+ # Decode the numpy array as an image using OpenCV
23
+ image = cv2.imdecode(image_array, cv2.IMREAD_COLOR)
24
+ return image
25
+
26
+ def music_link(emo):
27
+ if emo == "fear":
28
+ 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>'
29
+ elif emo == "angry":
30
+ 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>'
31
+ elif emo == 'neutral':
32
+ 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>'
33
+ elif emo =='sad':
34
+ 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>'
35
+ elif emo == 'disgust':
36
+ 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>'
37
+ elif emo == 'happy':
38
+ 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>'
39
+ elif emo == 'surprise':
40
+ 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>'
41
+ else:
42
+ 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>'
43
+
44
+ return res
45
+
46
+
47
+
48
+
49
+ @socket.on("connect")
50
+ def test_connect():
51
+ print("Connected")
52
+ emit("my response", {"data": "Connected"})
53
+
54
+ @socket.on("image")
55
+ def receive_image(image):
56
+ # Decode the base64-encoded image data
57
+ image = base64_to_image(image)
58
+ image = cv2.resize(image, (224, 224), interpolation=cv2.INTER_AREA)
59
+ # emit("processed_image", image)
60
+ # Predicts the model
61
+ cv2.imwrite("./res.jpg",image)
62
+ objs = DeepFace.analyze(img_path = "./res.jpg",
63
+ actions = ['emotion'])
64
+ time.sleep(3)
65
+ emo = objs[0]['dominant_emotion']
66
+ res = music_link(emo)
67
+ emit("result",{"emo":str(emo),"res":res})
68
+
69
+ @app.route("/")
70
+ def home():
71
+ return render_template("index.html")
72
+
73
+ if __name__ == '__main__':
74
+ # app.run(debug=True)
75
+ socket.run(app, debug=True,port=8080)
requirement.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ Flask-SocketIO
2
+ Flask
3
+ numpy
4
+ deepface