コード例 #1
0
    def __call__(self, sample):
        img, annot = sample.image, sample.annotation
        unique_labels = np.unique(
            annot[:, 4].astype('int').astype('str')).tolist()

        rotate_degree = random.uniform(-1 * self.degree, self.degree)
        bbs = BoundingBoxesOnImage([
            BoundingBox(x1=ann[0],
                        y1=ann[1],
                        x2=ann[2],
                        y2=ann[3],
                        label=str(int(ann[4]))) for ann in annot
        ],
                                   shape=img.shape)
        aug = iaa.BlendAlphaBoundingBoxes(
            labels=unique_labels,
            foreground=iaa.geometric.TranslateY(percent=0.1))
        img_aug, bbs_aug = aug(image=img, bounding_boxes=bbs)
        # drawn_img = bbs_aug.draw_on_image(img_aug * 255, size=2, color=[0, 255., 0])
        # import skimage
        # skimage.io.imsave('draw.png', drawn_img)
        # img = img.rotate(rotate_degree, Image.BILINEAR)
        # mask = mask.rotate(rotate_degree, Image.NEAREST)

        sample.image = img_aug
        sample.annotation = bbs_aug
コード例 #2
0
    def __call__(self, sample):
        img, annot = sample.image, sample.annotation
        unique_labels = np.unique(
            annot[:, 4].astype('int').astype('str')).tolist()

        bbs = BoundingBoxesOnImage([
            BoundingBox(x1=ann[0],
                        y1=ann[1],
                        x2=ann[2],
                        y2=ann[3],
                        label=str(int(ann[4]))) for ann in annot
        ],
                                   shape=img.shape)
        aug = iaa.BlendAlphaBoundingBoxes(labels=unique_labels,
                                          foreground=iaa.Cutout(
                                              nb_iterations=int(round(self.v)),
                                              size=0.05,
                                              fill_mode="gaussian"))
        img_aug, bbs_aug = aug(image=img, bounding_boxes=bbs)
        annot_aug = np.array(
            [[bb.x1, bb.y1, bb.x2, bb.y2,
              np.float32(bb.label)] for bb in bbs_aug])
        # the shape has to be at least (0,5)
        if len(annot_aug) == 0:
            annot_aug = np.zeros((0, 5))
        sample.image = img_aug
        sample.annotation = annot_aug
コード例 #3
0
    def __call__(self, sample):
        img, annot = sample.image, sample.annotation
        unique_labels = np.unique(
            annot[:, 4].astype('int').astype('str')).tolist()

        bbs = BoundingBoxesOnImage([
            BoundingBox(x1=ann[0],
                        y1=ann[1],
                        x2=ann[2],
                        y2=ann[3],
                        label=str(int(ann[4]))) for ann in annot
        ],
                                   shape=img.shape)
        aug = iaa.BlendAlphaBoundingBoxes(labels=unique_labels,
                                          foreground=iaa.pillike.EnhanceColor(
                                              self.v))
        img_aug, bbs_aug = aug(image=(img * 255.).astype('uint8'),
                               bounding_boxes=bbs)
        img_aug = img_aug.astype('float32') / 255.
        annot_aug = np.array(
            [[bb.x1, bb.y1, bb.x2, bb.y2,
              np.float32(bb.label)] for bb in bbs_aug])
        # the shape has to be at least (0,5)
        if len(annot_aug) == 0:
            annot_aug = np.zeros((0, 5))
        sample.image = img_aug
        sample.annotation = annot_aug
