예제 #1
0
def plot_sequence(tracks, masks, db, index, output_dir, alpha = 0.6, mask_thresh = 0.5, plot_masks = True):
    """Plots a whole sequence

    Args:
        tracks (dict): The dictionary containing the track dictionaries in the form tracks[track_id][frame] = bb
        db (torch.utils.data.Dataset): The dataset with the images belonging to the tracks (e.g. MOT_Sequence object)
        output_dir (String): Directory where to save the resulting images
        masks: Masks of the current frame
        mask_thresh: the treshold if we print a pixel or not
        index: index of the current frame
        plot_masks: if we want to plot masks
    """
    if not osp.exists(output_dir):
        os.makedirs(output_dir)

    # infinite color loop
    cyl = cy('ec', colors)
    loop_cy_iter = cyl()
    styles = defaultdict(lambda: next(loop_cy_iter))

    #for i, v in enumerate(db):
    v = db[index]
    im_path = v['img_path']
    im_name = osp.basename(im_path)
    im_output = osp.join(output_dir, im_name)
    im = cv2.imread(im_path)
    #im = im[:, :, (2, 1, 0)]
    sizes = np.shape(im)
    height = int(sizes[0])
    width = int(sizes[1])

    fig = plt.figure()
    zero_mask= torch.zeros_like(masks[0])
    one_mask = torch.ones_like(masks[0])
    for i,mask in enumerate(masks):
      masks[i]=torch.where(mask>mask_thresh, one_mask, zero_mask)

    finalmask = np.zeros([height,width,3])

    for j, t in tracks.items(): #[track_id][frame] loop over tracks and checks if the track is present in an image (index specifys which image)
        if index in t.keys():
          t_i = t[index]
          color = styles[j]['ec']
          maxiou=0
          tempmask = np.zeros([height,width,3])
          for mask in masks:
              temp_iou = iou(mask[0], t_i)
              if temp_iou>maxiou:
                tempmask[:,:,0] = mask.cpu().numpy()*color[0]
                tempmask[:,:,1] = mask.cpu().numpy()*color[1]
                tempmask[:,:,2] = mask.cpu().numpy()*color[2]
                maxiou=temp_iou

          if(maxiou>0.3):
            finalmask += alpha*tempmask



    masked_image = finalmask + im
    cv2.imwrite(output_dir +'/'+ str(im_name), masked_image)
예제 #2
0
def plot_sequence(tracks, db, first_n_frames=None, savepath=None):
    """Plots a whole sequence

    Args:
        tracks (dict): The dictionary containing the track dictionaries in the form tracks[track_id][frame] = bb
        db (torch.utils.data.Dataset): The dataset with the images belonging to the tracks (e.g. MOT_Sequence object)
    """

    # print("[*] Plotting whole sequence to {}".format(output_dir))

    # if not osp.exists(output_dir):
    # 	os.makedirs(output_dir)

    # infinite color loop
    cyl = cy('ec', colors)
    loop_cy_iter = cyl()
    styles = defaultdict(lambda: next(loop_cy_iter))

    for i, v in enumerate(db):
        img = v['img'].mul(255).permute(1, 2, 0).byte().numpy()
        width, height, _ = img.shape

        dpi = 96
        fig, ax = plt.subplots(1)  #, dpi=dpi)
        #fig.set_size_inches(width,height)#/ dpi, height / dpi)
        ax.set_axis_off()
        ax.imshow(img)

        for j, t in tracks.items():
            if i in t.keys():
                t_i = t[i]
                ax.add_patch(
                    plt.Rectangle((t_i[0], t_i[1]),
                                  t_i[2] - t_i[0],
                                  t_i[3] - t_i[1],
                                  fill=False,
                                  linewidth=1.0,
                                  **styles[j]))

                ax.annotate(j, (t_i[0] + (t_i[2] - t_i[0]) / 2.0, t_i[1] +
                                (t_i[3] - t_i[1]) / 2.0),
                            color=styles[j]['ec'],
                            weight='bold',
                            fontsize=6,
                            ha='center',
                            va='center')

        plt.axis('off')
        # plt.tight_layout()

        if savepath is not None:
            im_output = savepath + "/" + format(i, "04d") + ".png"
            plt.savefig(im_output, dpi=100)
        else:
            plt.show()
        plt.close()
        gc.collect()
        if first_n_frames is not None and first_n_frames - 1 == i:
            break
