Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,24 +3,27 @@ import numpy as np
|
|
3 |
import torch
|
4 |
import gradio as gr
|
5 |
from ultralytics import YOLO
|
|
|
6 |
|
7 |
# Load YOLOv12x model
|
8 |
-
MODEL_PATH = "yolov12x.pt"
|
9 |
model = YOLO(MODEL_PATH)
|
10 |
|
11 |
-
# COCO dataset class
|
12 |
-
|
13 |
-
TRUCK_CLASS_ID = 7 # "truck"
|
14 |
|
15 |
-
|
|
|
|
|
|
|
16 |
cap = cv2.VideoCapture(video_path)
|
17 |
if not cap.isOpened():
|
18 |
return "Error: Unable to open video file."
|
19 |
|
20 |
-
|
21 |
-
object_counts = {"people": [], "trucks": []}
|
22 |
frame_skip = 5 # Process every 5th frame for efficiency
|
23 |
|
|
|
24 |
while True:
|
25 |
ret, frame = cap.read()
|
26 |
if not ret:
|
@@ -33,31 +36,32 @@ def count_objects(video_path):
|
|
33 |
# Run YOLOv12x inference
|
34 |
results = model(frame, verbose=False)
|
35 |
|
36 |
-
|
37 |
for result in results:
|
38 |
for box in result.boxes:
|
39 |
class_id = int(box.cls.item()) # Get class ID
|
40 |
confidence = float(box.conf.item()) # Get confidence score
|
41 |
|
42 |
-
#
|
43 |
-
if class_id ==
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
47 |
|
48 |
-
|
49 |
-
|
|
|
50 |
|
51 |
cap.release()
|
52 |
|
53 |
-
return {
|
54 |
-
#"Max People in a Frame": int(np.max(object_counts["people"])) if object_counts["people"] else 0,
|
55 |
-
"Truck Count": int(np.max(object_counts["trucks"])) if object_counts["trucks"] else 0
|
56 |
-
}
|
57 |
|
58 |
# Gradio UI function
|
59 |
def analyze_video(video_file):
|
60 |
-
result =
|
61 |
return "\n".join([f"{key}: {value}" for key, value in result.items()])
|
62 |
|
63 |
# Define Gradio interface
|
@@ -65,10 +69,10 @@ iface = gr.Interface(
|
|
65 |
fn=analyze_video,
|
66 |
inputs=gr.Video(label="Upload Video"),
|
67 |
outputs=gr.Textbox(label="Analysis Result"),
|
68 |
-
title="YOLOv12x
|
69 |
-
description="Upload a video to count
|
70 |
)
|
71 |
|
72 |
# Launch the Gradio app
|
73 |
if __name__ == "__main__":
|
74 |
-
iface.launch()
|
|
|
3 |
import torch
|
4 |
import gradio as gr
|
5 |
from ultralytics import YOLO
|
6 |
+
from sort import Sort # SORT tracker
|
7 |
|
8 |
# Load YOLOv12x model
|
9 |
+
MODEL_PATH = "yolov12x.pt"
|
10 |
model = YOLO(MODEL_PATH)
|
11 |
|
12 |
+
# COCO dataset class ID for truck
|
13 |
+
TRUCK_CLASS_ID = 7 # "truck"
|
|
|
14 |
|
15 |
+
# Initialize SORT tracker
|
16 |
+
tracker = Sort()
|
17 |
+
|
18 |
+
def count_unique_trucks(video_path):
|
19 |
cap = cv2.VideoCapture(video_path)
|
20 |
if not cap.isOpened():
|
21 |
return "Error: Unable to open video file."
|
22 |
|
23 |
+
unique_truck_ids = set()
|
|
|
24 |
frame_skip = 5 # Process every 5th frame for efficiency
|
25 |
|
26 |
+
frame_count = 0
|
27 |
while True:
|
28 |
ret, frame = cap.read()
|
29 |
if not ret:
|
|
|
36 |
# Run YOLOv12x inference
|
37 |
results = model(frame, verbose=False)
|
38 |
|
39 |
+
detections = []
|
40 |
for result in results:
|
41 |
for box in result.boxes:
|
42 |
class_id = int(box.cls.item()) # Get class ID
|
43 |
confidence = float(box.conf.item()) # Get confidence score
|
44 |
|
45 |
+
# Track only trucks
|
46 |
+
if class_id == TRUCK_CLASS_ID and confidence > 0.5:
|
47 |
+
x1, y1, x2, y2 = map(int, box.xyxy[0]) # Get bounding box
|
48 |
+
detections.append([x1, y1, x2, y2, confidence])
|
49 |
+
|
50 |
+
if len(detections) > 0:
|
51 |
+
detections = np.array(detections)
|
52 |
+
tracked_objects = tracker.update(detections)
|
53 |
|
54 |
+
for obj in tracked_objects:
|
55 |
+
truck_id = int(obj[4]) # Unique ID assigned by SORT
|
56 |
+
unique_truck_ids.add(truck_id)
|
57 |
|
58 |
cap.release()
|
59 |
|
60 |
+
return {"Total Unique Trucks": len(unique_truck_ids)}
|
|
|
|
|
|
|
61 |
|
62 |
# Gradio UI function
|
63 |
def analyze_video(video_file):
|
64 |
+
result = count_unique_trucks(video_file)
|
65 |
return "\n".join([f"{key}: {value}" for key, value in result.items()])
|
66 |
|
67 |
# Define Gradio interface
|
|
|
69 |
fn=analyze_video,
|
70 |
inputs=gr.Video(label="Upload Video"),
|
71 |
outputs=gr.Textbox(label="Analysis Result"),
|
72 |
+
title="YOLOv12x Unique Truck Counter",
|
73 |
+
description="Upload a video to count unique trucks using YOLOv12x and SORT tracking."
|
74 |
)
|
75 |
|
76 |
# Launch the Gradio app
|
77 |
if __name__ == "__main__":
|
78 |
+
iface.launch()
|