Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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()
|
18 |
|
19 |
# Minimum confidence threshold for detection
|
20 |
-
CONFIDENCE_THRESHOLD = 0.4 #
|
21 |
|
22 |
# Distance threshold to avoid duplicate counts
|
23 |
DISTANCE_THRESHOLD = 50
|
@@ -30,12 +30,9 @@ TIME_INTERVALS = {
|
|
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,8 +56,8 @@ 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 |
-
#
|
63 |
-
frame_skip = min(fps * time_interval, total_frames //
|
64 |
|
65 |
frame_count = 0
|
66 |
|
@@ -87,38 +84,36 @@ def count_unique_trucks(video_path):
|
|
87 |
x1, y1, x2, y2 = map(int, box.xyxy[0]) # Get bounding box
|
88 |
detections.append([x1, y1, x2, y2, confidence])
|
89 |
|
90 |
-
#
|
91 |
-
|
92 |
|
93 |
-
|
94 |
-
|
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
|
105 |
|
106 |
truck_center = (x1 + x2) / 2, (y1 + y2) / 2 # Calculate truck center
|
107 |
|
108 |
-
#
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
if distance > DISTANCE_THRESHOLD:
|
114 |
-
unique_truck_ids.add(truck_id) # Add only if moved significantly
|
115 |
|
116 |
-
|
117 |
-
#
|
118 |
truck_history[truck_id] = {
|
119 |
-
"
|
120 |
-
"
|
|
|
121 |
}
|
|
|
|
|
|
|
|
|
|
|
122 |
unique_truck_ids.add(truck_id)
|
123 |
|
124 |
cap.release()
|
|
|
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 |
|
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 |
# 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 |
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()
|