Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
220 changes: 220 additions & 0 deletions code/new_models/app_local_streamlit_deepface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
# Required versions:
# protobuf==3.20.3, tensorflow==2.10

import streamlit as st
import cv2
from deepface import DeepFace
import numpy as np
import webbrowser
import requests
import re
import os
import time

# App config
st.set_page_config(page_title="Emotion-Based Music Player", layout="centered")
st.title("Facial Emotion Recognition App")
st.write("This app detects your facial expression, displays the predicted emotion, and plays a suitable song.")

#Footer
st.markdown(
"""
<style>
.footer {
position: fixed;
left: 0;
bottom: 0;
width: 100%;
background-color: #f1f1f1;
color: #000000;
text-align: center;
padding: 12px;
font-size: 15px;
box-shadow: 0 -1px 4px rgba(0,0,0,0.1);
border-top: 1px solid #ccc;
margin-top: 50px;
}
</style>

<div class="footer">
<a href="https://github.com/SGCODEX/Music-Recommendation-Using-Facial-Expressions.git" style="color: #0072E3; text-decoration: underline;">
Project by SGCODEX. Visit us and give this project a ⭐. Proudly part of open source programs like SWOC, IEEE-IGDTUW, GSSOC and more!!
</a>
</div>
""",
unsafe_allow_html=True
)

#st.title("🧠 Detected Emotion KPI")

# App state
if "last_emotion" not in st.session_state:
st.session_state.last_emotion = "Neutral"
if "show_video" not in st.session_state:
st.session_state.show_video = False

# Function to detect emotion
def detect_emotion(frame):
try:
results = DeepFace.analyze(frame, actions=["emotion"], enforce_detection=False)

# Handle single or multiple face results
if not isinstance(results, list):
results = [results]

for result in results:
emotion = result['dominant_emotion']
region = result['region']
x, y, w, h = region['x'], region['y'], region['w'], region['h']

# Draw bounding box around the detected face
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)

# label the detected emotion above the face
cv2.putText(frame, emotion, (x, y - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 255, 0), 2)

# Assume first emotion as dominant for app state
st.session_state.last_emotion = results[0]['dominant_emotion']

except Exception as e:
emotion = st.session_state.last_emotion
print("DeepFace failed:", e)

return frame, st.session_state.last_emotion

# ------------------------------
# πŸŽ₯ Live Camera Detection Mode
# ------------------------------
if not st.session_state.show_video:
col1, col2 = st.columns([1, 2])

with col1:
st.subheader("πŸ“Š Current Emotion")
emotion_placeholder = st.empty()

cap = cv2.VideoCapture(0)
capture = False

with col2:
st.subheader("πŸ“· Live Feed")
image_placeholder = st.empty()
st.markdown("<br>", unsafe_allow_html=True)
capture = st.button("🎡 Play Song on Captured Emotion")


st.markdown("---")

while cap.isOpened() and not capture:
ret, frame = cap.read()
if not ret:
break

frame = cv2.resize(frame, (320, 240))
frame, detected_emotion = detect_emotion(frame)
st.session_state.last_emotion = detected_emotion

# Update live KPI and video feed

emotion_colors = {
"Happy": "#DFF2BF",
"Sad": "#FFBABA",
"Angry": "#FFAAAA",
"Surprise": "#FFFFBA",
"Neutral": "#E0E0E0",
"Fear": "#D0BAFF",
"Disgust": "#B0FFBA"
}

bg_color = emotion_colors.get(detected_emotion, "#f9fff9")

emotion_placeholder.markdown(
f"""
<div style="
display: inline-block;
padding: 10px 24px;
border: 2px solid #000000;
border-radius: 14px;
background-color: {bg_color};
font-size: 20px;
font-weight: 600;
color: #333;
text-align: center;
margin-top: 10px;
min-width: 120px;
">
{detected_emotion}
</div>
""",
unsafe_allow_html=True
)


image_placeholder.image(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB), channels="RGB")

if cv2.waitKey(1) & 0xFF == ord('q'):
break
time.sleep(0.1) # Limit refresh rate

cap.release()
st.session_state.show_video = True # switch mode

# ------------------------------
# 🎧 Play Song For Detected Mood
# ------------------------------
if st.session_state.show_video:
st.markdown("## 🎧 Now Playing Music For Your Mood")
st.markdown(f"**Detected Mood:** `{st.session_state.last_emotion}`")

if st.button("πŸ” Detect Emotions Again"):
st.session_state.show_video = False
st.rerun()

search_query = f"https://www.youtube.com/results?search_query={st.session_state.last_emotion}+background+tunes"

# to fetch the search results page
response = requests.get(search_query)

# HTTP status code 200 = request was successful
if response.status_code != 200:
print("Failed to retrieve YouTube search results. Status code:", response.status_code)

html_content = response.text

match = re.search(r'/watch\?v=([^\"]+)', html_content)
if match:
video_id = match.group(1)
#video_url = f"https://www.youtube.com/watch?v={video_id}"
video_url = f"https://www.youtube.com/watch?v={video_id.encode('utf-8').decode('unicode_escape')}"

# printing the video URL for debugging purposes
st.video(video_url)
print("Opening YouTube video:", video_url)


# Benchmark: Compare DeepFace Inference Time (Optional)
with st.expander("πŸ“Š Benchmark Inference Time"):
if st.button("πŸ“Έ Capture Frame and Benchmark DeepFace"):
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
cap.release()

if ret:
start_time = time.time()
try:
result = DeepFace.analyze(frame, actions=["emotion"], enforce_detection=False)
end_time = time.time()

detected_emotion = result[0]['dominant_emotion']
st.image(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB), caption="Captured Frame", channels="RGB")
st.success(f"βœ… DeepFace Inference Time: `{round(end_time - start_time, 3)}s`")
st.write("🎭 Detected Emotion:", detected_emotion)
except Exception as e:
st.error(f"❌ DeepFace failed: {str(e)}")
else:
st.error("❌ Failed to capture frame from webcam.")





Loading