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