Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -135,33 +135,93 @@ def process_live_frame(frame):
|
|
| 135 |
else:
|
| 136 |
return processed, status_txt, None
|
| 137 |
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
#
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
if __name__ == "__main__":
|
| 167 |
logging.info("Launching Gradio appβ¦")
|
|
|
|
| 135 |
else:
|
| 136 |
return processed, status_txt, None
|
| 137 |
|
| 138 |
+
|
| 139 |
+
# βββββββββββββββββββββββββββββ UI Definition
|
| 140 |
+
def create_readme_tab():
|
| 141 |
+
"""Creates the content for the 'About' tab."""
|
| 142 |
+
with gr.Blocks(title="Drive Paddy - About Page") as readme_tab:
|
| 143 |
+
gr.Markdown(
|
| 144 |
+
"""
|
| 145 |
+
<div align="center">
|
| 146 |
+
<img src="https://em-content.zobj.net/source/samsung/380/automobile_1f697.png" alt="Car Emoji" width="100"/>
|
| 147 |
+
<h1>Drive Paddy</h1>
|
| 148 |
+
<p><strong>Your Drowsiness Detection Assistant</strong></p>
|
| 149 |
+
</div>
|
| 150 |
+
|
| 151 |
+
---
|
| 152 |
+
|
| 153 |
+
## π Features
|
| 154 |
+
- **Real-Time Webcam Streaming**: Directly processes your live camera feed for immediate feedback.
|
| 155 |
+
- **Efficient Geometric Analysis**: Uses `MediaPipe` for high-performance facial landmark detection.
|
| 156 |
+
- **Multi-Signal Analysis**: Detects eye closure (EAR), yawns (MAR), and head-nodding.
|
| 157 |
+
- **Stateful Alert System**: Plays a clear audio alert for new drowsiness events and intelligently re-arms itself, preventing alert fatigue.
|
| 158 |
+
- **Low-Light Warning**: Automatically detects and warns about poor lighting conditions.
|
| 159 |
+
- **Configurable**: Key detection thresholds and settings can be tuned via `config.yaml`.
|
| 160 |
+
|
| 161 |
+
---
|
| 162 |
+
|
| 163 |
+
## π οΈ How It Works
|
| 164 |
+
1. **Video Streaming**: The `gradio.Image` component captures the camera feed.
|
| 165 |
+
2. **Frame Processing**: Each frame is sent to the `GeometricProcessor`.
|
| 166 |
+
3. **Stateful Alerting**: The `AlertManager` class uses internal state to decide if a *new* alert should be triggered.
|
| 167 |
+
4. **Dynamic Updates**: The processed video, status text, and audio alerts are sent back to the frontend for a seamless real-time experience.
|
| 168 |
|
| 169 |
+
---
|
| 170 |
+
|
| 171 |
+
## π‘ Understanding the Live Status
|
| 172 |
+
The status panel provides real-time feedback on the following parameters:
|
| 173 |
+
|
| 174 |
+
- **`Lighting`**: Indicates the ambient light conditions.
|
| 175 |
+
- `Good`: Sufficient light for reliable detection.
|
| 176 |
+
- `Low`: Insufficient light. Detection is paused as the results would be unreliable.
|
| 177 |
+
|
| 178 |
+
- **`Status`**: The overall assessed level of driver alertness.
|
| 179 |
+
- `Awake`: The driver appears alert.
|
| 180 |
+
- `Slightly Drowsy`: Early signs of fatigue have been detected.
|
| 181 |
+
- `Very Drowsy`: Strong indicators of drowsiness are present. An alert is triggered.
|
| 182 |
+
|
| 183 |
+
- **`Score`**: A numerical value representing the accumulated evidence of drowsiness based on the weighted indicators (eye closure, yawning, head pose). A higher score corresponds to a greater level of detected drowsiness.
|
| 184 |
+
"""
|
| 185 |
+
)
|
| 186 |
+
return readme_tab
|
| 187 |
+
|
| 188 |
+
|
| 189 |
+
# βββββββββββββββββββββββββββββ UI <--- CHANGE
|
| 190 |
+
def create_detection_tab():
|
| 191 |
+
with gr.Blocks(title="Drive Paddy β πΉ Live Drowsiness Detection Tab") as detection_tab:
|
| 192 |
+
gr.Markdown("## πΉ Live Drowsiness Detection")
|
| 193 |
+
gr.Markdown("Press 'START' to activate your camera and begin monitoring. The console will show real-time logs.")
|
| 194 |
+
|
| 195 |
+
with gr.Row():
|
| 196 |
+
with gr.Column(scale=2):
|
| 197 |
+
cam = gr.Image(sources=["webcam"], streaming=True, label="Live Camera Feed")
|
| 198 |
+
with gr.Column(scale=1):
|
| 199 |
+
out_img = gr.Image(label="Processed Feed")
|
| 200 |
+
out_text = gr.Textbox(label="Live Status", lines=3, interactive=False)
|
| 201 |
+
|
| 202 |
+
# This audio component now acts as a placeholder.
|
| 203 |
+
# We make it invisible because we don't need to show the player controls.
|
| 204 |
+
# The backend will dynamically send a new, playable component to it.
|
| 205 |
+
out_audio = gr.Audio(
|
| 206 |
+
label="Alert",
|
| 207 |
+
autoplay=True,
|
| 208 |
+
visible=False, # Hiding the component for a cleaner UX
|
| 209 |
+
)
|
| 210 |
+
|
| 211 |
+
# The gr.State for managing the timestamp is no longer needed, simplifying the stream call.
|
| 212 |
+
cam.stream(
|
| 213 |
+
fn=process_live_frame,
|
| 214 |
+
inputs=[cam],
|
| 215 |
+
outputs=[out_img, out_text, out_audio] # The output now targets the placeholder
|
| 216 |
+
)
|
| 217 |
+
|
| 218 |
+
with gr.Blocks(title="π Drive Paddy β Drowsiness Detection", theme=gr.themes.Soft()) as app:
|
| 219 |
+
gr.Markdown("# π **Drive Paddy**")
|
| 220 |
+
with gr.Tabs():
|
| 221 |
+
with gr.TabItem("Live Detection"):
|
| 222 |
+
create_detection_tab()
|
| 223 |
+
with gr.TabItem("About this App"):
|
| 224 |
+
create_readme_tab()
|
| 225 |
|
| 226 |
if __name__ == "__main__":
|
| 227 |
logging.info("Launching Gradio appβ¦")
|