def get_relative_box_position(anchor, box):
    anchor_vec = Vec2(anchor.left, anchor.top)
    return BBox(
        box.top_left - anchor_vec,
        box.top_right - anchor_vec,
        box.bottom_right - anchor_vec,
        box.bottom_left - anchor_vec,
    )
def get_bboxes(bboxes):
    if len(bboxes.shape) == 2:
        bboxes = bboxes[..., None]

    bboxes = bboxes.transpose(2, 1, 0)
    boxes = []
    aabbs = []
    for box in bboxes:
        box = BBox._make([Vec2._make(b).to_int() for b in box])
        boxes.append(box)

        aabbs.append(box.to_aabb())

    return boxes, aabbs
Example #3
0
    def crop_chars(self, image, i):
        image_size = Size(*image.shape[-2:])
        image_bbox = BBox.from_axis_aligned_bbox(
            AxisAlignedBBox(0, 0, image_size.width, image_size.height))

        transform_params = []
        crop_vector = Vec2(x=image_bbox.top_right.x - image_bbox.top_left.x,
                           y=image_bbox.top_right.y - image_bbox.top_left.y)
        start_points = [
            (crop_vector * (i / self.num_words_per_image)).to_int()
            for i in range(self.num_words_per_image)
        ]
        crop_bbox_width = crop_vector * (1 / (self.num_words_per_image / 4)
                                         )  # divide by four for 75% overlap
        box_overlap = Vec2(
            (start_points[-1].x + crop_bbox_width.x - image_size.width) // 2,
            0)

        start_points = [point - box_overlap for point in start_points]

        with cuda.get_device_from_id(self.device_id):
            for start_point in start_points:
                top_left = image_bbox.top_left + start_point
                bottom_left = image_bbox.bottom_left + start_point
                top_right = image_bbox.top_left + start_point + crop_bbox_width
                bottom_right = image_bbox.bottom_left + start_point + crop_bbox_width
                crop_bbox = BBox(top_left, top_right, bottom_right,
                                 bottom_left)

                transform_params.append(
                    crop_bbox.get_affine_transform_params(Vec2(
                        x=image_size.width, y=image_size.height),
                                                          xp=self.xp))

            transform_params = self.xp.stack(transform_params, axis=0)
            return self.apply_transform_params(image, transform_params)