Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from PIL import Image
|
2 |
+
import numpy as np
|
3 |
+
import mediapipe as mp
|
4 |
+
import tensorflow as tf
|
5 |
+
import webbrowser
|
6 |
+
import streamlit as st
|
7 |
+
|
8 |
+
# Load model and labels
|
9 |
+
model = tf.keras.models.load_model("model.h5")
|
10 |
+
label = np.load("labels.npy")
|
11 |
+
|
12 |
+
# MediaPipe setup
|
13 |
+
holistic = mp.solutions.holistic
|
14 |
+
holis = holistic.Holistic()
|
15 |
+
drawing = mp.solutions.drawing_utils
|
16 |
+
|
17 |
+
# Streamlit UI setup
|
18 |
+
st.header("Fun Zone ! Capture your Emote !ππππ")
|
19 |
+
|
20 |
+
# Session state for managing emotion detection
|
21 |
+
if "emotion" not in st.session_state:
|
22 |
+
st.session_state["emotion"] = ""
|
23 |
+
|
24 |
+
|
25 |
+
# Function to process image and predict emotion
|
26 |
+
def process_image(image):
|
27 |
+
res = holis.process(np.array(image))
|
28 |
+
lst = []
|
29 |
+
|
30 |
+
if res.face_landmarks:
|
31 |
+
for i in res.face_landmarks.landmark:
|
32 |
+
lst.append(i.x - res.face_landmarks.landmark[1].x)
|
33 |
+
lst.append(i.y - res.face_landmarks.landmark[1].y)
|
34 |
+
|
35 |
+
if res.left_hand_landmarks:
|
36 |
+
for i in res.left_hand_landmarks.landmark:
|
37 |
+
lst.append(i.x - res.left_hand_landmarks.landmark[8].x)
|
38 |
+
lst.append(i.y - res.left_hand_landmarks.landmark[8].y)
|
39 |
+
else:
|
40 |
+
for _ in range(42):
|
41 |
+
lst.append(0.0)
|
42 |
+
|
43 |
+
if res.right_hand_landmarks:
|
44 |
+
for i in res.right_hand_landmarks.landmark:
|
45 |
+
lst.append(i.x - res.right_hand_landmarks.landmark[8].x)
|
46 |
+
lst.append(i.y - res.right_hand_landmarks.landmark[8].y)
|
47 |
+
else:
|
48 |
+
for _ in range(42):
|
49 |
+
lst.append(0.0)
|
50 |
+
|
51 |
+
lst = np.array(lst).reshape(1, -1)
|
52 |
+
pred = label[np.argmax(model.predict(lst))]
|
53 |
+
return res, pred
|
54 |
+
return res, ""
|
55 |
+
|
56 |
+
|
57 |
+
lang = st.text_input("Language")
|
58 |
+
singer = st.text_input("Cast")
|
59 |
+
|
60 |
+
# Upload button for capturing an image
|
61 |
+
uploaded_file = st.file_uploader("Capture your image", type=["jpg", "jpeg", "png"])
|
62 |
+
|
63 |
+
if uploaded_file:
|
64 |
+
# Process uploaded image using Pillow
|
65 |
+
img = Image.open(uploaded_file)
|
66 |
+
# Convert Pillow image to numpy array
|
67 |
+
image_np = np.array(img)
|
68 |
+
|
69 |
+
# Process image and predict emotion
|
70 |
+
res, emotion = process_image(image_np)
|
71 |
+
|
72 |
+
# Draw landmarks onto the image
|
73 |
+
if res.face_landmarks:
|
74 |
+
drawing.draw_landmarks(image_np, res.face_landmarks, holistic.FACEMESH_TESSELATION,
|
75 |
+
landmark_drawing_spec=drawing.DrawingSpec(color=(0, 0, 255), thickness=1, circle_radius=1),
|
76 |
+
connection_drawing_spec=drawing.DrawingSpec(color=(0, 255, 0), thickness=1))
|
77 |
+
if res.left_hand_landmarks:
|
78 |
+
drawing.draw_landmarks(image_np, res.left_hand_landmarks, mp.solutions.hands.HAND_CONNECTIONS)
|
79 |
+
if res.right_hand_landmarks:
|
80 |
+
drawing.draw_landmarks(image_np, res.right_hand_landmarks, mp.solutions.hands.HAND_CONNECTIONS)
|
81 |
+
|
82 |
+
# Display processed image
|
83 |
+
st.image(image_np, caption='Uploaded Image', use_column_width=True)
|
84 |
+
st.session_state["emotion"] = emotion
|
85 |
+
|
86 |
+
if emotion:
|
87 |
+
st.success(f"Emotion detected: {emotion}")
|
88 |
+
|
89 |
+
btn = st.button("Recommend me movies")
|
90 |
+
|
91 |
+
if btn:
|
92 |
+
if not st.session_state["emotion"]:
|
93 |
+
st.warning("Please upload an image to capture your emotion first")
|
94 |
+
else:
|
95 |
+
emotion = st.session_state["emotion"]
|
96 |
+
webbrowser.open(f"https://www.youtube.com/results?search_query={lang}+{emotion}+movie+{singer}")
|
97 |
+
st.session_state["emotion"] = "" # Reset emotion after recommendation
|