diff --git a/app.py b/app.py new file mode 100644 index 0000000..a441bd0 --- /dev/null +++ b/app.py @@ -0,0 +1,95 @@ +from flask import Flask, render_template, request, redirect, url_for, session +import cv2 +import numpy as np +import mediapipe as mp +from keras.models import load_model +import webbrowser +import os + +app = Flask(__name__) +app.secret_key = 'supersecretkey' # Set a secret key for session management + +model = load_model("model.h5") +label = np.load("labels.npy") +holistic = mp.solutions.holistic +hands = mp.solutions.hands +holis = holistic.Holistic() +drawing = mp.solutions.drawing_utils + +# Ensure emotion.npy exists +if not os.path.exists("emotion.npy"): + np.save("emotion.npy", np.array([""])) + +@app.route('/', methods=['GET', 'POST']) +def index(): + try: + emotion = np.load("emotion.npy")[0] + except FileNotFoundError: + emotion = "" + + lang = session.get('language', '') + singer = session.get('singer', '') + + if request.method == 'POST': + lang = request.form['language'] + singer = request.form['singer'] + session['language'] = lang + session['singer'] = singer + + if 'capture' in request.form: + capture_emotion() + elif 'recommend' in request.form: + emotion = np.load("emotion.npy")[0] + if not emotion: + warning = "Please let me capture your emotion first" + return render_template('index.html', warning=warning, language=lang, singer=singer) + else: + webbrowser.open(f"https://www.youtube.com/results?search_query={lang}+{emotion}+song+{singer}") + np.save("emotion.npy", np.array([""])) + session.pop('language', None) + session.pop('singer', None) + emotion = "" + + return render_template('index.html', emotion=emotion, language=lang, singer=singer) + +def capture_emotion(): + cap = cv2.VideoCapture(0) + while True: + ret, frame = cap.read() + if not ret: + break + frame = cv2.flip(frame, 1) + res = holis.process(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) + lst = [] + + if res.face_landmarks: + for i in res.face_landmarks.landmark: + lst.append(i.x - res.face_landmarks.landmark[1].x) + lst.append(i.y - res.face_landmarks.landmark[1].y) + + if res.left_hand_landmarks: + for i in res.left_hand_landmarks.landmark: + lst.append(i.x - res.left_hand_landmarks.landmark[8].x) + lst.append(i.y - res.left_hand_landmarks.landmark[8].y) + else: + for i in range(42): + lst.append(0.0) + + if res.right_hand_landmarks: + for i in res.right_hand_landmarks.landmark: + lst.append(i.x - res.right_hand_landmarks.landmark[8].x) + lst.append(i.y - res.right_hand_landmarks.landmark[8].y) + else: + for i in range(42): + lst.append(0.0) + + lst = np.array(lst).reshape(1, -1) + pred = label[np.argmax(model.predict(lst))] + np.save("emotion.npy", np.array([pred])) + break + + cap.release() + cv2.destroyAllWindows() + +if __name__ == '__main__': + app.run(debug=True) diff --git a/emotion.npy b/emotion.npy new file mode 100644 index 0000000..bf1612a Binary files /dev/null and b/emotion.npy differ diff --git a/labels.npy b/labels.npy new file mode 100644 index 0000000..4dd5b2f Binary files /dev/null and b/labels.npy differ diff --git a/model.h5 b/model.h5 new file mode 100644 index 0000000..1761732 Binary files /dev/null and b/model.h5 differ diff --git a/static/css/style.css b/static/css/style.css new file mode 100644 index 0000000..d47f410 --- /dev/null +++ b/static/css/style.css @@ -0,0 +1,29 @@ +body { + background-color: #f8f9fa; + display: flex; + flex-direction: column; + min-height: 100vh; +} + +.jumbotron { + background-color: #ffffff; + border: 1px solid #e3e6ea; + box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); +} + +.form-control { + width: 200px; +} + +.footer { + background-color: #f8f9fa; + border-top: 1px solid #e3e6ea; + padding: 10px 0; + position: absolute; + bottom: 0; + width: 100%; +} + +.social-icons i { + font-size: 1.5em; +} diff --git a/static/images/logo.png b/static/images/logo.png new file mode 100644 index 0000000..310c740 Binary files /dev/null and b/static/images/logo.png differ diff --git a/static/images/p1.jpeg b/static/images/p1.jpeg new file mode 100644 index 0000000..f7e8754 Binary files /dev/null and b/static/images/p1.jpeg differ diff --git a/static/images/p2.jpeg b/static/images/p2.jpeg new file mode 100644 index 0000000..388d78f Binary files /dev/null and b/static/images/p2.jpeg differ diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..cb97b0f --- /dev/null +++ b/templates/index.html @@ -0,0 +1,66 @@ + + + + + + Emotion Based Music Recommender + + + + + + +
+
+

Emotion Based Music Recommender

+

Let us recommend you music based on your current emotion.

+
+
+
+ + +
+
+ + +
+ + +
+ {% if warning %} + + {% endif %} +
+
+ + + + + + + + +