hb-setosys commited on
Commit
611534d
·
verified ·
1 Parent(s): ea4b8f4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -21
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"
@@ -14,13 +15,7 @@ TRUCK_CLASS_ID = 7 # "truck"
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,7 +25,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 +36,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)
@@ -53,7 +48,7 @@ def count_unique_trucks(video_path):
53
  confidence = float(box.conf.item()) # Get confidence score
54
 
55
  # Track only trucks
56
- if class_id == TRUCK_CLASS_ID and confidence > CONFIDENCE_THRESHOLD:
57
  x1, y1, x2, y2 = map(int, box.xyxy[0]) # Get bounding box
58
  detections.append([x1, y1, x2, y2, confidence])
59
 
@@ -72,7 +67,7 @@ def count_unique_trucks(video_path):
72
  last_position = truck_history[truck_id]["position"]
73
  distance = np.linalg.norm(np.array(truck_center) - np.array(last_position))
74
 
75
- if distance > DISTANCE_THRESHOLD:
76
  # If the truck moved significantly, count as new
77
  unique_truck_ids.add(truck_id)
78
 
@@ -85,24 +80,27 @@ 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()
 
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"
 
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
 
26
  # Get FPS of the video
27
  fps = int(cap.get(cv2.CAP_PROP_FPS))
28
+ frame_skip = max(1, int(fps * frame_skip_seconds)) # Dynamic frame skipping
29
 
30
  frame_count = 0
31
 
 
36
 
37
  frame_count += 1
38
  if frame_count % frame_skip != 0:
39
+ continue # Skip frames dynamically
40
 
41
  # Run YOLOv12x inference
42
  results = model(frame, verbose=False)
 
48
  confidence = float(box.conf.item()) # Get confidence score
49
 
50
  # Track only trucks
51
+ if class_id == TRUCK_CLASS_ID and confidence > confidence_threshold:
52
  x1, y1, x2, y2 = map(int, box.xyxy[0]) # Get bounding box
53
  detections.append([x1, y1, x2, y2, confidence])
54
 
 
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 > distance_threshold:
71
  # If the truck moved significantly, count as new
72
  unique_truck_ids.add(truck_id)
73
 
 
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, video_type, confidence, distance, frame_skip):
87
+ return count_unique_trucks(video_file, video_type, confidence, distance, frame_skip)
 
88
 
89
  # Define Gradio interface
 
90
  iface = gr.Interface(
91
  fn=analyze_video,
92
+ inputs=[
93
+ gr.Video(label="Upload Video"),
94
+ gr.Radio(["drone", "fixed"], label="Video Type"),
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.JSON(label="Analysis Result"),
100
+ title="YOLOv12x Dynamic Truck Counter",
101
+ description="Upload a video, adjust parameters, and analyze unique trucks."
102
  )
103
 
104
  # Launch the Gradio app
105
  if __name__ == "__main__":
106
+ iface.launch()