Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,21 +1,4 @@
|
|
1 |
-
|
2 |
-
import numpy as np
|
3 |
-
import torch
|
4 |
-
from ultralytics import YOLO
|
5 |
-
from sort import Sort
|
6 |
-
import gradio as gr
|
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, video_type, confidence_threshold, distance_threshold, frame_skip_seconds):
|
19 |
cap = cv2.VideoCapture(video_path)
|
20 |
if not cap.isOpened():
|
21 |
return "Error: Unable to open video file."
|
@@ -25,7 +8,7 @@ def count_unique_trucks(video_path, video_type, confidence_threshold, distance_t
|
|
25 |
|
26 |
# Get FPS of the video
|
27 |
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
28 |
-
frame_skip =
|
29 |
|
30 |
frame_count = 0
|
31 |
|
@@ -48,7 +31,7 @@ def count_unique_trucks(video_path, video_type, confidence_threshold, distance_t
|
|
48 |
confidence = float(box.conf.item()) # Get confidence score
|
49 |
|
50 |
# Track only trucks
|
51 |
-
if class_id == TRUCK_CLASS_ID and confidence >
|
52 |
x1, y1, x2, y2 = map(int, box.xyxy[0]) # Get bounding box
|
53 |
detections.append([x1, y1, x2, y2, confidence])
|
54 |
|
@@ -67,7 +50,7 @@ def count_unique_trucks(video_path, video_type, confidence_threshold, distance_t
|
|
67 |
last_position = truck_history[truck_id]["position"]
|
68 |
distance = np.linalg.norm(np.array(truck_center) - np.array(last_position))
|
69 |
|
70 |
-
if distance >
|
71 |
# If the truck moved significantly, count as new
|
72 |
unique_truck_ids.add(truck_id)
|
73 |
|
@@ -80,25 +63,25 @@ def count_unique_trucks(video_path, video_type, confidence_threshold, distance_t
|
|
80 |
unique_truck_ids.add(truck_id)
|
81 |
|
82 |
cap.release()
|
|
|
83 |
return {"Total Unique Trucks": len(unique_truck_ids)}
|
84 |
|
85 |
# Gradio UI function
|
86 |
-
def analyze_video(video_file,
|
87 |
-
|
|
|
88 |
|
89 |
# Define Gradio interface
|
|
|
90 |
iface = gr.Interface(
|
91 |
fn=analyze_video,
|
92 |
inputs=[
|
93 |
gr.Video(label="Upload Video"),
|
94 |
-
gr.
|
95 |
-
gr.Slider(0.3, 0.9, 0.5, label="Confidence Threshold"),
|
96 |
-
gr.Slider(10, 100, 50, label="Distance Threshold"),
|
97 |
-
gr.Slider(1, 10, 2, label="Frame Skip (Seconds)"),
|
98 |
],
|
99 |
-
outputs=gr.
|
100 |
-
title="YOLOv12x
|
101 |
-
description="Upload a video
|
102 |
)
|
103 |
|
104 |
# Launch the Gradio app
|
|
|
1 |
+
def count_unique_trucks(video_path, frame_skip_factor=2):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
cap = cv2.VideoCapture(video_path)
|
3 |
if not cap.isOpened():
|
4 |
return "Error: Unable to open video file."
|
|
|
8 |
|
9 |
# Get FPS of the video
|
10 |
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
11 |
+
frame_skip = fps * frame_skip_factor # Skip frames based on the dynamic factor
|
12 |
|
13 |
frame_count = 0
|
14 |
|
|
|
31 |
confidence = float(box.conf.item()) # Get confidence score
|
32 |
|
33 |
# Track only trucks
|
34 |
+
if class_id == TRUCK_CLASS_ID and confidence > CONFIDENCE_THRESHOLD:
|
35 |
x1, y1, x2, y2 = map(int, box.xyxy[0]) # Get bounding box
|
36 |
detections.append([x1, y1, x2, y2, confidence])
|
37 |
|
|
|
50 |
last_position = truck_history[truck_id]["position"]
|
51 |
distance = np.linalg.norm(np.array(truck_center) - np.array(last_position))
|
52 |
|
53 |
+
if distance > DISTANCE_THRESHOLD:
|
54 |
# If the truck moved significantly, count as new
|
55 |
unique_truck_ids.add(truck_id)
|
56 |
|
|
|
63 |
unique_truck_ids.add(truck_id)
|
64 |
|
65 |
cap.release()
|
66 |
+
|
67 |
return {"Total Unique Trucks": len(unique_truck_ids)}
|
68 |
|
69 |
# Gradio UI function
|
70 |
+
def analyze_video(video_file, frame_skip_factor):
|
71 |
+
result = count_unique_trucks(video_file, frame_skip_factor)
|
72 |
+
return "\n".join([f"{key}: {value}" for key, value in result.items()])
|
73 |
|
74 |
# Define Gradio interface
|
75 |
+
import gradio as gr
|
76 |
iface = gr.Interface(
|
77 |
fn=analyze_video,
|
78 |
inputs=[
|
79 |
gr.Video(label="Upload Video"),
|
80 |
+
gr.Slider(minimum=1, maximum=10, step=1, default=2, label="Frame Skip Factor"),
|
|
|
|
|
|
|
81 |
],
|
82 |
+
outputs=gr.Textbox(label="Analysis Result"),
|
83 |
+
title="YOLOv12x Unique Truck Counter",
|
84 |
+
description="Upload a video to count unique trucks using YOLOv12x and SORT tracking. Adjust the frame skip factor to control processing speed."
|
85 |
)
|
86 |
|
87 |
# Launch the Gradio app
|