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__))

for i in range(1,2):
    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()
    tracker1 = cv2.TrackerBoosting_create()
    tracker2 = cv2.TrackerBoosting_create()
    tracker3 = cv2.TrackerBoosting_create()
    tracker4 = cv2.TrackerBoosting_create()
    # tracker = cv2.TrackerMIL_create()

    # Read the first video frame
    # for i in range(1,10):
    ok, frame = video.read()

    # Define initial bounding box
    bbox1 = (824, 223, 91, 68)  # Found using our bounding_box_finder.py tool
    #bbox = (x, y, X长度, Y长度)
    bbox2 = (633, 327, 153, 125)
    bbox3 = (407, 316, 138, 122)
    bbox4 = (682, 192, 66, 25)

    # bbox = cv2.selectROI(frame, False)


    # 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
    tracker1.init(frame, bbox1)
    tracker2.init(frame, bbox2)
    tracker3.init(frame, bbox3)
    tracker4.init(frame, bbox4)

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

        # Update tracker
        target_located, bbox1 = tracker1.update(frame)
        if target_located:
            # Draw bounding box
            p1 = (int(bbox1[0]), int(bbox1[1]))
            p2 = (int(bbox1[0] + bbox1[2]), int(bbox1[1] + bbox1[3]))
            cv2.rectangle(frame, p1, p2, (255, 0, 0),2, 1)
        else:
            # Draw failure text
            cv2.putText(frame, "Tracking failure detected", (100, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)
        cv2.putText(frame, "Box", p1, cv2.FONT_HERSHEY_SIMPLEX, 0.75, (100, 170, 50), 1)

        target_located, bbox2 = tracker2.update(frame)
        if target_located:
            # Draw bounding box
            p1 = (int(bbox2[0]), int(bbox2[1]))
            p2 = (int(bbox2[0] + bbox2[2]), int(bbox2[1] + bbox2[3]))
            cv2.rectangle(frame, p1, p2, (255, 0, 0), 2, 1)
        else:
            # Draw failure text
            cv2.putText(frame, "Tracking failure detected", (100, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)
        cv2.putText(frame, "Back_wheel", p1, cv2.FONT_HERSHEY_SIMPLEX, 0.75, (100, 170, 50), 1)

        target_located, bbox3 = tracker3.update(frame)
        if target_located:
            # Draw bounding box
            p1 = (int(bbox3[0]), int(bbox3[1]))
            p2 = (int(bbox3[0] + bbox3[2]), int(bbox3[1] + bbox3[3]))
            cv2.rectangle(frame, p1, p2, (255, 0, 0), 2, 1)
        else:
            # Draw failure text
            cv2.putText(frame, "Tracking failure detected", (100, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)
        cv2.putText(frame, "Front_wheel", p1, cv2.FONT_HERSHEY_SIMPLEX, 0.75, (100, 170, 50), 1)

        target_located, bbox4 = tracker4.update(frame)
        if target_located:
            # Draw bounding box
            p1 = (int(bbox4[0]), int(bbox4[1]))
            p2 = (int(bbox4[0] + bbox4[2]), int(bbox4[1] + bbox4[3]))
            cv2.rectangle(frame, p1, p2, (255, 0, 0), 2, 1)
        else:
            # Draw failure text
            cv2.putText(frame, "Tracking failure detected", (100, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)
        cv2.putText(frame, "Tab", p1, cv2.FONT_HERSHEY_SIMPLEX, 0.75, (100, 170, 50), 1)

        FRAME_NOW = video.get(cv2.CAP_PROP_POS_FRAMES)  # 第0帧

        # cv2.imshow('1',frame)

        if (FRAME_NOW >= 485):
            ok = False

        while True:
            if (cv2.waitKey(1)==27):
                ok = False
            else:
                break
        frames.append(frame)

OUTPUT_VIDEO_PATH = "out/out.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()
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值