Example #1
0
def show_image_with_boxes(img, calib, objects=[], objects_pred=[]):
    """ Show image with 2D bounding boxes """
    img_2d = np.copy(img)  # for 2d bbox
    img_3d = np.copy(img)  # for 3d bbox

    for obj in objects:
        if obj.type not in care_types:
            continue
        cv2.rectangle(
            img_2d,
            (int(obj.xmin), int(obj.ymin)),
            (int(obj.xmax), int(obj.ymax)),
            (0, 255, 0),
            2,
        )
        box3d_pts_2d, _ = utils.compute_box_3d(obj, calib.P)
        img_3d = utils.draw_projected_box3d(img_3d, box3d_pts_2d)

    for obj in objects_pred:
        if obj.type not in care_types:
            continue
        cv2.rectangle(
            img_2d,
            (int(obj.xmin), int(obj.ymin)),
            (int(obj.xmax), int(obj.ymax)),
            (0, 0, 255),
            2,
        )
        box3d_pts_2d, _ = utils.compute_box_3d(obj, calib.P)
        img_3d = utils.draw_projected_box3d(img_3d,
                                            box3d_pts_2d,
                                            color=(0, 0, 255))

    return img_2d, img_3d
def show_image_with_boxes(img, objects, calib, show3d=True):
    ''' Show image with 2D bounding boxes '''
    img1 = np.copy(img)  # for 2d bbox
    img2 = np.copy(img)  # for 3d bbox
    img3 = np.copy(img)  # for 3d bbox
    for obj in objects:
        if obj.type == 'DontCare': continue
        cv2.rectangle(img1, (int(obj.xmin), int(obj.ymin)),
                      (int(obj.xmax), int(obj.ymax)), (0, 255, 0), 2)
        box3d_pts_2d, box3d_pts_3d = utils.compute_box_3d(obj, calib.P)
        img2 = utils.draw_projected_box3d(img2, box3d_pts_2d)

        # project
        box3d_pts_3d_velo = calib.project_rect_to_velo(box3d_pts_3d)
        #box3d_pts_32d = utils.box3d_to_rgb_box00(box3d_pts_3d_velo)
        box3d_pts_32d = calib.project_velo_to_image(box3d_pts_3d_velo)

        img3 = utils.draw_projected_box3d(img3, box3d_pts_32d)
    #print("img1:", img1.shape)
    Image.fromarray(img1).show()
    print("img3:", img3.shape)
    Image.fromarray(img3).show()
    show3d = False
    if show3d:
        print("img2:", img2.shape)
        Image.fromarray(img2).show()
Example #3
0
def show_image_with_boxes(img, objects, calib, show3d=True):
    """
    Show image with 2D bounding boxes
    :param img: image loaded
    :param objects:
    :param calib:
    :param show3d:
    :return:
    """
    img1 = np.copy(img)  # for 2d bbox
    img2 = np.copy(img)  # for 3d bbox
    for obj in objects:  # process each object in an image
        if obj.type == 'DontCare': continue  # remove 'DontCare' class
        # draw 2d bbox
        cv2.rectangle(img1, (int(obj.xmin), int(obj.ymin)),
                      (int(obj.xmax), int(obj.ymax)), (0, 255, 0), 2)

        # calculate 3d bbox for left color cam from P: P2
        box3d_pts_2d, box3d_pts_3d = utils.compute_box_3d(obj, calib.P)
        print("********************, ", obj.type)
        # print("********************, ", box3d_pts_2d.shape)
        img2 = utils.draw_projected_box3d(img2, box3d_pts_2d)

    #  Image.fromarray(img1).show()
    cv2.imshow('2D bounding box in image',
               cv2.cvtColor(img1, cv2.COLOR_BGR2RGB))
    if show3d:
        # Image.fromarray(img2).show()
        cv2.imshow('3D bounding box in image',
                   cv2.cvtColor(img2, cv2.COLOR_BGR2RGB))
    cv2.waitKey(0)
    cv2.destroyAllWindows()