예제 #3
0
def plot_sequence(tracks, db, output_dir):
    """Plots a whole sequence

    Args:
        tracks (dict): The dictionary containing the track dictionaries in the form tracks[track_id][frame] = bb
        db (torch.utils.data.Dataset): The dataset with the images belonging to the tracks (e.g. MOT_Sequence object)
        output_dir (String): Directory where to save the resulting images
    """

    print("[*] Plotting whole sequence to {}".format(output_dir))

    if not osp.exists(output_dir):
        os.makedirs(output_dir)

    # infinite color loop
    cyl = cy('ec', colors)
    loop_cy_iter = cyl()
    styles = defaultdict(lambda: next(loop_cy_iter))

    for i, v in enumerate(db):
        im_path = v['img_path']
        im_name = osp.basename(im_path)
        im_output = osp.join(output_dir, im_name)
        im = cv2.imread(im_path)
        im = im[:, :, (2, 1, 0)]

        sizes = np.shape(im)
        height = float(sizes[0])
        width = float(sizes[1])

        fig = plt.figure()
        fig.set_size_inches(width / 100, height / 100)
        ax = plt.Axes(fig, [0., 0., 1., 1.])
        ax.set_axis_off()
        fig.add_axes(ax)
        ax.imshow(im)

        for j, t in tracks.items():
            if i in t.keys():
                t_i = t[i]
                ax.add_patch(
                    plt.Rectangle(
                        (t_i[0], t_i[1]),
                        t_i[2] - t_i[0],
                        t_i[3] - t_i[1],
                        fill=False,
                        linewidth=1.0, **styles[j]
                    ))

                ax.annotate(j, (t_i[0] + (t_i[2] - t_i[0]) / 2.0, t_i[1] + (t_i[3] - t_i[1]) / 2.0),
                            color=styles[j]['ec'], weight='bold', fontsize=6, ha='center', va='center')

        plt.axis('off')
        # plt.tight_layout()
        plt.draw()
        plt.savefig(im_output, dpi=100)
        plt.close()
예제 #4
0
파일: plot.py 프로젝트: battyone/PIAA
def get_labelled_style_cycler(cmap='viridis'):

    try:
        cmap_colors = cm.get_cmap(cmap).colors
    except ValueError:
        raise Exception(f'Invalid colormap {cmap}')

    cyl = cy('c', cmap_colors)

    finite_cy_iter = iter(cyl)
    styles = defaultdict(lambda: next(finite_cy_iter))

    return styles
예제 #5
0
def plot_sequence(tracks, db, output_dir):

    if not osp.exists(output_dir):
        os.makedirs(output_dir)

    cyl = cy('ec', colors)
    loop_cy_iter = cyl()
    styles = defaultdict(lambda: next(loop_cy_iter))

    for i, v in enumerate(db):
        im_path = v['img_path']
        im_name = osp.basename(im_path)
        im_output = osp.join(output_dir, im_name)
        im = cv2.imread(im_path)
        im = im[:, :, (2, 1, 0)]

        sizes = np.shape(im)
        height = float(sizes[0])
        width = float(sizes[1])

        fig = plt.figure()
        fig.set_size_inches(width / 100, height / 100)
        ax = plt.Axes(fig, [0., 0., 1., 1.])
        ax.set_axis_off()
        fig.add_axes(ax)
        ax.imshow(im)

        for j, t in tracks.items():
            if i in t.keys():
                t_i = t[i]
                ax.add_patch(
                    plt.Rectangle((t_i[0], t_i[1]),
                                  t_i[2] - t_i[0],
                                  t_i[3] - t_i[1],
                                  fill=False,
                                  linewidth=2.0,
                                  **styles[j]))

                ax.annotate(j,
                            xy=(t_i[0], t_i[1] + 3),
                            color=styles[j]['ec'],
                            weight='bold',
                            fontsize=20,
                            ha='left',
                            va='bottom')

        plt.axis('off')
        # plt.tight_layout()
        plt.draw()
        plt.savefig(im_output, dpi=100)
        plt.close()
