Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app_gradio.py
|
2 |
+
import gradio as gr
|
3 |
+
import numpy as np
|
4 |
+
import os
|
5 |
+
import yaml
|
6 |
+
from dotenv import load_dotenv
|
7 |
+
import io
|
8 |
+
from scipy.io.wavfile import read as read_wav
|
9 |
+
|
10 |
+
# Correctly import from the drive_paddy package structure
|
11 |
+
from src.detection.factory import get_detector
|
12 |
+
from src.alerting.alert_system import get_alerter
|
13 |
+
|
14 |
+
# --- Load Configuration and Environment Variables ---
|
15 |
+
# This part is the same as our Streamlit app
|
16 |
+
load_dotenv()
|
17 |
+
config_path = 'config.yaml'
|
18 |
+
with open(config_path, 'r') as f:
|
19 |
+
config = yaml.safe_load(f)
|
20 |
+
secrets = {
|
21 |
+
"gemini_api_key": os.getenv("GEMINI_API_KEY"),
|
22 |
+
}
|
23 |
+
|
24 |
+
# --- Initialize Backend Components ---
|
25 |
+
# We create these once and reuse them.
|
26 |
+
detector = get_detector(config)
|
27 |
+
alerter = get_alerter(config, secrets["gemini_api_key"])
|
28 |
+
|
29 |
+
# --- Audio Processing for Gradio ---
|
30 |
+
# Gradio's gr.Audio component needs a specific format: (sample_rate, numpy_array)
|
31 |
+
def process_audio_for_gradio(audio_bytes):
|
32 |
+
"""Converts in-memory audio bytes to a format Gradio can play."""
|
33 |
+
# gTTS creates MP3, so we read it as such
|
34 |
+
byte_io = io.BytesIO(audio_bytes)
|
35 |
+
# The 'read' function from scipy.io.wavfile expects a WAV file.
|
36 |
+
# We need to first convert the MP3 bytes from gTTS to WAV bytes.
|
37 |
+
# This requires pydub.
|
38 |
+
try:
|
39 |
+
from pydub import AudioSegment
|
40 |
+
audio = AudioSegment.from_mp3(byte_io)
|
41 |
+
wav_byte_io = io.BytesIO()
|
42 |
+
audio.export(wav_byte_io, format="wav")
|
43 |
+
wav_byte_io.seek(0)
|
44 |
+
|
45 |
+
sample_rate, data = read_wav(wav_byte_io)
|
46 |
+
return (sample_rate, data)
|
47 |
+
except Exception as e:
|
48 |
+
print(f"Could not process audio for Gradio: {e}")
|
49 |
+
return None
|
50 |
+
|
51 |
+
# --- Main Processing Function for Gradio ---
|
52 |
+
# This function is the core of the app. It takes a webcam frame and returns
|
53 |
+
# updates for all the output components.
|
54 |
+
def process_live_frame(frame):
|
55 |
+
"""
|
56 |
+
Takes a single frame from the Gradio webcam input, processes it,
|
57 |
+
and returns the processed frame, status text, and any audio alerts.
|
58 |
+
"""
|
59 |
+
if frame is None:
|
60 |
+
# Return default values if frame is None
|
61 |
+
blank_image = np.zeros((480, 640, 3), dtype=np.uint8)
|
62 |
+
return blank_image, "Status: Inactive", None
|
63 |
+
|
64 |
+
# Process the frame using our existing detector
|
65 |
+
processed_frame, indicators, _ = detector.process_frame(frame)
|
66 |
+
drowsiness_level = indicators.get("drowsiness_level", "Awake")
|
67 |
+
lighting = indicators.get("lighting", "Good")
|
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
|
79 |
+
if drowsiness_level != "Awake":
|
80 |
+
audio_data = alerter.trigger_alert(level=drowsiness_level)
|
81 |
+
if audio_data:
|
82 |
+
audio_output = process_audio_for_gradio(audio_data)
|
83 |
+
else:
|
84 |
+
alerter.reset_alert()
|
85 |
+
|
86 |
+
# Return all the values needed to update the UI
|
87 |
+
return processed_frame, status_text, audio_output
|
88 |
+
|
89 |
+
# --- Gradio UI Definition ---
|
90 |
+
with gr.Blocks(theme=gr.themes.Default(primary_hue="blue", secondary_hue="blue")) as app:
|
91 |
+
gr.Markdown("# π Drive Paddy - Drowsiness Detection (Gradio)")
|
92 |
+
gr.Markdown("A live test using Gradio's webcam component. This can be more stable than WebRTC in some environments.")
|
93 |
+
|
94 |
+
with gr.Row():
|
95 |
+
with gr.Column():
|
96 |
+
# Input: Live webcam feed
|
97 |
+
webcam_input = gr.Image(sources=["webcam"], streaming=True, label="Live Camera Feed")
|
98 |
+
with gr.Column():
|
99 |
+
# Output 1: Processed video feed
|
100 |
+
processed_output = gr.Image(label="Processed Feed")
|
101 |
+
# Output 2: Live status text
|
102 |
+
status_output = gr.Textbox(label="Live Status", lines=3, interactive=False)
|
103 |
+
# Output 3: Hidden audio player for alerts
|
104 |
+
audio_alert_output = gr.Audio(autoplay=True, visible=False)
|
105 |
+
|
106 |
+
# Link the input to the processing function and the function to the outputs
|
107 |
+
webcam_input.stream(
|
108 |
+
fn=process_live_frame,
|
109 |
+
inputs=[webcam_input],
|
110 |
+
outputs=[processed_output, status_output, audio_alert_output]
|
111 |
+
)
|
112 |
+
|
113 |
+
# --- Launch the App ---
|
114 |
+
if __name__ == "__main__":
|
115 |
+
app.launch(debug=True)
|