esmaele commited on
Commit
94443d2
·
verified ·
1 Parent(s): ac6edee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -26
app.py CHANGED
@@ -4,8 +4,8 @@ import cv2
4
  import numpy as np
5
  import time
6
  from ultralytics import YOLO
7
- import spaces
8
  import os
 
9
 
10
  class CrowdDetection:
11
  def __init__(self, model_path="yolov8n.pt"):
@@ -27,8 +27,8 @@ class CrowdDetection:
27
  raise ValueError(f"❌ Failed to open video: {video_path}")
28
 
29
  fps = int(cap.get(cv2.CAP_PROP_FPS))
30
- width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
31
- height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
32
 
33
  output_path = "output_crowd.mp4"
34
  fourcc = cv2.VideoWriter_fourcc(*"mp4v")
@@ -44,6 +44,7 @@ class CrowdDetection:
44
  ret, frame = cap.read()
45
  if not ret:
46
  break
 
47
  frame_count += 1
48
 
49
  results = model(frame)
@@ -74,7 +75,7 @@ class CrowdDetection:
74
  class PeopleTracking:
75
  def __init__(self, yolo_model_path="yolov8n.pt"):
76
  self.model_path = yolo_model_path
77
-
78
  @spaces.GPU
79
  def people_tracking(self, video_path):
80
  try:
@@ -91,8 +92,8 @@ class PeopleTracking:
91
  raise ValueError(f"❌ Failed to open video: {video_path}")
92
 
93
  fps = int(cap.get(cv2.CAP_PROP_FPS))
94
- width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
95
- height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
96
  output_path = "output_tracking.mp4"
97
  out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*"mp4v"), fps, (width, height))
98
  if not out.isOpened():
@@ -104,6 +105,7 @@ class PeopleTracking:
104
  if not ret:
105
  break
106
 
 
107
  results = model.track(frame, persist=True)
108
  for result in results:
109
  boxes = result.boxes.xyxy.cpu().numpy()
@@ -129,7 +131,7 @@ class PeopleTracking:
129
  class FallDetection:
130
  def __init__(self, yolo_model_path="yolov8l.pt"):
131
  self.model_path = yolo_model_path
132
-
133
  @spaces.GPU
134
  def fall_detect(self, video_path):
135
  try:
@@ -146,8 +148,8 @@ class FallDetection:
146
  raise ValueError(f"❌ Failed to open video: {video_path}")
147
 
148
  fps = int(cap.get(cv2.CAP_PROP_FPS))
149
- width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
150
- height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
151
  output_path = "output_fall.mp4"
152
  out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*"mp4v"), fps, (width, height))
153
  if not out.isOpened():
@@ -158,7 +160,7 @@ class FallDetection:
158
  ret, frame = cap.read()
159
  if not ret:
160
  break
161
-
162
  results = model(frame)
163
  for result in results:
164
  boxes = result.boxes.xyxy.cpu().numpy()
@@ -194,7 +196,7 @@ class FallDetection:
194
  class FightDetection:
195
  def __init__(self, yolo_model_path="yolov8n-pose.pt"):
196
  self.model_path = yolo_model_path
197
-
198
  @spaces.GPU
199
  def fight_detect(self, video_path):
200
  try:
@@ -211,8 +213,8 @@ class FightDetection:
211
  raise ValueError(f"❌ Failed to open video: {video_path}")
212
 
213
  fps = int(cap.get(cv2.CAP_PROP_FPS))
214
- width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
215
- height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
216
  output_path = "output_fight.mp4"
217
  out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*"mp4v"), fps, (width, height))
218
  if not out.isOpened():
@@ -223,7 +225,8 @@ class FightDetection:
223
  ret, frame = cap.read()
224
  if not ret:
225
  break
226
-
 
227
  results = model.track(frame, persist=True)
228
  fight_detected = False
229
  person_count = 0
@@ -262,8 +265,8 @@ class IntrusionDetection:
262
  self.max_intrusion_time = max_intrusion_time
263
  self.iou_threshold = iou_threshold
264
  self.conf_threshold = conf_threshold
 