예제 #6
0
def plot_sequence(tracks, im_dir, output_dir):
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    # infinite color loop
    cyl = cy('ec', colors)
    loop_cy_iter = cyl()
    styles = defaultdict(lambda: next(loop_cy_iter))

    im_names = os.listdir(im_dir)
    for i, im_name in enumerate(im_names):
        im_output = os.path.join(output_dir, im_name)
        im = Image.open(os.path.join(im_dir, im_name))

        sizes = np.shape(im)
        height = float(sizes[0])
        width = float(sizes[1])

        fig = plt.figure()
        fig.set_size_inches(width / 100, height / 100)
        ax = plt.Axes(fig, [0., 0., 1., 1.])
        ax.set_axis_off()
        fig.add_axes(ax)
        ax.imshow(im)

        for j, t in tracks.items():
            if i in t.keys():
                t_i = t[i]
                ax.add_patch(
                    plt.Rectangle((t_i[0], t_i[1]),
                                  t_i[2] - t_i[0],
                                  t_i[3] - t_i[1],
                                  fill=False,
                                  linewidth=1.0,
                                  **styles[j]))

                ax.annotate(j, (t_i[0] + (t_i[2] - t_i[0]) / 2.0, t_i[1] +
                                (t_i[3] - t_i[1]) / 2.0),
                            color=styles[j]['ec'],
                            weight='bold',
                            fontsize=12,
                            ha='center',
                            va='center')

        plt.axis('off')
        # plt.tight_layout()
        plt.draw()
        plt.savefig(im_output, dpi=100)
        plt.close()
def plot_crab(hdr):
    fig, ax = plt.subplots()
    tab = db.get_table(hdr, stream_name='primary')
    cols = tab.columns
    readbacks = [k for k in cols if 'readback' in k and 'elevation' not in k]
    setpoint = [k for k in cols if 'setpoint' in k and 'elevation' not in k]
    cyl = cy('color', ['r', 'g', 'b', 'k'])
    finite_cy_iter = iter(cyl)
    dd = defaultdict(lambda : next(finite_cy_iter))
    side_map = {'upper': 'r', 'lower': 'b'}
    end_map = {'ds': 2, 'us': 3}
    val_map = {'setpoint': ':', 'readback':'-'}

    for nm in setpoint + readbacks:
        _, end, side, val = mtr = nm.rsplit('_')
        ax.plot('time', nm, data=tab, color=side_map[side], 
                lw=end_map[end], ls=val_map[val])
    end_h = [mpatches.Patch(color=v, label=k) for k,v in side_map.items()]
    side_h = [mlines.Line2D([],[], color='k', lw=v, label=k)
           for k,v in end_map.items()]
    val_h = [mlines.Line2D([],[], color='k', ls=v, label=k)
           for k,v in val_map.items()]
    ax.legend(handles=end_h + side_h + val_h, loc='best')
    ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M:%S'))
예제 #8
0
def plot_tracks(blobs, tracks, gt_tracks=None, output_dir=None, name=None):
    # output_dir = get_output_dir("anchor_gt_demo")
    im_paths = blobs['im_paths']
    if not name:
        im0_name = osp.basename(im_paths[0])
    else:
        im0_name = str(name) + ".jpg"
    im0 = cv2.imread(im_paths[0])
    im1 = cv2.imread(im_paths[1])
    im0 = im0[:, :, (2, 1, 0)]
    im1 = im1[:, :, (2, 1, 0)]

    im_scales = blobs['im_info'][0, 2]

    tracks = tracks.data.cpu().numpy() / im_scales
    num_tracks = tracks.shape[0]

    fig, ax = plt.subplots(1, 2, figsize=(12, 6))

    ax[0].imshow(im0, aspect='equal')
    ax[1].imshow(im1, aspect='equal')

    # infinte color loop
    cyl = cy('ec', colors)
    loop_cy_iter = cyl()
    styles = defaultdict(lambda: next(loop_cy_iter))

    ax[0].set_title(('{} tracks').format(num_tracks), fontsize=14)

    for i, t in enumerate(tracks):
        t0 = t[0]
        t1 = t[1]
        ax[0].add_patch(
            plt.Rectangle(
                (t0[0], t0[1]),
                t0[2] - t0[0],
                t0[3] - t0[1], fill=False,
                linewidth=1.0, **styles[i]
            ))

        ax[1].add_patch(
            plt.Rectangle(
                (t1[0], t1[1]),
                t1[2] - t1[0],
                t1[3] - t1[1], fill=False,
                linewidth=1.0, **styles[i]
            ))

    if gt_tracks:
        for gt in gt_tracks:
            for i in range(2):
                ax[i].add_patch(
                    plt.Rectangle(
                        (gt[i][0], gt[i][1]),
                        gt[i][2] - gt[i][0],
                        gt[i][3] - gt[i][1], fill=False,
                        edgecolor='blue', linewidth=1.0
                    ))

    plt.axis('off')
    plt.tight_layout()
    plt.draw()
    image = None
    if output_dir:
        im_output = osp.join(output_dir, im0_name)
        plt.savefig(im_output)
    else:
        image = np.fromstring(fig.canvas.tostring_rgb(), dtype='uint8')
        image = image.reshape(fig.canvas.get_width_height()[::-1] + (3,))
    plt.close()
    return image
