Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,20 +1,88 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
-
import
|
|
|
|
|
|
|
4 |
|
5 |
-
# Function to check GPU availability
|
6 |
-
@spaces.GPU # This tells Hugging Face to allocate a GPU when this function is called
|
7 |
-
def check_gpu():
|
8 |
-
if torch.cuda.is_available():
|
9 |
-
return "✅ GPU is available! CUDA device: " + torch.cuda.get_device_name(0)
|
10 |
-
else:
|
11 |
-
return "❌ No GPU detected!"
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
)
|
19 |
|
20 |
-
|
|
|
|
|
|
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):
|
11 |
+
# Determine the best available device
|
12 |
+
if torch.cuda.is_available():
|
13 |
+
self.device = torch.device("cuda")
|
14 |
+
print(f"Using CUDA Device: {torch.cuda.get_device_name(0)}")
|
15 |
+
else:
|
16 |
+
self.device = torch.device("cpu")
|
17 |
+
print("Using CPU as no CUDA device is available")
|
18 |
+
|
19 |
+
try:
|
20 |
+
# Load the YOLO model on the selected device
|
21 |
+
self.model = YOLO(yolo_model_path).to(self.device)
|
22 |
+
except Exception as e:
|
23 |
+
print(f"Error loading YOLO model: {e}")
|
24 |
+
self.model = None
|
25 |
+
|
26 |
+
self.crowd_threshold = crowd_threshold
|
27 |
+
|
28 |
+
def detect_crowd(self, video_path):
|
29 |
+
if self.model is None:
|
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():
|
39 |
+
ret, frame = cap.read()
|
40 |
+
if not ret:
|
41 |
+
break
|
42 |
+
|
43 |
+
# Perform detection
|
44 |
+
results = self.model(frame)
|
45 |
+
person_count = 0
|
46 |
+
|
47 |
+
for result in results:
|
48 |
+
boxes = result.boxes.xyxy.cpu().numpy()
|
49 |
+
classes = result.boxes.cls.cpu().numpy()
|
50 |
+
|
51 |
+
for box, cls in zip(boxes, classes):
|
52 |
+
if int(cls) == 0: # YOLO class ID 0 = "person"
|
53 |
+
person_count += 1
|
54 |
+
x1, y1, x2, y2 = map(int, box)
|
55 |
+
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
56 |
+
cv2.putText(frame, "Person", (x1, y1 - 10),
|
57 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
|
58 |
+
|
59 |
+
alert_text = "Crowd Alert!" if person_count > self.crowd_threshold else f"People: {person_count}"
|
60 |
+
cv2.putText(frame, alert_text, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1,
|
61 |
+
(0, 0, 255) if person_count > self.crowd_threshold else (0, 255, 0), 2)
|
62 |
+
|
63 |
+
out.write(frame)
|
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(
|
80 |
+
fn=process_video,
|
81 |
+
inputs=gr.Video(label="Upload Video"),
|
82 |
+
outputs=gr.Video(label="Processed Video"),
|
83 |
+
title="Crowd Detection using YOLOv8"
|
84 |
)
|
85 |
|
86 |
+
# Remove share=True for Hugging Face Spaces
|
87 |
+
if __name__ == "__main__":
|
88 |
+
interface.launch()
|