Skip to content

Commit

Permalink
Initial files related to project. 1st Interation
Browse files Browse the repository at this point in the history
  • Loading branch information
saadkhi committed Jul 28, 2024
1 parent 45d37dd commit addf9fc
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 0 deletions.
95 changes: 95 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -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)
Binary file added emotion.npy
Binary file not shown.
Binary file added labels.npy
Binary file not shown.
Binary file added model.h5
Binary file not shown.
29 changes: 29 additions & 0 deletions static/css/style.css
Original file line number Diff line number Diff line change
@@ -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;
}
Binary file added static/images/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/images/p1.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/images/p2.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
66 changes: 66 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Emotion Based Music Recommender</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<link rel="stylesheet" href="{{ url_for('static', filename='css/styles.css') }}">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">EmoEnt</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link" href="#">Sign Up</a>
</li>
</ul>
</div>
</nav>

<div class="container">
<div class="jumbotron mt-5">
<h1 class="display-4">Emotion Based Music Recommender</h1>
<p class="lead">Let us recommend you music based on your current emotion.</p>
<hr class="my-4">
<form method="post" class="form-inline justify-content-center">
<div class="form-group mx-sm-3 mb-2">
<label for="language" class="sr-only">Language</label>
<input type="text" class="form-control" id="language" name="language" placeholder="Language" value="{{ language }}" required>
</div>
<div class="form-group mx-sm-3 mb-2">
<label for="singer" class="sr-only">Singer</label>
<input type="text" class="form-control" id="singer" name="singer" placeholder="Singer" value="{{ singer }}" required>
</div>
<button type="submit" name="capture" class="btn btn-primary mb-2 mx-sm-3">Capture Emotion</button>
<button type="submit" name="recommend" class="btn btn-success mb-2">Recommend me songs</button>
</form>
{% if warning %}
<div class="alert alert-warning mt-3" role="alert">
{{ warning }}
</div>
{% endif %}
</div>
</div>

<footer class="footer mt-auto py-3 bg-light">
<div class="container text-center">
<div class="social-icons mb-3">
<a href="#" class="text-dark mx-2"><i class="fab fa-facebook-f"></i></a>
<a href="#" class="text-dark mx-2"><i class="fab fa-twitter"></i></a>
<a href="#" class="text-dark mx-2"><i class="fab fa-instagram"></i></a>
</div>
<p>Created by Your Name and Collaborators</p>
</div>
</footer>

<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<script src="https://kit.fontawesome.com/a076d05399.js" crossorigin="anonymous"></script>
</body>
</html>

0 comments on commit addf9fc

Please sign in to comment.