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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -12
app.py CHANGED
@@ -414,7 +414,6 @@ class IntrusionDetectionEn:
414
  self.max_intrusion_time = max_intrusion_time
415
  self.iou_threshold = iou_threshold
416
  self.conf_threshold = conf_threshold
417
-
418
 
419
  # Predefined staff uniform colors (RGB format)
420
  self.staff_colors = [
@@ -424,7 +423,11 @@ class IntrusionDetectionEn:
424
  (143, 147, 136), # Gray-green
425
  (48, 59, 71) # Dark blue/gray
426
  ]
427
-
 
 
 
 
428
  def is_staff(self, person_crop):
429
  """Checks if the detected person is a staff member based on clothing color."""
430
  avg_color = np.mean(person_crop, axis=(0, 1)) # Compute average color (BGR)
@@ -437,18 +440,9 @@ class IntrusionDetectionEn:
437
  return True
438
  return False
439
 
440
-
441
  @spaces.GPU
442
  def intrusion_detect_en(self, video_path):
443
  try:
444
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
445
- if not os.path.exists(self.model_path):
446
- model = YOLO("yolov8n.pt")
447
- model.save(self.model_path)
448
- else:
449
- model = YOLO(self.model_path)
450
- model.to(device)
451
-
452
  cap = cv2.VideoCapture(video_path)
453
  if not cap.isOpened():
454
  raise ValueError(f"❌ Failed to open video: {video_path}")
@@ -472,7 +466,7 @@ class IntrusionDetectionEn:
472
  break
473
  frame_count += 1
474
 
475
- results = model(frame)
476
  for result in results:
477
  boxes = result.boxes.xyxy.cpu().numpy()
478
  classes = result.boxes.cls.cpu().numpy()
@@ -501,6 +495,7 @@ class IntrusionDetectionEn:
501
  except Exception as e:
502
  raise ValueError(f"Error in detect_intrusion: {str(e)}")
503
 
 
504
  import cv2
505
  import numpy as np
506
  from ultralytics import YOLO
 
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 = [
 
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)
 
440
  return True
441
  return False
442
 
 
443
  @spaces.GPU
444
  def intrusion_detect_en(self, video_path):
445
  try:
 
 
 
 
 
 
 
 
446
  cap = cv2.VideoCapture(video_path)
447
  if not cap.isOpened():
448
  raise ValueError(f"❌ Failed to open video: {video_path}")
 
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()
 
495
  except Exception as e:
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