HFUsman commited on
Commit
519af5a
·
verified ·
1 Parent(s): bcdc4d7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -58
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
- import tensorflow as tf
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 load_stress_model():
12
- return load_model("stress_model.h5") # Replace with your actual model file
 
13
 
14
- model = load_stress_model()
 
15
 
16
- # Mediapipe face detection setup
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 detects stress levels in real-time using your webcam.")
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
- cap = cv2.VideoCapture(0) # Open webcam
 
 
32
 
33
- while cap.isOpened():
 
 
34
  ret, frame = cap.read()
35
  if not ret:
36
- st.write("Failed to grab frame.")
37
  break
38
 
39
- # Flip and process the frame
40
- frame = cv2.flip(frame, 1)
41
- rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
42
- results = face_mesh.process(rgb_frame)
43
-
44
- # Draw face mesh
45
- if results.multi_face_landmarks:
46
- for face_landmarks in results.multi_face_landmarks:
47
- mp.solutions.drawing_utils.draw_landmarks(
48
- image=frame,
49
- landmark_list=face_landmarks,
50
- connections=mp_face_mesh.FACEMESH_TESSELATION,
51
- landmark_drawing_spec=None,
52
- connection_drawing_spec=mp.solutions.drawing_styles.get_default_face_mesh_tesselation_style()
53
- )
54
-
55
- # Resize for model prediction
56
- img_resized = cv2.resize(rgb_frame, (224, 224)) / 255.0
57
- img_resized = np.expand_dims(img_resized, axis=0)
58
-
59
- # Model prediction
60
- prediction = model.predict(img_resized)
61
- stress_level = np.argmax(prediction)
62
-
63
- # Display stress level
64
- cv2.putText(frame, f"Stress Level: {stress_level}", (10, 30),
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
- cv2.destroyAllWindows()
 
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.")