File size: 6,548 Bytes
39b7b21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import requests
import cv2
import numpy as np
from datetime import datetime
from pathlib import Path
import logging
import base64
import io
import json

logger = logging.getLogger(__name__)


class DiscordAlertManager:
    """Manages Discord webhook alerts for hygiene violations."""
    
    def __init__(self, discord_config: dict):
        """

        discord_config: {

            'webhook_url': 'your_webhook_url'

        }

        """
        self.webhook_url = discord_config['webhook_url']
        self.alert_cooldown = 300
        self.last_alert_time = None
        self.dirty_start_time = None
        self.dirty_threshold_seconds = 10
        self.dirty_coverage_threshold = 0.06
        
    def should_send_alert(self, dirty_coverage: float, current_time: datetime) -> bool:
        """Same logic as before"""
        if dirty_coverage < self.dirty_coverage_threshold:
            self.dirty_start_time = None
            return False
        
        if self.dirty_start_time is None:
            self.dirty_start_time = current_time
            return False
        
        dirty_duration = (current_time - self.dirty_start_time).total_seconds()
        if dirty_duration < self.dirty_threshold_seconds:
            return False
        
        if self.last_alert_time is not None:
            time_since_last = (current_time - self.last_alert_time).total_seconds()
            if time_since_last < self.alert_cooldown:
                return False
        
        return True
    
    def generate_heatmap_image(self, frame: np.ndarray, gmm_heatmap: np.ndarray, 

                              output_path: str) -> str:
        """Generate heatmap visualization"""
        result = frame.copy()
        height, width = result.shape[:2]
        
        if gmm_heatmap.shape != (height, width):
            heatmap_resized = cv2.resize(gmm_heatmap, (width, height))
        else:
            heatmap_resized = gmm_heatmap
        
        heatmap_colored = cv2.applyColorMap(
            (heatmap_resized * 255).astype(np.uint8),
            cv2.COLORMAP_JET
        )
        
        alpha = 0.5
        result = cv2.addWeighted(frame, 1 - alpha, heatmap_colored, alpha, 0)
        
        # Add info panel
        avg_dirt = np.mean(heatmap_resized)
        max_dirt = np.max(heatmap_resized)
        dirty_pixels = np.sum(heatmap_resized > 0.60)
        coverage_percent = (dirty_pixels / heatmap_resized.size) * 100
        
        cv2.rectangle(result, (10, 10), (400, 120), (0, 0, 0), -1)
        cv2.rectangle(result, (10, 10), (400, 120), (255, 255, 255), 2)
        
        info_text = [
            f"Average Dirt: {avg_dirt:.2f}",
            f"Maximum Dirt: {max_dirt:.2f}",
            f"Red Zone: {coverage_percent:.1f}%",
            f"Time: {datetime.now().strftime('%H:%M:%S')}"
        ]
        
        for i, text in enumerate(info_text):
            cv2.putText(result, text, (20, 35 + i * 25),
                       cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 1)
        
        cv2.imwrite(output_path, result)
        return output_path
    
    def send_alert(self, camera_name: str, dirty_coverage: float, 

              dirty_duration: int, frame: np.ndarray, 

              gmm_heatmap: np.ndarray) -> bool:
        """Send Discord webhook alert with embedded image"""
        try:
            # Generate image
            temp_image_path = f"tmp/heatmap_{datetime.now().timestamp()}.png"
            self.generate_heatmap_image(frame, gmm_heatmap, temp_image_path)
            
            # Calculate duration
            duration_mins = dirty_duration // 60
            duration_secs = dirty_duration % 60
            
            # Create rich embed
            embed = {
                "title": "🚨 CLEANING ALERT",
                "description": f"**{camera_name}** requires immediate attention!",
                "color": 15158332,  # Red color (#E74C3C)
                "fields": [
                    {
                        "name": "📍 Location",
                        "value": camera_name,
                        "inline": True
                    },
                    {
                        "name": "🔴 Coverage",
                        "value": f"{dirty_coverage*100:.1f}%",
                        "inline": True
                    },
                    {
                        "name": "⏱ Duration",
                        "value": f"{duration_mins}m {duration_secs}s",
                        "inline": True
                    },
                    {
                        "name": "⚠️ Action Required",
                        "value": "Table has exceeded cleanliness threshold and needs cleaning.",
                        "inline": False
                    }
                ],
                "footer": {
                    "text": "Kitchen Hygiene Monitoring System"
                },
                "timestamp": datetime.utcnow().isoformat()
            }
            
            # Prepare webhook payload with embeds
            payload = {
                "username": "Hygiene Monitor Bot",
                "avatar_url": "https://cdn-icons-png.flaticon.com/512/3699/3699516.png",
                "embeds": [embed]
            }
            
            # Read the image file
            with open(temp_image_path, 'rb') as f:
                image_data = f.read()
            
            # Prepare the multipart form data
            files = {
                'payload_json': (None, json.dumps(payload), 'application/json'),
                'file': ('heatmap.png', image_data, 'image/png')
            }
            
            # Send the request
            response = requests.post(self.webhook_url, files=files)
            
            if response.status_code in [200, 204]:
                self.last_alert_time = datetime.now()
                logger.info(f"✅ Discord alert sent for {camera_name}")
                Path(temp_image_path).unlink(missing_ok=True)
                return True
            else:
                logger.error(f"Discord webhook error: {response.status_code} - {response.text}")
                return False
                
        except Exception as e:
            logger.error(f"Failed to send Discord alert: {str(e)}")
            import traceback
            logger.error(traceback.format_exc())
            return False