def show_image_with_boxes(img,
                          data_idx,
                          objects,
                          calib,
                          show3d=True,
                          depth=None):
    """ Show image with 2D bounding boxes """
    img1 = np.copy(img)  # for 2d bbox
    img2 = np.copy(img)  # for 3d bbox
    img3 = np.copy(img)  # for 3d bbox
    for obj in objects:
        if obj.type == "DontCare":
            continue
        cv2.rectangle(
            img1,
            (int(obj.xmin), int(obj.ymin)),
            (int(obj.xmax), int(obj.ymax)),
            (0, 255, 0),
            2,
        )
        box3d_pts_2d, box3d_pts_3d = utils.compute_box_3d(obj, calib.P)
        img2 = utils.draw_projected_box3d(img2, box3d_pts_2d)

        # project
        # box3d_pts_3d_velo = calib.project_rect_to_velo(box3d_pts_3d)
        # box3d_pts_32d = utils.box3d_to_rgb_box00(box3d_pts_3d_velo)
        # box3d_pts_32d = calib.project_velo_to_image(box3d_pts_3d_velo)
        # img3 = utils.draw_projected_box3d(img3, box3d_pts_32d)
    # print("img1:", img1.shape)

    # cv2.imshow("2dbox", img1)
    cv2.imwrite('results/%s_image_with_boxes.png' % data_idx, img2)
def show_image_with_boxes(img, objects, calib, show3d=True, depth=None):
    """ Show image with 2D bounding boxes """
    img1 = np.copy(img)  # for 2d bbox
    img2 = np.copy(img)  # for 3d bbox
    img3 = np.copy(img)  # for 3d bbox
    for obj in objects:
        if obj.type == "DontCare":
            continue
        cv2.rectangle(
            img1,
            (int(obj.xmin), int(obj.ymin)),
            (int(obj.xmax), int(obj.ymax)),
            (0, 255, 0),
            2,
        )
        box3d_pts_2d, box3d_pts_3d = utils.compute_box_3d(obj, calib.P)
        img2 = utils.draw_projected_box3d(img2, box3d_pts_2d)

        # project
        # box3d_pts_3d_velo = calib.project_rect_to_velo(box3d_pts_3d)
        # box3d_pts_32d = utils.box3d_to_rgb_box00(box3d_pts_3d_velo)
        # box3d_pts_32d = calib.project_velo_to_image(box3d_pts_3d_velo)
        # img3 = utils.draw_projected_box3d(img3, box3d_pts_32d)
    # print("img1:", img1.shape)
    cv2.imshow("2dbox", img1)
    # print("img3:",img3.shape)
    # Image.fromarray(img3).show()
    show3d = True
    if show3d:
        # print("img2:",img2.shape)
        cv2.imshow("3dbox", img2)
    if depth is not None:
        cv2.imshow("depth", depth)
Example #6
0
def show_image_with_pred_boxes(img,
                               objects,
                               calib,
                               data_idx,
                               savepath,
                               show3d=True,
                               depth=None):
    """ Show image with 2D bounding boxes """
    #img1 = cv2.imread(savepath + "/" + str(data_idx) + ".png")
    #img2 = cv2.imread(savepath + "/" + str(data_idx) + ".png")
    img1 = np.copy(img)  # for 2d bbox
    img2 = np.copy(img)  # for 3d bbox
    for obj in objects:
        if obj.type == "DontCare":
            continue
        cv2.rectangle(
            img1,
            (int(obj.xmin), int(obj.ymin)),
            (int(obj.xmax), int(obj.ymax)),
            (0, 255, 0),
            2,
        )
        box3d_pts_2d, _ = utils.compute_box_3d(obj, calib.P)
        if box3d_pts_2d is not None:
            img2 = utils.draw_projected_box3d(img2,
                                              box3d_pts_2d,
                                              color=(0, 0, 255))
    show3d = True
    #cv2.imwrite(savepath + "/" + str(data_idx) + "2d.png", img1)
    if show3d:
        cv2.imwrite(savepath + "/" + str(data_idx) + "_pred.png", img2)
    if depth is not None:
        cv2.imwrite("imgs/depth" + ".png", depth)
    return img1, img2