예제 #9
0
def plot_sequence(tracks, db, first_n_frames=None, save_folder=None):
    """Plots a whole sequence

    Args:
        tracks (dict): The dictionary containing the track dictionaries in the form tracks[track_id][frame] = bb
        db (torch.utils.data.Dataset): The dataset with the images belonging to the tracks (e.g. MOT_Sequence object)
    """

    # print("[*] Plotting whole sequence to {}".format(output_dir))

    # if not osp.exists(output_dir):
    # 	os.makedirs(output_dir)

    # infinite color loop
    cyl = cy('ec', colors)
    loop_cy_iter = cyl()
    styles = defaultdict(lambda: next(loop_cy_iter))

    for i, v in enumerate(db):
        img = v['img'].mul(255).permute(1, 2, 0).byte().numpy()
        height, width, _ = img.shape

        dpi = 160

        # What size does the figure need to be in inches to fit the image?
        figsize = width / float(dpi), height / float(dpi)

        # Create a figure of the right size with one axes that takes up the full figure
        fig = plt.figure(figsize=figsize)
        ax = fig.add_axes([0, 0, 1, 1])
        ax.set_aspect('equal', adjustable='box')

        # Hide spines, ticks, etc.
        ax.axis('off')

        # Display the image.
        ax.imshow(img)

        for j, t in tracks.items():
            if i in t.keys():
                t_i = t[i]
                ax.add_patch(
                    plt.Rectangle(
                        (t_i[0], t_i[1]),
                        t_i[2] - t_i[0],
                        t_i[3] - t_i[1],
                        fill=False,
                        linewidth=1.0, **styles[j]
                    ))

                ax.annotate(j, (t_i[0] + (t_i[2] - t_i[0]) / 2.0, t_i[1] + (t_i[3] - t_i[1]) / 2.0),
                            color=styles[j]['ec'], weight='bold', fontsize=10, ha='center', va='center')

        if save_folder:
            fig.savefig(os.path.join(save_folder, f'{i:04d}.png'), dpi=dpi)
            plt.close()
        else:
            fig.show()

        if first_n_frames is not None and first_n_frames - 1 == i:
            break
def my_main(_config):

    print(_config)

    dataset = "mot_train_"
    detections = "FRCNN"

    ##########################
    # Initialize the modules #
    ##########################

    print("[*] Beginning evaluation...")
    module_dir = get_output_dir('MOT17')
    results_dir = module_dir
    module_dir = osp.join(module_dir, 'eval/video_red_green')
    #output_dir = osp.join(results_dir, 'plots')
    #if not osp.exists(output_dir):
    #    os.makedirs(output_dir)

    #sequences_raw = ["MOT17-13", "MOT17-11", "MOT17-10", "MOT17-09", "MOT17-05", "MOT17-04", "MOT17-02", ]

    #sequences = ["{}-{}".format(s, detections) for s in sequences_raw]
    #sequences = sequences[:1]

    # tracker = ["FRCNN_Base", "HAM_SADF17", "MOTDT17", "EDMT17", "IOU17", "MHT_bLSTM", "FWT_17", "jCC", "MHT_DAM_17"]
    # tracker = ["Baseline", "BnW", "FWT_17", "jCC", "MOTDT17", "MHT_DAM_17"]
    tracker = ["FWT", "jCC", "MOTDT17"]#, "Tracktor++"]
    baseline = "Tracktor"

    for t in tracker:
        print("[*] Evaluating {}".format(t))
        for db in Datasets(dataset):
            ################################
            # Make videos for each tracker #
            ################################

            s = "{}-{}".format(db, detections)

            gt_file = osp.join(cfg.DATA_DIR, "MOT17Labels", "train", s, "gt", "gt.txt")
            res_file = osp.join(results_dir, t, s+".txt")
            base_file = osp.join(results_dir, baseline, s+".txt")

            stDB = read_txt_to_struct(res_file)
            gtDB = read_txt_to_struct(gt_file)

            gtDB, distractor_ids = extract_valid_gt_data(gtDB)
            _, M_res, gtDB, stDB = evaluate_new(stDB, gtDB, distractor_ids)
            gt_ids_res = np.unique(gtDB[:, 1])

            bsDB = read_txt_to_struct(base_file)
            gtDB = read_txt_to_struct(gt_file)

            gtDB, distractor_ids = extract_valid_gt_data(gtDB)
            _, M_bs, gtDB, stDB = evaluate_new(bsDB, gtDB, distractor_ids)
            gt_ids_base = np.unique(gtDB[:, 1])


            gtDB = read_txt_to_struct(gt_file)
            # filter out so that confidence and id = 1
            gtDB = gtDB[gtDB[:,7] == 1]
            gtDB = gtDB[gtDB[:,6] == 1]

            #st_ids = np.unique(stDB[:, 1])
            #gt_ids = np.unique(gtDB[:, 1])
            gt_frames = np.unique(gtDB[:, 0])
            f_gt = len(gt_frames)

            gt_inds = [{} for i in range(f_gt)]
            #st_inds = [{} for i in range(f_gt)]

            # hash the indices to speed up indexing
            for i in range(gtDB.shape[0]):
                frame = np.where(gt_frames == gtDB[i, 0])[0][0]
                #gid = np.where(gt_ids == gtDB[i, 1])[0][0]
                gt_id = int(gtDB[i,1])
                gt_inds[frame][gt_id] = i

            #gt_frames_list = list(gt_frames)
            #for i in range(stDB.shape[0]):
                # sometimes detection missed in certain frames, thus should be assigned to groundtruth frame id for alignment
            #    frame = gt_frames_list.index(stDB[i, 0])
            #    sid = np.where(st_ids == stDB[i, 1])[0][0]
            #    st_inds[frame][sid] = i

            results = []
            for frame in range(f_gt):
                # get gt_ids in res
                m_res = M_res[frame]
                gids = list(m_res.keys())
                res_gt = []
                for gid in gids:
                    res_gt.append(gt_ids_res[gid])

                # get gt_ids in base
                m_bs = M_bs[frame]
                gids = list(m_bs.keys())
                base_gt = []
                for gid in gids:
                    base_gt.append(gt_ids_base[gid])

                # get unique gt ids
                unique_gt = np.unique(res_gt + base_gt)
                #print("res gt: {}".format(res_gt))
                #print("base gt: {}".format(base_gt))

                for gt in unique_gt:
                    gt = int(gt)
                    #print(gt)
                    res = np.zeros(6)
                    res[0] = frame+1
                    res[2:6] = gtDB[gt_inds[frame][gt], 2:6]
                    if gt in res_gt and gt in base_gt:
                        res[1] = 1
                    elif gt in base_gt:
                        res[1] = 2
                    elif gt in res_gt:
                        res[1] = 3
                    results.append(res)

            results = np.array(results)

            output_dir = osp.join(module_dir, t, s)
            if not osp.exists(output_dir):
                os.makedirs(output_dir)

            print("[*] Plotting whole sequence to {}".format(output_dir))

            # infinte color loop
            cyl = cy('ec', colors)
            loop_cy_iter = cyl()
            styles = defaultdict(lambda : next(loop_cy_iter))

            for frame,v in enumerate(db,1):
                im_path = v['im_path']
                im_name = osp.basename(im_path)
                im_output = osp.join(output_dir, im_name)
                im = cv2.imread(im_path)
                im = im[:, :, (2, 1, 0)]

                sizes = np.shape(im)
                height = float(sizes[0])
                width = float(sizes[1])

                fig = plt.figure()
                #fig.set_size_inches(w,h)
                #fig.set_size_inches(width/height, 1, forward=False)
                #fig.set_size_inches(width/100, height/100)
                scale = width/640
                #fig.set_size_inches(640/100, height*scale/100)
                fig.set_size_inches(width/100, height/100)
                ax = plt.Axes(fig, [0., 0., 1., 1.])
                ax.set_axis_off()
                fig.add_axes(ax)
                ax.imshow(im)

                res_frame = results[results[:,0]==frame]

                for j in range(res_frame.shape[0]):
                    box = res_frame[j,2:6]
                    gt_id = int(res_frame[j,1])
                    ax.add_patch(
                        plt.Rectangle((box[0], box[1]),
                            box[2] - box[0],
                            box[3] - box[1], fill=False,
                            linewidth=1.3*scale, color=colors[gt_id])
                            #**styles[gt_id])
                    )

                plt.axis('off')
                #plt.tight_layout()
                plt.draw()
                plt.savefig(im_output, dpi=100)
                plt.close()
def my_main(_config):

    print(_config)

    dataset = "mot_train_"
    detections = "SDP"

    ##########################
    # Initialize the modules #
    ##########################
    
    print("[*] Beginning evaluation...")
    module_dir = get_output_dir('videos')
    results_dir = osp.join(module_dir, 'results')
    module_dir = osp.join(module_dir, 'normal_new')
    #output_dir = osp.join(results_dir, 'plots')
    #if not osp.exists(output_dir):
    #    os.makedirs(output_dir)

    #sequences_raw = ["MOT17-13", "MOT17-11", "MOT17-10", "MOT17-09", "MOT17-05", "MOT17-04", "MOT17-02", ]
    
    #sequences = ["{}-{}".format(s, detections) for s in sequences_raw]
    #sequences = sequences[:1]
    
    tracker = ["FRCNN_Base", "HAM_SADF17", "MOTDT17", "EDMT17", "IOU17", "MHT_bLSTM", "FWT_17", "jCC", "MHT_DAM_17"]
    tracker = ["Baseline", "BnW", "FWT_17", "jCC", "MOTDT17", "MHT_DAM_17"]
    tracker = ["Baseline", "BnW", "FWT_17", "jCC", "MOTDT17"]
    
    for t in tracker:
        print("[*] Evaluating {}".format(t))
        for db in Datasets(dataset):
            ################################
            # Make videos for each tracker #
            ################################

            s = "{}-{}".format(db, detections)

            gt_file = osp.join(cfg.DATA_DIR, "MOT17Labels", "train", s, "gt", "gt.txt")
            res_file = osp.join(results_dir, t, s+".txt")

            stDB = read_txt_to_struct(res_file)
            gtDB = read_txt_to_struct(gt_file)
            
            gtDB, distractor_ids = extract_valid_gt_data(gtDB)
            _, M, gtDB, stDB = evaluate_new(stDB, gtDB, distractor_ids)

            st_ids = np.unique(stDB[:, 1])
            gt_ids = np.unique(gtDB[:, 1])
            gt_frames = np.unique(gtDB[:, 0])
            f_gt = len(gt_frames)

            gt_inds = [{} for i in range(f_gt)]
            st_inds = [{} for i in range(f_gt)]

            # hash the indices to speed up indexing
            for i in range(gtDB.shape[0]):
                frame = np.where(gt_frames == gtDB[i, 0])[0][0]
                gid = np.where(gt_ids == gtDB[i, 1])[0][0]
                gt_inds[frame][gid] = i

            gt_frames_list = list(gt_frames)
            for i in range(stDB.shape[0]):
                # sometimes detection missed in certain frames, thus should be assigned to groundtruth frame id for alignment
                frame = gt_frames_list.index(stDB[i, 0])
                sid = np.where(st_ids == stDB[i, 1])[0][0]
                st_inds[frame][sid] = i
            
            # set all ids of st to -1
            #stDB[:,1] = -1

            # set all results to corresponding gt id
            #for frame in range(f_gt):
            #    m = M[frame]
            #    for gid, sid in m.items():
            #        gt_row = gt_inds[frame][gid]
            #        gt_id = int(gtDB[gt_row, 1])
            #        st_row = st_inds[frame][sid]
            #        stDB[st_row, 1] = gt_id

            output_dir = osp.join(module_dir, t, s)
            if not osp.exists(output_dir):
                os.makedirs(output_dir)

            print("[*] Plotting whole sequence to {}".format(output_dir))

            # infinte color loop
            cyl = cy('ec', colors)
            loop_cy_iter = cyl()
            styles = defaultdict(lambda : next(loop_cy_iter))

            for frame,v in enumerate(db,1):
                im_path = v['im_path']
                im_name = osp.basename(im_path)
                im_output = osp.join(output_dir, im_name)
                im = cv2.imread(im_path)
                im = im[:, :, (2, 1, 0)]

                sizes = np.shape(im)
                height = float(sizes[0])
                width = float(sizes[1])

                fig = plt.figure()
                #fig.set_size_inches(w,h)
                #fig.set_size_inches(width/height, 1, forward=False)
                #fig.set_size_inches(width/100, height/100)
                scale = width/640
                #fig.set_size_inches(640/100, height*scale/100)
                fig.set_size_inches(width/100, height/100)
                ax = plt.Axes(fig, [0., 0., 1., 1.])
                ax.set_axis_off()
                fig.add_axes(ax)
                ax.imshow(im)

                st_frame = stDB[stDB[:,0]==frame]

                for j in range(st_frame.shape[0]):
                    box = st_frame[j,2:6]
                    gt_id = st_frame[j,1]
                    ax.add_patch(
                        plt.Rectangle((box[0], box[1]),
                            box[2] - box[0],
                            box[3] - box[1], fill=False,
                            linewidth=1.3*scale, **styles[gt_id])
                    )

                plt.axis('off')
                #plt.tight_layout()
                plt.draw()
                plt.savefig(im_output, dpi=100)
                plt.close()
