Skip to content

About

YOLOv4 object detection with OpenCV DNN and a from-scratch centroid tracker

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

8 Commits

Folders and files

Repository files navigation

Object Detection & Tracking from Scratch with OpenCV

Python OpenCV YOLOv4

A lightweight object detection and tracking pipeline built from first principles. It uses YOLOv4 through OpenCV's DNN module to detect objects, and a simple centroid tracker (written by hand, without Deep SORT) to give each object a persistent ID across frames.


How it works

  1. Detection (object_detection.py)
    • Loads YOLOv4 weights and config with cv2.dnn.readNet.
    • Uses the CUDA backend when it is available.
    • Runs inference at 608×608 with a confidence threshold of 0.5 and an NMS threshold of 0.4.
  2. Tracking (object_tracking.py)
    • Computes the centre point of every bounding box.
    • Matches each centre to the tracked objects from the previous frame by Euclidean distance (< 20 px).
    • Updates IDs that match, removes IDs for objects that disappear, and assigns new IDs to new detections.
    • Draws boxes, centre points and IDs on each frame.

Getting started

git clone https://github.com/YogiOnCode/OpenCV_Obj_Detection_from_Scratch.git
cd OpenCV_Obj_Detection_from_Scratch
pip install -r requirements.txt

Download the YOLOv4 model files (Darknet releases) and place them as follows:

dnn_model/
├── yolov4.weights
├── yolov4.cfg
└── classes.txt      # COCO class names

Update the paths in ObjectDetection.__init__ if your folder layout differs, then run:

python object_tracking.py

Press Esc to quit.

Repository structure

├── object_detection.py   # YOLOv4 detector wrapper (OpenCV DNN)
├── object_tracking.py    # Centroid tracker + visualization
└── code.py               # Earlier variant of the tracking loop

Limitations and next steps

  • No occlusion handling: IDs are lost when objects overlap or leave the frame briefly.
  • Distance-only matching: fast-moving objects can be reassigned to new IDs.
  • Next steps: add Kalman-filter prediction or Deep SORT for appearance-based re-identification.

Tech stack

Python · OpenCV (DNN, CUDA backend) · YOLOv4 · NumPy

License

Released under the MIT License.

Author

Yogeswaran Amsavalli · GitHub

About

YOLOv4 object detection with OpenCV DNN and a from-scratch centroid tracker

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages