Skip to content

About

YOLOv4 detection with a greedy centroid tracker, CPU/CUDA paths, and annotated video output

Topics

Resources

Stars

12 stars

Watchers

1 watching

Forks

Repository files navigation

Object tracking with YOLOv4 and OpenCV

A compact object-detection and centroid-tracking demo. YOLOv4 runs through OpenCV's DNN module, and a small greedy tracker maintains IDs across frames of the included los_angeles.mp4 clip.

What it shows

  • YOLOv4 inference through cv2.dnn_DetectionModel.
  • Optional CUDA backend with a CPU fallback.
  • Greedy nearest-centroid association with unique track IDs.
  • Optional annotated MP4 output or live display.

The tracker is an educational baseline. It does not use a Kalman filter, appearance re-identification, or a modern multi-object tracking model.

Setup

python3 -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements.txt

Download the YOLOv4 weights into dnn_model/ (about 250 MB):

curl -L -o dnn_model/yolov4.weights \
  https://github.com/AlexeyAB/darknet/releases/download/yolov4/yolov4.weights

The repository includes dnn_model/yolov4.cfg and dnn_model/classes.txt. The weights are deliberately not committed.

Run

# Display the tracker window
python object_tracking.py --display

# Write an annotated video
python object_tracking.py --output tracked.mp4

# Force CPU if CUDA is available but not wanted
python object_tracking.py --display --cpu

Use python object_tracking.py --help for all options.

How the tracker works

For each frame:

  1. YOLOv4 returns class ids, confidences, and boxes.
  2. Each box becomes a center point and a detection record.
  3. Existing tracks are matched to the nearest unused detection within --max-distance.
  4. Unmatched detections create new track IDs.
  5. Tracks that miss a frame are dropped.

This is intentionally simple and can switch IDs when objects cross or detections flicker. The next step would be a motion model such as SORT or a re-identification embedding.

Scope and limitations

  • The included video is a demo, not a benchmark dataset.
  • No mAP, MOTA, or IDF1 numbers are claimed.
  • The detector is only as accurate as the YOLOv4 weights and confidence threshold.
  • The CUDA path depends on an OpenCV build with CUDA support; CPU is the default fallback.
  • The code expects Python 3.9+.

License

MIT. See LICENSE. YOLOv4 and its weights retain their upstream licenses.

About

YOLOv4 detection with a greedy centroid tracker, CPU/CUDA paths, and annotated video output

Topics

Resources

Stars

12 stars

Watchers

1 watching

Forks

Contributors

Languages