HFUsman commited on
Commit
76c4abc
·
verified ·
1 Parent(s): 0601601

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -32
app.py CHANGED
@@ -1,49 +1,75 @@
1
- import cv2
2
  import streamlit as st
 
 
3
  import numpy as np
4
- from some_model_library import load_model, predict_stress
 
 
5
 
6
- import os
7
- import subprocess
 
 
8
 
9
- # Install groq dynamically
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
- # Load pre-trained model
18
- model = load_model('path_to_your_model')
19
 
20
- # Streamlit interface
21
- st.title("Real-Time Stress Level Detection")
22
- st.text("Using your laptop camera to detect stress levels in real-time.")
23
 
24
- # Initialize webcam
25
- stframe = st.empty()
26
- cap = cv2.VideoCapture(0)
27
 
28
- if not cap.isOpened():
29
- st.error("Could not access the camera.")
30
- else:
31
- while True:
32
  ret, frame = cap.read()
33
  if not ret:
34
- st.error("Failed to read from camera.")
35
  break
36
-
37
- # Process the frame (resize, detect face, etc.)
38
- processed_frame = preprocess_frame(frame)
39
-
40
- # Predict stress level
41
- stress_level = predict_stress(model, processed_frame)
42
-
43
- # Display results
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  stframe.image(frame, channels="BGR")
45
- st.metric("Stress Level", stress_level)
 
 
 
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()