265
  @spaces.GPU
266
-
267
  def intrusion_detect(self, video_path):
268
  try:
269
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
@@ -279,8 +282,8 @@ class IntrusionDetection:
279
  raise ValueError(f"❌ Failed to open video: {video_path}")
280
 
281
  fps = int(cap.get(cv2.CAP_PROP_FPS))
282
- width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
283
- height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
284
 
285
  output_path = "output_intrusion.mp4"
286
  fourcc = cv2.VideoWriter_fourcc(*"mp4v")
@@ -296,6 +299,7 @@ class IntrusionDetection:
296
  if not ret:
297
  break
298
  frame_count += 1
 
299
 
300
  results = model(frame)
301
  for result in results:
@@ -326,21 +330,75 @@ import numpy as np
326
  from ultralytics import YOLO
327
  from shapely.geometry import Point, Polygon
328
  import time
 
 
 
 
 
 
 
 
 
 
 
 
329
 
330
- class LoiteringDetector:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
331
  def __init__(self, model_path='loitering_model.pt'):
332
- self.model = YOLO(model_path)
333
 
334
  @spaces.GPU
335
  def loitering_detect(self, video_path, area):
336
  # Create polygon zone
 
 
 
 
337
  time_threshold = 7
338
  detection_threshold = 0.6
339
  zone_points = None
340
  if area == '131':
341
  zone_points = [(842//1.5, 514//1.7), (686//1.5, 290//1.7), (775//1.5, 279//1.7), (961//1.5, 488//1.7)]
342
  elif area == '145':
343
- zone_points = [(153, 850), (139, 535), (239, 497), (291, 857)]
344
  zone = Polygon(zone_points)
345
 
346
  # Open video
@@ -361,7 +419,7 @@ class LoiteringDetector:
361
 
362
  frame = cv2.resize(frame, (width, height))
363
  # Perform object detection and tracking
364
- results = self.model.track(frame, persist=True, classes=[0], conf=detection_threshold) # 0 is the class ID for person
365
 
366
  # List to store time information for display
367
  time_display = []
@@ -421,7 +479,8 @@ def process_video(feature, video, area=None):
421
  "Fall Detection": FallDetection,
422
  "Fight Detection": FightDetection,
423
  "Intrusion Detection": IntrusionDetection,
424
- "Loitering Detection": LoiteringDetection
 
425
  }
426
 
427
  try:
@@ -429,7 +488,7 @@ def process_video(feature, video, area=None):
429
  method_name = feature.lower().replace(" ", "_").replace("detection", "detect") # Ensures correct method name
430
 
431
  if feature == "Loitering Detection":
432
- output_path = detector.detect_loitering(video, area) # Pass area if required
433
  else:
434
  output_path = getattr(detector, method_name)(video)
435
 
@@ -443,10 +502,11 @@ interface = gr.Interface(
443
  inputs=[
444
  gr.Dropdown(choices=[
445
  "Crowd Detection", "People Tracking", "Fall Detection",
446
- "Fight Detection", "Intrusion Detection", "Loitering Detection"
 
447
  ], label="Select Feature"),
448
  gr.Video(label="Upload Video"),
449
- gr.Textbox(label="Loitering Area (131 or 145)") # Can be replaced with a drawing tool
450
  ],
451
  outputs=[
452
  gr.Textbox(label="Status"),
@@ -457,4 +517,4 @@ interface = gr.Interface(
457
  )
458
 
459
  if __name__ == "__main__":
460
- interface.launch(debug=True)
 
4
  import numpy as np
5
  import time
6
  from ultralytics import YOLO
 
7
  import os
8
+ import spaces
9
 
10
  class CrowdDetection:
11
  def __init__(self, model_path="yolov8n.pt"):
 
27
  raise ValueError(f"❌ Failed to open video: {video_path}")
28
 
29
  fps = int(cap.get(cv2.CAP_PROP_FPS))
30
+ width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH) * 0.5)
31
+ height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT) * 0.5)
32
 
33
  output_path = "output_crowd.mp4"
