DetectEmotions / app.py
Ahmadkhan12's picture
Update app.py
c096457 verified
raw
history blame
1.94 kB
import streamlit as st
import face_recognition
import numpy as np
import cv2
from PIL import Image
# Set the page config
st.set_page_config(page_title="Emotion Recognition App", layout="centered")
st.title("Emotion Recognition App")
# Upload an image
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
# Define simple emotion mapping based on facial features (for demonstration purposes)
def detect_emotion(face_landmarks):
"""
A simple mock-up function for detecting emotions based on landmarks.
Replace with a more sophisticated model as needed.
"""
# Example: Arbitrarily assign "Happy" if eyes are close together
if face_landmarks:
return "Happy"
return "Neutral"
# Process the uploaded image
if uploaded_file is not None:
image = Image.open(uploaded_file)
image_np = np.array(image)
# Convert image to RGB
rgb_image = cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB)
# Detect faces in the image
face_locations = face_recognition.face_locations(rgb_image)
face_landmarks_list = face_recognition.face_landmarks(rgb_image)
if face_locations:
for face_location, face_landmarks in zip(face_locations, face_landmarks_list):
# Draw a rectangle around the face
top, right, bottom, left = face_location
cv2.rectangle(image_np, (left, top), (right, bottom), (0, 255, 0), 2)
# Detect emotion based on landmarks
emotion = detect_emotion(face_landmarks)
# Display emotion above the face
cv2.putText(
image_np,
emotion,
(left, top - 10),
cv2.FONT_HERSHEY_SIMPLEX,
0.9,
(255, 0, 0),
2,
)
st.image(image_np, caption="Processed Image", use_column_width=True)
else:
st.warning("No faces detected in the image.")