Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,7 +8,67 @@ import spaces
|
|
8 |
|
9 |
@spaces.GPU
|
10 |
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
class PeopleTracking:
|
13 |
def __init__(self, yolo_model_path="yolov8n.pt"):
|
14 |
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
@@ -128,6 +188,7 @@ class FightDetection:
|
|
128 |
# Function to process video based on selected feature
|
129 |
def process_video(feature, video):
|
130 |
detectors = {
|
|
|
131 |
"People Tracking": PeopleTracking,
|
132 |
"Fall Detection": FallDetection,
|
133 |
"Fight Detection": FightDetection
|
@@ -141,7 +202,7 @@ def process_video(feature, video):
|
|
141 |
interface = gr.Interface(
|
142 |
fn=process_video,
|
143 |
inputs=[
|
144 |
-
gr.Dropdown(choices=["People Tracking", "Fall Detection", "Fight Detection"], label="Select Feature"),
|
145 |
gr.Video(label="Upload Video")
|
146 |
],
|
147 |
outputs=gr.Video(label="Processed Video"),
|
|
|
8 |
|
9 |
@spaces.GPU
|
10 |
|
11 |
+
class CrowdDetection:
|
12 |
+
def __init__(self, model_path="yolov8n.pt", crowd_threshold=10):
|
13 |
+
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
14 |
+
print(f"🔍 Using device: {self.device}")
|
15 |
+
self.model = YOLO(model_path).to(self.device)
|
16 |
+
self.crowd_threshold = crowd_threshold
|
17 |
+
|
18 |
+
def detect_crowd(self, video_path):
|
19 |
+
cap = cv2.VideoCapture(video_path)
|
20 |
+
if not cap.isOpened():
|
21 |
+
raise ValueError(f"❌ Failed to open video: {video_path}")
|
22 |
+
|
23 |
+
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
24 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
25 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
26 |
+
print(f"🎥 Video details - FPS: {fps}, Width: {width}, Height: {height}")
|
27 |
+
|
28 |
+
output_path = "output_crowd.mp4"
|
29 |
+
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
|
30 |
+
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
|
31 |
+
|
32 |
+
frame_count = 0
|
33 |
+
|
34 |
+
while cap.isOpened():
|
35 |
+
ret, frame = cap.read()
|
36 |
+
if not ret:
|
37 |
+
break
|
38 |
+
|
39 |
+
frame_count += 1
|
40 |
+
results = self.model(frame)
|
41 |
+
person_count = 0
|
42 |
+
|
43 |
+
for result in results:
|
44 |
+
boxes = result.boxes.xyxy.cpu().numpy()
|
45 |
+
classes = result.boxes.cls.cpu().numpy()
|
46 |
+
|
47 |
+
for box, cls in zip(boxes, classes):
|
48 |
+
if int(cls) == 0: # Class ID 0 = "person"
|
49 |
+
person_count += 1
|
50 |
+
x1, y1, x2, y2 = map(int, box)
|
51 |
+
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
52 |
+
cv2.putText(frame, "Person", (x1, y1 - 10),
|
53 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
|
54 |
+
|
55 |
+
alert_text = "Crowd Alert!" if person_count > self.crowd_threshold else f"People: {person_count}"
|
56 |
+
cv2.putText(frame, alert_text, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1,
|
57 |
+
(0, 0, 255) if person_count > self.crowd_threshold else (0, 255, 0), 2)
|
58 |
+
out.write(frame)
|
59 |
+
|
60 |
+
cap.release()
|
61 |
+
out.release()
|
62 |
+
|
63 |
+
if frame_count == 0:
|
64 |
+
raise ValueError("❌ No frames were processed!")
|
65 |
+
|
66 |
+
if not os.path.exists(output_path):
|
67 |
+
raise FileNotFoundError(f"❌ Output video not found: {output_path}")
|
68 |
+
|
69 |
+
print(f"✅ Processed video saved at: {output_path}")
|
70 |
+
return output_path
|
71 |
+
|
72 |
class PeopleTracking:
|
73 |
def __init__(self, yolo_model_path="yolov8n.pt"):
|
74 |
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
188 |
# Function to process video based on selected feature
|
189 |
def process_video(feature, video):
|
190 |
detectors = {
|
191 |
+
"Crowd Detection": CrowdDetection,
|
192 |
"People Tracking": PeopleTracking,
|
193 |
"Fall Detection": FallDetection,
|
194 |
"Fight Detection": FightDetection
|
|
|
202 |
interface = gr.Interface(
|
203 |
fn=process_video,
|
204 |
inputs=[
|
205 |
+
gr.Dropdown(choices=["Crowd Detection", "People Tracking", "Fall Detection", "Fight Detection"], label="Select Feature"),
|
206 |
gr.Video(label="Upload Video")
|
207 |
],
|
208 |
outputs=gr.Video(label="Processed Video"),
|