def test(self):

        layer_1 = np.zeros((100, 100, 3))
        box_1 = cv2.boxPoints(((50, 50), (20, 20), 0))
        layer_1 = cv2.fillPoly(layer_1,
                               pts=[np.int0(box_1)],
                               color=(255, 255, 255))

        layer_2 = np.zeros((100, 100, 3))
        box_2 = cv2.boxPoints(((70, 30), (10, 10), 0))
        layer_2 = cv2.fillPoly(layer_2,
                               pts=[np.int0(box_2)],
                               color=(0, 0, 255))

        rasterizer = Rasterizer()
        image = rasterizer.combine(
            [layer_1.astype('uint8'),
             layer_2.astype('uint8')])

        answer = np.zeros((100, 100, 3))
        answer = cv2.fillPoly(answer,
                              pts=[np.int0(box_1)],
                              color=(255, 255, 255))
        answer = cv2.fillPoly(answer, pts=[np.int0(box_2)], color=(0, 0, 255))
        answer = answer.astype('uint8')

        np.testing.assert_allclose(answer, image)
Example #2
0
class StaticLayerRasterizer(StaticLayerRepresentation):
    """
    Creates a representation of the static map layers where
    the map layers are given a color and rasterized onto a
    three channel image.
    """

    def __init__(self, helper: PredictHelper,
                 layer_names: List[str] = None,
                 colors: List[Color] = None,
                 resolution: float = 0.1,  # meters / pixel
                 meters_ahead: float = 40, meters_behind: float = 10,
                 meters_left: float = 25, meters_right: float = 25):

        self.helper = helper
        self.maps = load_all_maps(helper)

        if not layer_names:
            layer_names = ['drivable_area', 'ped_crossing', 'walkway']
        self.layer_names = layer_names

        if not colors:
            colors = [(255, 255, 255), (119, 136, 153), (0, 0, 255)]
        self.colors = colors

        self.resolution = resolution
        self.meters_ahead = meters_ahead
        self.meters_behind = meters_behind
        self.meters_left = meters_left
        self.meters_right = meters_right
        self.combinator = Rasterizer()

    def make_representation(self, instance_token: str, sample_token: str) -> np.ndarray:
        """
        Makes rasterized representation of static map layers.
        :param instance_token: Token for instance.
        :param sample_token: Token for sample.
        :return: Three channel image.
        """

        sample_annotation = self.helper.get_sample_annotation(instance_token, sample_token)
        map_name = self.helper.get_map_name_from_sample_token(sample_token)

        x, y = sample_annotation['translation'][:2]

        yaw = quaternion_yaw(Quaternion(sample_annotation['rotation']))

        yaw_corrected = correct_yaw(yaw)

        image_side_length = 2 * max(self.meters_ahead, self.meters_behind,
                                    self.meters_left, self.meters_right)
        image_side_length_pixels = int(image_side_length / self.resolution)

        patchbox = get_patchbox(x, y, image_side_length)

        angle_in_degrees = angle_of_rotation(yaw_corrected) * 180 / np.pi

        canvas_size = (image_side_length_pixels, image_side_length_pixels)

        masks = self.maps[map_name].get_map_mask(patchbox, angle_in_degrees, self.layer_names, canvas_size=canvas_size)

        images = []
        for mask, color in zip(masks, self.colors):
            images.append(change_color_of_binary_mask(np.repeat(mask[::-1, :, np.newaxis], 3, 2), color))

        lanes = draw_lanes_in_agent_frame(image_side_length_pixels, x, y, yaw, radius=50,
                                          image_resolution=self.resolution, discretization_resolution_meters=1,
                                          map_api=self.maps[map_name])

        images.append(lanes)

        image = self.combinator.combine(images)

        row_crop, col_crop = get_crops(self.meters_ahead, self.meters_behind, self.meters_left,
                                       self.meters_right, self.resolution,
                                       int(image_side_length / self.resolution))

        return image[row_crop, col_crop, :]

    def generate_mask(self, translation, rotation, sample_token: str):

        map_name = self.helper.get_map_name_from_sample_token(sample_token)

        # translation factors (ego frame)
        x, y = translation[:2]
        yaw = quaternion_yaw(Quaternion(rotation))
        yaw_corrected = correct_yaw(yaw)

        # 1. generate map masks
        image_side_length = 2 * max(self.meters_ahead, self.meters_behind, self.meters_left, self.meters_right)
        image_side_length_pixels = int(image_side_length / self.resolution)
        patchbox = get_patchbox(x, y, image_side_length)
        angle_in_degrees = angle_of_rotation(yaw_corrected) * 180 / np.pi
        canvas_size = (image_side_length_pixels, image_side_length_pixels)

        masks = self.maps[map_name].get_map_mask(patchbox, angle_in_degrees, self.layer_names, canvas_size=canvas_size)

        # 2. generate guided lanes
        agent_pixels = int(image_side_length_pixels / 2), int(image_side_length_pixels / 2)
        base_image = np.zeros((image_side_length_pixels, image_side_length_pixels, 3))

        meter_resolution = 0.5
        radius = 50.0
        lanes = get_lanes_in_radius(x, y, radius=radius, discretization_meters=meter_resolution, map_api=self.maps[map_name])
        image_with_lanes = draw_lanes_on_image(base_image, lanes, (x, y), yaw,
                                               agent_pixels, self.resolution, color_function=color_by_yaw)
        rotation_mat = get_rotation_matrix(image_with_lanes.shape, yaw)
        rotated_image = cv2.warpAffine(image_with_lanes, rotation_mat, image_with_lanes.shape[:2])
        rotated_image_lanes = rotated_image.astype("uint8")

        # 3. combine masks
        images = []
        for mask, color in zip(masks, self.colors):
            images.append(change_color_of_binary_mask(np.repeat(mask[::-1, :, np.newaxis], 3, 2), color))
        map_img = self.combinator.combine(images)
        images.append(rotated_image_lanes)
        map_img_with_lanes = self.combinator.combine(images)

        # crop
        row_crop, col_crop = get_crops(self.meters_ahead, self.meters_behind, self.meters_left,
                                       self.meters_right, self.resolution,
                                       int(image_side_length / self.resolution))

        return np.array(images)[:, row_crop, col_crop, :], lanes, map_img, map_img_with_lanes
