diff --git a/app.py b/app.py index df3fd48..6bc64b9 100644 --- a/app.py +++ b/app.py @@ -109,21 +109,7 @@ def detect_objects_from_webcam(line_data): # Resize the frame to (1020, 600) frame = cv2.resize(frame, (1020, 600)) - # Draw counting line (dashed yellow line with black gaps) - cv2.line(frame, line_start, line_end, (0, 255, 255), 2, cv2.LINE_AA) - # Draw dashed effect - line_length = int(np.sqrt((line_end[0] - line_start[0])**2 + (line_end[1] - line_start[1])**2)) - dash_length = 20 - for i in range(0, line_length, dash_length * 2): - t1 = i / line_length - t2 = min((i + dash_length) / line_length, 1.0) - x1 = int(line_start[0] + t1 * (line_end[0] - line_start[0])) - y1 = int(line_start[1] + t1 * (line_end[1] - line_start[1])) - x2 = int(line_start[0] + t2 * (line_end[0] - line_start[0])) - y2 = int(line_start[1] + t2 * (line_end[1] - line_start[1])) - cv2.line(frame, (x1, y1), (x2, y2), (0, 0, 0), 2) - - # Run YOLOv8 tracking on the frame + # Run YOLOv8 tracking on the frame (BEFORE drawing the counting line!) results = model.track(frame, persist=True) if results[0].boxes is not None and results[0].boxes.id is not None: @@ -171,6 +157,20 @@ def detect_objects_from_webcam(line_data): # Draw center point (yellow) cv2.circle(frame, (center_x, center_y), 3, (0, 255, 255), -1) + # Draw counting line AFTER YOLO detection (dashed yellow line) + cv2.line(frame, line_start, line_end, (0, 255, 255), 3, cv2.LINE_AA) + # Draw dashed effect + line_length = int(np.sqrt((line_end[0] - line_start[0])**2 + (line_end[1] - line_start[1])**2)) + dash_length = 20 + for i in range(0, line_length, dash_length * 2): + t1 = i / line_length + t2 = min((i + dash_length) / line_length, 1.0) + x1 = int(line_start[0] + t1 * (line_end[0] - line_start[0])) + y1 = int(line_start[1] + t1 * (line_end[1] - line_start[1])) + x2 = int(line_start[0] + t2 * (line_end[0] - line_start[0])) + y2 = int(line_start[1] + t2 * (line_end[1] - line_start[1])) + cv2.line(frame, (x1, y1), (x2, y2), (0, 0, 0), 3) + # Display vehicle count with type breakdown cv2.rectangle(frame, (10, 10), (350, 140), (0, 0, 0), -1) cv2.putText(frame, f'Gesamt: {vehicle_count}', (20, 35), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2) @@ -247,21 +247,7 @@ def detect_objects_from_video(video_path, line_data): # Resize the frame to (1020, 600) frame = cv2.resize(frame, (1020, 600)) - # Draw counting line (dashed yellow line with black gaps) - cv2.line(frame, line_start, line_end, (0, 255, 255), 2, cv2.LINE_AA) - # Draw dashed effect - line_length = int(np.sqrt((line_end[0] - line_start[0])**2 + (line_end[1] - line_start[1])**2)) - dash_length = 20 - for i in range(0, line_length, dash_length * 2): - t1 = i / line_length - t2 = min((i + dash_length) / line_length, 1.0) - x1 = int(line_start[0] + t1 * (line_end[0] - line_start[0])) - y1 = int(line_start[1] + t1 * (line_end[1] - line_start[1])) - x2 = int(line_start[0] + t2 * (line_end[0] - line_start[0])) - y2 = int(line_start[1] + t2 * (line_end[1] - line_start[1])) - cv2.line(frame, (x1, y1), (x2, y2), (0, 0, 0), 2) - - # Run YOLOv8 tracking on the frame + # Run YOLOv8 tracking on the frame (BEFORE drawing the counting line!) results = model.track(frame, persist=True) if results[0].boxes is not None and results[0].boxes.id is not None: @@ -309,6 +295,20 @@ def detect_objects_from_video(video_path, line_data): # Draw center point (yellow) cv2.circle(frame, (center_x, center_y), 3, (0, 255, 255), -1) + # Draw counting line AFTER YOLO detection (dashed yellow line) + cv2.line(frame, line_start, line_end, (0, 255, 255), 3, cv2.LINE_AA) + # Draw dashed effect + line_length = int(np.sqrt((line_end[0] - line_start[0])**2 + (line_end[1] - line_start[1])**2)) + dash_length = 20 + for i in range(0, line_length, dash_length * 2): + t1 = i / line_length + t2 = min((i + dash_length) / line_length, 1.0) + x1 = int(line_start[0] + t1 * (line_end[0] - line_start[0])) + y1 = int(line_start[1] + t1 * (line_end[1] - line_start[1])) + x2 = int(line_start[0] + t2 * (line_end[0] - line_start[0])) + y2 = int(line_start[1] + t2 * (line_end[1] - line_start[1])) + cv2.line(frame, (x1, y1), (x2, y2), (0, 0, 0), 3) + # Display vehicle count with type breakdown cv2.rectangle(frame, (10, 10), (350, 140), (0, 0, 0), -1) cv2.putText(frame, f'Gesamt: {vehicle_count}', (20, 35), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2)