Testys commited on
Commit
935769d
Β·
verified Β·
1 Parent(s): 054723b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -75
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
- # CONFIG & BACKEND SET-UP
 
21
  # ──────────────────────────────────────────────────────────
22
- load_dotenv()
 
 
 
23
 
 
24
  logging.basicConfig(
25
  level=logging.INFO,
26
- format="%(asctime)s %(levelname)s β”‚ %(message)s",
27
  datefmt="%H:%M:%S",
28
  )
29
 
 
 
 
 
30
 
31
- with open("config.yaml", "r") as f:
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
- print("Backend ready.")
 
 
 
 
 
 
 
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.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
- dt_ms = (time.time() - t0) * 1000.0
 
 
 
 
 
66
  logging.info(f"{dt_ms:6.1f} ms β”‚ {lighting:<4} β”‚ {level:<14} β”‚ score={score:.2f}")
67
 
68
- status_txt = f"Lighting: {lighting}\n"
69
- status_txt += ("Detection paused due to low light."
70
- if lighting == "Low"
71
- else f"Status: {level}\nScore: {score:.2f}")
 
72
 
73
- if level != "Awake" and lighting != "Low":
74
- audio_bytes = audio_bytes
75
- logging.info(f"Printing {audio_bytes}")
76
- if audio_bytes:
77
- audio_out = audio_bytes
 
 
 
 
 
78
 
79
  return processed, status_txt, audio_out
80
 
81
- # ──────────────────────────────────────────────────────────
82
- # GRADIO UI
83
- # ──────────────────────────────────────────────────────────
84
- with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as app:
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
- webcam = gr.Image(sources=["webcam"], streaming=True,
91
- label="Live Camera Feed")
92
  with gr.Column(scale=1):
93
- processed_img = gr.Image(label="Processed Feed")
94
- status_box = gr.Textbox(label="Live Status", lines=3, interactive=False)
95
- alert_audio = gr.Audio(label="Alert",
96
- autoplay=True,
97
- streaming=True)
98
-
99
- webcam.stream(
100
- fn=process_live_frame,
101
- inputs=webcam,
102
- outputs=[processed_img, status_box, alert_audio],
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 …")