def run_on_tracking_image(img, objects, calib):
    img1 = np.copy(img)  # for 2d bbox
    img2 = np.copy(img)  # for 3d bbox
    for obj in objects:
        track_id = int(obj.track)
        color = create_unique_color_uchar(track_id)
        rectangle(img1,
                  obj.xmin,
                  obj.ymin,
                  obj.xmax,
                  obj.ymax,
                  color,
                  label=str(track_id))

        box3d_pts_2d, box3d_pts_3d = utils.compute_box_3d(obj, calib.P)
        if box3d_pts_2d is None: continue
        img2 = utils.draw_projected_box3d(img2,
                                          box3d_pts_2d,
                                          color,
                                          thickness=2)
        left_top = np.amin(box3d_pts_2d, axis=0)
        put_track_id(img2,
                     left_top[0],
                     left_top[1],
                     str(track_id),
                     color,
                     thickness=2)

    # img2 = overlay_class_names(img2, objects, calib)
    return img1, img2
Example #8
0
def show_image_with_boxes_results(img, objects,results, calib, show3d=False):
    ''' Show image with 2D bounding boxes '''
    img1 = np.copy(img) # for 2d bbox
    img2 = np.copy(img) # for 3d bbox
    for obj in objects:
        if obj.type=='DontCare':continue
        cv2.rectangle(img1, (int(obj.xmin),int(obj.ymin)),
            (int(obj.xmax),int(obj.ymax)), (0,255,0), 2)
        box3d_pts_2d, box3d_pts_3d = utils.compute_box_3d(obj, calib.P)
        img2 = utils.draw_projected_box3d(img2, box3d_pts_2d)
    for obj in results:
        if obj.type=='DontCare':continue
        cv2.rectangle(img1, (int(obj.xmin),int(obj.ymin)),
            (int(obj.xmax),int(obj.ymax)), (255,0,0), 2)
        box3d_pts_2d, box3d_pts_3d = utils.compute_box_3d(obj, calib.P)
        #print()
        img2 = utils.draw_projected_box3d(img2, box3d_pts_2d)
    Image.fromarray(img1).show()
    if show3d:
        Image.fromarray(img2).show()
Example #9
0
def show_image_with_boxes(img, objects, calib, show3d=True):
    ''' Show image with 2D bounding boxes '''
    img1 = np.copy(img) # for 2d bbox
    img2 = np.copy(img) # for 3d bbox
    for obj in objects:
        if obj.type=='DontCare':continue
        cv2.rectangle(img1, (int(obj.xmin),int(obj.ymin)),
            (int(obj.xmax),int(obj.ymax)), (0,255,0), 2)
        box3d_pts_2d, box3d_pts_3d = utils.compute_box_3d(obj, calib.P)
        img2 = utils.draw_projected_box3d(img2, box3d_pts_2d)
    Image.fromarray(img1).show()
    if show3d:
        Image.fromarray(img2).show()
