Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,75 +1,59 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import cv2
|
| 3 |
-
import mediapipe as mp
|
| 4 |
import numpy as np
|
| 5 |
-
|
| 6 |
-
from tensorflow.keras.models import load_model
|
| 7 |
-
from PIL import Image
|
| 8 |
|
| 9 |
-
# Load the pre-trained model
|
| 10 |
@st.cache_resource
|
| 11 |
-
def
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
-
|
|
|
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
mp_face_mesh = mp.solutions.face_mesh
|
| 18 |
-
face_mesh = mp_face_mesh.FaceMesh(min_detection_confidence=0.5, min_tracking_confidence=0.5)
|
| 19 |
-
|
| 20 |
-
# Streamlit app UI
|
| 21 |
st.title("Real-Time Stress Detection")
|
| 22 |
-
st.write("This app
|
| 23 |
-
|
| 24 |
-
# Start webcam
|
| 25 |
-
run_camera = st.button("Start Webcam")
|
| 26 |
-
|
| 27 |
-
if run_camera:
|
| 28 |
-
st.write("Press 'q' to stop the camera.")
|
| 29 |
-
stframe = st.empty() # Placeholder for video frames
|
| 30 |
|
| 31 |
-
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
|
|
|
|
|
|
| 34 |
ret, frame = cap.read()
|
| 35 |
if not ret:
|
| 36 |
-
st.
|
| 37 |
break
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
#
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
#
|
| 64 |
-
|
| 65 |
-
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA)
|
| 66 |
-
|
| 67 |
-
# Stream to Streamlit
|
| 68 |
-
stframe.image(frame, channels="BGR")
|
| 69 |
-
|
| 70 |
-
# Break on 'q' key
|
| 71 |
-
if cv2.waitKey(1) & 0xFF == ord('q'):
|
| 72 |
break
|
| 73 |
|
| 74 |
cap.release()
|
| 75 |
-
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import cv2
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
+
from transformers import pipeline
|
|
|
|
|
|
|
| 5 |
|
|
|
|
| 6 |
@st.cache_resource
|
| 7 |
+
def load_emotion_model():
|
| 8 |
+
# Load emotion recognition pipeline
|
| 9 |
+
return pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base")
|
| 10 |
|
| 11 |
+
# Load the model
|
| 12 |
+
emotion_model = load_emotion_model()
|
| 13 |
|
| 14 |
+
# Streamlit UI
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
st.title("Real-Time Stress Detection")
|
| 16 |
+
st.write("This app uses a Hugging Face emotion recognition model to estimate stress levels based on webcam input.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
# Activate webcam
|
| 19 |
+
run = st.checkbox("Run Webcam")
|
| 20 |
+
frame_placeholder = st.empty()
|
| 21 |
|
| 22 |
+
if run:
|
| 23 |
+
cap = cv2.VideoCapture(0) # Start webcam
|
| 24 |
+
while True:
|
| 25 |
ret, frame = cap.read()
|
| 26 |
if not ret:
|
| 27 |
+
st.error("Failed to access webcam.")
|
| 28 |
break
|
| 29 |
|
| 30 |
+
# Process frame for display
|
| 31 |
+
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 32 |
+
frame_placeholder.image(frame_rgb, channels="RGB")
|
| 33 |
+
|
| 34 |
+
# Mock text-based emotion input for testing
|
| 35 |
+
# Replace this with input from a more suitable sensor or feature extractor
|
| 36 |
+
emotion_input = "I'm feeling anxious and stressed."
|
| 37 |
+
|
| 38 |
+
# Predict emotion
|
| 39 |
+
predictions = emotion_model(emotion_input)
|
| 40 |
+
predicted_emotion = predictions[0]["label"]
|
| 41 |
+
st.write(f"Predicted Emotion: {predicted_emotion}")
|
| 42 |
+
|
| 43 |
+
# Map emotion to stress level (example logic)
|
| 44 |
+
stress_mapping = {
|
| 45 |
+
"joy": "Low Stress",
|
| 46 |
+
"sadness": "High Stress",
|
| 47 |
+
"anger": "High Stress",
|
| 48 |
+
"fear": "High Stress",
|
| 49 |
+
"neutral": "Moderate Stress"
|
| 50 |
+
}
|
| 51 |
+
stress_level = stress_mapping.get(predicted_emotion, "Unknown")
|
| 52 |
+
st.write(f"Estimated Stress Level: {stress_level}")
|
| 53 |
+
|
| 54 |
+
# Break on user input
|
| 55 |
+
if st.button("Stop"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
break
|
| 57 |
|
| 58 |
cap.release()
|
| 59 |
+
st.write("Webcam stopped.")
|