예제 #1
0
def kitti_bbox2results(boxes_lidar, scores, labels, meta, class_names=None):
    calib = meta['calib']
    sample_id = meta['sample_idx']
    image_shape = meta['img_shape'][: 2]
    if scores is None \
        or len(scores) == 0 \
            or boxes_lidar is None \
                or len(boxes_lidar) == 0:

        return kitti.empty_result_anno()

    hehe = kitti.get_start_result_anno()
    hehe.update({'image_idx': []})

    boxes_lidar[:, -1] = limit_period(
        boxes_lidar[:, -1], offset=0.5, period=np.pi * 2,
    )
    boxes_cam = np.zeros_like(boxes_lidar)
    boxes_cam[:, :3] = project_velo_to_rect(boxes_lidar[:, :3], calib)
    boxes_cam[:, 3:] = boxes_lidar[:, [4, 5, 3, 6]]
    corners_cam = center_to_corner_box3d(boxes_cam, origin=[0.5, 1.0, 0.5], axis=1)
    corners_rgb = project_rect_to_image(corners_cam, calib)
    minxy = np.min(corners_rgb, axis=1)
    maxxy = np.max(corners_rgb, axis=1)
    box2d_rgb = np.concatenate([minxy, maxxy], axis=1)
    alphas = -np.arctan2(-boxes_lidar[:, 1], boxes_lidar[:, 0]) + boxes_lidar[:, 6]

    for i, (lb, score, box3d, box2d, alpha) in enumerate(zip(labels, scores, boxes_cam, box2d_rgb, alphas)):
        if box2d[0] > image_shape[1] or box2d[1] > image_shape[0]:
            continue
        if box2d[2] < 0 or box2d[3] < 0:
            continue
        box2d[2:] = np.minimum(box2d[2:], image_shape[::-1])
        box2d[:2] = np.maximum(box2d[:2], [0, 0])

        hehe["name"].append(class_names[lb])
        hehe["truncated"].append(0.0)
        hehe["occluded"].append(0)
        hehe["alpha"].append(alpha)
        hehe["bbox"].append(box2d)
        hehe["dimensions"].append(box3d[[3, 4, 5]])
        hehe["location"].append(box3d[:3])
        hehe["rotation_y"].append(box3d[6])
        hehe["score"].append(score)
        hehe['image_idx'].append(int(sample_id))

    try:
        hehe = {n: np.stack(v) for n, v in hehe.items()}
    except:
        return kitti.empty_result_anno()

    return hehe
예제 #2
0
def kitti_bbox2results(boxes_lidar, scores, meta, labels=None):
    calib = meta['calib']
    sample_id = meta['sample_idx']

    if scores is None \
        or len(scores) == 0 \
            or boxes_lidar is None \
                or len(boxes_lidar) == 0:

        predictions_dict = kitti_empty_results(sample_id)

    else:
        if labels is None:
            labels = np.zeros(len(boxes_lidar), dtype=np.long)

        boxes_lidar[:, -1] = limit_period(
            boxes_lidar[:, -1],
            offset=0.5,
            period=np.pi * 2,
        )

        boxes_cam = np.zeros_like(boxes_lidar)
        boxes_cam[:, :3] = project_velo_to_rect(boxes_lidar[:, :3], calib)
        boxes_cam[:, 3:] = boxes_lidar[:, [4, 5, 3, 6]]

        corners_cam = center_to_corner_box3d(boxes_cam,
                                             origin=[0.5, 1.0, 0.5],
                                             axis=1)
        corners_rgb = project_rect_to_image(corners_cam, calib)

        minxy = np.min(corners_rgb, axis=1)
        maxxy = np.max(corners_rgb, axis=1)

        box2d_rgb = np.concatenate([minxy, maxxy], axis=1)

        alphas = -np.arctan2(-boxes_lidar[:, 1],
                             boxes_lidar[:, 0]) + boxes_lidar[:, 6]

        predictions_dict = {
            "bbox": box2d_rgb, "box3d_camera": boxes_cam, "box3d_lidar": boxes_lidar,\
            "alphas": alphas, "scores": scores, "label_preds": labels, "image_idx": sample_id
        }

    return predictions_dict
