hb-setosys commited on
Commit
69545c2
·
verified ·
1 Parent(s): 94dab0b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -10
app.py CHANGED
@@ -1,4 +1,26 @@
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,7 +30,7 @@ def count_unique_trucks(video_path, frame_skip_factor=2):
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
 
@@ -19,7 +41,7 @@ def count_unique_trucks(video_path, frame_skip_factor=2):
19
 
20
  frame_count += 1
21
  if frame_count % frame_skip != 0:
22
- continue # Skip frames dynamically
23
 
24
  # Run YOLOv12x inference
25
  results = model(frame, verbose=False)
@@ -67,23 +89,21 @@ def count_unique_trucks(video_path, frame_skip_factor=2):
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, value=2, label="Frame Skip Factor"), # Fixed default value
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
88
  if __name__ == "__main__":
89
  iface.launch()
 
 
1
+ import cv2
2
+ import numpy as np
3
+ import torch
4
+ from ultralytics import YOLO
5
+ from sort import Sort
6
+
7
+ # Load YOLOv12x model
8
+ MODEL_PATH = "yolov12x.pt"
9
+ model = YOLO(MODEL_PATH)
10
+
11
+ # COCO dataset class ID for truck
12
+ TRUCK_CLASS_ID = 7 # "truck"
13
+
14
+ # Initialize SORT tracker
15
+ tracker = Sort()
16
+
17
+ # Minimum confidence threshold for detection
18
+ CONFIDENCE_THRESHOLD = 0.5
19
+
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."
 
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
+