Example #3
0
class StaticLayerRasterizer(StaticLayerRepresentation):
    """
    Creates a representation of the static map layers where
    the map layers are given a color and rasterized onto a
    three channel image.
    """
    def __init__(
            self,
            helper: PredictHelper,
            layer_names: List[str] = None,
            colors: List[Color] = None,
            resolution: float = 0.1,  # meters / pixel
            meters_ahead: float = 40,
            meters_behind: float = 10,
            meters_left: float = 25,
            meters_right: float = 25):

        self.helper = helper
        self.maps = load_all_maps(helper)

        if not layer_names:
            layer_names = ['drivable_area', 'ped_crossing', 'walkway']
        self.layer_names = layer_names

        if not colors:
            colors = [(255, 255, 255), (119, 136, 153), (0, 0, 255)]
        self.colors = colors

        self.resolution = resolution
        self.meters_ahead = meters_ahead
        self.meters_behind = meters_behind
        self.meters_left = meters_left
        self.meters_right = meters_right
        self.combinator = Rasterizer()

    def make_representation(self,
                            instance_token: str = None,
                            sample_token: str = None,
                            ego=False,
                            ego_pose=None) -> np.ndarray:
        """
        Makes rasterized representation of static map layers.
        :param instance_token: Token for instance.
        :param sample_token: Token for sample.
        :return: Three channel image.
        """

        if not ego:
            sample_annotation = self.helper.get_sample_annotation(
                instance_token, sample_token)
        else:
            if ego_pose is None:
                sample_ = self.helper.data.get('sample', sample_token)
                sample_data = self.helper.data.get(
                    'sample_data', sample_['data']['CAM_FRONT'])
                ego_pose = self.helper.data.get('ego_pose',
                                                sample_data['ego_pose_token'])
            sample_annotation = {
                'translation': ego_pose['translation'],
                'rotation': ego_pose['rotation'],
                'instance_token': None
            }

        map_name = self.helper.get_map_name_from_sample_token(sample_token)

        x, y = sample_annotation['translation'][:2]

        yaw = quaternion_yaw(Quaternion(sample_annotation['rotation']))

        yaw_corrected = correct_yaw(yaw)

        image_side_length = 2 * max(self.meters_ahead, self.meters_behind,
                                    self.meters_left, self.meters_right)
        image_side_length_pixels = int(image_side_length / self.resolution)

        patchbox = get_patchbox(x, y, image_side_length)

        angle_in_degrees = angle_of_rotation(yaw_corrected) * 180 / np.pi

        canvas_size = (image_side_length_pixels, image_side_length_pixels)

        masks = self.maps[map_name].get_map_mask(patchbox,
                                                 angle_in_degrees,
                                                 self.layer_names,
                                                 canvas_size=canvas_size)

        images = []
        for mask, color in zip(masks, self.colors):
            images.append(
                change_color_of_binary_mask(
                    np.repeat(mask[::-1, :, np.newaxis], 3, 2), color))

        lanes = draw_lanes_in_agent_frame(image_side_length_pixels,
                                          x,
                                          y,
                                          yaw,
                                          radius=50,
                                          image_resolution=self.resolution,
                                          discretization_resolution_meters=1,
                                          map_api=self.maps[map_name])

        images.append(lanes)

        image = self.combinator.combine(images)

        row_crop, col_crop = get_crops(
            self.meters_ahead, self.meters_behind, self.meters_left,
            self.meters_right, self.resolution,
            int(image_side_length / self.resolution))

        return image[row_crop, col_crop, :]