34
  fourcc = cv2.VideoWriter_fourcc(*"mp4v")
 
44
  ret, frame = cap.read()
45
  if not ret:
46
  break
47
+ frame = cv2.resize(frame, (width, height))
48
  frame_count += 1
49
 
50
  results = model(frame)
 
75
  class PeopleTracking:
76
  def __init__(self, yolo_model_path="yolov8n.pt"):
77
  self.model_path = yolo_model_path
78
+
79
  @spaces.GPU
80
  def people_tracking(self, video_path):
81
  try:
 
92
  raise ValueError(f"❌ Failed to open video: {video_path}")
93
 
94
  fps = int(cap.get(cv2.CAP_PROP_FPS))
95
+ width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH) * 0.5)
96
+ height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT) * 0.5)
97
  output_path = "output_tracking.mp4"
98
  out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*"mp4v"), fps, (width, height))
99
  if not out.isOpened():
 
105
  if not ret:
106
  break
107
 
108
+ frame = cv2.resize(frame, (width, height))
109
  results = model.track(frame, persist=True)
110
  for result in results:
111
  boxes = result.boxes.xyxy.cpu().numpy()
 
131
  class FallDetection:
132
  def __init__(self, yolo_model_path="yolov8l.pt"):
133
  self.model_path = yolo_model_path
134
+
135
  @spaces.GPU
136
  def fall_detect(self, video_path):
137
  try:
 
148
  raise ValueError(f"❌ Failed to open video: {video_path}")
149
 
150
  fps = int(cap.get(cv2.CAP_PROP_FPS))
151
+ width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH) * 0.5)
152
+ height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT) * 0.5)
153
  output_path = "output_fall.mp4"
154
  out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*"mp4v"), fps, (width, height))
155
  if not out.isOpened():
 
160
  ret, frame = cap.read()
161
  if not ret:
162
  break
163
+ frame = cv2.resize(frame, (width, height))
164
  results = model(frame)
165
  for result in results:
166
  boxes = result.boxes.xyxy.cpu().numpy()
 
196
  class FightDetection:
197
  def __init__(self, yolo_model_path="yolov8n-pose.pt"):
198
  self.model_path = yolo_model_path
199
+
200
  @spaces.GPU
201
  def fight_detect(self, video_path):
202
  try:
 
213
  raise ValueError(f"❌ Failed to open video: {video_path}")
214
 
215
  fps = int(cap.get(cv2.CAP_PROP_FPS))
216
+ width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH) * 0.5)
217
+ height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT) * 0.5)
218
  output_path = "output_fight.mp4"
219
  out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*"mp4v"), fps, (width, height))
220
  if not out.isOpened():
 
225
  ret, frame = cap.read()
226
  if not ret:
227
  break
228
+
229
+ frame = cv2.resize(frame, (width, height))
230
  results = model.track(frame, persist=True)
231
  fight_detected = False
232
  person_count = 0
 
265
  self.max_intrusion_time = max_intrusion_time
266
  self.iou_threshold = iou_threshold
267
  self.conf_threshold = conf_threshold
268
+
269
  @spaces.GPU
 
270
  def intrusion_detect(self, video_path):
271
  try:
272
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 
282
  raise ValueError(f"❌ Failed to open video: {video_path}")
283
 
284
  fps = int(cap.get(cv2.CAP_PROP_FPS))
285
+ width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH) * 0.5)
286
+ height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT) * 0.5)
287
 
288
  output_path = "output_intrusion.mp4"
289
  fourcc = cv2.VideoWriter_fourcc(*"mp4v")
 
299
  if not ret:
300
  break
301
  frame_count += 1
302
+ frame = cv2.resize(frame, (width, height))
303
 
304
  results = model(frame)
305
  for result in results:
 
330
  from ultralytics import YOLO
331
  from shapely.geometry import Point, Polygon
332
  import time
