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
Python 3.11, OpenCV (opencv-contrib-python), PyCharm or VS Code
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".
| Component | Qty |
|---|---|
| Laptop / Computer | 1 |
| Webcam (built-in or external) | 1 |
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()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.
You need: Laptop / Computer (1), Webcam (built-in or external) (1). Software: Python 3.11, OpenCV (opencv-contrib-python), PyCharm or VS Code.
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.
The complete, tested Python code is free on this page. Copy it into Python 3.11 and upload it and run it.
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.