예제 #12
0
def my_main(_config):

    print(_config)

    dataset = "mot_train_"
    detections = "FRCNN"

    ##########################
    # Initialize the modules #
    ##########################

    print("[*] Beginning evaluation...")
    module_dir = get_output_dir('MOT17')
    results_dir = module_dir
    module_dir = osp.join(module_dir, 'eval/video_fp')
    #output_dir = osp.join(results_dir, 'plots')
    #if not osp.exists(output_dir):
    #    os.makedirs(output_dir)

    #sequences_raw = ["MOT17-13", "MOT17-11", "MOT17-10", "MOT17-09", "MOT17-05", "MOT17-04", "MOT17-02", ]

    #sequences = ["{}-{}".format(s, detections) for s in sequences_raw]
    #sequences = sequences[:1]

    # tracker = ["FRCNN_Base", "HAM_SADF17", "MOTDT17", "EDMT17", "IOU17", "MHT_bLSTM", "FWT_17", "jCC", "MHT_DAM_17"]
    # tracker = ["Baseline", "BnW", "FWT_17", "jCC", "MOTDT17", "MHT_DAM_17"]
    tracker = ["Tracktor", "FWT", "jCC", "MOTDT17"]
    #tracker = ["Baseline"]

    for t in tracker:
        print("[*] Evaluating {}".format(t))
        if True:
            #for db in Datasets(dataset):
            ################################
            # Make videos for each tracker #
            ################################
            db = Datasets(dataset)[2]

            s = "{}-{}".format(db, detections)

            gt_file = osp.join(cfg.DATA_DIR, "MOT17Labels", "train", s, "gt",
                               "gt.txt")
            res_file = osp.join(results_dir, t, s + ".txt")

            stDB = read_txt_to_struct(res_file)
            gtDB = read_txt_to_struct(gt_file)

            gtDB, distractor_ids = extract_valid_gt_data(gtDB)
            _, M, gtDB, stDB = evaluate_new(stDB, gtDB, distractor_ids)
            #gt_ids_res = np.unique(gtDB[:, 1])

            #gtDB = read_txt_to_struct(gt_file)
            # filter out so that confidence and id = 1
            #gtDB = gtDB[gtDB[:,7] == 1]
            #gtDB = gtDB[gtDB[:,6] == 1]

            st_ids = np.unique(stDB[:, 1])
            #gt_ids = np.unique(gtDB[:, 1])
            gt_frames = np.unique(gtDB[:, 0])
            f_gt = len(gt_frames)

            #gt_inds = [{} for i in range(f_gt)]
            st_inds = [{} for i in range(f_gt)]

            # hash the indices to speed up indexing
            #for i in range(gtDB.shape[0]):
            #    frame = np.where(gt_frames == gtDB[i, 0])[0][0]
            #gid = np.where(gt_ids == gtDB[i, 1])[0][0]
            #    gt_id = int(gtDB[i,1])
            #    gt_inds[frame][gt_id] = i

            gt_frames_list = list(gt_frames)
            for i in range(stDB.shape[0]):
                # sometimes detection missed in certain frames, thus should be assigned to groundtruth frame id for alignment
                frame = gt_frames_list.index(stDB[i, 0])
                sid = np.where(st_ids == stDB[i, 1])[0][0]
                st_inds[frame][sid] = i

            #stDB = read_txt_to_struct(res_file)

            results = []
            for frame in range(f_gt):
                # get gt_ids in res
                m = M[frame]
                matched_sids = list(m.values())

                #frame_sids = list(st_inds[frame].keys())

                f = gt_frames_list[frame]
                st_frame = stDB[stDB[:, 0] == f]
                st_uniq_ids = np.unique(st_frame[:, 1])

                for st_id in st_uniq_ids:
                    sid = -1
                    si = np.where(st_ids == st_id)[0]
                    if len(si) > 0:
                        sid = si[0]
                    if sid not in matched_sids:
                        res = np.zeros(6)
                        res[0] = frame + 1
                        st_track = st_frame[st_frame[:, 1] == st_id]
                        res[2:6] = st_track[0, 2:6]
                        results.append(res)
                    else:
                        matched_sids.remove(sid)

            results = np.array(results)
            print(results.shape[0])

            output_dir = osp.join(module_dir, t, s)
            if not osp.exists(output_dir):
                os.makedirs(output_dir)

            print("[*] Plotting whole sequence to {}".format(output_dir))

            # infinte color loop
            cyl = cy('ec', colors)
            loop_cy_iter = cyl()
            styles = defaultdict(lambda: next(loop_cy_iter))

            for frame, v in enumerate(db, 1):
                im_path = v['im_path']
                im_name = osp.basename(im_path)
                im_output = osp.join(output_dir, im_name)
                im = cv2.imread(im_path)
                im = im[:, :, (2, 1, 0)]

                sizes = np.shape(im)
                height = float(sizes[0])
                width = float(sizes[1])

                fig = plt.figure()
                #fig.set_size_inches(w,h)
                #fig.set_size_inches(width/height, 1, forward=False)
                #fig.set_size_inches(width/100, height/100)
                scale = width / 640
                #fig.set_size_inches(640/100, height*scale/100)
                fig.set_size_inches(width / 100, height / 100)
                ax = plt.Axes(fig, [0., 0., 1., 1.])
                ax.set_axis_off()
                fig.add_axes(ax)
                ax.imshow(im)

                res_frame = results[results[:, 0] == frame]

                for j in range(res_frame.shape[0]):
                    box = res_frame[j, 2:6]
                    gt_id = int(res_frame[j, 1])
                    ax.add_patch(
                        plt.Rectangle((box[0], box[1]),
                                      box[2] - box[0],
                                      box[3] - box[1],
                                      fill=False,
                                      linewidth=1.3 * scale,
                                      color='blue'))

                ax.annotate(t, (width - 250, height - 100),
                            color='white',
                            weight='bold',
                            fontsize=72,
                            ha='center',
                            va='center')

                plt.axis('off')
                plt.draw()
                plt.savefig(im_output, dpi=100)
                plt.close()