def show_image_with_boxes(img, objects, calib, show3d=True, depth=None):
    """ Show image with 2D bounding boxes """
    img1 = np.copy(img)  # for 2d bbox
    img2 = np.copy(img)  # for 3d bbox
    #img3 = np.copy(img)  # for 3d bbox
    global i
    print(len(objects))
    for obj in objects:
        print(obj.type)
        if obj.type == "DontCare":
            continue
        cv2.rectangle(
            img1,
            (int(obj.xmin), int(obj.ymin)),
            (int(obj.xmax), int(obj.ymax)),
            (0, 255, 0),
            2,
        )
        # if (obj.type == "car" or obj.type == "Van" or obj.type == "Truck") and (obj.alpha >0 and obj.alpha < 1.57):
        if (obj.type == "Car" or obj.type == "Van" or obj.type == "Truck"):
            # print("###################")
            box3d_pts_2d, _ = utils.compute_box_3d(obj, calib.P)
            if  box3d_pts_2d is None:
                continue

            img2, img_extract, coord = utils.draw_projected_box3d(img2, box3d_pts_2d)
            if img_extract is not None and img_extract.shape[0] >= 60 and img_extract.shape[1] >= 40:
                # print(img_extract.shape)
                # print("writing img and bbox")
                cv2.imwrite('./result/{}.jpg'.format(i), img_extract)
                np.savetxt('./result/' + str(i), coord)

                i += 1

        # project
        # box3d_pts_3d_velo = calib.project_rect_to_velo(box3d_pts_3d)
        # box3d_pts_32d = utils.box3d_to_rgb_box00(box3d_pts_3d_velo)
        # box3d_pts_32d = calib.project_velo_to_image(box3d_pts_3d_velo)
        # img3 = utils.draw_projected_box3d(img3, box3d_pts_32d)
    # print("img1:", img1.shape)
    # cv2.imshow("2dbox", img1)
    # print("img3:",img3.shape)
    # Image.fromarray(img3).show()
    show3d = True
    # if show3d:
    #     # print("img2:",img2.shape)
    #     cv2.imshow("3dbox", img2)
    # if depth is not None:
    #     cv2.imshow("depth", depth)
    
    return img1, img2
def run_on_opencv_image(img, objects, calib):
    img1 = np.copy(img)  # for 2d bbox
    img2 = np.copy(img)  # for 3d bbox
    for obj in objects:
        if obj.type not in color_map: continue
        cv2.rectangle(img1, (int(obj.xmin), int(obj.ymin)),
                      (int(obj.xmax), int(obj.ymax)), color_map[obj.type], 2)
        box3d_pts_2d, box3d_pts_3d = utils.compute_box_3d(obj, calib.P)
        if box3d_pts_2d is None: continue
        img2 = utils.draw_projected_box3d(img2,
                                          box3d_pts_2d,
                                          color_map[obj.type],
                                          thickness=2)
    # img2 = overlay_class_names(img2, objects, calib)
    return img1, img2
Example #12
0
def save_image_with_boxes(img, objects, calib, path_to_save):
    """ Show image with 2D bounding boxes """
    if objects is None:
        print('no objects')
        return 0

    img1 = np.copy(img)  # for 3d bbox
    for obj in objects:
        if obj.type == "DontCare":
            continue
        box3d_pts_2d, box3d_pts_3d = utils.compute_box_3d(obj, calib.P)
        if box3d_pts_2d is None:
            continue
        img1 = utils.draw_projected_box3d(img1, box3d_pts_2d)

    cv2.imwrite(path_to_save, img1)
Example #13
0
def return_image_with_boxes(img, objects, calib, show3d=True):
    ''' Show image with 2D bounding boxes '''
    img1 = np.copy(img)  # for 2d bbox
    img2 = np.copy(img)  # for 3d bbox
    for obj in objects:
        if obj.type == 'DontCare': continue
        cv2.rectangle(img1, (int(obj.xmin), int(obj.ymin)),
                      (int(obj.xmax), int(obj.ymax)), (0, 255, 0), 2)
        box3d_pts_2d, box3d_pts_3d = utils.compute_box_3d(obj, calib.P)
        if np.sum(box3d_pts_2d == None) != 1:
            img2 = utils.draw_projected_box3d(img2, box3d_pts_2d)
    # Image.fromarray(img1).show()
    if show3d:
        # Image.fromarray(img2).show()
        return img1, img2
    else:
        return img1
Example #14
0
def show_image_with_results(img, results, calib, show3d=True):
    ''' Show image with 2D bounding boxes '''
    img1 = np.copy(img) # for 2d bbox
    img2 = np.copy(img) # for 3d bbox
    # i = 0;
    for obj in results:
        if obj.type=='DontCare':continue
        cv2.rectangle(img1, (int(obj.xmin),int(obj.ymin)),
            (int(obj.xmax),int(obj.ymax)), (255,0,0), 2)
        font = cv2.FONT_HERSHEY_SIMPLEX
        cv2.putText(img1,str(obj.score),(int(obj.xmin),int(obj.ymin)), font, 2,(255,255,255),2,cv2.LINE_AA)
        box3d_pts_2d, box3d_pts_3d = utils.compute_box_3d(obj, calib.P)
        #print(box3d_pts_2d)
        #print(box3d_pts_3d)
        img2 = utils.draw_projected_box3d(img2, box3d_pts_2d)
        # i = i + 1
    Image.fromarray(img1).show()
    if show3d:
        Image.fromarray(img2).show()
Example #15
0
def show_image_with_boxes_right(img_right, objects, calib, show3d=True):
    ''' Show image with 2D bounding boxes '''
    img1 = np.copy(img_right)  # for 2d bbox
    img2 = np.copy(img_right)  # for 3d bbox
    for obj in objects:
        if obj.type == 'DontCare': continue

        box3d_pts_2d, box3d_pts_3d = utils.compute_box_3d(obj, calib.P3)
        img2 = utils.draw_projected_box3d(img2, box3d_pts_2d)

        xcord = box3d_pts_2d[:, 0]
        ycord = box3d_pts_2d[:, 1]

        cv2.rectangle(img1, (int(min(xcord)), int(min(ycord))),
                      (int(max(xcord)), int(max(ycord))), (0, 255, 0), 2)

    Image.fromarray(img1).show(title="Left Image 2D")
    if show3d:
        Image.fromarray(img2).show(title="Left Image 3D")
Example #16
0
def show_image_with_boxes(data_idx,
                          img,
                          objects,
                          calib,
                          show3d=True,
                          depth=None):
    img_res = np.copy(img)
    for obj in objects:
        if obj.type == "DontCare":
            continue

        obj.score = norm_score(obj.score)
        if obj.score < args.threshold:
            continue

        box3d_pts_2d, box3d_pts_3d = utils.compute_box_3d(obj, calib.P)
        img_res = utils.draw_projected_box3d(img_res, box3d_pts_2d, obj)

    cv2.imwrite(path.join(output_dir, '%.3d_image_with_boxes.png' % data_idx),
                img_res)
Example #17
0
def show_image_with_boxes(img,
                          objects,
                          calib,
                          data_idx,
                          savepath,
                          ignores_count,
                          show3d=True,
                          depth=None):
    """ Show image with 2D bounding boxes """
    img1 = np.copy(img)  # for 2d bbox
    img2 = np.copy(img)  # for 3d bbox
    ignores = filter_objects(objects)
    for obj in ignores:
        if obj == True:
            ignores_count += 1
    index = 0
    for obj in objects:
        if obj.type == "DontCare":
            continue
        if ignores[index] == True:
            ignores_count += 1
            index += 1
            continue
        index += 1
        cv2.rectangle(
            img1,
            (int(obj.xmin), int(obj.ymin)),
            (int(obj.xmax), int(obj.ymax)),
            (0, 255, 0),
            2,
        )
        box3d_pts_2d, _ = utils.compute_box_3d(obj, calib.P)
        if box3d_pts_2d is not None:
            img2 = utils.draw_projected_box3d(img2, box3d_pts_2d)
    show3d = True
    cv2.imwrite(savepath + "/2d_ignores.png", img1)
    if show3d:
        cv2.imwrite(savepath + "/" + str(data_idx) + ".png", img2)
    if depth is not None:
        cv2.imwrite("imgs/depth" + ".png", depth)
    return img1, img2, ignores_count
