Step-by-step information to counting strolling ants on a tree utilizing detection and monitoring strategies.
Counting objects in movies is a difficult Laptop Imaginative and prescient activity. In contrast to counting objects in static pictures, movies contain further complexities, since objects can transfer, turn into occluded, or seem and disappear at completely different occasions, which complicates the counting course of.
On this tutorial, we’ll display tips on how to depend ants shifting alongside a tree, utilizing Object Detection and monitoring strategies. We’ll harness Ultralytics platform to combine YOLOv8 mannequin for detection, BoT-SORT for monitoring, and a line counter to depend the ants.
In a typical video object counting pipeline, every body undergoes a sequence of processes: detection, monitoring, and counting. Right here’s a quick overview of every step:
- Detection: An object detector identifies and locates objects in every body, producing bounding bins round them.
- Monitoring: A tracker follows these objects throughout frames, assigning distinctive IDs to every object to make sure they’re counted solely as soon as.
- Counting: The counting module aggregates this info and provides every new object to supply correct outcomes.
Connecting an object detector, a tracker, and a counter would possibly require in depth coding. Happily, the Ultralytics library [1] simplifies this course of by offering a handy pipeline that seamlessly integrates these elements.
Step one is to detect the ants in every body produce bounding bins round them. On this tutorial, we’ll use a YOLOv8 detector that I educated upfront to detect ants. I used Grounding DINO [2] to label the info, after which I used the annotated knowledge to coach the YOLOv8 mannequin. If you wish to study extra about coaching a YOLO mannequin, seek advice from my earlier put up on training YOLOv5, because the ideas are comparable. To your software, you should utilize a pre-trained mannequin or prepare a customized mannequin of your personal.
To get began, we have to initialize the detector with the pre-trained weights:
from ultralytics import YOLO# Initialize YOLOv8 mannequin with pre-trained weights
mannequin = YOLO("/path/to/your/yolo_model.pt")
In a while, we’ll use the detector to detect ants in every body throughout the video loop, integrating the detection with the monitoring course of.
Since ants seem a number of occasions throughout the video frames, it’s important to trace every ant and assign it a singular ID, to make sure that every ant is counted solely as soon as. Ultralytics helps each BoT-SORT [3] and ByteTrack [4] for monitoring.
- ByteTrack: Offers a steadiness between accuracy and pace, with decrease computational complexity. It might not deal with occlusions and digicam movement in addition to BoT-SORT.
- BoT-SORT: Gives improved monitoring accuracy and robustness over ByteTrack, particularly in difficult eventualities with occlusions and digicam movement. Nevertheless, it comes at the price of greater computational complexity and decrease body charges.
The selection between these algorithms is determined by the precise necessities of your software.
How BoT-SORT Works: BoT-SORT is a multi-object tracker, that means it may observe a number of objects on the identical time. It combines movement and look info together with digicam movement compensation. The objects’ positions are predicted utilizing a Kalman filter, and the matches to current tracks are based mostly on each their location and visible options. This strategy permits BoT-SORT to keep up correct tracks even within the presence of occlusions or when the digicam is shifting.
A well-configured tracker can compensate for the detector’s gentle faults. For instance if the article detector quickly fails to detect an ant, the tracker can keep the ant’s observe utilizing movement and look cues.
The detector and tracker are used iteratively on every body throughout the video loop to provide the tracks. That is the way you combine it into your video processing loop:
tracks = mannequin.observe(body, persist=True, tracker=’botsort.yaml’, iou=0.2)
The tracker configuration is outlined within the ‘botsort.yaml’ file. You possibly can modify these parameters to greatest suit your wants. To alter the tracker to ByteTrack, merely move ‘bytetrack.yaml’ to the tracker parameter.
Make sure that the Intersection Over Union (IoU) worth suits your software necessities; the IoU threshold (used for non-maximum suppression) determines how shut detections have to be to be thought-about the identical object. The persist=True
argument tells the tracker that the present body is a part of a sequence and to count on tracks from the earlier body to persist into the present body.
Now that we now have detected and tracked the ants, the ultimate step is to depend the distinctive ants that crosses a delegated line within the video. The ObjectCounter
class from the Ultralytics library permits us to outline a counting area, which could be a line or a polygon. For this tutorial, we’ll use a easy line as our counting area. This strategy reduces errors by guaranteeing that an ant is counted solely as soon as when it crosses the road, even when its distinctive ID modifications resulting from monitoring errors.
First, we initialize the ObjectCounter
earlier than the video loop:
counter = options.ObjectCounter(
view_img=True, # Show the picture throughout processing
reg_pts=[(512, 320), (512, 1850)], # Area of curiosity factors
classes_names=mannequin.names, # Class names from the YOLO mannequin
draw_tracks=True, # Draw monitoring traces for objects
line_thickness=2, # Thickness of the traces drawn
)
Contained in the video loop, the ObjectCounter
will depend the tracks produced by the tracker. The factors of the road are handed to the counter on the reg_pts parameter, within the [(x1, y1), (x2, y2)] format. When the middle level of an ant’s bounding field crosses the road for the primary time, it’s added to the depend in keeping with its trajectory path. Objects shifting in a sure path counted as ‘In’, and objects shifting to the opposite path counted as ‘Out’.
# Use the Object Counter to depend new objects
body = counter.start_counting(body, tracks)
Now that we now have seen the counting elements, let’s combine the code with the video loop and save the ensuing video.
# Set up and import Required Libraries
%pip set up ultralytics
import cv2
from ultralytics import YOLO, options# Outline paths:
path_input_video = '/path/to/your/input_video.mp4'
path_output_video = "/path/to/your/output_video.avi"
path_model = "/path/to/your/yolo_model.pt"
# Initialize YOLOv8 Detection Mannequin
mannequin = YOLO(path_model)
# Initialize Object Counter
counter = options.ObjectCounter(
view_img=True, # Show the picture throughout processing
reg_pts=[(512, 320), (512, 1850)], # Area of curiosity factors
classes_names=mannequin.names, # Class names from the YOLO mannequin
draw_tracks=True, # Draw monitoring traces for objects
line_thickness=2, # Thickness of the traces drawn
)
# Open the Video File
cap = cv2.VideoCapture(path_input_video)
assert cap.isOpened(), "Error studying video file"
# Initialize the Video Author to avoid wasting resulted video
video_writer = cv2.VideoWriter(path_output_video, cv2.VideoWriter_fourcc(*"mp4v"), 30, (1080, 1920))
# itterate over video frames:
frame_count = 0
whereas cap.isOpened():
success, body = cap.learn()
if not success:
print("Video body is empty or video processing has been efficiently accomplished.")
break
# Carry out object monitoring on the present body
tracks = mannequin.observe(body, persist=True, tracker='botsort.yaml', iou=0.2)
# Use the Object Counter to depend objects within the body and get the annotated picture
body = counter.start_counting(body, tracks)
# Write the annotated body to the output video
video_writer.write(body)
frame_count += 1
# Launch all Sources:
cap.launch()
video_writer.launch()
cv2.destroyAllWindows()
# Print counting outcomes:
print(f'In: {counter.in_counts}nOut: {counter.out_counts}nTotal: {counter.in_counts + counter.out_counts}')
print(f'Saves output video to {path_output_video}')
The code above integrates object detection and monitoring right into a video processing loop to avoid wasting the annotated video. Utilizing OpenCV, we open the enter video and arrange a video author for the output. In every body, we carry out object monitoring with BoTSORT, depend the objects, and annotate the body. The annotated frames, together with bounding bins, distinctive IDs, trajectories, and ‘in’ and ‘out’ counts, are saved to the output video. The ‘in’ and ‘out’ counts could be retrieved from counter.in_counts
and counter.out_counts
, respectively, and are additionally printed on the output video.
Within the annotated video, we appropriately counted a complete of 85 ants, with 34 getting into and 51 exiting. For exact counts, it’s essential that the detector performs properly and the tracker is properly configured. A well-configured tracker can compensate for detector misses, guaranteeing continuity in monitoring.
Within the annotated video we are able to see that the tracker dealt with lacking detections very properly, as evidenced by the disappearance of the bounding field round an ant and its return in subsequent frames with the right ID. Moreover, monitoring errors that assigned completely different IDs to the identical object (e.g., ant #42 turning into #48) didn’t have an effect on the counts since solely the ants that cross the road are counted.
On this tutorial, we explored tips on how to depend objects in movies utilizing superior object detection and monitoring strategies. We utilized YOLOv8 for detecting ants and BoT-SORT for strong monitoring, all built-in seamlessly with the Ultralytics library.
Need to study extra?
References
[2] Grounding DINO: Marrying DINO with Grounded Pre-Training for Open-Set Object Detection
[3] BoT-SORT: Robust Associations Multi-Pedestrian Tracking
[4] ByteTrack: Multi-Object Tracking by Associating Every Detection Box