コード例 #4
0
    def __call__(self, sample):
        img, annot = sample['img'], sample['annot']
        unique_labels = np.unique(
            annot[:, 4].astype('int').astype('str')).tolist()

        bbs = BoundingBoxesOnImage([
            BoundingBox(x1=ann[0],
                        y1=ann[1],
                        x2=ann[2],
                        y2=ann[3],
                        label=str(int(ann[4]))) for ann in annot
        ],
                                   shape=img.shape)
        aug = iaa.BlendAlphaBoundingBoxes(
            labels=unique_labels,
            foreground=iaa.pillike.Solarize(threshold=0.))
        img_aug, bbs_aug = aug(image=(img * 2. - 1.), bounding_boxes=bbs)
        img_aug = (img_aug + 1.) / 2
        annot_aug = np.array(
            [[bb.x1, bb.y1, bb.x2, bb.y2,
              np.float32(bb.label)] for bb in bbs_aug])
        # the shape has to be at least (0,5)
        if len(annot_aug) == 0:
            annot_aug = np.zeros((0, 5))
        return {'img': img_aug, 'annot': annot_aug}
コード例 #5
0
    def __call__(self, sample):
        img, annot = sample['img'], sample['annot']
        unique_labels = np.unique(
            annot[:, 4].astype('int').astype('str')).tolist()

        bbs = BoundingBoxesOnImage([
            BoundingBox(x1=ann[0],
                        y1=ann[1],
                        x2=ann[2],
                        y2=ann[3],
                        label=str(int(ann[4]))) for ann in annot
        ],
                                   shape=img.shape)
        aug = iaa.BlendAlphaBoundingBoxes(
            labels=unique_labels,
            foreground=iaa.AllChannelsHistogramEqualization())
        img_aug, bbs_aug = aug(image=(img * 255.).astype('uint8'),
                               bounding_boxes=bbs)
        img_aug = img_aug.astype('float32') / 255.
        annot_aug = np.array(
            [[bb.x1, bb.y1, bb.x2, bb.y2,
              np.float32(bb.label)] for bb in bbs_aug])
        # the shape has to be at least (0,5)
        if len(annot_aug) == 0:
            annot_aug = np.zeros((0, 5))
        return {'img': img_aug, 'annot': annot_aug}
コード例 #6
0
def cutout_bbox(magnitude: int, **kwargs) -> iaa.BlendAlphaBoundingBoxes:
    """
    Only apply cutout to the bounding box area. Passing the
    height and width of the image as integers and as keywords
    will scale the bounding box according to the policy. Note, the
    cutout location is chosen randomly and will only appear if it
    falls within the bounding box.

    :type magnitude: int
    :param magnitude: magnitude of cutout
    :param kwargs:
        height: height of the image as int
        width: width of the image as int
    :rtype: iaa.BlendAlphaBoundingBoxes
    :return: Method to apply cutout only to bounding boxes
    """
    level = int((magnitude / _MAX_MAGNITUDE) * CUTOUT_BBOX)
    cutout_args = {}
    if 'height' in kwargs and 'width' in kwargs:
        size = tuple(
            np.clip([level / kwargs['height'], level / kwargs['width']], 0.0,
                    1.0))
        cutout_args['size'] = size
    return iaa.BlendAlphaBoundingBoxes(None,
                                       foreground=iaa.Cutout(**cutout_args))
コード例 #7
0
def chapter_augmenters_blendalphaboundingboxes():
    fn_start = "blend/blendalphaboundingboxes"

    aug = iaa.BlendAlphaBoundingBoxes(None, background=iaa.Multiply(0.0))
    batch = ia.Batch(
        images=[ia.quokka(size=(128, 128))] * (4 * 1),
        bounding_boxes=[ia.quokka_bounding_boxes(size=(128, 128))] * (4 * 1))
    run_and_save_augseq_batch(fn_start + "_multiply_background.jpg",
                              aug,
                              batch,
                              cols=4,
                              rows=1)