Example #18
0
def show_image_with_boxes(img, objects, calib, show3d=True, depth=None):
    """ Show image with 2D bounding boxes """
    # img1 = np.copy(img)  # for 2d bbox
    img2 = np.copy(img)  # for 3d bbox
    #img3 = np.copy(img)  # for 3d bbox
    # import pdb; pdb.set_trace()
    for obj in objects:
        if obj.type == "DontCare":
            continue
        # cv2.rectangle(
        #     img1,
        #     (int(obj.xmin), int(obj.ymin)),
        #     (int(obj.xmax), int(obj.ymax)),
        #     (0, 255, 0),
        #     2,
        # )
        # import pdb; pdb.set_trace()
        box3d_pts_2d, _, check = utils.compute_box_3d(obj, calib)
        if not check:
            continue
        img2 = utils.draw_projected_box3d(img2, box3d_pts_2d)

        # project
        # box3d_pts_3d_velo = calib.project_rect_to_velo(box3d_pts_3d)
        # box3d_pts_32d = utils.box3d_to_rgb_box00(box3d_pts_3d_velo)
        # box3d_pts_32d = calib.project_velo_to_image(box3d_pts_3d_velo)
        # img3 = utils.draw_projected_box3d(img3, box3d_pts_32d)
    # print("img1:", img1.shape)
    # cv2.imshow("2dbox", img1)
    # print("img3:",img3.shape)
    # Image.fromarray(img3).show()
    # show3d = True
    # if show3d:
    #     # print("img2:",img2.shape)
    #     cv2.imshow("3dbox", img2)
    # if depth is not None:
    #     cv2.imshow("depth", depth)

    return img2
Example #19
0
def return_image_with_preds(img, objects, calib, show3d=True):
    ''' Show image with 2D bounding boxes '''
    img1 = np.copy(img)  # for 2d bbox
    img2 = np.copy(img)  # for 3d bbox
    for obj in objects:
        if obj.type == 'DontCare': continue
        cv2.rectangle(img1, (int(obj.xmin), int(obj.ymin)),
                      (int(obj.xmax), int(obj.ymax)), (0, 255, 0), 2)
        cv2.putText(img1,
                    "%.2f" % (obj.score),
                    org=(int(obj.xmin), int(obj.ymin)),
                    fontFace=cv2.FONT_HERSHEY_TRIPLEX,
                    fontScale=0.5,
                    color=(0, 0, 255))
        box3d_pts_2d, box3d_pts_3d = utils.compute_box_3d(obj, calib.P)
        if np.sum(box3d_pts_2d == None) != 1:
            img2 = utils.draw_projected_box3d(img2, box3d_pts_2d)
    # Image.fromarray(img1).show()
    if show3d:
        # Image.fromarray(img2).show()
        return img1, img2
    else:
        return img1
Example #20
0
def show_image_with_boxes(img,
                          objects=None,
                          calib=None,
                          show3d=True,
                          depth=None,
                          track_pred=None):
    """ Show image with 2D bounding boxes """
    img1 = np.copy(img)  # for 3d detection bbox
    img2 = np.copy(img)  # for 3d tracking bbox
    img3 = np.copy(img)  # for 3d bbox
    if objects is not None:
        for obj in objects:
            # if obj.type == "DontCare":
            #     continue
            # cv2.rectangle(
            #     img1,
            #     (int(obj.xmin), int(obj.ymin)),
            #     (int(obj.xmax), int(obj.ymax)),
            #     (0, 255, 0),
            #     2,
            # )
            box3d_pts_2d, box3d_pts_3d = utils.compute_box_3d(obj, calib.P)
            try:  #判断读到的结果数据是否有问题
                box3d_pts_2d.any()
            except:  # 不用抛出错误,有错跳出本次循环即可,加上错误类型时会抛出错误
                print('detection results hanve some problems!')
                continue
            else:
                img1 = utils.draw_projected_box3d(img1, box3d_pts_2d)

    if track_pred is not None:  # tracking results
        for obj in track_pred:
            box3d_pts_2d, box3d_pts_3d = utils.compute_box_3d(obj, calib.P)
            #print(box3d_pts_2d)
            try:  #判断读到的结果数据是否有问题
                box3d_pts_2d.any()
            except:  # 不用抛出错误,有错跳出本次循环即可,加上错误类型时会抛出错误
                print('tracking results hanve some problems!')
                continue
            else:
                img2 = utils.draw_projected_box3d(img2, box3d_pts_2d)

                font = cv2.FONT_HERSHEY_SIMPLEX
                cv2.putText(img2, 'trackid:%d' % obj.trackid, (int(
                    (obj.xmin + obj.xmax) / 2.1), int(obj.ymin - 10)), font,
                            0.5, (0, 255, 0), 1)
                #画trackid 绘制的文字,位置,字型,字体大小,文字颜色,线型

        # project
        # box3d_pts_3d_velo = calib.project_rect_to_velo(box3d_pts_3d)
        # box3d_pts_32d = utils.box3d_to_rgb_box00(box3d_pts_3d_velo)
        # box3d_pts_32d = calib.project_velo_to_image(box3d_pts_3d_velo)
        # img3 = utils.draw_projected_box3d(img3, box3d_pts_32d)
    # print("img1:", img1.shape)
    #cv2.imshow("2dbox", img1)

    cv2.namedWindow("tracking results", cv2.WINDOW_FREERATIO)  # 可鼠标调节自适应比例
    cv2.namedWindow("detection results", cv2.WINDOW_FREERATIO)  # 可鼠标调节自适应比例
    cv2.imshow("detection results", img1)
    # print("img3:",img3.shape)
    # Image.fromarray(img3).show()
    #show3d = True
    if show3d:  # show tracking results
        # print("img2:",img2.shape)
        cv2.imshow("tracking results", img2)
    if depth is not None:
        cv2.imshow("depth", depth)
