Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,7 +3,6 @@ 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"
|
@@ -21,7 +20,7 @@ CONFIDENCE_THRESHOLD = 0.5
|
|
21 |
# Distance threshold to avoid duplicate counts
|
22 |
DISTANCE_THRESHOLD = 50
|
23 |
|
24 |
-
def count_unique_trucks(video_path
|
25 |
cap = cv2.VideoCapture(video_path)
|
26 |
if not cap.isOpened():
|
27 |
return "Error: Unable to open video file."
|
@@ -30,14 +29,8 @@ def count_unique_trucks(video_path, time_interval):
|
|
30 |
truck_history = {}
|
31 |
|
32 |
# Get FPS of the video
|
33 |
-
fps = cap.get(cv2.CAP_PROP_FPS)
|
34 |
-
|
35 |
-
# Ensure FPS is valid, fallback to default if needed
|
36 |
-
if fps is None or fps <= 0:
|
37 |
-
fps = 30 # Default fallback FPS
|
38 |
-
|
39 |
-
# Set frame skip dynamically based on FPS and time_interval
|
40 |
-
frame_skip = max(int(fps * time_interval), 1) # Ensure frame_skip is at least 1
|
41 |
|
42 |
frame_count = 0
|
43 |
|
@@ -48,7 +41,7 @@ def count_unique_trucks(video_path, time_interval):
|
|
48 |
|
49 |
frame_count += 1
|
50 |
if frame_count % frame_skip != 0:
|
51 |
-
continue # Skip frames
|
52 |
|
53 |
# Run YOLOv12x inference
|
54 |
results = model(frame, verbose=False)
|
@@ -96,22 +89,21 @@ def count_unique_trucks(video_path, time_interval):
|
|
96 |
return {"Total Unique Trucks": len(unique_truck_ids)}
|
97 |
|
98 |
# Gradio UI function
|
99 |
-
def analyze_video(video_file
|
100 |
-
result = count_unique_trucks(video_file
|
101 |
return "\n".join([f"{key}: {value}" for key, value in result.items()])
|
102 |
|
103 |
-
# Define Gradio interface
|
|
|
104 |
iface = gr.Interface(
|
105 |
fn=analyze_video,
|
106 |
-
inputs=
|
107 |
-
gr.Video(label="Upload Video"),
|
108 |
-
gr.Slider(minimum=1, maximum=20, step=1, value=7, label="Time Interval (Seconds)")
|
109 |
-
],
|
110 |
outputs=gr.Textbox(label="Analysis Result"),
|
111 |
title="YOLOv12x Unique Truck Counter",
|
112 |
-
description="Upload a video
|
113 |
)
|
114 |
|
115 |
# Launch the Gradio app
|
116 |
if __name__ == "__main__":
|
117 |
iface.launch()
|
|
|
|
3 |
import torch
|
4 |
from ultralytics import YOLO
|
5 |
from sort import Sort
|
|
|
6 |
|
7 |
# Load YOLOv12x model
|
8 |
MODEL_PATH = "yolov12x.pt"
|
|
|
20 |
# Distance threshold to avoid duplicate counts
|
21 |
DISTANCE_THRESHOLD = 50
|
22 |
|
23 |
+
def count_unique_trucks(video_path):
|
24 |
cap = cv2.VideoCapture(video_path)
|
25 |
if not cap.isOpened():
|
26 |
return "Error: Unable to open video file."
|
|
|
29 |
truck_history = {}
|
30 |
|
31 |
# Get FPS of the video
|
32 |
+
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
33 |
+
frame_skip = fps * 2 # Skip frames every 5 seconds
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
frame_count = 0
|
36 |
|
|
|
41 |
|
42 |
frame_count += 1
|
43 |
if frame_count % frame_skip != 0:
|
44 |
+
continue # Skip frames to process only every 5 seconds
|
45 |
|
46 |
# Run YOLOv12x inference
|
47 |
results = model(frame, verbose=False)
|
|
|
89 |
return {"Total Unique Trucks": len(unique_truck_ids)}
|
90 |
|
91 |
# Gradio UI function
|
92 |
+
def analyze_video(video_file):
|
93 |
+
result = count_unique_trucks(video_file)
|
94 |
return "\n".join([f"{key}: {value}" for key, value in result.items()])
|
95 |
|
96 |
+
# Define Gradio interface
|
97 |
+
import gradio as gr
|
98 |
iface = gr.Interface(
|
99 |
fn=analyze_video,
|
100 |
+
inputs=gr.Video(label="Upload Video"),
|
|
|
|
|
|
|
101 |
outputs=gr.Textbox(label="Analysis Result"),
|
102 |
title="YOLOv12x Unique Truck Counter",
|
103 |
+
description="Upload a video to count unique trucks using YOLOv12x and SORT tracking."
|
104 |
)
|
105 |
|
106 |
# Launch the Gradio app
|
107 |
if __name__ == "__main__":
|
108 |
iface.launch()
|
109 |
+
|