Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -68,11 +68,25 @@ def process_live_frame(frame):
|
|
68 |
score = indicators.get("details", {}).get("Score", 0)
|
69 |
|
70 |
# Build the status text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
status_text = f"Lighting: {lighting}\n"
|
72 |
if lighting == "Low":
|
73 |
status_text += "Detection paused due to low light."
|
74 |
else:
|
75 |
-
status_text += f"Status: {drowsiness_level}\nScore: {score:.2f}"
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
# Handle alerts
|
78 |
audio_output = None
|
@@ -83,6 +97,7 @@ def process_live_frame(frame):
|
|
83 |
else:
|
84 |
alerter.reset_alert()
|
85 |
|
|
|
86 |
return processed_frame, status_text, audio_output
|
87 |
|
88 |
# --- UI Definition for the Live Detection Page ---
|
@@ -98,12 +113,23 @@ def create_live_detection_page():
|
|
98 |
status_output = gr.Textbox(label="Live Status", lines=3, interactive=False)
|
99 |
# Audio player is now visible for debugging and user feedback.
|
100 |
audio_alert_output = gr.Audio(autoplay=True, visible=True, label="Alert Sound")
|
101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
webcam_input.stream(
|
103 |
fn=process_live_frame,
|
104 |
-
inputs=[webcam_input],
|
105 |
-
outputs=[processed_output, status_output, audio_alert_output]
|
106 |
-
|
|
|
107 |
return live_detection_page
|
108 |
|
109 |
# --- UI Definition for the Home Page ---
|
|
|
68 |
score = indicators.get("details", {}).get("Score", 0)
|
69 |
|
70 |
# Build the status text
|
71 |
+
# Determine drowsiness level based on the UI slider's value
|
72 |
+
drowsiness_level = "Awake"
|
73 |
+
if score >= VERY_DROWSY_DEFAULT: # Use a fixed upper threshold
|
74 |
+
drowsiness_level = "Very Drowsy"
|
75 |
+
elif score >= sensitivity_threshold: # Use the slider for slight drowsiness
|
76 |
+
drowsiness_level = "Slightly Drowsy"
|
77 |
+
|
78 |
+
# Build the status text with explicit details
|
79 |
status_text = f"Lighting: {lighting}\n"
|
80 |
if lighting == "Low":
|
81 |
status_text += "Detection paused due to low light."
|
82 |
else:
|
83 |
+
status_text += f"Status: {drowsiness_level}\nScore: {score:.2f} (Threshold: {sensitivity_threshold:.2f})"
|
84 |
+
# Explicitly show what is being detected
|
85 |
+
if score > 0:
|
86 |
+
if indicators.get('eye_closure'): status_text += "\n- Eyes Closed Detected"
|
87 |
+
if indicators.get('yawning'): status_text += "\n- Yawn Detected"
|
88 |
+
if indicators.get('head_nod'): status_text += "\n- Head Nod Detected"
|
89 |
+
if indicators.get('looking_away'): status_text += "\n- Looking Away Detected"
|
90 |
|
91 |
# Handle alerts
|
92 |
audio_output = None
|
|
|
97 |
else:
|
98 |
alerter.reset_alert()
|
99 |
|
100 |
+
# Return all the values needed to update the UI
|
101 |
return processed_frame, status_text, audio_output
|
102 |
|
103 |
# --- UI Definition for the Live Detection Page ---
|
|
|
113 |
status_output = gr.Textbox(label="Live Status", lines=3, interactive=False)
|
114 |
# Audio player is now visible for debugging and user feedback.
|
115 |
audio_alert_output = gr.Audio(autoplay=True, visible=True, label="Alert Sound")
|
116 |
+
# --- Added Sensitivity Slider ---
|
117 |
+
sensitivity_slider = gr.Slider(
|
118 |
+
minimum=0.1,
|
119 |
+
maximum=1.0,
|
120 |
+
value=SLIGHTLY_DROWSY_DEFAULT,
|
121 |
+
step=0.05,
|
122 |
+
label="Alert Sensitivity Threshold",
|
123 |
+
info="Lower value = more sensitive to drowsiness signs."
|
124 |
+
)
|
125 |
+
|
126 |
+
# Link the inputs (webcam and slider) to the processing function and its outputs
|
127 |
webcam_input.stream(
|
128 |
fn=process_live_frame,
|
129 |
+
inputs=[webcam_input, sensitivity_slider],
|
130 |
+
outputs=[processed_output, status_output, audio_alert_output],
|
131 |
+
every=0.1
|
132 |
+
)
|
133 |
return live_detection_page
|
134 |
|
135 |
# --- UI Definition for the Home Page ---
|