← All answersTracking & counting

How do I count objects in a video with YOLO?

Short answer

To count objects in a video with YOLO, run detection with tracking enabled so every object keeps a stable ID across frames, then count each ID once as it crosses a counting line or enters a region. Ultralytics ships an ObjectCounter solution that does this out of the box.

Counting is not the same as detecting. A detector finds objects in a single frame, so if you just add up detections you count the same car dozens of times as it drives through. The fix is tracking: give each object a stable ID that persists across frames, then count each ID only once.

The reliable recipe

  • Run YOLO with a tracker (ByteTrack or BoT-SORT) so each object gets a persistent ID.
  • Define a counting line or a polygon region where counting happens.
  • Increment the count the first time an ID crosses the line or enters the region, never again.

The quickest way: the ObjectCounter solution

Ultralytics ships a ready-made counter so you do not have to wire up the tracking and geometry yourself:

import cv2
from ultralytics import solutions

cap = cv2.VideoCapture("traffic.mp4")

# A line (2 points) or a region (4+ points) where objects are counted
region = [(20, 400), (1080, 400)]

counter = solutions.ObjectCounter(model="yolo11n.pt", region=region, show=True)

while cap.isOpened():
    ok, frame = cap.read()
    if not ok:
        break
    results = counter(frame)   # draws boxes + running in/out counts

cap.release()

Things that quietly break the count

  • Line in the wrong place: put it where objects are big and clearly separated, not far away where they overlap.
  • ID switches: if objects swap IDs when they cross or occlude each other, tune the tracker or use a slightly larger model.
  • Too low a confidence threshold: flickering false detections create phantom counts.
  • Counting per frame instead of per ID: always de-duplicate on the track ID.

If you need counts per zone (for example, people in each area of a store) use a polygon region instead of a line, and keep one counter per region.

Stuck on this in a real project?

Book a free call and I’ll give you a straight answer on your specific case.

Book a consultation