0% found this document useful (0 votes)
10 views

Object Tracking OpenCV

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Object Tracking OpenCV

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Chat Application Example

Object Tracking using OpenCV

import cv2

# Initialize the tracker

tracker = cv2.TrackerCSRT_create()

# Open the video file or capture device

video = cv2.VideoCapture(0) # Change the argument to a file path for video file

# Read the first frame of the video

ret, frame = video.read()

if not ret:

print("Cannot read video file")

exit()

# Select the bounding box on the frame

bbox = cv2.selectROI(frame, False)

# Initialize the tracker with the first frame and the bounding box

tracker.init(frame, bbox)

while True:

# Read a new frame

ret, frame = video.read()


Chat Application Example

if not ret:

break

# Update the tracker

ret, bbox = tracker.update(frame)

# Draw the bounding box on the frame

if ret:

p1 = (int(bbox[0]), int(bbox[1]))

p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3]))

cv2.rectangle(frame, p1, p2, (255, 0, 0), 2, 1)

else:

cv2.putText(frame, "Tracking failure detected", (100, 80), cv2.FONT_HERSHEY_SIMPLEX,

0.75, (0, 0, 255), 2)

# Display the frame

cv2.imshow("Tracking", frame)

# Exit if 'q' is pressed

if cv2.waitKey(1) & 0xFF == ord('q'):

break

# Release the video capture and close windows

video.release()

cv2.destroyAllWindows()
Chat Application Example

Explanation:

1. Initialize the Tracker:

tracker = cv2.TrackerCSRT_create() initializes a CSRT tracker. You can replace

TrackerCSRT_create with other tracker types like TrackerKCF_create(), TrackerTLD_create(),

TrackerMIL_create(), etc.

2. Open Video Capture:

video = cv2.VideoCapture(0) opens the default camera. Replace 0 with a file path to use a video

file.

3. Select ROI (Region of Interest):

bbox = cv2.selectROI(frame, False) allows you to manually select the object to track in the first

frame.

4. Initialize the Tracker:

tracker.init(frame, bbox) initializes the tracker with the first frame and the selected bounding box.

5. Tracking Loop:

- Read new frames from the video.

- Update the tracker with the new frame using tracker.update(frame).

- Draw the bounding box on the frame if tracking is successful.

- Display the frame using cv2.imshow("Tracking", frame).

6. Exit Condition:
Chat Application Example

The loop breaks if 'q' is pressed.

7. Release Resources:

video.release() and cv2.destroyAllWindows() release the video capture object and close all

OpenCV windows.

This code provides a basic template for object tracking using OpenCV. Depending on your specific

use case, you might need to modify or extend it. For more advanced tracking, consider using deep

learning-based methods, which can handle more complex scenarios and provide better accuracy.

You might also like