コード例 #8
0
    def __call__(self, sample):
        img, annot = sample['img'], sample['annot']
        unique_labels = np.unique(annot[:, 4].astype('int').astype('str')).tolist()

        bbs = BoundingBoxesOnImage([BoundingBox(x1=ann[0], y1=ann[1], x2=ann[2], y2=ann[3], label=str(int(ann[4]))) for ann in annot], shape=img.shape)
        aug = iaa.BlendAlphaBoundingBoxes(labels=unique_labels, foreground=iaa.geometric.TranslateY(percent=0.1))
        img_aug, bbs_aug = aug(image=img, bounding_boxes=bbs)
        annot_aug = np.array([[bb.x1, bb.y1, bb.x2, bb.y2, np.float32(bb.label)] for bb in bbs_aug])
        # drawn_img = bbs_aug.draw_on_image(img_aug * 255, size=2, color=[0, 255., 0])
        # import skimage
        # skimage.io.imsave('draw.png', drawn_img)
        return {'image': img_aug, 'annot': annot_aug}
コード例 #9
0
def fliplr_boxes(_: int) -> iaa.BlendAlphaBoundingBoxes:
    """
    Flip only the bounding boxes horizontally

    Tensorflow Policy Equivalent: flip_only_bboxes

    :type _: int
    :param _: Unused, kept to fit within the ecosystem
    :rtype: iaa.AllChannelsHistogramEqualization
    :return: Method to flip bounding boxes horizontally
    """
    return iaa.BlendAlphaBoundingBoxes(None, foreground=iaa.Fliplr(1.0))
コード例 #10
0
def translate_y_bbox(magnitude: int) -> iaa.BlendAlphaBoundingBoxes:
    """
    Translate bounding boxes only on the y-axis

    Tensorflow Policy Equivalent: translate_y

    :type magnitude: int
    :param magnitude: magnitude of translation
    :rtype: iaa.BlendAlphaBoundingBoxes
    :return: Method to apply y translation to bounding boxes
    """
    level = _translate_mag_to_arg(magnitude, bbox=True)
    return iaa.BlendAlphaBoundingBoxes(
        None, foreground=iaa.geometric.TranslateY(px=level))
コード例 #11
0
def shear_y_bbox(magnitude: int) -> iaa.BlendAlphaBoundingBoxes:
    """
    Apply y shear only to bboxes

    Tensorflow Policy Equivalent: shear_y_only_bboxes

    :type magnitude: int
    :param magnitude: magnitude of y shear
    :rtype: iaa.BlendAlphaBoundingBoxes
    :return: Method to y shear bounding boxes
    """
    level = _shear_mag_to_arg(magnitude)
    return iaa.BlendAlphaBoundingBoxes(
        None,
        foreground=iaa.ShearY(level),
    )
コード例 #12
0
    def __call__(self, sample):
        img, annot = sample.image, sample.annotation
        unique_labels = np.unique(
            annot[:, 4].astype('int').astype('str')).tolist()

        bbs = BoundingBoxesOnImage([
            BoundingBox(x1=ann[0],
                        y1=ann[1],
                        x2=ann[2],
                        y2=ann[3],
                        label=str(int(ann[4]))) for ann in annot
        ],
                                   shape=img.shape)
        aug = iaa.BlendAlphaBoundingBoxes(
            labels=unique_labels,
            foreground=iaa.geometric.TranslateX(percent=self.v))
        img_aug, bbs_aug = aug(image=img, bounding_boxes=bbs)
        annot_aug = np.array(
            [[bb.x1, bb.y1, bb.x2, bb.y2,
              np.float32(bb.label)] for bb in bbs_aug])
        masks = []
        if sample.masks_and_category is not None:
            for index in sample.masks_and_category:
                masks.append(index[0])

        mask_aug = []
        if masks is not None:
            for mask in masks:
                mask = SegmentationMapsOnImage(mask, shape=img.shape).arr
                segmap, _ = aug(image=mask, bounding_boxes=bbs)

                mask_aug.append(segmap)
            # back to 2D array
            mask_result = []
            for mask in mask_aug:
                mask_result.append(mask[:, :, 0])

            for i, index in enumerate(sample.masks_and_category):
                index[0] = mask_result[i]

        # the shape has to be at least (0,5)
        if len(annot_aug) == 0:
            annot_aug = np.zeros((0, 5))
        sample.image = img_aug
        sample.annotation = annot_aug