hb-setosys commited on
Commit
a210028
·
verified ·
1 Parent(s): 4e5fb8f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -6
app.py CHANGED
@@ -22,7 +22,8 @@ def count_unique_trucks(video_path):
22
 
23
  unique_truck_ids = set()
24
  frame_skip = 5 # Process every 5th frame for efficiency
25
- truck_tracker_history = {} # To store truck ID with frame count
 
26
 
27
  frame_count = 0
28
  while True:
@@ -54,15 +55,32 @@ def count_unique_trucks(video_path):
54
 
55
  for obj in tracked_objects:
56
  truck_id = int(obj[4]) # Unique ID assigned by SORT
 
57
 
58
- # Only add truck ID if it appears in multiple frames (avoid one-frame detections)
 
 
 
59
  if truck_id not in truck_tracker_history:
60
- truck_tracker_history[truck_id] = frame_count
 
 
 
 
61
  else:
62
- # If truck ID appears in multiple frames, count as unique
63
- if frame_count - truck_tracker_history[truck_id] > frame_skip:
 
 
 
 
 
 
64
  unique_truck_ids.add(truck_id)
65
- truck_tracker_history[truck_id] = frame_count
 
 
 
66
 
67
  cap.release()
68
 
 
22
 
23
  unique_truck_ids = set()
24
  frame_skip = 5 # Process every 5th frame for efficiency
25
+ truck_tracker_history = {} # To store truck ID with frame count and position
26
+ min_distance_threshold = 50 # Minimum distance between truck positions to be counted as different
27
 
28
  frame_count = 0
29
  while True:
 
55
 
56
  for obj in tracked_objects:
57
  truck_id = int(obj[4]) # Unique ID assigned by SORT
58
+ x1, y1, x2, y2 = obj[:4] # Get the bounding box coordinates
59
 
60
+ # Compute the center of the truck for better tracking
61
+ truck_center = ((x1 + x2) / 2, (y1 + y2) / 2)
62
+
63
+ # Only add truck ID if it's sufficiently far from previously tracked trucks
64
  if truck_id not in truck_tracker_history:
65
+ truck_tracker_history[truck_id] = {
66
+ "frame_count": frame_count,
67
+ "position": truck_center
68
+ }
69
+ unique_truck_ids.add(truck_id) # Add the truck as a unique truck
70
  else:
71
+ last_truck = truck_tracker_history[truck_id]
72
+ last_position = last_truck["position"]
73
+
74
+ # Calculate the distance between the current and last positions
75
+ distance = np.linalg.norm(np.array(truck_center) - np.array(last_position))
76
+
77
+ # If the distance between truck positions is greater than the threshold, it's a new truck detection
78
+ if distance > min_distance_threshold:
79
  unique_truck_ids.add(truck_id)
80
+ truck_tracker_history[truck_id] = {
81
+ "frame_count": frame_count,
82
+ "position": truck_center
83
+ }
84
 
85
  cap.release()
86