mkhodary101 commited on
Commit
eaf5124
·
verified ·
1 Parent(s): 970d1a0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -7
app.py CHANGED
@@ -1,10 +1,11 @@
1
  import gradio as gr
2
  import torch
3
  import cv2
 
4
  from ultralytics import YOLO
5
- import spaces
6
- @spaces.GPU
7
 
 
8
 
9
  class CrowdDetection:
10
  def __init__(self, yolo_model_path="yolov8n.pt", crowd_threshold=10):
@@ -30,9 +31,16 @@ class CrowdDetection:
30
  raise ValueError("YOLO model failed to load")
31
 
32
  cap = cv2.VideoCapture(video_path)
 
 
 
 
 
33
  output_path = "output_crowd.mp4"
 
 
34
  fourcc = cv2.VideoWriter_fourcc(*"mp4v")
35
- out = cv2.VideoWriter(output_path, fourcc, int(cap.get(cv2.CAP_PROP_FPS)),
36
  (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))))
37
 
38
  while cap.isOpened():
@@ -64,16 +72,29 @@ class CrowdDetection:
64
 
65
  cap.release()
66
  out.release()
67
- return output_path
 
 
 
 
 
 
68
 
69
  def process_video(video):
70
  try:
 
 
71
  detector = CrowdDetection()
72
  output_video = detector.detect_crowd(video)
 
 
 
 
 
73
  return output_video
74
  except Exception as e:
75
  print(f"Video processing error: {e}")
76
- return None
77
 
78
  # Gradio Interface for Hugging Face Spaces
79
  interface = gr.Interface(
@@ -83,6 +104,5 @@ interface = gr.Interface(
83
  title="Crowd Detection using YOLOv8"
84
  )
85
 
86
- # Remove share=True for Hugging Face Spaces
87
  if __name__ == "__main__":
88
- interface.launch()
 
1
  import gradio as gr
2
  import torch
3
  import cv2
4
+ import os
5
  from ultralytics import YOLO
6
+ import spaces
 
7
 
8
+ @spaces.GPU # Ensures GPU is allocated for this function
9
 
10
  class CrowdDetection:
11
  def __init__(self, yolo_model_path="yolov8n.pt", crowd_threshold=10):
 
31
  raise ValueError("YOLO model failed to load")
32
 
33
  cap = cv2.VideoCapture(video_path)
34
+
35
+ # Ensure video is opened successfully
36
+ if not cap.isOpened():
37
+ raise ValueError(f"Failed to open video: {video_path}")
38
+
39
  output_path = "output_crowd.mp4"
40
+ output_full_path = os.path.abspath(output_path) # Convert to absolute path
41
+
42
  fourcc = cv2.VideoWriter_fourcc(*"mp4v")
43
+ out = cv2.VideoWriter(output_full_path, fourcc, int(cap.get(cv2.CAP_PROP_FPS)),
44
  (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))))
45
 
46
  while cap.isOpened():
 
72
 
73
  cap.release()
74
  out.release()
75
+
76
+ # Ensure output file exists before returning
77
+ if not os.path.exists(output_full_path):
78
+ raise FileNotFoundError(f"Output video not found: {output_full_path}")
79
+
80
+ print(f"Processed video saved at: {output_full_path}")
81
+ return output_full_path
82
 
83
  def process_video(video):
84
  try:
85
+ print(f"Received video: {video}")
86
+
87
  detector = CrowdDetection()
88
  output_video = detector.detect_crowd(video)
89
+
90
+ if not os.path.exists(output_video): # Ensure output file exists
91
+ raise FileNotFoundError(f"Output video does not exist: {output_video}")
92
+
93
+ print(f"Returning processed video: {output_video}")
94
  return output_video
95
  except Exception as e:
96
  print(f"Video processing error: {e}")
97
+ return None # Prevent crashing the app
98
 
99
  # Gradio Interface for Hugging Face Spaces
100
  interface = gr.Interface(
 
104
  title="Crowd Detection using YOLOv8"
105
  )
106
 
 
107
  if __name__ == "__main__":
108
+ interface.launch()