Example #21
0
def show_image_with_boxes(img, objects, calib, show3d=True, depth=None):
    """ Show image with 2D bounding boxes """
    img1 = np.copy(img)  # for 2d bbox
    img2 = np.copy(img)  # for 3d bbox
    #img3 = np.copy(img)  # for 3d bbox
    #TODO: change the color of boxes
    for obj in objects:
        if obj.type == "DontCare":
            continue
        if obj.type == "Car":
            cv2.rectangle(
                img1,
                (int(obj.xmin), int(obj.ymin)),
                (int(obj.xmax), int(obj.ymax)),
                (0, 255, 0),
                2,
            )
        if obj.type == "Pedestrian":
            cv2.rectangle(
                img1,
                (int(obj.xmin), int(obj.ymin)),
                (int(obj.xmax), int(obj.ymax)),
                (255, 255, 0),
                2,
            )
        if obj.type == "Cyclist":
            cv2.rectangle(
                img1,
                (int(obj.xmin), int(obj.ymin)),
                (int(obj.xmax), int(obj.ymax)),
                (0, 255, 255),
                2,
            )
        box3d_pts_2d, _ = utils.compute_box_3d(obj, calib.P)
        if box3d_pts_2d is None:
            print("something wrong in the 3D box.")
            continue
        if obj.type == "Car":
            img2 = utils.draw_projected_box3d(img2,
                                              box3d_pts_2d,
                                              color=(0, 255, 0))
        elif obj.type == "Pedestrian":
            img2 = utils.draw_projected_box3d(img2,
                                              box3d_pts_2d,
                                              color=(255, 255, 0))
        elif obj.type == "Cyclist":
            img2 = utils.draw_projected_box3d(img2,
                                              box3d_pts_2d,
                                              color=(0, 255, 255))

        # project
        # box3d_pts_3d_velo = calib.project_rect_to_velo(box3d_pts_3d)
        # box3d_pts_32d = utils.box3d_to_rgb_box00(box3d_pts_3d_velo)
        # box3d_pts_32d = calib.project_velo_to_image(box3d_pts_3d_velo)
        # img3 = utils.draw_projected_box3d(img3, box3d_pts_32d)
    # print("img1:", img1.shape)
    cv2.imshow("2dbox", img1)
    # print("img3:",img3.shape)
    # Image.fromarray(img3).show()
    show3d = True
    if show3d:
        # print("img2:",img2.shape)
        cv2.imshow("3dbox", img2)
    if depth is not None:
        cv2.imshow("depth", depth)

    return img1, img2
