mkhodary101 commited on
Commit
7ef0fcc
·
verified ·
1 Parent(s): 4fbc361

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -2
app.py CHANGED
@@ -12,8 +12,16 @@ class CrowdDetection:
12
  def __init__(self, model_path="yolov8n.pt"):
13
  """Initialize the YOLO model once to avoid PicklingError."""
14
  self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
15
- self.model = YOLO(model_path).to(self.device) # Load model once
 
 
 
 
 
 
 
16
 
 
17
  def detect_crowd(self, video_path):
18
  """Process video using YOLOv8 for crowd detection."""
19
  cap = cv2.VideoCapture(video_path)
@@ -28,6 +36,10 @@ class CrowdDetection:
28
  fourcc = cv2.VideoWriter_fourcc(*"mp4v")
29
  out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
30
 
 
 
 
 
31
  CROWD_THRESHOLD = 10
32
  frame_count = 0
33
 
@@ -75,8 +87,37 @@ class CrowdDetection:
75
  if not os.path.exists(output_path):
76
  raise FileNotFoundError(f"❌ Output video not found: {output_path}")
77
 
78
- return output_path # Return file path instead of video object
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
 
 
80
 
81
 
82
  class PeopleTracking:
 
12
  def __init__(self, model_path="yolov8n.pt"):
13
  """Initialize the YOLO model once to avoid PicklingError."""
14
  self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
15
+ if not os.path.exists(model_path):
16
+ # Download the model if not present
17
+ from ultralytics import YOLO
18
+ self.model = YOLO("yolov8n.pt") # This downloads the model automatically
19
+ self.model.save(model_path) # Save locally
20
+ else:
21
+ self.model = YOLO(model_path)
22
+ self.model.to(self.device)
23
 
24
+ @spaces.GPU
25
  def detect_crowd(self, video_path):
26
  """Process video using YOLOv8 for crowd detection."""
27
  cap = cv2.VideoCapture(video_path)
 
36
  fourcc = cv2.VideoWriter_fourcc(*"mp4v")
37
  out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
38
 
39
+ if not out.isOpened():
40
+ cap.release()
41
+ raise ValueError(f"❌ Failed to initialize video writer for {output_path}")
42
+
43
  CROWD_THRESHOLD = 10
44
  frame_count = 0
45
 
 
87
  if not os.path.exists(output_path):
88
  raise FileNotFoundError(f"❌ Output video not found: {output_path}")
89
 
90
+ return output_path
91
+
92
+ # Define Gradio interface function
93
+ def process_video(video):
94
+ try:
95
+ detector = CrowdDetection() # Instantiate inside to avoid pickling
96
+ output_path = detector.detect_crowd(video)
97
+ return "Crowd detection complete!", output_path
98
+ except Exception as e:
99
+ return f"Error: {str(e)}", None
100
+
101
+ # Create Gradio interface
102
+ with gr.Blocks() as demo:
103
+ gr.Markdown("# Crowd Detection with YOLOv8")
104
+ gr.Markdown("Upload a video to detect people and get crowd alerts (threshold: 10 people)")
105
+
106
+ with gr.Row():
107
+ with gr.Column():
108
+ video_input = gr.Video(label="Upload Video")
109
+ submit_btn = gr.Button("Detect Crowd")
110
+ with gr.Column():
111
+ status_output = gr.Textbox(label="Status")
112
+ video_output = gr.Video(label="Result")
113
+
114
+ submit_btn.click(
115
+ fn=process_video,
116
+ inputs=[video_input],
117
+ outputs=[status_output, video_output]
118
+ )
119
 
120
+ demo.launch(debug=True)
121
 
122
 
123
  class PeopleTracking: