AI & MLBeginnerPRJ-079

Live Object Tracker

Draw a box around any object and the webcam follows it as it moves.

Built and tested by Aboobacker sidheeq K P · Industrial Robotics Institute

Laptop / PC with webcam

Python 3.11, OpenCV (opencv-contrib-python), PyCharm or VS Code

LevelBeginner
Build time1–2 hours
BoardLaptop / PC with webcam
Parts2 components

What is the Live Object Tracker?

A real-time object tracker. Press S, drag a box around anything in the webcam view, and OpenCV’s CSRT tracker follows that object frame by frame with a green box. If the object is hidden or moves too fast, the screen shows "Lost Object".

How it works

  1. The webcam shows a live, mirrored video.
  2. Press S to freeze the frame and drag a box around the object.
  3. OpenCV creates a CSRT tracker from that selection.
  4. Each new frame, the tracker finds the object again and redraws the green box.
  5. If tracking fails, "Lost Object" appears in red. Press Q to quit.

Parts list

ComponentQty
Laptop / Computer1
Webcam (built-in or external)1

Software

  • Python 3.11
  • OpenCV (opencv-contrib-python)
  • PyCharm or VS Code

How to build the Live Object Tracker

  1. Install Python 3.11 and tick "Add Python to PATH".
  2. Open a terminal and run: pip install opencv-contrib-python
  3. Paste the code into a new Python file and run it.
  4. Press S, drag a box around an object and press Enter or Space.
  5. Move the object and watch the box follow it. Press Q to quit.

Live Object Tracker code

Complete, tested Python code for this project. Copy it into Python 3.11 and upload it and run it.

import cv2

cap = cv2.VideoCapture(0)

tracker = None
tracking = False
initBB = None

print("Press 's' to select object")
print("Press 'q' to quit")

while True:
    ret, frame = cap.read()
    if not ret:
        break

    frame = cv2.flip(frame, 1)

    if tracking and tracker 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, 255, 0), 2)
            cv2.putText(frame, "Tracking", (10, 30),
                        cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
        else:
            cv2.putText(frame, "Lost Object", (10, 30),
                        cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
    else:
        cv2.putText(frame, "Press 's' and select object", (10, 30),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 255, 255), 2)

    cv2.imshow("Object Tracking", frame)
    key = cv2.waitKey(1) & 0xFF

    if key == ord("s"):
        initBB = cv2.selectROI("Object Tracking", frame,
                               fromCenter=False, showCrosshair=True)
        tracker = cv2.TrackerCSRT_create()
        tracker.init(frame, initBB)
        tracking = True
    elif key == ord("q"):
        break

cap.release()
cv2.destroyAllWindows()

Real-world uses

  • Security camera tracking
  • Sports and ball tracking
  • Robot vision
  • Learning object tracking

Frequently asked questions

What is a Live Object Tracker?

A real-time object tracker. Press S, drag a box around anything in the webcam view, and OpenCV’s CSRT tracker follows that object frame by frame with a green box.

What components are needed for the Live Object Tracker?

You need: Laptop / Computer (1), Webcam (built-in or external) (1). Software: Python 3.11, OpenCV (opencv-contrib-python), PyCharm or VS Code.

Is the Live Object Tracker project good for beginners?

Yes. It is rated beginner and takes about 1–2 hours to build, using the Laptop / PC with webcam. The guide on this page explains every connection and step.

Where can I get the code for the Live Object Tracker?

The complete, tested Python code is free on this page. Copy it into Python 3.11 and upload it and run it.

Do I need any hardware for the Live Object Tracker?

No kit is needed. This project runs on a laptop or PC with webcam. For help with the setup, message IRI on WhatsApp at +91 86060 82634.

Industrial Robotics Institute
Logo