Example #22
0
def show_image_with_boxes(img,
                          objects,
                          pred_objects,
                          calib,
                          save_dir,
                          data_idx,
                          scale,
                          save_img=False,
                          show3d=True,
                          depth=None):
    """ Show image with 2D bounding boxes """
    img1 = np.copy(img)  # for 2d bbox
    img2 = np.copy(img)  # for 3d bbox

    for obj in objects:
        if obj.type != 'Car' or obj.t[2] > 70.4:
            continue
        cv2.rectangle(
            img1,
            (int(obj.xmin), int(obj.ymin)),
            (int(obj.xmax), int(obj.ymax)),
            (0, 255, 0),
            1,
        )

        box3d_pts_2d, box3d_pts_3d = utils.compute_box_3d(obj, calib.P)
        if (box3d_pts_2d is None):
            continue
        img2 = utils.draw_projected_box3d(img2, box3d_pts_2d)

    if (pred_objects != None):
        for obj in pred_objects:
            if obj.type != 'Car' or obj.t[2] > 70.4:
                continue
            cv2.rectangle(
                img1,
                (int(obj.xmin), int(obj.ymin)),
                (int(obj.xmax), int(obj.ymax)),
                (0, 0, 255),
                1,
            )

            box3d_pts_2d, box3d_pts_3d = utils.compute_box_3d(obj, calib.P)
            if (box3d_pts_2d is None):
                continue
            img2 = utils.draw_projected_box3d(img2,
                                              box3d_pts_2d,
                                              color=(0, 0, 255))

    bbox2d_path = save_dir + '2dbox/' + str(data_idx).zfill(6) + '.png'
    bbox3d_path = save_dir + '3dbox/' + str(data_idx).zfill(6) + '.png'

    if (save_img):
        cv2.imwrite(bbox2d_path, img1)
    else:
        cv2.imshow("2dbox", img1)

    if show3d:
        if (save_img):
            cv2.imwrite(bbox3d_path, img2)
        else:
            cv2.imshow("3dbox", img2)
    if depth is not None:
        cv2.imshow("depth", depth)
Example #23
0
def show_image_with_boxes(img,
                          objects,
                          calib,
                          show3d=True,
                          color=(0, 255, 0),
                          scores=[],
                          colors=[],
                          show=True):
    ''' Show image with 2D bounding boxes '''
    img1 = np.copy(img)  # for 2d bbox
    img2 = np.copy(img)  # for 3d bbox

    if len(colors) != 0:
        assert len(colors) == len(objects)

    for i, obj in enumerate(objects):
        if obj.type == 'DontCare':
            continue
        if len(colors) != 0:
            color = colors[i]
        cv2.rectangle(img1, (int(obj.xmin), int(obj.ymin)),
                      (int(obj.xmax), int(obj.ymax)), color, 2)

        if len(scores) != 0:
            # Show text.
            area = (obj.xmax - obj.xmin) * (obj.ymax - obj.ymin)

            if area > 25:
                cv2.putText(
                    img1,
                    '%.2f' % scores[i], (int(
                        (obj.xmin + obj.xmax) / 2), max(int(obj.ymin) - 3, 0)),
                    cv2.FONT_HERSHEY_PLAIN,
                    fontScale=0.8,
                    color=color,
                    thickness=1,
                    lineType=cv2.LINE_AA)

        box3d_pts_2d, box3d_pts_3d = utils.compute_box_3d(obj, calib.P)
        if box3d_pts_2d is not None:
            img2 = utils.draw_projected_box3d(img2, box3d_pts_2d, color)

            if len(scores) != 0:
                xx1, yy1 = np.min(box3d_pts_2d, 0)
                xx2, yy2 = np.max(box3d_pts_2d, 0)
                if (xx2 - xx1) * (yy2 - yy1) > 25:
                    cv2.putText(img2,
                                '%.2f' % scores[i], (int(
                                    (xx1 + xx2) / 2), max(int(yy1) - 3, 0)),
                                cv2.FONT_HERSHEY_PLAIN,
                                fontScale=0.8,
                                color=color,
                                thickness=1,
                                lineType=cv2.LINE_AA)

    if show:
        Image.fromarray(img1).show()
        if show3d:
            Image.fromarray(img2).show()

    return img1, img2