예제 #3
0
def show_lidar_on_image(pc_velo, img, calib, img_width, img_height):
    ''' Project LiDAR points to image '''
    imgfov_pc_velo, pts_2d, fov_inds = get_lidar_in_image_fov(
        pc_velo, calib, 0, 0, img_width, img_height, True)
    imgfov_pts_2d = pts_2d[fov_inds, :]
    # wn update
    imgfov_pc_rect = utils.project_velo_to_rect(imgfov_pc_velo, calib)

    import matplotlib.pyplot as plt
    cmap = plt.cm.get_cmap('hsv', 256)
    cmap = np.array([cmap(i) for i in range(256)])[:, :3] * 255

    for i in range(imgfov_pts_2d.shape[0]):
        depth = imgfov_pc_rect[i, 2]
        color = cmap[int(640.0 / depth), :]
        cv2.circle(img, (int(np.round(
            imgfov_pts_2d[i, 0])), int(np.round(imgfov_pts_2d[i, 1]))),
                   2,
                   color=tuple(color),
                   thickness=-1)
    Image.fromarray(img).show()
    return img
    def sample_all(self, gt_boxes, gt_types, road_planes=None, calib=None):
        avoid_coll_boxes = gt_boxes

        sampled = []
        sampled_gt_boxes = []

        for i, class_name in enumerate(self._sample_classes):
            sampled_num_per_class = int(
                self._sample_max_num[i] -
                np.sum([n == class_name for n in gt_types]))
            if sampled_num_per_class > 0:
                sampled_cls = self.sample(avoid_coll_boxes,
                                          sampled_num_per_class, i)
            else:
                sampled_cls = []

            sampled += sampled_cls

            if len(sampled_cls) > 0:
                if len(sampled_cls) == 1:
                    sampled_gt_box = sampled_cls[0]["box3d_lidar"][np.newaxis,
                                                                   ...]
                else:
                    sampled_gt_box = np.stack(
                        [s["box3d_lidar"] for s in sampled_cls], axis=0)

                sampled_gt_boxes += [sampled_gt_box]
                avoid_coll_boxes = np.concatenate(
                    [avoid_coll_boxes, sampled_gt_box], axis=0)

        if len(sampled) > 0:
            sampled_gt_boxes = np.concatenate(sampled_gt_boxes, axis=0)
            if road_planes is not None:
                center = sampled_gt_boxes[:, 0:3]
                a, b, c, d = road_planes
                center_cam = project_velo_to_rect(center, calib)
                cur_height_cam = (-d - a * center_cam[:, 0] -
                                  c * center_cam[:, 2]) / b
                center_cam[:, 1] = cur_height_cam
                lidar_tmp_point = project_rect_to_velo(center_cam, calib)
                cur_lidar_height = lidar_tmp_point[:, 2]
                mv_height = sampled_gt_boxes[:, 2] - cur_lidar_height
                sampled_gt_boxes[:, 2] -= mv_height  # lidar view
            s_points_list = []
            sampled_gt_types = []
            for i, info in enumerate(sampled):
                s_points = np.fromfile(str(
                    pathlib.Path(self.root_path) / info["path"]),
                                       dtype=np.float32)
                s_points = s_points.reshape([-1, 4])
                s_points[:, :3] += info["box3d_lidar"][:3]
                if road_planes is not None:
                    s_points[:, 2] -= mv_height[i]
                s_points_list.append(s_points)
                sampled_gt_types.append(info['name'])

            return sampled_gt_boxes.astype(
                np.float32), sampled_gt_types, np.concatenate(s_points_list,
                                                              axis=0)

        else:
            return np.empty((0, 7), dtype=np.float32), [], np.empty(
                (0, 4), dtype=np.float32)