File size: 2,815 Bytes
ddec2b7
 
 
 
 
 
 
 
 
 
 
 
012a2ee
ddec2b7
 
 
 
 
 
 
 
 
 
 
 
012a2ee
ddec2b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
012a2ee
ddec2b7
 
 
012a2ee
ddec2b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
012a2ee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import cv2
import numpy as np
import time
import threading

class AlertSystem:
    def __init__(self):
        self.alert_active = False
        self.alert_start_time = 0
        self.alert_cooldown = 3.0  # seconds between alerts
        self.last_alert_time = 0
        self.flash_alpha = 0.0
        self.sound_enabled = True  # Will try to use winsound
    
    def trigger_alert(self, risk_score, status):
        """Trigger alert based on risk level"""
        current_time = time.time()
        
        if risk_score >= 70:
            if not self.alert_active:
                self.alert_active = True
                self.alert_start_time = current_time
                self.flash_alpha = 0.7
                
                # Play sound for danger
                if (current_time - self.last_alert_time) > self.alert_cooldown:
                    self._play_alert_sound()
                    self.last_alert_time = current_time
            else:
                # Pulse the flash effect
                elapsed = current_time - self.alert_start_time
                self.flash_alpha = 0.5 + 0.3 * np.sin(elapsed * 10)
        elif risk_score >= 50:
            self.alert_active = True
            self.flash_alpha = 0.3
        else:
            self.alert_active = False
            self.flash_alpha = 0.0
    
    def _play_alert_sound(self):
        """Play alert sound in separate thread"""
        def play():
            try:
                # Windows beep
                import winsound
                winsound.Beep(1000, 300)
            except:
                # Fallback for Linux/Cloud - silent
                pass
        threading.Thread(target=play, daemon=True).start()
    
    def apply_visual_alert(self, frame):
        """Apply visual alert overlay to frame"""
        if self.alert_active and self.flash_alpha > 0:
            h, w = frame.shape[:2]
            overlay = frame.copy()
            
            if self.flash_alpha > 0.5:
                # Red flash for danger
                cv2.rectangle(overlay, (0, 0), (w, h), (0, 0, 255), -1)
            else:
                # Yellow flash for warning
                cv2.rectangle(overlay, (0, 0), (w, h), (0, 255, 255), -1)
            
            frame = cv2.addWeighted(overlay, self.flash_alpha * 0.3, frame, 1 - self.flash_alpha * 0.3, 0)
        
        return frame
    
    def draw_alert_text(self, frame, status, color):
        """Draw alert text on frame"""
        if self.alert_active:
            h, w = frame.shape[:2]
            
            # Alert banner at top
            cv2.rectangle(frame, (0, 0), (w, 40), (0, 0, 0), -1)
            cv2.putText(frame, f"⚠️ {status} ⚠️", (w//2 - 100, 30),
                       cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2)
        
        return frame