python opencv 多目标跟踪

教程里有单目标跟踪:
https://www.learnopencv.com/object-tracking-using-opencv-cpp-python/#comments
https://github.com/makelove/OpenCV-Python-Tutorial/tree/master/my02-视频-对象跟踪
https://blog.csdn.net/sinat_32582203/article/details/80017608

多目标跟踪就是添加多个tracker就好了。

    # -*- coding:utf-8 -*-
    
    import cv2
    import sys
    
    
    (major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')
    print(major_ver, minor_ver, subminor_ver)
    
    if __name__ == '__main__':
    
        # Set up tracker.
        # Instead of MIL, you can also use
    
        tracker_types = ['BOOSTING', 'MIL', 'KCF', 'TLD', 'MEDIANFLOW', 'GOTURN']
        tracker_type = tracker_types[1]
    
        if int(minor_ver) < 3:
            tracker = cv2.Tracker_create(tracker_type)
        else:
            if tracker_type == 'BOOSTING':
                tracker = cv2.TrackerBoosting_create()
            if tracker_type == 'MIL':
                tracker = cv2.TrackerMIL_create()
            if tracker_type == 'KCF':
                tracker = cv2.TrackerKCF_create()
            if tracker_type == 'TLD':
                tracker = cv2.TrackerTLD_create()
            if tracker_type == 'MEDIANFLOW':
                tracker = cv2.TrackerMedianFlow_create()
            if tracker_type == 'GOTURN':
                tracker = cv2.TrackerGOTURN_create()
    
        # Read video
        video = cv2.VideoCapture("chaplin.mp4")
        # video = cv2.VideoCapture(0)
    
        # Exit if video not opened.
        if not video.isOpened():
            print("Could not open video")
            sys.exit()
    
        # Read first frame.
        ok, frame = video.read()
        if not ok:
            print('Cannot read video file')
            sys.exit()
    
        # Define an initial bounding box
        bbox = (287, 23, 86, 320)
    
        # Uncomment the line below to select a different bounding box
        bbox1 = cv2.selectROI(frame, False)
        bbox2 = cv2.selectROI(frame, False)
        print(bbox1, bbox2)
        # Initialize tracker with first frame and bounding box
        tracker1 = cv2.TrackerMIL_create()
        ok1 = tracker.init(frame, bbox1)
        ok2 = tracker1.init(frame, bbox2)
    
        fourcc = cv2.VideoWriter_fourcc(*'XVID')
        out = cv2.VideoWriter('track_point.mp4', fourcc, 10, (640, 360))
    
        while True:
            # Read a new frame
            ok, frame = video.read()
            if not ok1:
                break
    
            # Start timer
            timer = cv2.getTickCount()
    
            # Update tracker
            ok1, bbox1 = tracker.update(frame)
            ok2, bbox2 = tracker1.update(frame)
            print(bbox1, bbox2)
            # Calculate Frames per second (FPS)
            fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer)
    
            # Draw bounding box
            if ok2:
                # Tracking success
                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:
                # Tracking failure
                cv2.putText(frame, "Tracking failure detected", (100, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)
    
            if ok1:
                # Tracking success
                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:
                # Tracking failure
                cv2.putText(frame, "Tracking failure detected", (100, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)
    
            # Display tracker type on frame
            cv2.putText(frame, tracker_type + " Tracker", (100, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2)
    
            # Display FPS on frame
            cv2.putText(frame, "FPS : " + str(int(fps)), (100, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2)
    
            out.write(frame)
            # Display result
            cv2.imshow("Tracking", frame)
    
            # Exit if ESC pressed
            k = cv2.waitKey(1) & 0xff
            if k == 27: break
    
        out.release()

开始的时候选两次框,点两次enter就行。

刚刚又发现了更好的多目标跟踪代码:https://blog.csdn.net/xiao__run/article/details/81084209

    # -*- coding:utf-8 -*-
    
    import cv2
    
    '''
    if len(sys.argv) != 2:
        print('Input video name is missing')
        exit()
    '''
    
    print('Select 3 tracking targets')
    
    cv2.namedWindow("tracking")
    camera = cv2.VideoCapture(0)
    tracker = cv2.MultiTracker_create()
    init_once = False
    
    ok, image=camera.read()
    if not ok:
        print('Failed to read video')
        exit()
    
    bbox1 = cv2.selectROI('tracking', image)
    bbox2 = cv2.selectROI('tracking', image)
    bbox3 = cv2.selectROI('tracking', image)
    
    while camera.isOpened():
        ok, image=camera.read()
        if not ok:
            print ('no image to read')
            break
    
        if not init_once:
            ok = tracker.add(cv2.TrackerMIL_create(), image, bbox1)
            ok = tracker.add(cv2.TrackerMIL_create(), image, bbox2)
            ok = tracker.add(cv2.TrackerMIL_create(), image, bbox3)
            init_once = True
    
        ok, boxes = tracker.update(image)
        print(ok, boxes)
    
        for newbox in boxes:
            p1 = (int(newbox[0]), int(newbox[1]))
            p2 = (int(newbox[0] + newbox[2]), int(newbox[1] + newbox[3]))
            cv2.rectangle(image, p1, p2, (200,0,0))
    
        cv2.imshow('tracking', image)
        k = cv2.waitKey(1)
        if k == 27 : break # esc pressed

你可能感兴趣的:(python opencv 多目标跟踪)