python opencv 学习————tracker目标追踪(初试)

import sys
import cv2
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.patches as patches

print("Python Version: {}".format(sys.version.split('|')[0]))
print("OpenCV Version: {}".format(cv2.__version__))
print("Matplotlib Version: {}".format(matplotlib.__version__))

VIDEO_PATH = "videos/ZuoYe.mp4"
video = cv2.VideoCapture(VIDEO_PATH)

if not video.isOpened():
    print("Could not load video.")
else:
    f_width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
    f_height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))

    print("Video loaded!\n")
    print("Frame width: {} px".format(f_width))
    print("Frame height: {} px".format(f_height))

# Uncomment the tracker to use for each run

# tracker = cv2.TrackerTLD_create()
tracker = cv2.TrackerBoosting_create()
# tracker = cv2.TrackerMIL_create()


# Read the first video frame

ok, frame = video.read()

# Define initial bounding box
bbox = (824, 223, 91, 68)  # Found using our bounding_box_finder.py tool
#bbox = (x, y, X长度, Y长度)


# 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)
# cv2.circle(frame,824,10,(0,255,0))
# plt.imshow(frame)
# plt.show()

# # Save annotated frames to a list
# frames = [frame]

# Intialise tracker
tracker.init(frame, bbox)

while ok:
    # Read next frame
    ok, frame = video.read()

    # Start timer
    #timer = cv2.getTickCount()

    # Update tracker
    target_located, bbox = tracker.update(frame)

    # Calculate frames per second
    #fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer)

    if target_located:
        # 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),1, 1)
    else:
        # Draw failure text
        cv2.putText(frame, "Tracking failure detected", (100, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)

    # Display fps on frame
    # cv2.putText(frame, "FPS : " + str(int(fps)), (100, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2);
    cv2.imshow('1',frame)
    cv2.waitKey(1)

#     frames.append(frame)
# OUTPUT_VIDEO_PATH = "out/BB8_Boosting.mp4"
# video_out = cv2.VideoWriter(OUTPUT_VIDEO_PATH,
#                             cv2.VideoWriter_fourcc('h', '2', '6', '4'),
#                             30,
#                             (f_width, f_height))
#
# for frame in frames:
#     video_out.write(frame)
#     plt.imshow(frame)
#     plt.show()
#
# video_out.release()
  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值