import time
import cv2
import sys

tracker = cv2.TrackerCSRT_create()
video = cv2.VideoCapture('./1.mp4')
if not video.isOpened():
    print("Could not open video")
    sys.exit()

i = 0
while True:
    ok, frame = video.read()

    if ok is None or frame is None:
        break
    else:
        i += 1
        frame = cv2.resize(frame, (960, 540))

    if i == 1:
        box = cv2.selectROI("Frame", frame, fromCenter=False, showCrosshair=True)
        ok = tracker.init(frame, box)
        if not ok:
            continue
    success, bbox = tracker.update(frame)

    if success:
        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)

    cv2.imshow("Frame", frame)
    key = cv2.waitKey(1) & 0xFF
    
    if key == 27:
        break

video.release()
cv2.destroyAllWindows()

原文链接:
https://www.csdn.net/tags/OtTaUg4sMzc5MTQtYmxvZwO0O0OO0O0O.html

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