333
+ import tempfile
334
+ import moviepy.editor as mpy
335
+
336
+ class FireAndSmokeDetection:
337
+ def __init__(self, model_path='fire_model.pt'):
338
+ self.model_path = model_path
339
+
340
+ @spaces.GPU
341
+ def fire_and_smoke_detect(self, video_path):
342
+ model = YOLO(self.model_path, task="detect")
343
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
344
+ model.to(device)
345
 
346
+ cap = cv2.VideoCapture(video_path)
347
+
348
+ fps = cap.get(cv2.CAP_PROP_FPS)
349
+ if not fps or fps == 0:
350
+ fps = 30
351
+ fps = int(fps)
352
+
353
+ frames = []
354
+ while cap.isOpened():
355
+ ret, frame = cap.read()
356
+ if not ret:
357
+ break
358
+ frames.append(frame)
359
+ cap.release()
360
+
361
+ if not frames:
362
+ return None
363
+
364
+ processed_frames = []
365
+ total_frames = len(frames)
366
+
367
+ # Process frames one by one (with progress feedback)
368
+ for i, frame in enumerate(frames):
369
+ result = model(frame)
370
+ processed_frame = result[0].plot()
371
+ processed_frames.append(processed_frame)
372
+
373
+ # Convert frames from BGR (OpenCV) to RGB (MoviePy expects RGB)
374
+ processed_frames_rgb = [cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) for frame in processed_frames]
375
+
376
+ # Use MoviePy to assemble the video file using H.264 encoding
377
+ output_video_path = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4").name
378
+ clip = mpy.ImageSequenceClip(processed_frames_rgb, fps=fps)
379
+ clip.write_videofile(output_video_path, codec='libx264', audio=False, verbose=False, logger=None)
380
+
381
+ return output_video_path
382
+
383
+
384
+ class LoiteringDetection:
385
  def __init__(self, model_path='loitering_model.pt'):
386
+ self.model_path = model_path
387
 
388
  @spaces.GPU
389
  def loitering_detect(self, video_path, area):
390
  # Create polygon zone
391
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
392
+ model = YOLO(self.model_path)
393
+ model.to(device)
394
+ person_info = {}
395
  time_threshold = 7
396
  detection_threshold = 0.6
397
  zone_points = None
398
  if area == '131':
399
  zone_points = [(842//1.5, 514//1.7), (686//1.5, 290//1.7), (775//1.5, 279//1.7), (961//1.5, 488//1.7)]
400
  elif area == '145':
401
+ zone_points = [(153//1.8, 850//1.7), (139//1.8, 535//1.7), (239//1.8, 497//1.7), (291//1.8, 857//1.7)]
402
  zone = Polygon(zone_points)
403
 
404
  # Open video
 
419
 
420
  frame = cv2.resize(frame, (width, height))
421
  # Perform object detection and tracking
422
+ results = model.track(frame, persist=True, classes=[0], conf=detection_threshold) # 0 is the class ID for person
423
 
424
  # List to store time information for display
425
  time_display = []
 
479
  "Fall Detection": FallDetection,
480
  "Fight Detection": FightDetection,
481
  "Intrusion Detection": IntrusionDetection,
482
+ "Loitering Detection": LoiteringDetection,
483
+ "Fire And Smoke Detection": FireAndSmokeDetection
484
  }
485
 
486
  try:
 
488
  method_name = feature.lower().replace(" ", "_").replace("detection", "detect") # Ensures correct method name
489
 
490
  if feature == "Loitering Detection":
491
+ output_path = detector.loitering_detect(video, area) # Pass area if required
492
  else:
493
  output_path = getattr(detector, method_name)(video)
494
 
 
502
  inputs=[
503
  gr.Dropdown(choices=[
504
  "Crowd Detection", "People Tracking", "Fall Detection",
505
+ "Fight Detection", "Intrusion Detection", "Loitering Detection",
506
+ "Fire And Smoke Detection"
507
  ], label="Select Feature"),
508
  gr.Video(label="Upload Video"),
509
+ gr.Textbox(label="Loitering Area (131 or 145)")
510
  ],
511
  outputs=[
512
  gr.Textbox(label="Status"),
 
517
  )
518
 
519
  if __name__ == "__main__":
520
+ interface.launch(debug=True)