A real-time object tracking system that combines YOLOv12 detection with OpenCV's CSRT tracker. Point your webcam at any scene, click on an object, and the system tracks it continuously — automatically re-detecting it if tracking is lost.
- Detection phase — YOLOv12 runs on the live webcam feed and draws bounding boxes around all detected objects in real time.
- Selection — Click on any detected object with your mouse. If multiple bounding boxes overlap at that point, the system picks the smallest (most specific) one.
- Tracking phase — OpenCV's CSRT tracker takes over, tracking the selected object frame by frame without running the full detection model on every frame.
- Re-detection — If the tracker loses the object (e.g., it leaves the frame or is occluded), YOLOv12 re-runs automatically and resumes tracking as soon as the object reappears with confidence ≥ 0.7.
- Real-time detection with YOLOv12 (nano variant — fast and lightweight)
- Click-to-track — select any object from the live feed with a single mouse click
- CSRT tracking — accurate, robust tracker for smooth object following
- Automatic re-detection — recovers tracking after object loss without user intervention
- Smallest-box selection — intelligently selects the most specific bounding box when objects overlap
| Tool | Role |
|---|---|
| YOLOv12 (Ultralytics) | Real-time object detection |
| OpenCV CSRT Tracker | Frame-by-frame object tracking |
| Python 3.8+ | Core language |
real-time-object-tracking/
│
├── object_tracker.py # Main script — detection, selection, and tracking
├── test_webcam.py # Simple webcam test to verify camera access
├── yolov12n.pt # YOLOv12 nano pretrained model weights
- Install dependencies:
pip install ultralytics opencv-python- Verify your webcam works:
python test_webcam.py- Run the tracker:
python object_tracker.py| Action | Key / Input |
|---|---|
| Select object to track | Left mouse click on detected object |
| Quit | Press q |
https://www.linkedin.com/feed/update/urn:li:activity:7300530083985780736/
- The model used is
yolov12n.pt(nano) — optimized for speed on CPU. - CSRT is chosen over simpler trackers (KCF, MOSSE) for its higher accuracy on fast-moving or partially occluded objects.
- Re-detection only triggers if the CSRT tracker explicitly fails — keeping CPU usage low during stable tracking.