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,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 |
-
#
|
60 |
-
frame_skip =
|
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 |
-
#
|
88 |
-
|
89 |
|
90 |
-
|
91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
#
|
101 |
-
|
102 |
-
|
103 |
-
|
|
|
|
|
|
|
104 |
|
105 |
-
|
106 |
-
#
|
107 |
truck_history[truck_id] = {
|
108 |
-
"
|
109 |
-
"
|
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()
|