Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,49 +1,75 @@
|
|
| 1 |
-
import cv2
|
| 2 |
import streamlit as st
|
|
|
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
-
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
try:
|
| 11 |
-
import groq
|
| 12 |
-
except ImportError:
|
| 13 |
-
subprocess.check_call([sys.executable, "-m", "pip", "install", "groq==0.13.1"])
|
| 14 |
|
|
|
|
|
|
|
|
|
|
| 15 |
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
|
| 20 |
-
|
| 21 |
-
st.
|
| 22 |
-
st.
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
stframe = st.empty()
|
| 26 |
-
cap = cv2.VideoCapture(0)
|
| 27 |
|
| 28 |
-
|
| 29 |
-
st.error("Could not access the camera.")
|
| 30 |
-
else:
|
| 31 |
-
while True:
|
| 32 |
ret, frame = cap.read()
|
| 33 |
if not ret:
|
| 34 |
-
st.
|
| 35 |
break
|
| 36 |
-
|
| 37 |
-
#
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
stframe.image(frame, channels="BGR")
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
cap.release()
|
| 48 |
cv2.destroyAllWindows()
|
| 49 |
-
|
|
|
|
|
|
|
| 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()
|
|
|