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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -11
app.py CHANGED
@@ -3,6 +3,7 @@ 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"
@@ -20,7 +21,7 @@ CONFIDENCE_THRESHOLD = 0.5
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,7 +31,7 @@ def count_unique_trucks(video_path):
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,7 +42,7 @@ def count_unique_trucks(video_path):
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)
@@ -57,7 +58,7 @@ def count_unique_trucks(video_path):
57
  x1, y1, x2, y2 = map(int, box.xyxy[0]) # Get bounding box
58
  detections.append([x1, y1, x2, y2, confidence])
59
 
60
- if len(detections) > 0:
61
  detections = np.array(detections)
62
  tracked_objects = tracker.update(detections)
63
 
@@ -85,25 +86,25 @@ def count_unique_trucks(video_path):
85
  unique_truck_ids.add(truck_id)
86
 
87
  cap.release()
88
-
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
-
 
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
  # Distance threshold to avoid duplicate counts
22
  DISTANCE_THRESHOLD = 50
23
 
24
+ def count_unique_trucks(video_path, frame_skip_factor):
25
  cap = cv2.VideoCapture(video_path)
26
  if not cap.isOpened():
27
  return "Error: Unable to open video file."
 
31
 
32
  # Get FPS of the video
33
  fps = int(cap.get(cv2.CAP_PROP_FPS))
34
+ frame_skip = fps * frame_skip_factor # Skip frames dynamically based on user input
35
 
36
  frame_count = 0
37
 
 
42
 
43
  frame_count += 1
44
  if frame_count % frame_skip != 0:
45
+ continue # Skip frames based on user-defined factor
46
 
47
  # Run YOLOv12x inference
48
  results = model(frame, verbose=False)
 
58
  x1, y1, x2, y2 = map(int, box.xyxy[0]) # Get bounding box
59
  detections.append([x1, y1, x2, y2, confidence])
60
 
61
+ if detections:
62
  detections = np.array(detections)
63
  tracked_objects = tracker.update(detections)
64
 
 
86
  unique_truck_ids.add(truck_id)
87
 
88
  cap.release()
 
89
  return {"Total Unique Trucks": len(unique_truck_ids)}
90
 
91
  # Gradio UI function
92
+ def analyze_video(video_file, frame_skip_factor):
93
+ result = count_unique_trucks(video_file, frame_skip_factor)
94
  return "\n".join([f"{key}: {value}" for key, value in result.items()])
95
 
96
  # Define Gradio interface
 
97
  iface = gr.Interface(
98
  fn=analyze_video,
99
+ inputs=[
100
+ gr.Video(label="Upload Video"),
101
+ gr.Slider(minimum=1, maximum=10, step=1, value=2, label="Frame Skip Factor") # Fixed default issue
102
+ ],
103
  outputs=gr.Textbox(label="Analysis Result"),
104
  title="YOLOv12x Unique Truck Counter",
105
+ description="Upload a video to count unique trucks using YOLOv12x and SORT tracking. Adjust the frame skip factor to control processing speed."
106
  )
107
 
108
  # Launch the Gradio app
109
  if __name__ == "__main__":
110
  iface.launch()