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.
- 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.
python3 -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements.txtDownload 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.weightsThe repository includes dnn_model/yolov4.cfg and dnn_model/classes.txt. The weights
are deliberately not committed.
# 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 --cpuUse python object_tracking.py --help for all options.
For each frame:
- YOLOv4 returns class ids, confidences, and boxes.
- Each box becomes a center point and a detection record.
- Existing tracks are matched to the nearest unused detection within
--max-distance. - Unmatched detections create new track IDs.
- 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.
- 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+.
MIT. See LICENSE. YOLOv4 and its weights retain their upstream licenses.