Exemple #1
0
def draw_box3d_on_camera(rgb, boxes3d, color=(255, 0, 255), thickness=1, text_lables=[]):
    projections = box3d.box3d_to_rgb_box(boxes3d)
    rgb = box3d.draw_rgb_projections(rgb, projections, color=color, thickness=thickness)
    font = cv2.FONT_HERSHEY_SIMPLEX
    for i,text in enumerate(text_lables):
        text_pos = (np.min(projections[i,:, 0]), max(np.min(projections[i,:, 1]), 15) )
        cv2.putText(rgb, text, text_pos, font, 0.7, (0, 255, 100), 1, cv2.LINE_AA)

    return rgb
Exemple #2
0
def draw_box3d_on_camera(rgb, boxes3d, color=(255, 0, 255), thickness=1, text_lables=[]):

    projections = box3d.box3d_to_rgb_box(boxes3d)
    rgb = box3d.draw_rgb_projections(rgb, projections, color=color, thickness=thickness)
    font = cv2.FONT_HERSHEY_SIMPLEX
    for i,text in enumerate(text_lables):
        text_pos = (np.min(projections[i,:, 0]), max(np.min(projections[i,:, 1]), 15) )
        cv2.putText(rgb, text, text_pos, font, 0.7, (0, 255, 100), 1, cv2.LINE_AA)

    return rgb
Exemple #3
0
def project_to_rgb_roi(rois3d, calib_velo_to_rgb):
    num = len(rois3d)
    rois = np.zeros((num, 5), dtype=np.int32)
    projections = box.box3d_to_rgb_box(rois3d, calib_velo_to_rgb)
    for n in range(num):
        qs = projections[n]
        minx = np.min(qs[:, 0])
        maxx = np.max(qs[:, 0])
        miny = np.min(qs[:, 1])
        maxy = np.max(qs[:, 1])
        rois[n, 1:5] = minx, miny, maxx, maxy

    return rois
Exemple #4
0
def project_to_rgb_roi(rois3d):
    num  = len(rois3d)
    rois = np.zeros((num,5),dtype=np.int32)
    projections = box.box3d_to_rgb_box(rois3d)
    for n in range(num):
        qs = projections[n]
        minx = np.min(qs[:,0])
        maxx = np.max(qs[:,0])
        miny = np.min(qs[:,1])
        maxy = np.max(qs[:,1])
        rois[n,1:5] = minx,miny,maxx,maxy

    return rois
