多目标跟踪 opencv +dlib +python实现

import cv2
import dlib

# Path to the video frames


video_folder='/data/datasets/HB_1_1_25_20170411_142850_03d4f4e4.mp4' #读取视频格式
cap = cv2.VideoCapture(video_folder)

tracker = dlib.correlation_tracker()
tracker2 = dlib.correlation_tracker()
tracker3 = dlib.correlation_tracker()          # 如果是需求追踪几个目标 创建几个追踪的类

selection = None
track_window = None                    # 初始化 get first frame box in window
drag_start = None

def onmouse(event, x, y, flags, param):
    global selection,track_window,drag_start
    if event == cv2.EVENT_LBUTTONDOWN:                # the mouse to click
        drag_start = (x, y)
        track_window = None
    if drag_start:
        xmin = min(x, drag_start[0])
        ymin = min(y, drag_start[1])
        xmax = max(x, drag_start[0])
        ymax = max(y, drag_start[1])
        selection = (xmin, ymin, xmax, ymax)          # save mouse draw box coord
    if event == cv2.EVENT_LBUTTONUP:
        drag_start = None
        track_window = selection
        selection = None


def main():
    track_window1=()
    track_window2=()
    track_window3=()

    cv2.namedWindow('image',1)

    cv2.setMouseCallback('image',onmouse)
    # We will track the frames as we load them off of disk
    # for k, f in enumerate(sorted(glob.glob(os.path.join(video_folder, "*.jpg")))):   #如果读取文件夹下解析的frame 可用 例如voc 等已处理好的数据            
    #     print(f)
    #     print("Processing Frame {}".format(k))
    #     img_raw = cv2.imread(f)
    #     img_raw = cv2.resize(img_raw, (256, 256), interpolation=cv2.INTER_CUBIC)
    #     image = img_raw.copy()
    k=0
    while 1:
        # AVI_path = glob.glob(video_folder + '*.mp4')
        # vidos = filter(lambda x: x.endswith('mp4'), AVI_path)
        # for each_video in vidos:
        #     print each_video
        #     while 1:
        #         cap = cv2.VideoCapture(each_video)
        #         # cap.set(cv2.CAP_PROP_POS_FRAMES, frame_count)
        #         ret, frame = cap.read()


        ret, frame = cap.read()         #get image from first frame
        img_raw=frame
        #print img_raw
        image=img_raw.copy()
        if k==0:
            while True:
                img_first=image.copy()
        # We need to initialize the tracker on the first frame
        # if k == 0:
        #     while True:
        #         img_first = image.copy()  #         print '#######',img_first



                if track_window:               #从mouse 画出的bbox 中得到追踪的框
                    cv2.rectangle(img_first,(track_window[0],track_window[1]),(track_window[2],track_window[3]),(0,0,255),1)
                elif selection:
                    cv2.rectangle(img_first,(selection[0],selection[1]),(selection[2],selection[3]),(0,0,255),1)



########################以下时get 多个目标的第一帧的box#######################
                if track_window1:
                    cv2.rectangle(img_first,(track_window1[0],track_window1[1]),(track_window1[2],track_window1[3]),(0,0,255),1)
                if track_window2:
                    cv2.rectangle(img_first,(track_window2[0],track_window2[1]),(track_window2[2],track_window2[3]),(0,255,255),1)
                if track_window3:
                    cv2.rectangle(img_first,(track_window3[0],track_window3[1]),(track_window3[2],track_window3[3]),(255,0,255),1)
                cv2.imshow('image',img_first)
                if cv2.waitKey(10) == 32:    #####设置 asci  空格是32                 # 
                    if not track_window1:
                        track_window1 = track_window
                    elif not track_window2:
                        track_window2 = track_window
                    elif not track_window3:
                        track_window3 = track_window
                    else:
                        break
            #print '------------',track_window
            tracker.start_track(image,dlib.rectangle(track_window1[0], track_window1[1], track_window1[2], track_window1[3]))
            tracker2.start_track(image,dlib.rectangle(track_window2[0], track_window2[1], track_window2[2], track_window2[3]))
            tracker3.start_track(image,dlib.rectangle(track_window3[0], track_window3[1], track_window3[2], track_window3[3]))
        else:
            # Else we just attempt to track from the previous frame
            tracker.update(image)
            tracker2.update(image)
            tracker3.update(image)

        # Get previous box and draw on showing image
        box_predict = tracker.get_position()
        box2_predict = tracker2.get_position()
        box3_predict = tracker3.get_position()
        #print 'box_predict####',box_predict
        cv2.rectangle(image,(int(box_predict.left()),int(box_predict.top())),(int(box_predict.right()),int(box_predict.bottom())),(0,0,255),1)
        cv2.rectangle(image, (int(box2_predict.left()), int(box2_predict.top())),(int(box2_predict.right()), int(box2_predict.bottom())), (0, 0, 255), 1)
        cv2.rectangle(image, (int(box3_predict.left()), int(box3_predict.top())),(int(box3_predict.right()), int(box3_predict.bottom())), (0, 0, 255), 1)
        cv2.imshow('image',image)
        c=cv2.waitKey(10)
        k+=1

    cv2.destroyAllWindows()

if __name__ == '__main__':
    main()

实现:

在视频序列的第一帧鼠标框定多个目标的box ,然后实现在视频序列中跟踪多个目标:

但是这种方法无法实用于遮挡问题 以及调整box的大小问题 

可试用调用其它追踪的model 去实现。后续如果有model 则尝试

多目标跟踪 opencv +dlib +python实现_第1张图片

你可能感兴趣的:(Opencv,学习,检测与追踪,tracking)