Spaces:
Sleeping
Upload 2 files
Browse filesEmotion Detection App, a cutting-edge tool designed to analyze and detect human emotions effortlessly. Whether you want to process images, analyze videos, or capture live emotions through your camera, this app delivers fast and accurate results with a user-friendly interface.
Features:-
➤Detect emotions in real-time using advanced AI models.
➤Upload images or play videos to analyze emotional expressions.
➤Real-time emotion recognition through live camera feeds.
➤Light and dark mode compatibility for a personalized experience.
➤Elegant signature branding, adding a touch of sophistication to the app.
With its sleek design and advanced capabilities, the Emotion Detection App is perfect for anyone looking to explore the fascinating world of emotions, whether for personal, educational, or professional purposes.
- app.py +125 -0
- requirements.txt +7 -0
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import cv2
|
3 |
+
import torch
|
4 |
+
from PIL import Image
|
5 |
+
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
6 |
+
|
7 |
+
# Load processor and model
|
8 |
+
processor = AutoImageProcessor.from_pretrained("RickyIG/emotion_face_image_classification")
|
9 |
+
model = AutoModelForImageClassification.from_pretrained("RickyIG/emotion_face_image_classification")
|
10 |
+
|
11 |
+
# Title of the Streamlit app
|
12 |
+
st.title("Emotion Detection App")
|
13 |
+
|
14 |
+
# Option to choose between uploading image, video, or using live camera
|
15 |
+
option = st.radio("Select an option", ("Upload Image", "Upload Video", "Use Live Camera"))
|
16 |
+
|
17 |
+
if option == "Upload Image":
|
18 |
+
# Upload image
|
19 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
20 |
+
|
21 |
+
if uploaded_file is not None:
|
22 |
+
# Display the uploaded image
|
23 |
+
image = Image.open(uploaded_file)
|
24 |
+
st.image(image, caption="Uploaded Image", use_container_width=True)
|
25 |
+
|
26 |
+
# Preprocess the image
|
27 |
+
inputs = processor(images=image, return_tensors="pt")
|
28 |
+
|
29 |
+
# Make predictions
|
30 |
+
with torch.no_grad():
|
31 |
+
outputs = model(**inputs)
|
32 |
+
logits = outputs.logits # raw model outputs (before softmax)
|
33 |
+
predicted_class_idx = logits.argmax(-1).item() # predicted class index
|
34 |
+
|
35 |
+
# Get the label of the predicted class
|
36 |
+
label = model.config.id2label[predicted_class_idx]
|
37 |
+
|
38 |
+
# Display the result
|
39 |
+
st.write(f"Predicted Emotion: {label}")
|
40 |
+
|
41 |
+
elif option == "Upload Video":
|
42 |
+
# Upload video file
|
43 |
+
uploaded_video = st.file_uploader("Choose a video...", type=["mp4", "avi", "mov"])
|
44 |
+
|
45 |
+
if uploaded_video is not None:
|
46 |
+
# Save video to a temporary path
|
47 |
+
temp_video_path = "/tmp/uploaded_video.mp4"
|
48 |
+
with open(temp_video_path, "wb") as f:
|
49 |
+
f.write(uploaded_video.read())
|
50 |
+
|
51 |
+
# Open the video using OpenCV
|
52 |
+
cap = cv2.VideoCapture(temp_video_path)
|
53 |
+
|
54 |
+
if not cap.isOpened():
|
55 |
+
st.error("Error: Could not open video.")
|
56 |
+
else:
|
57 |
+
stframe = st.empty() # Placeholder to display live video feed
|
58 |
+
|
59 |
+
while True:
|
60 |
+
# Capture frame-by-frame
|
61 |
+
ret, frame = cap.read()
|
62 |
+
|
63 |
+
if not ret:
|
64 |
+
break
|
65 |
+
|
66 |
+
# Convert frame (BGR) to RGB (PIL format)
|
67 |
+
image = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
68 |
+
|
69 |
+
# Preprocess the image
|
70 |
+
inputs = processor(images=image, return_tensors="pt")
|
71 |
+
|
72 |
+
# Make predictions
|
73 |
+
with torch.no_grad():
|
74 |
+
outputs = model(**inputs)
|
75 |
+
logits = outputs.logits
|
76 |
+
predicted_class_idx = logits.argmax(-1).item()
|
77 |
+
label = model.config.id2label[predicted_class_idx]
|
78 |
+
|
79 |
+
# Display the result
|
80 |
+
cv2.putText(frame, f"Emotion: {label}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA)
|
81 |
+
|
82 |
+
# Convert the frame to RGB for Streamlit
|
83 |
+
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
84 |
+
stframe.image(frame_rgb, channels="RGB", use_container_width=True)
|
85 |
+
|
86 |
+
cap.release()
|
87 |
+
|
88 |
+
elif option == "Use Live Camera":
|
89 |
+
cap = cv2.VideoCapture(0)
|
90 |
+
|
91 |
+
if not cap.isOpened():
|
92 |
+
st.error("Error: Could not open webcam.")
|
93 |
+
else:
|
94 |
+
stframe = st.empty()
|
95 |
+
|
96 |
+
while True:
|
97 |
+
ret, frame = cap.read()
|
98 |
+
|
99 |
+
if not ret:
|
100 |
+
break
|
101 |
+
|
102 |
+
# Convert frame (BGR) to RGB (PIL format)
|
103 |
+
image = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
104 |
+
|
105 |
+
# Preprocess the image
|
106 |
+
inputs = processor(images=image, return_tensors="pt")
|
107 |
+
|
108 |
+
# Make predictions
|
109 |
+
with torch.no_grad():
|
110 |
+
outputs = model(**inputs)
|
111 |
+
logits = outputs.logits
|
112 |
+
predicted_class_idx = logits.argmax(-1).item()
|
113 |
+
label = model.config.id2label[predicted_class_idx]
|
114 |
+
|
115 |
+
# Display the result
|
116 |
+
cv2.putText(frame, f"Emotion: {label}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA)
|
117 |
+
|
118 |
+
# Convert the frame to RGB for Streamlit
|
119 |
+
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
120 |
+
stframe.image(frame_rgb, channels="RGB", use_container_width=True)
|
121 |
+
|
122 |
+
cap.release()
|
123 |
+
|
124 |
+
# Add a dynamic signature with Bastliga font
|
125 |
+
st.markdown("<br><br><h5 style='text-align: center;'>Developed by M.Nabeel</h5>", unsafe_allow_html=True)
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit==1.41.1
|
2 |
+
opencv-python==4.10.0.84
|
3 |
+
torch==2.5.1
|
4 |
+
transformers==4.33.2
|
5 |
+
Pillow==11.0.0
|
6 |
+
torch==2.5.1
|
7 |
+
tokenizers==0.13.3
|