当前位置 博文首页 > 文章内容

    Gentleman_ZH:python实现目标跟踪(opencv)

    作者:21344 栏目:未分类 时间:2021-11-25 22:31:38

     1.单目标跟踪

    import cv2
    import sys
    
    (major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')
    print(major_ver, minor_ver, subminor_ver)
    
    if __name__ == '__main__':
        # 创建跟踪器
        tracker_type = 'MIL'
        tracker = cv2.TrackerMIL_create()
        # 读入视频
        video = cv2.VideoCapture("./data/1.mp4")
        # 读入第一帧
        ok, frame = video.read()
        if not ok:
            print('Cannot read video file')
            sys.exit()
        # 定义一个bounding box
        bbox = (287, 23, 86, 320)
        bbox = cv2.selectROI(frame, False)
        # 用第一帧初始化
        ok = tracker.init(frame, bbox)
    
        while True:
            ok, frame = video.read()
            if not ok:
                break
            # Start timer
            timer = cv2.getTickCount()
            # Update tracker
            ok, bbox = tracker.update(frame)
            # Cakculate FPS
            fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer)
            # Draw bonding box
            if ok:
                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 failed detected", (100, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)
            # 展示tracker类型
            cv2.putText(frame, tracker_type+"Tracker", (100, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2)
            # 展示FPS
            cv2.putText(frame, "FPS:"+str(fps), (100, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2)
            # Result
            cv2.imshow("Tracking", frame)
    
            # Exit
            k = cv2.waitKey(1) & 0xff
            if k ==27 : break
    

    2.多目标跟踪

    使用GOTURN作为跟踪器时,须将goturn.caffemodel和goturn.prototxt放到工作目录才能运行,解决问题链接https://stackoverflow.com/questions/48802603/getting-deep-learning-tracker-goturn-to-run-opencv-python

    import cv2
    import sys
    
    (major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')
    print(major_ver, minor_ver, subminor_ver)
    
    if __name__ == '__main__':
        # 创建跟踪器
        # 'BOOSTING', 'MIL','KCF', 'TLD', 'MEDIANFLOW', 'GOTURN', 'MOSSE'
        tracker_type = 'MIL'
        tracker = cv2.MultiTracker_create()
        # 创建窗口
        cv2.namedWindow("Tracking")
        # 读入视频
        video = cv2.VideoCapture("./data/1.mp4")
        # 读入第一帧
        ok, frame = video.read()
        if not ok:
            print('Cannot read video file')
            sys.exit()
        # 定义一个bounding box
        box1 = cv2.selectROI("Tracking", frame)
        box2 = cv2.selectROI("Tracking", frame)
        box3 = cv2.selectROI("Tracking", frame)
        # 用第一帧初始化
        ok = tracker.add(cv2.TrackerMIL_create(), frame, box1)
        ok1 = tracker.add(cv2.TrackerMIL_create(), frame, box2)
        ok2 = tracker.add(cv2.TrackerMIL_create(), frame, box3)
        while True:
            ok, frame = video.read()
            if not ok:
                break
            # Start timer
            timer = cv2.getTickCount()
            # Update tracker
            ok, boxes = tracker.update(frame)
            print(ok, boxes)
            # Cakculate FPS
            fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer)
            for box in boxes:
                # Draw bonding box
                if ok:
                    p1 = (int(box[0]), int(box[1]))
                    p2 = (int(box[0] + box[2]), int(box[1] + box[3]))
                    cv2.rectangle(frame, p1, p2, (255, 0, 0), 2, 1)
                else:
                    cv2.putText(frame, "Tracking failed detected", (100, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255),2)
            # 展示tracker类型
            cv2.putText(frame, tracker_type+"Tracker", (100, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2)
            # 展示FPS
            cv2.putText(frame, "FPS:"+str(fps), (100, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2)
            # Result
            cv2.imshow("Tracking", frame)
    
            # Exit
            k = cv2.waitKey(1) & 0xff
            if k ==27 : break
    

     

     

    cs