I am implementing functionality to track people entering and exiting a space. However, I am encountering an issue when a user stops at the door. The center point of the bounding box interacts with the line multiple times, causing the count to increase repeatedly. How can I resolve this? I want each person to be counted only once for entering or exiting.
POLYGON = np.array([[rect[0][0], rect[0][1]], [rect[1][0], rect[0][1]], [rect[1][0], rect[1][1]], [rect[0][0], rect[1][1]]])
print(f"Polygon Coordinates: {POLYGON}")
# Define the line from the user-drawn line
LINE_START = sv.Point(line[0][0], line[0][1])
LINE_END = sv.Point(line[1][0], line[1][1])
print(f"Line Coordinates: Start={LINE_START}, End={LINE_END}")
# Initialize the line zone for counting
LINE_ZONE = sv.LineZone(start=LINE_END, end=LINE_START, triggering_anchors=(sv.Position.CENTER,))
tracker = sv.ByteTrack(minimum_consecutive_frames=6)
# Initialize annotators
box_annotator = sv.BoxAnnotator(thickness=1)
label_annotator = sv.LabelAnnotator(text_thickness=2, text_scale=1)
polygon_zone = sv.PolygonZone(polygon=POLYGON, triggering_anchors=(sv.Position.CENTER,))
trace_annotator = sv.TraceAnnotator()
line_zone_annotator = sv.LineZoneAnnotator(text_scale=0.8, text_orient_to_line=True)
smoother = sv.DetectionsSmoother(length=5)
# Variables for in/out counting
in_count = 0
out_count = 0
# Process video frames
for frame in tqdm(frames_generator, total=video_info.total_frames):
# Detect objects in the frame
results = model(frame, imgsz=1280, conf=0.5, classes=[0], device='cuda:0', verbose=False)[0]
detections = sv.Detections.from_ultralytics(results)
# Apply the polygon zone to filter detections
detections = detections[polygon_zone.trigger(detections=detections)]
# Track the detections
detections = tracker.update_with_detections(detections)
detections = smoother.update_with_detections(detections)
print(detections)
# Trigger the line zone and count in/out events
a=LINE_ZONE.trigger(detections=detections)
# print(LINE_ZONE.in_count)
# print(LINE_ZONE.in_count_per_class)
# print(LINE_ZONE._in_count_per_class)
# print(a)
# Increment counters based on direction (in/out)
# in_count += LINE_ZONE.in_count
# out_count += LINE_ZONE.out_count
lable = [
f"{id}"
for id in detections.tracker_id
]
# Annotate the frame
frame_copy = frame.copy()
frame_copy = sv.draw_polygon(scene=frame_copy, polygon=POLYGON, color=sv.Color.RED, thickness=2)
frame_copy = box_annotator.annotate(scene=frame_copy, detections=detections)
frame_copy = trace_annotator.annotate(scene=frame_copy, detections=detections)
frame_copy = sv.draw_line(scene=frame_copy, start=LINE_START, end=LINE_END, color=sv.Color.RED, thickness=2)
frame_copy = line_zone_annotator.annotate(frame_copy, line_counter=LINE_ZONE)
frame_copy = label_annotator.annotate(scene=frame_copy, detections=detections, labels=lable)
# Display counts on the frame
# cv2.putText(frame_copy, f"In: {in_count}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
# cv2.putText(frame_copy, f"Out: {out_count}", (10, 70), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
# Display the frame
cv2.imshow("Processed Frame", frame_copy)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release resources
cv2.destroyAllWindows()