Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,106 +1,98 @@
|
|
1 |
# app_gradio.py
|
2 |
-
import gradio as gr
|
3 |
-
import numpy as np
|
4 |
-
import torch
|
5 |
-
import os, yaml, soundfile as sf
|
6 |
-
from dotenv import load_dotenv
|
7 |
-
from threading import Thread
|
8 |
-
import logging
|
9 |
-
import time
|
10 |
-
|
11 |
-
|
12 |
-
# --- TTS & AI Imports ---
|
13 |
-
# from parler_tts import ParlerTTSForConditionalGeneration
|
14 |
-
# from transformers import AutoTokenizer, AutoFeatureExtractor
|
15 |
-
# from streamer import ParlerTTSStreamer # local file
|
16 |
-
|
17 |
-
from src.detection.factory import get_detector
|
18 |
-
|
19 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
20 |
-
#
|
|
|
21 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
22 |
-
|
|
|
|
|
|
|
23 |
|
|
|
24 |
logging.basicConfig(
|
25 |
level=logging.INFO,
|
26 |
-
format="%(asctime)s
|
27 |
datefmt="%H:%M:%S",
|
28 |
)
|
29 |
|
|
|
|
|
|
|
|
|
30 |
|
31 |
-
|
32 |
-
config = yaml.safe_load(f)
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
print("Initializing detector and alerter β¦")
|
37 |
-
detector = get_detector(config)
|
38 |
-
|
39 |
-
path = config["alerting"]["alert_sound_path"]
|
40 |
-
with open(path, "rb") as f:
|
41 |
-
audio_bytes = f.read()
|
42 |
-
print(f"[AlertSystem] loaded sound: {audio_bytes}")
|
43 |
-
|
44 |
-
|
45 |
-
if audio_bytes is None:
|
46 |
-
logging.warning("No alert sound loaded; driver will not hear any audio!")
|
47 |
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
|
|
|
|
|
|
50 |
|
51 |
-
#
|
52 |
-
# FRAME PROCESSOR
|
53 |
-
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
54 |
def process_live_frame(frame):
|
|
|
|
|
55 |
if frame is None:
|
56 |
return np.zeros((480, 640, 3), np.uint8), "Status: Inactive", None
|
57 |
|
58 |
-
t0 = time.
|
59 |
-
|
60 |
-
processed, indicators, _ = detector.process_frame(frame)
|
61 |
-
level = indicators.get("drowsiness_level", "Awake")
|
62 |
-
lighting = indicators.get("lighting", "Good")
|
63 |
-
score = indicators.get("details", {}).get("Score", 0)
|
64 |
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
66 |
logging.info(f"{dt_ms:6.1f} ms β {lighting:<4} β {level:<14} β score={score:.2f}")
|
67 |
|
68 |
-
status_txt =
|
69 |
-
|
70 |
-
|
71 |
-
|
|
|
72 |
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
|
|
|
|
|
|
|
|
|
|
78 |
|
79 |
return processed, status_txt, audio_out
|
80 |
|
81 |
-
#
|
82 |
-
|
83 |
-
#
|
84 |
-
|
85 |
-
gr.Markdown("# π Drive Paddy β Drowsiness Detection")
|
86 |
-
gr.Markdown("Live detection with real-time voice alerts.")
|
87 |
|
88 |
with gr.Row():
|
89 |
with gr.Column(scale=2):
|
90 |
-
|
91 |
-
label="Live Camera Feed")
|
92 |
with gr.Column(scale=1):
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
|
|
|
|
104 |
|
105 |
if __name__ == "__main__":
|
106 |
logging.info("Launching Gradio app β¦")
|
|
|
1 |
# app_gradio.py
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
3 |
+
# Webcam β geometric detector β static WAV alert (with cooldown)
|
4 |
+
# Live console logs of per-frame latency + status.
|
5 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
6 |
+
import time, os, yaml, logging, numpy as np, gradio as gr, soundfile as sf
|
7 |
+
from dotenv import load_dotenv
|
8 |
+
|
9 |
+
from src.detection.factory import get_detector # your existing factory
|
10 |
|
11 |
+
# βββββββββββββββββββββββββββββ logging
|
12 |
logging.basicConfig(
|
13 |
level=logging.INFO,
|
14 |
+
format="%(asctime)s β %(message)s",
|
15 |
datefmt="%H:%M:%S",
|
16 |
)
|
17 |
|
18 |
+
# βββββββββββββββββββββββββββββ config / detector
|
19 |
+
load_dotenv()
|
20 |
+
with open("config.yaml") as f:
|
21 |
+
CFG = yaml.safe_load(f)
|
22 |
|
23 |
+
detector = get_detector(CFG)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
+
# βββββββββββββββββββββββββββββ alert sound (read once)
|
26 |
+
wav_path = CFG["alerting"]["alert_sound_path"]
|
27 |
+
try:
|
28 |
+
ALERT_SR, ALERT_DATA = sf.read(wav_path, dtype="float32") # (sr, np.ndarray)
|
29 |
+
logging.info(f"Loaded alert sound: {wav_path} ({len(ALERT_DATA)/ALERT_SR:.2f}s)")
|
30 |
+
except Exception as e:
|
31 |
+
ALERT_SR, ALERT_DATA = None, None
|
32 |
+
logging.warning(f"Failed to load alert sound: {e}")
|
33 |
|
34 |
+
# βββββββββββββββββββββββββββββ simple cooldown
|
35 |
+
ALERT_COOLDOWN = CFG["alerting"].get("alert_cooldown_seconds", 7)
|
36 |
+
_last_alert_ts = 0.0
|
37 |
|
38 |
+
# βββββββββββββββββββββββββββββ frame callback
|
|
|
|
|
39 |
def process_live_frame(frame):
|
40 |
+
global _last_alert_ts
|
41 |
+
|
42 |
if frame is None:
|
43 |
return np.zeros((480, 640, 3), np.uint8), "Status: Inactive", None
|
44 |
|
45 |
+
t0 = time.perf_counter()
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
+
processed, indic, _ = detector.process_frame(frame)
|
48 |
+
level = indic.get("drowsiness_level", "Awake")
|
49 |
+
lighting = indic.get("lighting", "Good")
|
50 |
+
score = indic.get("details", {}).get("Score", 0.0)
|
51 |
+
|
52 |
+
dt_ms = (time.perf_counter() - t0) * 1000.0
|
53 |
logging.info(f"{dt_ms:6.1f} ms β {lighting:<4} β {level:<14} β score={score:.2f}")
|
54 |
|
55 |
+
status_txt = (
|
56 |
+
f"Lighting: {lighting}\n"
|
57 |
+
+ ("Detection paused β low light." if lighting == "Low"
|
58 |
+
else f"Status: {level}\nScore: {score:.2f}")
|
59 |
+
)
|
60 |
|
61 |
+
# decide whether to play the alert
|
62 |
+
audio_out = None
|
63 |
+
if (
|
64 |
+
ALERT_DATA is not None
|
65 |
+
and level != "Awake"
|
66 |
+
and lighting != "Low"
|
67 |
+
and (time.time() - _last_alert_ts) > ALERT_COOLDOWN
|
68 |
+
):
|
69 |
+
_last_alert_ts = time.time()
|
70 |
+
audio_out = (ALERT_SR, ALERT_DATA.copy()) # hand a fresh copy to Gradio
|
71 |
|
72 |
return processed, status_txt, audio_out
|
73 |
|
74 |
+
# βββββββββββββββββββββββββββββ UI
|
75 |
+
with gr.Blocks(title="Drive Paddy β Drowsiness Detection") as app:
|
76 |
+
gr.Markdown("# π **Drive Paddy** β Static-file Alert Demo")
|
77 |
+
gr.Markdown("Webcam-based drowsiness detection Β· console shows real-time logs.")
|
|
|
|
|
78 |
|
79 |
with gr.Row():
|
80 |
with gr.Column(scale=2):
|
81 |
+
cam = gr.Image(sources=["webcam"], streaming=True, label="Live Camera Feed")
|
|
|
82 |
with gr.Column(scale=1):
|
83 |
+
out_img = gr.Image(label="Processed Feed")
|
84 |
+
out_text = gr.Textbox(label="Live Status", lines=3, interactive=False)
|
85 |
+
out_audio = gr.Audio(
|
86 |
+
label="Alert",
|
87 |
+
autoplay=True,
|
88 |
+
type="numpy", # expects (sr, np.ndarray)
|
89 |
+
visible=True,
|
90 |
+
height=60,
|
91 |
+
)
|
92 |
+
|
93 |
+
cam.stream(fn=process_live_frame,
|
94 |
+
inputs=cam,
|
95 |
+
outputs=[out_img, out_text, out_audio])
|
96 |
|
97 |
if __name__ == "__main__":
|
98 |
logging.info("Launching Gradio app β¦")
|