Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -38,10 +38,12 @@ detector = get_detector(CFG)
|
|
| 38 |
# It handles its own state (last alert time) internally.
|
| 39 |
class AlertManager:
|
| 40 |
def __init__(self, config):
|
| 41 |
-
self.cooldown_seconds = config.get("alert_cooldown_seconds",
|
| 42 |
self.last_alert_time = 0
|
| 43 |
self.alert_data = None
|
| 44 |
self.sample_rate = None
|
|
|
|
|
|
|
| 45 |
self._load_sound(config.get("alert_sound_path"))
|
| 46 |
|
| 47 |
def _load_sound(self, wav_path):
|
|
@@ -49,6 +51,7 @@ class AlertManager:
|
|
| 49 |
logging.warning("No 'alert_sound_path' found in config.")
|
| 50 |
return
|
| 51 |
try:
|
|
|
|
| 52 |
data, sr = sf.read(wav_path, dtype="int16")
|
| 53 |
self.alert_data = data
|
| 54 |
self.sample_rate = sr
|
|
@@ -58,18 +61,34 @@ class AlertManager:
|
|
| 58 |
self.alert_data = None
|
| 59 |
|
| 60 |
def trigger_alert(self, level, lighting):
|
| 61 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
is_drowsy = level != "Awake"
|
| 63 |
is_good_light = lighting != "Low"
|
|
|
|
| 64 |
is_ready = (time.monotonic() - self.last_alert_time) > self.cooldown_seconds
|
| 65 |
-
|
| 66 |
if self.alert_data is not None and is_drowsy and is_good_light and is_ready:
|
| 67 |
self.last_alert_time = time.monotonic()
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
| 69 |
return (self.sample_rate, self.alert_data.copy())
|
| 70 |
-
|
| 71 |
-
return None
|
| 72 |
|
|
|
|
| 73 |
# Initialize the alert manager
|
| 74 |
alert_manager = AlertManager(CFG["alerting"])
|
| 75 |
|
|
|
|
| 38 |
# It handles its own state (last alert time) internally.
|
| 39 |
class AlertManager:
|
| 40 |
def __init__(self, config):
|
| 41 |
+
self.cooldown_seconds = config.get("alert_cooldown_seconds", 5)
|
| 42 |
self.last_alert_time = 0
|
| 43 |
self.alert_data = None
|
| 44 |
self.sample_rate = None
|
| 45 |
+
# --- NEW: State variable to track if an alert is active ---
|
| 46 |
+
self.is_alert_active = False
|
| 47 |
self._load_sound(config.get("alert_sound_path"))
|
| 48 |
|
| 49 |
def _load_sound(self, wav_path):
|
|
|
|
| 51 |
logging.warning("No 'alert_sound_path' found in config.")
|
| 52 |
return
|
| 53 |
try:
|
| 54 |
+
# Load as int16 to avoid the Gradio conversion warning
|
| 55 |
data, sr = sf.read(wav_path, dtype="int16")
|
| 56 |
self.alert_data = data
|
| 57 |
self.sample_rate = sr
|
|
|
|
| 61 |
self.alert_data = None
|
| 62 |
|
| 63 |
def trigger_alert(self, level, lighting):
|
| 64 |
+
"""
|
| 65 |
+
Checks conditions and returns audio payload if a new alert should fire.
|
| 66 |
+
This is now stateful.
|
| 67 |
+
"""
|
| 68 |
+
# --- NEW LOGIC: Part 1 ---
|
| 69 |
+
# If an alert is currently active, we do nothing until the user is 'Awake'.
|
| 70 |
+
if self.is_alert_active:
|
| 71 |
+
if level == "Awake":
|
| 72 |
+
logging.info("✅ Alert state reset. User is Awake. Re-arming system.")
|
| 73 |
+
self.is_alert_active = False
|
| 74 |
+
return None # Important: Return None to prevent any sound
|
| 75 |
+
|
| 76 |
+
# --- ORIGINAL LOGIC (with a small change) ---
|
| 77 |
+
# If no alert is active, check for conditions to fire a new one.
|
| 78 |
is_drowsy = level != "Awake"
|
| 79 |
is_good_light = lighting != "Low"
|
| 80 |
+
# The time-based cooldown is still useful to prevent flickering alerts.
|
| 81 |
is_ready = (time.monotonic() - self.last_alert_time) > self.cooldown_seconds
|
| 82 |
+
|
| 83 |
if self.alert_data is not None and is_drowsy and is_good_light and is_ready:
|
| 84 |
self.last_alert_time = time.monotonic()
|
| 85 |
+
# --- NEW LOGIC: Part 2 ---
|
| 86 |
+
# Set the alert to active so it doesn't fire again immediately.
|
| 87 |
+
self.is_alert_active = True
|
| 88 |
+
logging.info("🔊 Drowsiness detected! Firing alert and setting state to active.")
|
| 89 |
return (self.sample_rate, self.alert_data.copy())
|
|
|
|
|
|
|
| 90 |
|
| 91 |
+
return None
|
| 92 |
# Initialize the alert manager
|
| 93 |
alert_manager = AlertManager(CFG["alerting"])
|
| 94 |
|