Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,437 +1,440 @@
|
|
| 1 |
-
# -*- coding: utf-8 -*-
|
| 2 |
-
"""app.py
|
| 3 |
-
Automated Fire and Accident Detection for CCTV with Freshdesk Ticket Creation
|
| 4 |
-
"""
|
| 5 |
-
|
| 6 |
-
import
|
| 7 |
-
import
|
| 8 |
-
|
| 9 |
-
import
|
| 10 |
-
import
|
| 11 |
-
|
| 12 |
-
import
|
| 13 |
-
import
|
| 14 |
-
from
|
| 15 |
-
import
|
| 16 |
-
import
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
os.
|
| 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 |
-
return
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
image_url = save_image(img, "
|
| 76 |
-
image_path = os.path.join(MEDIA_DIR, "
|
| 77 |
-
elif incident_type.lower() == "
|
| 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 |
-
return f"
|
| 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 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
return
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
if
|
| 342 |
-
create_freshdesk_ticket("
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
detection_info
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
return
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
-
|
| 382 |
-
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
with
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
-
|
| 431 |
-
-
|
| 432 |
-
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
|
|
|
|
|
|
|
|
|
| 437 |
iface.launch()
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""app.py
|
| 3 |
+
Automated Fire and Accident Detection for CCTV with Freshdesk Ticket Creation
|
| 4 |
+
"""
|
| 5 |
+
# Near the top of app.py, after imports
|
| 6 |
+
import ultralytics.nn.modules.block
|
| 7 |
+
from custom_blocks import SCDown
|
| 8 |
+
ultralytics.nn.modules.block.SCDown = SCDown
|
| 9 |
+
import cv2
|
| 10 |
+
import os
|
| 11 |
+
import PIL.Image as Image
|
| 12 |
+
import gradio as gr
|
| 13 |
+
import numpy as np
|
| 14 |
+
from ultralytics import YOLO
|
| 15 |
+
import requests
|
| 16 |
+
import json
|
| 17 |
+
from datetime import datetime
|
| 18 |
+
import tempfile
|
| 19 |
+
import torch
|
| 20 |
+
import uuid # Added for unique filenames
|
| 21 |
+
|
| 22 |
+
# Freshdesk Configuration
|
| 23 |
+
FRESHDESK_DOMAIN = "7kctech-supportdesk.freshdesk.com"
|
| 24 |
+
API_KEY = os.getenv("FRESHDESK_API_KEY", "JoJNI8nIY3hWQsk87e") # Fallback for local testing
|
| 25 |
+
|
| 26 |
+
# Base URL for Hugging Face Space
|
| 27 |
+
BASE_URL = "https://huggingface.co/spaces/Zynaly/Surveillance-Intelligent-Camera/tree/main"
|
| 28 |
+
|
| 29 |
+
# Directory for saving images
|
| 30 |
+
MEDIA_DIR = "media"
|
| 31 |
+
FIRE_DIR = os.path.join(MEDIA_DIR, "fire")
|
| 32 |
+
ACCIDENT_DIR = os.path.join(MEDIA_DIR, "accidents")
|
| 33 |
+
|
| 34 |
+
# Create directories if they don't exist
|
| 35 |
+
os.makedirs(FIRE_DIR, exist_ok=True)
|
| 36 |
+
os.makedirs(ACCIDENT_DIR, exist_ok=True)
|
| 37 |
+
|
| 38 |
+
# Fixed thresholds for automated detection
|
| 39 |
+
FIRE_CONF_THRESHOLD = 0.25
|
| 40 |
+
FIRE_IOU_THRESHOLD = 0.45
|
| 41 |
+
ACCIDENT_CONF_THRESHOLD = 0.3
|
| 42 |
+
ACCIDENT_IOU_THRESHOLD = 0.55
|
| 43 |
+
|
| 44 |
+
# Load models with explicit task definition
|
| 45 |
+
fire_model = YOLO("fire.pt", task="detect") # Fire detection model
|
| 46 |
+
accident_model = YOLO("best.pt", task="detect") # Accident detection model
|
| 47 |
+
|
| 48 |
+
# Function to save image and return its URL
|
| 49 |
+
def save_image(image, incident_type):
|
| 50 |
+
try:
|
| 51 |
+
# Generate unique filename
|
| 52 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 53 |
+
unique_id = str(uuid.uuid4())[:8]
|
| 54 |
+
filename = f"{incident_type}_{timestamp}_{unique_id}.jpg"
|
| 55 |
+
save_path = os.path.join(MEDIA_DIR, incident_type, filename)
|
| 56 |
+
|
| 57 |
+
# Save the image
|
| 58 |
+
image.save(save_path)
|
| 59 |
+
|
| 60 |
+
# Construct the URL
|
| 61 |
+
url = f"{BASE_URL}/{save_path}"
|
| 62 |
+
return url
|
| 63 |
+
except Exception as e:
|
| 64 |
+
print(f"Error saving image: {str(e)}")
|
| 65 |
+
return None
|
| 66 |
+
|
| 67 |
+
# Function to create Freshdesk ticket
|
| 68 |
+
# Function to create Freshdesk ticket with image attachment
|
| 69 |
+
# Function to create Freshdesk ticket with image attachment
|
| 70 |
+
def create_freshdesk_ticket(incident_type, confidence_score, img):
|
| 71 |
+
# Save the image to the appropriate directory and get its local path
|
| 72 |
+
image_url = None
|
| 73 |
+
image_path = None
|
| 74 |
+
if incident_type.lower() == "fire incident":
|
| 75 |
+
image_url = save_image(img, "fire")
|
| 76 |
+
image_path = os.path.join(MEDIA_DIR, "fire", os.path.basename(image_url.split("/")[-1]))
|
| 77 |
+
elif incident_type.lower() == "accident incident":
|
| 78 |
+
image_url = save_image(img, "accidents")
|
| 79 |
+
image_path = os.path.join(MEDIA_DIR, "accidents", os.path.basename(image_url.split("/")[-1]))
|
| 80 |
+
elif incident_type.lower() == "fire and accident incident":
|
| 81 |
+
fire_url = save_image(img, "fire")
|
| 82 |
+
accident_url = save_image(img, "accidents")
|
| 83 |
+
image_url = fire_url or accident_url
|
| 84 |
+
image_path = os.path.join(MEDIA_DIR, "fire" if fire_url else "accidents", os.path.basename(image_url.split("/")[-1]))
|
| 85 |
+
|
| 86 |
+
# Shortened subject
|
| 87 |
+
subject = f"{incident_type} Detected - Confidence: {confidence_score*100:.1f}%"
|
| 88 |
+
|
| 89 |
+
# Detailed description
|
| 90 |
+
description = f"""
|
| 91 |
+
{incident_type} is critical.
|
| 92 |
+
Details:
|
| 93 |
+
1. Address: 123 Main Street, Lahore
|
| 94 |
+
2. Phone: 923013225853
|
| 95 |
+
3. Confidence Score: {confidence_score*100:.1f}%
|
| 96 |
+
4. Image URL: {image_url or 'https://example.com/roboi.jpg'}
|
| 97 |
+
5. Incident Time: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
|
| 98 |
+
"""
|
| 99 |
+
|
| 100 |
+
ticket_data = {
|
| 101 |
+
"email": "safe.city@example.com",
|
| 102 |
+
"subject": subject,
|
| 103 |
+
"description": description,
|
| 104 |
+
"priority": 4, # Urgent
|
| 105 |
+
"status": 2 # Open
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
# Create ticket
|
| 109 |
+
url = f"https://{FRESHDESK_DOMAIN}/api/v2/tickets"
|
| 110 |
+
headers = {"Content-Type": "application/json"}
|
| 111 |
+
|
| 112 |
+
response = requests.post(
|
| 113 |
+
url,
|
| 114 |
+
auth=(API_KEY, "X"),
|
| 115 |
+
headers=headers,
|
| 116 |
+
data=json.dumps(ticket_data)
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
if response.status_code == 201:
|
| 120 |
+
ticket = response.json()
|
| 121 |
+
ticket_id = ticket.get('id')
|
| 122 |
+
print(f"✅ Ticket created successfully: Ticket ID {ticket_id}")
|
| 123 |
+
print(json.dumps(ticket, indent=2))
|
| 124 |
+
|
| 125 |
+
# Attach image to ticket if image_path exists
|
| 126 |
+
if image_path and os.path.exists(image_path):
|
| 127 |
+
attachment_url = f"https://{FRESHDESK_DOMAIN}/api/v2/tickets/{ticket_id}/attachments"
|
| 128 |
+
try:
|
| 129 |
+
with open(image_path, 'rb') as f:
|
| 130 |
+
files = {'attachments[]': (os.path.basename(image_path), f, 'image/jpeg')}
|
| 131 |
+
# Do not set Content-Type header; let requests handle it
|
| 132 |
+
attachment_response = requests.post(
|
| 133 |
+
attachment_url,
|
| 134 |
+
auth=(API_KEY, "X"),
|
| 135 |
+
files=files
|
| 136 |
+
)
|
| 137 |
+
if attachment_response.status_code == 201:
|
| 138 |
+
print(f"✅ Image attached to ticket {ticket_id}")
|
| 139 |
+
else:
|
| 140 |
+
print(f"❌ Failed to attach image: {attachment_response.status_code} - {attachment_response.text}")
|
| 141 |
+
except Exception as e:
|
| 142 |
+
print(f"❌ Error accessing image file {image_path}: {str(e)}")
|
| 143 |
+
else:
|
| 144 |
+
print(f"❌ Image file not found: {image_path}")
|
| 145 |
+
|
| 146 |
+
return f"Ticket created for {incident_type} with ID {ticket_id}"
|
| 147 |
+
else:
|
| 148 |
+
print(f"❌ Failed to create ticket: {response.status_code} - {response.text}")
|
| 149 |
+
return f"Failed to create ticket for {incident_type}: {response.status_code} - {response.text}"
|
| 150 |
+
# Image inference function
|
| 151 |
+
def detect_image(image):
|
| 152 |
+
try:
|
| 153 |
+
pil_img = image
|
| 154 |
+
|
| 155 |
+
# Fire detection
|
| 156 |
+
fire_results = fire_model.predict(
|
| 157 |
+
source=pil_img,
|
| 158 |
+
conf=FIRE_CONF_THRESHOLD,
|
| 159 |
+
iou=FIRE_IOU_THRESHOLD,
|
| 160 |
+
show_labels=True,
|
| 161 |
+
show_conf=True,
|
| 162 |
+
imgsz=640,
|
| 163 |
+
verbose=False
|
| 164 |
+
)
|
| 165 |
+
|
| 166 |
+
fire_detected = False
|
| 167 |
+
fire_confidence = 0.0
|
| 168 |
+
fire_annotated_img = fire_results[0].plot()
|
| 169 |
+
fire_confidences = []
|
| 170 |
+
fire_classes = []
|
| 171 |
+
for r in fire_results:
|
| 172 |
+
if r.boxes:
|
| 173 |
+
for box in r.boxes:
|
| 174 |
+
confidence = box.conf[0].item()
|
| 175 |
+
class_id = int(box.cls[0].item())
|
| 176 |
+
fire_confidences.append(confidence)
|
| 177 |
+
fire_classes.append(class_id)
|
| 178 |
+
if confidence >= FIRE_CONF_THRESHOLD:
|
| 179 |
+
fire_detected = True
|
| 180 |
+
fire_confidence = max(fire_confidence, confidence)
|
| 181 |
+
print(f"Fire model raw confidences: {fire_confidences}, classes: {fire_classes}")
|
| 182 |
+
|
| 183 |
+
# Accident detection
|
| 184 |
+
accident_results = accident_model.predict(
|
| 185 |
+
source=pil_img,
|
| 186 |
+
conf=ACCIDENT_CONF_THRESHOLD,
|
| 187 |
+
iou=ACCIDENT_IOU_THRESHOLD,
|
| 188 |
+
show_labels=True,
|
| 189 |
+
show_conf=True,
|
| 190 |
+
imgsz=640,
|
| 191 |
+
verbose=False
|
| 192 |
+
)
|
| 193 |
+
|
| 194 |
+
accident_detected = False
|
| 195 |
+
accident_confidence = 0.0
|
| 196 |
+
accident_annotated_img = accident_results[0].plot()
|
| 197 |
+
accident_confidences = []
|
| 198 |
+
accident_classes = []
|
| 199 |
+
accident_boxes = accident_results[0].boxes
|
| 200 |
+
if accident_boxes:
|
| 201 |
+
for box in accident_boxes:
|
| 202 |
+
confidence = box.conf[0].item()
|
| 203 |
+
class_id = int(box.cls[0].item())
|
| 204 |
+
accident_confidences.append(confidence)
|
| 205 |
+
accident_classes.append(class_id)
|
| 206 |
+
if confidence >= ACCIDENT_CONF_THRESHOLD:
|
| 207 |
+
accident_detected = True
|
| 208 |
+
accident_confidence = max(accident_confidence, confidence)
|
| 209 |
+
print(f"Accident model raw confidences: {accident_confidences}, classes: {accident_classes}")
|
| 210 |
+
|
| 211 |
+
# Combine annotated images
|
| 212 |
+
fire_annotated_img = np.array(fire_annotated_img)
|
| 213 |
+
accident_annotated_img = np.array(accident_annotated_img)
|
| 214 |
+
combined_img = Image.fromarray(np.maximum(fire_annotated_img, accident_annotated_img))
|
| 215 |
+
|
| 216 |
+
# Detection info
|
| 217 |
+
detection_info = "Detection Results:\n"
|
| 218 |
+
if fire_detected:
|
| 219 |
+
detection_info += f"Fire detected with confidence: {fire_confidence*100:.1f}%\n"
|
| 220 |
+
else:
|
| 221 |
+
detection_info += f"No fire detected. Raw confidences: {fire_confidences}, Classes: {fire_classes}\n"
|
| 222 |
+
if accident_detected:
|
| 223 |
+
detection_info += f"Accident detected with confidence: {accident_confidence*100:.1f}%\n"
|
| 224 |
+
else:
|
| 225 |
+
detection_info += f"No accident detected. Raw confidences: {accident_confidences}, Classes: {accident_classes}\n"
|
| 226 |
+
|
| 227 |
+
# Create a single Freshdesk ticket
|
| 228 |
+
ticket_info = ""
|
| 229 |
+
if fire_detected and not accident_detected:
|
| 230 |
+
ticket_info = create_freshdesk_ticket("Fire Incident", fire_confidence, pil_img)
|
| 231 |
+
elif accident_detected and not fire_detected:
|
| 232 |
+
ticket_info = create_freshdesk_ticket("Accident Incident", accident_confidence, pil_img)
|
| 233 |
+
elif fire_detected and accident_detected:
|
| 234 |
+
ticket_info = create_freshdesk_ticket("Fire and Accident Incident", max(fire_confidence, accident_confidence), pil_img)
|
| 235 |
+
else:
|
| 236 |
+
ticket_info = "No ticket created: No incidents detected"
|
| 237 |
+
|
| 238 |
+
return combined_img, detection_info, ticket_info
|
| 239 |
+
|
| 240 |
+
except Exception as e:
|
| 241 |
+
return image, f"Error during detection: {str(e)}\nRaw confidences: Fire {fire_confidences}, Accident {accident_confidences}", "No ticket created due to error"
|
| 242 |
+
|
| 243 |
+
# Video processing function
|
| 244 |
+
def detect_video(video_path):
|
| 245 |
+
try:
|
| 246 |
+
cap = cv2.VideoCapture(video_path)
|
| 247 |
+
|
| 248 |
+
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 249 |
+
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 250 |
+
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
| 251 |
+
|
| 252 |
+
output_path = tempfile.mktemp(suffix='.mp4')
|
| 253 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 254 |
+
out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height))
|
| 255 |
+
|
| 256 |
+
frame_count = 0
|
| 257 |
+
fire_detected_once = False
|
| 258 |
+
accident_detected_once = False
|
| 259 |
+
fire_detection_frames = []
|
| 260 |
+
accident_detection_frames = []
|
| 261 |
+
fire_confidences_all = []
|
| 262 |
+
accident_confidences_all = []
|
| 263 |
+
fire_classes_all = []
|
| 264 |
+
accident_classes_all = []
|
| 265 |
+
|
| 266 |
+
while cap.isOpened():
|
| 267 |
+
ret, frame = cap.read()
|
| 268 |
+
if not ret:
|
| 269 |
+
break
|
| 270 |
+
|
| 271 |
+
frame_count += 1
|
| 272 |
+
pil_img = Image.fromarray(frame[..., ::-1]) # Convert BGR to RGB
|
| 273 |
+
|
| 274 |
+
# Fire detection
|
| 275 |
+
fire_results = fire_model.predict(
|
| 276 |
+
source=pil_img,
|
| 277 |
+
conf=FIRE_CONF_THRESHOLD,
|
| 278 |
+
iou=FIRE_IOU_THRESHOLD,
|
| 279 |
+
show_labels=True,
|
| 280 |
+
show_conf=True,
|
| 281 |
+
imgsz=640,
|
| 282 |
+
verbose=False
|
| 283 |
+
)
|
| 284 |
+
|
| 285 |
+
fire_detected = False
|
| 286 |
+
fire_confidence = 0.0
|
| 287 |
+
fire_annotated_frame = fire_results[0].plot()
|
| 288 |
+
fire_confidences = []
|
| 289 |
+
fire_classes = []
|
| 290 |
+
for r in fire_results:
|
| 291 |
+
if r.boxes:
|
| 292 |
+
for box in r.boxes:
|
| 293 |
+
confidence = box.conf[0].item()
|
| 294 |
+
class_id = int(box.cls[0].item())
|
| 295 |
+
fire_confidences.append(confidence)
|
| 296 |
+
fire_classes.append(class_id)
|
| 297 |
+
if confidence >= FIRE_CONF_THRESHOLD:
|
| 298 |
+
fire_detected = True
|
| 299 |
+
fire_confidence = max(fire_confidence, confidence)
|
| 300 |
+
fire_detection_frames.append(frame_count)
|
| 301 |
+
fire_confidences_all.extend(fire_confidences)
|
| 302 |
+
fire_classes_all.extend(fire_classes)
|
| 303 |
+
|
| 304 |
+
# Accident detection
|
| 305 |
+
accident_results = accident_model.predict(
|
| 306 |
+
source=pil_img,
|
| 307 |
+
conf=ACCIDENT_CONF_THRESHOLD,
|
| 308 |
+
iou=ACCIDENT_IOU_THRESHOLD,
|
| 309 |
+
show_labels=True,
|
| 310 |
+
show_conf=True,
|
| 311 |
+
imgsz=640,
|
| 312 |
+
verbose=False
|
| 313 |
+
)
|
| 314 |
+
|
| 315 |
+
accident_detected = False
|
| 316 |
+
accident_confidence = 0.0
|
| 317 |
+
accident_annotated_frame = accident_results[0].plot()
|
| 318 |
+
accident_confidences = []
|
| 319 |
+
accident_classes = []
|
| 320 |
+
accident_boxes = accident_results[0].boxes
|
| 321 |
+
if accident_boxes:
|
| 322 |
+
for box in accident_boxes:
|
| 323 |
+
confidence = box.conf[0].item()
|
| 324 |
+
class_id = int(box.cls[0].item())
|
| 325 |
+
accident_confidences.append(confidence)
|
| 326 |
+
accident_classes.append(class_id)
|
| 327 |
+
if confidence >= ACCIDENT_CONF_THRESHOLD:
|
| 328 |
+
accident_detected = True
|
| 329 |
+
accident_confidence = max(accident_confidence, confidence)
|
| 330 |
+
accident_detection_frames.append(frame_count)
|
| 331 |
+
accident_confidences_all.extend(accident_confidences)
|
| 332 |
+
accident_classes_all.extend(accident_classes)
|
| 333 |
+
|
| 334 |
+
# Combine annotated frames
|
| 335 |
+
fire_annotated_frame = np.array(fire_annotated_frame)
|
| 336 |
+
accident_annotated_frame = np.array(accident_annotated_frame)
|
| 337 |
+
combined_frame = np.maximum(fire_annotated_frame, accident_annotated_frame)
|
| 338 |
+
out.write(combined_frame)
|
| 339 |
+
|
| 340 |
+
# Create a single ticket for the first detection of each incident type
|
| 341 |
+
if fire_detected and not fire_detected_once:
|
| 342 |
+
create_freshdesk_ticket("Fire Incident", fire_confidence, pil_img)
|
| 343 |
+
fire_detected_once = True
|
| 344 |
+
if accident_detected and not accident_detected_once:
|
| 345 |
+
create_freshdesk_ticket("Accident Incident", accident_confidence, pil_img)
|
| 346 |
+
accident_detected_once = True
|
| 347 |
+
|
| 348 |
+
cap.release()
|
| 349 |
+
out.release()
|
| 350 |
+
|
| 351 |
+
detection_info = f"Video processed successfully!\n"
|
| 352 |
+
detection_info += f"Total frames: {frame_count}\n"
|
| 353 |
+
detection_info += f"Frames with fire detections: {len(set(fire_detection_frames))}\n"
|
| 354 |
+
detection_info += f"Frames with accident detections: {len(set(accident_detection_frames))}\n"
|
| 355 |
+
if fire_detection_frames:
|
| 356 |
+
detection_info += f"Fire detection frames: {sorted(set(fire_detection_frames))[:10]}...\n"
|
| 357 |
+
else:
|
| 358 |
+
detection_info += f"No fire detections. Raw confidences (sample): {fire_confidences_all[:10]}, Classes: {fire_classes_all[:10]}...\n"
|
| 359 |
+
if accident_detection_frames:
|
| 360 |
+
detection_info += f"Accident detection frames: {sorted(set(accident_detection_frames))[:10]}...\n"
|
| 361 |
+
else:
|
| 362 |
+
detection_info += f"No accident detections. Raw confidences (sample): {accident_confidences_all[:10]}, Classes: {accident_classes_all[:10]}...\n"
|
| 363 |
+
|
| 364 |
+
ticket_info = f"Tickets created: {'Fire' if fire_detected_once else ''}{' and ' if fire_detected_once and accident_detected_once else ''}{'Accident' if accident_detected_once else ''}." if fire_detected_once or accident_detected_once else "No tickets created: No incidents detected"
|
| 365 |
+
|
| 366 |
+
return output_path, detection_info, ticket_info
|
| 367 |
+
|
| 368 |
+
except Exception as e:
|
| 369 |
+
return None, f"Error processing video: {str(e)}\nRaw confidences: Fire {fire_confidences_all[:10]}, Accident {accident_confidences_all[:10]}", "No ticket created due to error"
|
| 370 |
+
|
| 371 |
+
# Create Gradio interface for CCTV automation
|
| 372 |
+
with gr.Blocks(title="Rapid Rescue - Automated CCTV Fire and Accident Detection") as iface:
|
| 373 |
+
gr.Markdown("""
|
| 374 |
+
# 🚨 Rapid Rescue - Automated CCTV Fire and Accident Detection System
|
| 375 |
+
|
| 376 |
+
This AI system automatically detects fires and accidents in images and videos from CCTV feeds using two YOLO models:
|
| 377 |
+
- YOLOv8n for fire detection (Confidence: 0.25, IoU: 0.45)
|
| 378 |
+
- YOLOv8m for accident detection (Confidence: 0.3, IoU: 0.55)
|
| 379 |
+
|
| 380 |
+
**Features:**
|
| 381 |
+
- Fully automated detection with fixed thresholds
|
| 382 |
+
- Creates Freshdesk tickets for detected incidents with saved image URLs
|
| 383 |
+
- Supports both images and videos from CCTV feeds
|
| 384 |
+
- Images saved in media/fire and media/accidents directories
|
| 385 |
+
- Optimized for deployment on Hugging Face Spaces
|
| 386 |
+
|
| 387 |
+
**Usage:**
|
| 388 |
+
1. Upload an image or video from a CCTV feed
|
| 389 |
+
2. Click process to run detection
|
| 390 |
+
3. View results with bounding boxes, confidence scores, class labels, and ticket creation status
|
| 391 |
+
""")
|
| 392 |
+
|
| 393 |
+
with gr.Tabs():
|
| 394 |
+
with gr.Tab("Image Detection"):
|
| 395 |
+
with gr.Row():
|
| 396 |
+
with gr.Column():
|
| 397 |
+
image_input = gr.Image(type="pil", label="Upload CCTV Image")
|
| 398 |
+
image_button = gr.Button("Detect Fire and Accidents", variant="primary")
|
| 399 |
+
|
| 400 |
+
with gr.Column():
|
| 401 |
+
image_output = gr.Image(label="Detection Results")
|
| 402 |
+
image_info = gr.Textbox(label="Detection Information", lines=8)
|
| 403 |
+
ticket_info = gr.Textbox(label="Ticket Creation Status", lines=2)
|
| 404 |
+
|
| 405 |
+
image_button.click(
|
| 406 |
+
fn=detect_image,
|
| 407 |
+
inputs=[image_input],
|
| 408 |
+
outputs=[image_output, image_info, ticket_info]
|
| 409 |
+
)
|
| 410 |
+
|
| 411 |
+
with gr.Tab("Video Detection"):
|
| 412 |
+
with gr.Row():
|
| 413 |
+
with gr.Column():
|
| 414 |
+
video_input = gr.Video(label="Upload Video")
|
| 415 |
+
video_button = gr.Button("Process Video", variant="primary")
|
| 416 |
+
|
| 417 |
+
with gr.Column():
|
| 418 |
+
video_output = gr.Video(label="Processed Video")
|
| 419 |
+
video_info = gr.Textbox(label="Processing Information", lines=8)
|
| 420 |
+
ticket_info = gr.Textbox(label="Ticket Creation Status", lines=2)
|
| 421 |
+
|
| 422 |
+
video_button.click(
|
| 423 |
+
fn=detect_video,
|
| 424 |
+
inputs=[video_input],
|
| 425 |
+
outputs=[video_output, video_info, ticket_info]
|
| 426 |
+
)
|
| 427 |
+
|
| 428 |
+
gr.Markdown("""
|
| 429 |
+
### Notes
|
| 430 |
+
- Freshdesk tickets are created automatically when fire or accident is detected (one per incident type).
|
| 431 |
+
- Images are saved in media/fire or media/accidents directories with unique filenames.
|
| 432 |
+
- Ticket includes URL to the saved image.
|
| 433 |
+
- For videos, one ticket is created per incident type with the first detected frame saved.
|
| 434 |
+
- Deploy on Hugging Face Spaces with `requirements.txt` and model files (`fire.pt`, `best.pt`).
|
| 435 |
+
- Debug info includes raw confidence scores and class labels to verify detection performance.
|
| 436 |
+
""")
|
| 437 |
+
|
| 438 |
+
# Launch the app
|
| 439 |
+
if __name__ == "__main__":
|
| 440 |
iface.launch()
|