Spaces:
Running
Running
import cv2 | |
# Initialize video capture | |
cap = cv2.VideoCapture('mot.mp4') | |
# Initialize tracker | |
tracker = cv2.TrackerCSRT_create() | |
# Read the first frame | |
ret, frame = cap.read() | |
if not ret: | |
print("Failed to read video") | |
exit() | |
# Detect initial bounding box | |
bbox = cv2.selectROI(frame, False) | |
# Initialize tracker with first frame and bounding box | |
tracker.init(frame, bbox) | |
while True: | |
ret, frame = cap.read() | |
if not ret: | |
break | |
# Update tracker | |
success, bbox = tracker.update(frame) | |
if success: | |
# Draw bounding box | |
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: | |
# Tracking failure | |
cv2.putText(frame, "Tracking failure detected", (100,80), cv2.FONT_HERSHEY_SIMPLEX, 0.75,(0,0,255),2) | |
# Display result | |
cv2.imshow("Tracking", frame) | |
# Exit if ESC pressed | |
if cv2.waitKey(1) & 0xFF == 27: # ESC key | |
break | |
cap.release() | |
cv2.destroyAllWindows() | |