Real-time Object Detection and Tracking using OpenCV and Python

In this blog post, we will build a real-time object detection and tracking system using OpenCV and Python. OpenCV is a popular open-source computer vision library, and Python is a user-friendly programming language. Combining both, we can create a powerful computer vision application.


Before diving into the code, make sure you have the following installed:

  • Python 3.x
  • OpenCV 4.x
  • Numpy

You can install OpenCV and Numpy using the following commands:

pip install opencv-python
pip install numpy

Object Detection using Haar Cascades

To detect objects in real-time, we will use Haar cascades. Haar cascades are XML files containing classifiers to identify specific objects. OpenCV provides several pre-trained Haar cascades for detecting faces, eyes, and other objects.

You can download the required Haar cascade files from the OpenCV GitHub repository.

For this guide, we will use the haarcascade_frontalface_default.xml file to detect faces.

Object Tracking using OpenCV

For object tracking, we will use OpenCV's Tracker class. OpenCV provides several tracking algorithms, such as BOOSTING, MIL, KCF, TLD, MEDIANFLOW, MOSSE, and CSRT. Each algorithm has its pros and cons, and you can choose the one that suits your needs.

Implementing Real-time Object Detection and Tracking

Now, let's dive into the code. First, import the required libraries:

import cv2
import numpy as np

Next, initialize the Haar cascade classifier for face detection:

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

Choose a tracking algorithm and create a tracker object:

tracker = cv2.TrackerKCF_create()

Initialize the video capture object to read from the webcam:

cap = cv2.VideoCapture(0)

Now, create a loop to read the video frames and process them:

# Initialize variables
initBB = None
while True:
    # Read the frame
    ret, frame =
    if not ret:

    # Convert the frame to grayscale
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Detect faces
    faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))

    # Draw rectangles around detected faces
    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)

    # Initialize the tracker
    if initBB is None and len(faces) > 0:
        initBB = (faces[0][0], faces[0][1], faces[0][2], faces[0][3])
        tracker.init(frame, initBB)

    # Update the tracker and draw the tracking box
    if initBB is not None:
        success, box = tracker.update(frame)
        if success:
            (x, y, w, h) = [int(v) for v in box]
            cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2)

    # Display the resulting frame
    cv2.imshow('Real-time Object Detection and Tracking', frame)

    # Exit the loop if 'q' is pressed
    if cv2.waitKey(1) & 0xFF == ord('q'):

# Release the video capture object and close all windows

This code detects faces in real-time and tracks the first detected face using the chosen tracking algorithm.


In this blog post, we demonstrated how to create a real-time object detection and tracking system using OpenCV and Python. You can further customize this code to detect and track other objects by using different Haar cascades and tracking algorithms. With some practice, you can create powerful real-time computer vision applications using OpenCV and Python.

An AI coworker, not just a copilot

View VelocityAI