hb-setosys commited on
Commit
1fdf523
·
verified ·
1 Parent(s): 7f2b13a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -24
app.py CHANGED
@@ -14,10 +14,10 @@ model = YOLO(MODEL_PATH)
14
  TRUCK_CLASS_ID = 7 # "truck"
15
 
16
  # Initialize SORT tracker
17
- tracker = Sort(max_age=20, min_hits=3, iou_threshold=0.3) # Improved tracking stability
18
 
19
  # Minimum confidence threshold for detection
20
- CONFIDENCE_THRESHOLD = 0.4 # Adjusted to capture more trucks
21
 
22
  # Distance threshold to avoid duplicate counts
23
  DISTANCE_THRESHOLD = 50
@@ -30,9 +30,12 @@ TIME_INTERVALS = {
30
 
31
  def determine_time_interval(video_filename):
32
  """ Determines frame skip interval based on keywords in the filename. """
 
33
  for keyword, interval in TIME_INTERVALS.items():
34
  if keyword in video_filename:
 
35
  return interval
 
36
  return 5 # Default interval
37
 
38
  def count_unique_trucks(video_path):
@@ -56,8 +59,8 @@ def count_unique_trucks(video_path):
56
  # Get total frames in the video
57
  total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
58
 
59
- # Dynamically adjust frame skipping based on FPS and movement density
60
- frame_skip = max(1, min(fps * time_interval // 2, total_frames // 10))
61
 
62
  frame_count = 0
63
 
@@ -84,36 +87,38 @@ def count_unique_trucks(video_path):
84
  x1, y1, x2, y2 = map(int, box.xyxy[0]) # Get bounding box
85
  detections.append([x1, y1, x2, y2, confidence])
86
 
87
- # Convert detections to numpy array for SORT
88
- detections = np.array(detections) if len(detections) > 0 else np.empty((0, 5))
89
 
90
- # Update SORT tracker
91
- tracked_objects = tracker.update(detections)
 
 
 
 
 
 
92
 
93
- # Track movement history to avoid duplicate counts
94
  for obj in tracked_objects:
95
  truck_id = int(obj[4]) # Unique ID assigned by SORT
96
- x1, y1, x2, y2 = obj[:4] # Get bounding box coordinates
97
 
98
  truck_center = (x1 + x2) / 2, (y1 + y2) / 2 # Calculate truck center
99
 
100
- # Entry-exit zone logic (e.g., bottom 20% of the frame)
101
- frame_height, frame_width = frame.shape[:2]
102
- entry_line = frame_height * 0.8 # Bottom 20% of the frame
103
- exit_line = frame_height * 0.2 # Top 20% of the frame
 
 
 
104
 
105
- if truck_id not in truck_history:
106
- # New truck detected
107
  truck_history[truck_id] = {
108
- "position": truck_center,
109
- "crossed_entry": truck_center[1] > entry_line,
110
- "crossed_exit": False
111
  }
112
- continue
113
-
114
- # If the truck crosses from entry to exit, count it
115
- if truck_history[truck_id]["crossed_entry"] and truck_center[1] < exit_line:
116
- truck_history[truck_id]["crossed_exit"] = True
117
  unique_truck_ids.add(truck_id)
118
 
119
  cap.release()
 
14
  TRUCK_CLASS_ID = 7 # "truck"
15
 
16
  # Initialize SORT tracker
17
+ tracker = Sort()
18
 
19
  # Minimum confidence threshold for detection
20
+ CONFIDENCE_THRESHOLD = 0.4 # Lowered for better detection
21
 
22
  # Distance threshold to avoid duplicate counts
23
  DISTANCE_THRESHOLD = 50
 
30
 
31
  def determine_time_interval(video_filename):
32
  """ Determines frame skip interval based on keywords in the filename. """
33
+ print(f"Checking filename: {video_filename}") # Debugging
34
  for keyword, interval in TIME_INTERVALS.items():
35
  if keyword in video_filename:
36
+ print(f"Matched keyword: {keyword} -> Interval: {interval}") # Debugging
37
  return interval
38
+ print("No keyword match, using default interval: 5") # Debugging
39
  return 5 # Default interval
40
 
41
  def count_unique_trucks(video_path):
 
59
  # Get total frames in the video
60
  total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
61
 
62
+ # Ensure frame_skip does not exceed total frames
63
+ frame_skip = min(fps * time_interval, total_frames // 2) # Reduced skipping
64
 
65
  frame_count = 0
66
 
 
87
  x1, y1, x2, y2 = map(int, box.xyxy[0]) # Get bounding box
88
  detections.append([x1, y1, x2, y2, confidence])
89
 
90
+ # Debugging: Check detections
91
+ print(f"Frame {frame_count}: Detections -> {detections}")
92
 
93
+ if len(detections) > 0:
94
+ detections = np.array(detections)
95
+ tracked_objects = tracker.update(detections)
96
+ else:
97
+ tracked_objects = [] # Prevent tracker from resetting
98
+
99
+ # Debugging: Check tracked objects
100
+ print(f"Frame {frame_count}: Tracked Objects -> {tracked_objects}")
101
 
 
102
  for obj in tracked_objects:
103
  truck_id = int(obj[4]) # Unique ID assigned by SORT
104
+ x1, y1, x2, y2 = obj[:4] # Get the bounding box coordinates
105
 
106
  truck_center = (x1 + x2) / 2, (y1 + y2) / 2 # Calculate truck center
107
 
108
+ # If truck is already in history, check movement distance
109
+ if truck_id in truck_history:
110
+ last_position = truck_history[truck_id]["position"]
111
+ distance = np.linalg.norm(np.array(truck_center) - np.array(last_position))
112
+
113
+ if distance > DISTANCE_THRESHOLD:
114
+ unique_truck_ids.add(truck_id) # Add only if moved significantly
115
 
116
+ else:
117
+ # If truck is not in history, add it
118
  truck_history[truck_id] = {
119
+ "frame_count": frame_count,
120
+ "position": truck_center
 
121
  }
 
 
 
 
 
122
  unique_truck_ids.add(truck_id)
123
 
124
  cap.release()