Exemple #5
0
def pred_and_save(tracklet_pred_dir,
                  dataset,
                  generate_video=False,
                  frame_offset=16,
                  log_tag=None,
                  weights_tag=None):
    # Tracklet_saver will check whether the file already exists.
    tracklet = Tracklet_saver(tracklet_pred_dir, 'pred')
    os.makedirs(os.path.join(log_dir, 'image'), exist_ok=True)
    gt_tracklet = Tracklet_saver(tracklet_pred_dir, 'gt')

    top_shape, front_shape, rgb_shape = dataset.get_shape()
    predict = mv3d.Predictor(top_shape,
                             front_shape,
                             rgb_shape,
                             log_tag=log_tag,
                             weights_tag=weights_tag)

    if generate_video:
        vid_in = skvideo.io.FFmpegWriter(os.path.join(log_dir, 'output.mp4'))

    # timer
    timer_step = 100
    if cfg.TRACKING_TIMER:
        time_it = timer()

    print('dataset.size')
    print(dataset.size)
    lenght = []
    gt_lenght = []

    frame_num = 0
    for i in range(dataset.size if fast_test == False else frame_offset + 1):

        rgb, top, front, _, _, _ = dataset.load(size=1)

        frame_num = i - frame_offset
        print('frame_num')
        print(frame_num)
        if frame_num < 0:
            continue

        gt_boxes3d_tmp = np.load(
            '/home/mohsen/Desktop/MV3D/data/preprocessed/kitti/gt_boxes3d/object3d/test/%05d.npy'
            % i)

        #remove gt boxes with hiegh less than 40
        gt_boxes3d_list = []
        for gt_box3d_tmp in gt_boxes3d_tmp:
            # if gt_box3d_tmp[0,0]>0:
            gt_box3d_tmp_list = []
            gt_box3d_tmp_list.append(gt_box3d_tmp)
            gt_project = box3d.box3d_to_rgb_box(gt_box3d_tmp_list)

            if abs(gt_project[0][0, 1] - gt_project[0][4, 1]) >= 40:
                gt_box3d = gt_box3d_tmp
                gt_boxes3d_list.append(gt_box3d)
        gt_boxes3d = np.array(gt_boxes3d_list)
        # gt_boxes3d = gt_boxes3d_tmp

        #####################################
        boxes3d_tmp, probs = predict(top, front, rgb)

        predict.dump_log(log_subdir=log_subdir, n_frame=i)

        # time timer_step iterations. Turn it on/off in config.py
        if cfg.TRACKING_TIMER and i % timer_step == 0 and i != 0:
            predict.track_log.write('It takes %0.2f secs for inferring %d frames. \n' % \
                                    (time_it.time_diff_per_n_loops(), timer_step))

        # for debugging: save image and show image.
        top_image = draw_top_image(top[0])
        rgb_image = rgb[0]

        if len(gt_boxes3d) != 0:

            gt_lenght.append(len(gt_boxes3d))

            gt_translation, gt_size, gt_rotation = boxes3d_decompose(
                gt_boxes3d[:, :, :])

            # todo: remove it after gtbox is ok
            gt_size[:, 1:3] = gt_size[:, 1:3] / cfg.TRACKLET_GTBOX_LENGTH_SCALE

            for j in range(len(gt_translation)):
                gt_tracklet.add_tracklet(frame_num, gt_size[j],
                                         gt_translation[j], gt_rotation[j])

        #remove predicted boxes with hiegh less than 40
        boxes3d_list = []
        for box3d_tmp in boxes3d_tmp:
            # if box3d_tmp[0, 0] > 0:

            box3d_tmp_list = []
            box3d_tmp_list.append(box3d_tmp)
            project = box3d.box3d_to_rgb_box(box3d_tmp_list)

            if abs(project[0][0, 1] - project[0][4, 1]) >= 40:
                print(project[0][0, 1] - project[0][4, 1])
                pred_box3d = box3d_tmp
                boxes3d_list.append(pred_box3d)
        boxes3d = np.array(boxes3d_list)
        # boxes3d = boxes3d_tmp

        #####################################
        print('sizes')
        print(np.size(boxes3d))
        print(gt_boxes3d)
        print(np.size(gt_boxes3d))

        if len(boxes3d) != 0:
            lenght.append(len(boxes3d))

            top_image = draw_box3d_on_top(top_image,
                                          boxes3d[:, :, :],
                                          color=(80, 80, 0),
                                          thickness=3)
            rgb_image = draw_box3d_on_camera(rgb_image,
                                             boxes3d[:, :, :],
                                             color=(0, 0, 80),
                                             thickness=3)

            if len(gt_boxes3d) != 0:
                rgb_image = draw_box3d_on_camera(rgb_image,
                                                 gt_boxes3d[:, :, :],
                                                 color=(0, 80, 0),
                                                 thickness=3)

            translation, size, rotation = boxes3d_decompose(boxes3d[:, :, :])

            # todo: remove it after gtbox is ok
            size[:, 1:3] = size[:, 1:3] / cfg.TRACKLET_GTBOX_LENGTH_SCALE

            for j in range(len(translation)):
                tracklet.add_tracklet(frame_num, size[j], translation[j],
                                      rotation[j])
        resize_scale = top_image.shape[0] / rgb_image.shape[0]
        rgb_image = cv2.resize(
            rgb_image,
            (int(rgb_image.shape[1] * resize_scale), top_image.shape[0]))
        rgb_image = cv2.cvtColor(rgb_image, cv2.COLOR_BGR2RGB)
        new_image = np.concatenate((top_image, rgb_image), axis=1)
        cv2.imwrite(os.path.join(log_dir, 'image', '%5d_image.jpg' % i),
                    new_image)

        if generate_video:
            vid_in.writeFrame(new_image)
            vid_in.close()

    print(lenght)
    print(sum(lenght))
    tracklet.write_tracklet()
    predict.dump_weigths(os.path.join(log_dir, 'pretrained_model'))
    print(gt_lenght)
    print(sum(gt_lenght))
    gt_tracklet.write_tracklet()

    if cfg.TRACKING_TIMER:
        predict.log_msg.write('It takes %0.2f secs for inferring the whole test dataset. \n' % \
                              (time_it.total_time()))

    print("tracklet file named tracklet_labels.xml is written successfully.")
    return tracklet.path, gt_tracklet.path