Testys commited on
Commit
d023885
Β·
verified Β·
1 Parent(s): 447da48

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -16
app.py CHANGED
@@ -37,7 +37,7 @@ class AlertManager:
37
  def __init__(self, config):
38
  self.cooldown_seconds = config.get("alert_cooldown_seconds", 5)
39
  self.last_alert_time = 0
40
- self.is_alert_active = False
41
  self._load_sound(config.get("alert_sound_path"))
42
 
43
  def _load_sound(self, wav_path):
@@ -56,23 +56,27 @@ class AlertManager:
56
  self.alert_data = None
57
 
58
  def trigger_alert(self, level, lighting):
59
- if self.is_alert_active:
60
- if level == "Awake":
61
- logging.info("βœ… Alert state reset. User is Awake. Re-arming system.")
62
- self.is_alert_active = False
63
- return None
64
-
65
  is_drowsy = level != "Awake"
66
  is_good_light = lighting != "Low"
67
- is_ready = (time.monotonic() - self.last_alert_time) > self.cooldown_seconds
68
 
69
- if self.alert_data is not None and is_drowsy and is_good_light and is_ready:
70
- self.last_alert_time = time.monotonic()
71
- self.is_alert_active = True
72
- logging.info("πŸ”Š Drowsiness detected! Firing alert.")
73
- return (self.sample_rate, self.alert_data.copy())
 
 
 
 
 
 
 
74
  return None
75
 
 
76
  alert_manager = AlertManager(CFG.get("alerting", {}))
77
 
78
  # ───────────────────────────── Frame Processing for Tab 1 (Image Stream) - UPDATED
@@ -82,7 +86,7 @@ def process_live_frame(frame):
82
 
83
  t0 = time.perf_counter()
84
  try:
85
- # --- FIX: Call with draw_visuals=True ---
86
  processed, indic = detector.process_frame(frame, draw_visuals=True)
87
  except Exception as e:
88
  logging.error(f"Error processing frame: {e}")
@@ -103,7 +107,6 @@ def process_live_frame(frame):
103
  return processed, status_txt, gr.Audio(value=audio_payload, autoplay=True) if audio_payload else None
104
 
105
 
106
- # ───────────────────────────── Frame Processing for Tab 2 (Analysis-Only) - UPDATED
107
  def process_for_stats_only(frame):
108
  """
109
  Processes a frame but does not return any video/image output.
@@ -114,7 +117,7 @@ def process_for_stats_only(frame):
114
 
115
  t0 = time.perf_counter()
116
  try:
117
- # --- FIX: Call with draw_visuals=False. The first returned value will be None. ---
118
  _, indic = detector.process_frame(frame, draw_visuals=False)
119
  except Exception as e:
120
  logging.error(f"Error processing frame: {e}")
 
37
  def __init__(self, config):
38
  self.cooldown_seconds = config.get("alert_cooldown_seconds", 5)
39
  self.last_alert_time = 0
40
+ # --- FIX: The is_alert_active flag is no longer needed ---
41
  self._load_sound(config.get("alert_sound_path"))
42
 
43
  def _load_sound(self, wav_path):
 
56
  self.alert_data = None
57
 
58
  def trigger_alert(self, level, lighting):
59
+ """
60
+ Triggers a repeating alert if drowsiness persists, governed by a cooldown.
61
+ """
 
 
 
62
  is_drowsy = level != "Awake"
63
  is_good_light = lighting != "Low"
 
64
 
65
+ # --- FIX: New simplified logic for repeating alerts ---
66
+ # Check for drowsiness conditions first.
67
+ if is_drowsy and is_good_light and self.alert_data is not None:
68
+ # Then, check if the cooldown period has passed since the last alert.
69
+ cooldown_passed = (time.monotonic() - self.last_alert_time) > self.cooldown_seconds
70
+ if cooldown_passed:
71
+ # If conditions are met, fire the alert and reset the timer.
72
+ self.last_alert_time = time.monotonic()
73
+ logging.info(f"πŸ”Š Drowsiness alert! Repeating every {self.cooldown_seconds}s until 'Awake'.")
74
+ return (self.sample_rate, self.alert_data.copy())
75
+
76
+ # If conditions are not met (e.g., user is Awake or cooldown hasn't passed), do nothing.
77
  return None
78
 
79
+
80
  alert_manager = AlertManager(CFG.get("alerting", {}))
81
 
82
  # ───────────────────────────── Frame Processing for Tab 1 (Image Stream) - UPDATED
 
86
 
87
  t0 = time.perf_counter()
88
  try:
89
+ # Call with draw_visuals=True
90
  processed, indic = detector.process_frame(frame, draw_visuals=True)
91
  except Exception as e:
92
  logging.error(f"Error processing frame: {e}")
 
107
  return processed, status_txt, gr.Audio(value=audio_payload, autoplay=True) if audio_payload else None
108
 
109
 
 
110
  def process_for_stats_only(frame):
111
  """
112
  Processes a frame but does not return any video/image output.
 
117
 
118
  t0 = time.perf_counter()
119
  try:
120
+ # Call with draw_visuals=False. The first returned value will be None.
121
  _, indic = detector.process_frame(frame, draw_visuals=False)
122
  except Exception as e:
123
  logging.error(f"Error processing frame: {e}")