Update app.py
Browse files
app.py
CHANGED
@@ -1,22 +1,25 @@
|
|
1 |
import gradio as gr
|
2 |
import cv2
|
3 |
import numpy as np
|
4 |
-
import torch
|
5 |
from ultralytics import YOLO
|
|
|
6 |
|
7 |
# Load YOLOv8 model (pre-trained on COCO dataset)
|
8 |
-
model = YOLO("yolov8x.pt") #
|
9 |
|
10 |
# Class label for trucks (COCO dataset)
|
11 |
TRUCK_CLASS_ID = 7 # "truck" in COCO dataset
|
12 |
|
|
|
|
|
|
|
13 |
def count_trucks(video_path):
|
14 |
cap = cv2.VideoCapture(video_path)
|
15 |
if not cap.isOpened():
|
16 |
return "Error: Unable to open video file."
|
17 |
|
18 |
frame_count = 0
|
19 |
-
|
20 |
frame_skip = 5 # Process every 5th frame for efficiency
|
21 |
|
22 |
while True:
|
@@ -26,27 +29,39 @@ def count_trucks(video_path):
|
|
26 |
|
27 |
frame_count += 1
|
28 |
if frame_count % frame_skip != 0:
|
29 |
-
continue # Skip frames
|
30 |
|
31 |
# Run YOLOv8 inference
|
32 |
results = model(frame, verbose=False)
|
33 |
|
34 |
-
|
35 |
for result in results:
|
36 |
for box in result.boxes:
|
37 |
class_id = int(box.cls.item()) # Get class ID
|
38 |
confidence = float(box.conf.item()) # Get confidence score
|
|
|
39 |
|
40 |
if class_id == TRUCK_CLASS_ID and confidence > 0.6:
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
|
|
|
|
|
|
|
44 |
|
45 |
cap.release()
|
46 |
|
47 |
return {
|
48 |
-
"Total Trucks in Video":
|
49 |
-
#"Avg Trucks Per Frame": round(np.mean(truck_count_per_frame), 2) if truck_count_per_frame else 0
|
50 |
}
|
51 |
|
52 |
# Gradio UI function
|
@@ -59,8 +74,8 @@ interface = gr.Interface(
|
|
59 |
fn=analyze_video,
|
60 |
inputs=gr.Video(label="Upload Video"),
|
61 |
outputs=gr.Textbox(label="Truck Counting Results"),
|
62 |
-
title="YOLOv8-based Truck Counter",
|
63 |
-
description="Upload a video to detect and count trucks using YOLOv8."
|
64 |
)
|
65 |
|
66 |
# Launch app
|
|
|
1 |
import gradio as gr
|
2 |
import cv2
|
3 |
import numpy as np
|
|
|
4 |
from ultralytics import YOLO
|
5 |
+
from sort import Sort # SORT tracker
|
6 |
|
7 |
# Load YOLOv8 model (pre-trained on COCO dataset)
|
8 |
+
model = YOLO("yolov8x.pt") # Highest accuracy version
|
9 |
|
10 |
# Class label for trucks (COCO dataset)
|
11 |
TRUCK_CLASS_ID = 7 # "truck" in COCO dataset
|
12 |
|
13 |
+
# Initialize SORT tracker
|
14 |
+
tracker = Sort()
|
15 |
+
|
16 |
def count_trucks(video_path):
|
17 |
cap = cv2.VideoCapture(video_path)
|
18 |
if not cap.isOpened():
|
19 |
return "Error: Unable to open video file."
|
20 |
|
21 |
frame_count = 0
|
22 |
+
unique_truck_ids = set()
|
23 |
frame_skip = 5 # Process every 5th frame for efficiency
|
24 |
|
25 |
while True:
|
|
|
29 |
|
30 |
frame_count += 1
|
31 |
if frame_count % frame_skip != 0:
|
32 |
+
continue # Skip frames for efficiency
|
33 |
|
34 |
# Run YOLOv8 inference
|
35 |
results = model(frame, verbose=False)
|
36 |
|
37 |
+
detections = []
|
38 |
for result in results:
|
39 |
for box in result.boxes:
|
40 |
class_id = int(box.cls.item()) # Get class ID
|
41 |
confidence = float(box.conf.item()) # Get confidence score
|
42 |
+
x1, y1, x2, y2 = map(int, box.xyxy[0]) # Get bounding box
|
43 |
|
44 |
if class_id == TRUCK_CLASS_ID and confidence > 0.6:
|
45 |
+
detections.append([x1, y1, x2, y2, confidence]) # Append detection
|
46 |
+
|
47 |
+
# Convert to numpy array for SORT input
|
48 |
+
if len(detections) > 0:
|
49 |
+
detections = np.array(detections)
|
50 |
+
else:
|
51 |
+
detections = np.empty((0, 5)) # Empty array when no trucks detected
|
52 |
+
|
53 |
+
# Update tracker
|
54 |
+
tracked_objects = tracker.update(detections)
|
55 |
|
56 |
+
# Store unique truck IDs
|
57 |
+
for obj in tracked_objects:
|
58 |
+
truck_id = int(obj[4]) # SORT assigns unique IDs
|
59 |
+
unique_truck_ids.add(truck_id)
|
60 |
|
61 |
cap.release()
|
62 |
|
63 |
return {
|
64 |
+
"Total Unique Trucks in Video": len(unique_truck_ids)
|
|
|
65 |
}
|
66 |
|
67 |
# Gradio UI function
|
|
|
74 |
fn=analyze_video,
|
75 |
inputs=gr.Video(label="Upload Video"),
|
76 |
outputs=gr.Textbox(label="Truck Counting Results"),
|
77 |
+
title="YOLOv8-based Truck Counter with Object Tracking",
|
78 |
+
description="Upload a video to detect and count unique trucks using YOLOv8 and SORT tracker."
|
79 |
)
|
80 |
|
81 |
# Launch app
|