Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,38 +1,60 @@
|
|
1 |
import streamlit as st
|
2 |
-
|
3 |
-
from PIL import Image
|
4 |
import numpy as np
|
|
|
|
|
5 |
|
6 |
-
#
|
7 |
-
st.
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import face_recognition
|
|
|
3 |
import numpy as np
|
4 |
+
import cv2
|
5 |
+
from PIL import Image
|
6 |
|
7 |
+
# Set the page config
|
8 |
+
st.set_page_config(page_title="Emotion Recognition App", layout="centered")
|
9 |
+
|
10 |
+
st.title("Emotion Recognition App")
|
11 |
+
|
12 |
+
# Upload an image
|
13 |
+
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
14 |
+
|
15 |
+
# Define simple emotion mapping based on facial features (for demonstration purposes)
|
16 |
+
def detect_emotion(face_landmarks):
|
17 |
+
"""
|
18 |
+
A simple mock-up function for detecting emotions based on landmarks.
|
19 |
+
Replace with a more sophisticated model as needed.
|
20 |
+
"""
|
21 |
+
# Example: Arbitrarily assign "Happy" if eyes are close together
|
22 |
+
if face_landmarks:
|
23 |
+
return "Happy"
|
24 |
+
return "Neutral"
|
25 |
+
|
26 |
+
# Process the uploaded image
|
27 |
+
if uploaded_file is not None:
|
28 |
+
image = Image.open(uploaded_file)
|
29 |
+
image_np = np.array(image)
|
30 |
+
|
31 |
+
# Convert image to RGB
|
32 |
+
rgb_image = cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB)
|
33 |
+
|
34 |
+
# Detect faces in the image
|
35 |
+
face_locations = face_recognition.face_locations(rgb_image)
|
36 |
+
face_landmarks_list = face_recognition.face_landmarks(rgb_image)
|
37 |
+
|
38 |
+
if face_locations:
|
39 |
+
for face_location, face_landmarks in zip(face_locations, face_landmarks_list):
|
40 |
+
# Draw a rectangle around the face
|
41 |
+
top, right, bottom, left = face_location
|
42 |
+
cv2.rectangle(image_np, (left, top), (right, bottom), (0, 255, 0), 2)
|
43 |
+
|
44 |
+
# Detect emotion based on landmarks
|
45 |
+
emotion = detect_emotion(face_landmarks)
|
46 |
+
|
47 |
+
# Display emotion above the face
|
48 |
+
cv2.putText(
|
49 |
+
image_np,
|
50 |
+
emotion,
|
51 |
+
(left, top - 10),
|
52 |
+
cv2.FONT_HERSHEY_SIMPLEX,
|
53 |
+
0.9,
|
54 |
+
(255, 0, 0),
|
55 |
+
2,
|
56 |
+
)
|
57 |
+
|
58 |
+
st.image(image_np, caption="Processed Image", use_column_width=True)
|
59 |
+
else:
|
60 |
+
st.warning("No faces detected in the image.")
|