mkhodary101 commited on
Commit
d65b7e7
·
verified ·
1 Parent(s): 1cce518

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -7
app.py CHANGED
@@ -408,13 +408,20 @@ class IntrusionDetection:
408
  except Exception as e:
409
  raise ValueError(f"Error in detect_intrusion: {str(e)}")
410
 
 
 
 
 
 
 
 
411
  class IntrusionDetectionEn:
412
  def __init__(self, model_path="yolov8n.pt", max_intrusion_time=300, iou_threshold=0.5, conf_threshold=0.7):
413
  self.model_path = model_path
414
  self.max_intrusion_time = max_intrusion_time
415
  self.iou_threshold = iou_threshold
416
  self.conf_threshold = conf_threshold
417
-
418
  # Predefined staff uniform colors (RGB format)
419
  self.staff_colors = [
420
  (139, 143, 133), # Grayish tone
@@ -423,11 +430,12 @@ class IntrusionDetectionEn:
423
  (143, 147, 136), # Gray-green
424
  (48, 59, 71) # Dark blue/gray
425
  ]
426
-
427
- # 🔹 Load the model once
428
  self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
429
- self.model = YOLO(self.model_path).to(self.device)
430
-
 
431
  def is_staff(self, person_crop):
432
  """Checks if the detected person is a staff member based on clothing color."""
433
  avg_color = np.mean(person_crop, axis=(0, 1)) # Compute average color (BGR)
@@ -439,7 +447,7 @@ class IntrusionDetectionEn:
439
  if dist < 30: # Threshold to consider it a match
440
  return True
441
  return False
442
-
443
  @spaces.GPU
444
  def intrusion_detect_en(self, video_path):
445
  try:
@@ -466,7 +474,7 @@ class IntrusionDetectionEn:
466
  break
467
  frame_count += 1
468
 
469
- results = self.model(frame) # 🔹 Use preloaded model
470
  for result in results:
471
  boxes = result.boxes.xyxy.cpu().numpy()
472
  classes = result.boxes.cls.cpu().numpy()
@@ -496,6 +504,7 @@ class IntrusionDetectionEn:
496
  raise ValueError(f"Error in detect_intrusion: {str(e)}")
497
 
498
 
 
499
  import cv2
500
  import numpy as np
501
  from ultralytics import YOLO
 
408
  except Exception as e:
409
  raise ValueError(f"Error in detect_intrusion: {str(e)}")
410
 
411
+ import torch
412
+ import cv2
413
+ import numpy as np
414
+ import os
415
+ from ultralytics import YOLO
416
+ import gradio as gr
417
+
418
  class IntrusionDetectionEn:
419
  def __init__(self, model_path="yolov8n.pt", max_intrusion_time=300, iou_threshold=0.5, conf_threshold=0.7):
420
  self.model_path = model_path
421
  self.max_intrusion_time = max_intrusion_time
422
  self.iou_threshold = iou_threshold
423
  self.conf_threshold = conf_threshold
424
+
425
  # Predefined staff uniform colors (RGB format)
426
  self.staff_colors = [
427
  (139, 143, 133), # Grayish tone
 
430
  (143, 147, 136), # Gray-green
431
  (48, 59, 71) # Dark blue/gray
432
  ]
433
+
434
+ # Initialize device and model once
435
  self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
436
+ self.model = YOLO(self.model_path)
437
+ self.model.to(self.device)
438
+
439
  def is_staff(self, person_crop):
440
  """Checks if the detected person is a staff member based on clothing color."""
441
  avg_color = np.mean(person_crop, axis=(0, 1)) # Compute average color (BGR)
 
447
  if dist < 30: # Threshold to consider it a match
448
  return True
449
  return False
450
+
451
  @spaces.GPU
452
  def intrusion_detect_en(self, video_path):
453
  try:
 
474
  break
475
  frame_count += 1
476
 
477
+ results = self.model(frame)
478
  for result in results:
479
  boxes = result.boxes.xyxy.cpu().numpy()
480
  classes = result.boxes.cls.cpu().numpy()
 
504
  raise ValueError(f"Error in detect_intrusion: {str(e)}")
505
 
506
 
507
+
508
  import cv2
509
  import numpy as np
510
  from ultralytics import YOLO