示例#1
0
def _rect_to_bbox(rect: Rect) -> BoundingBox:
    return BoundingBox(
        x1=max(rect[0][1], 0),
        y1=max(rect[0][0], 0),
        x2=rect[0][1] + rect[1][1],
        y2=rect[0][0] + rect[1][0],
    )
示例#2
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}
示例#3
0
    def augment(self, img, masks=None, classes=None, boxes=None):
        returns = []
        aug_det = self.augmenters.to_deterministic()

        # augment image
        input_size = img.shape
        img = aug_det.augment_image(img)
        returns.append(img)

        # augment masks
        new_masks = aug_det.augment_image(masks)
        null_masks = new_masks.sum(axis=0).sum(axis=0) == 0
        new_masks = new_masks[:, :, ~null_masks]
        returns.append(new_masks)

        # if removed any mask, remove corresponding class
        if classes is not None:
            classes = classes[~null_masks]
            returns.append(classes)

        if boxes is not None:
            # augment boxes
            bboxes = BoundingBoxesOnImage([BoundingBox(*box) for box in boxes],
                                          input_size)
            new_bboxes = aug_det.augment_bounding_boxes([bboxes])[0]
            new_bboxes = new_bboxes.remove_out_of_image().cut_out_of_image()
            boxes = [[box.x1, box.y1, box.x2, box.y2]
                     for box in new_bboxes.bounding_boxes]
            boxes = np.array(boxes)
            returns.append(boxes)
        return tuple(returns)
示例#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.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
示例#6
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
示例#7
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
    def __call__(self, sample):
        img, annot = sample['img'], sample['annot']

        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.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])

        return {'image': img_aug, 'annot': annot_aug}
    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}
示例#10
0
def augment_image(image, bbs, image_size=640):
    bbs = [BoundingBox(*bb) for bb in bbs]
    bbs = BoundingBoxesOnImage(bbs, shape=image.shape)
    resize = iaa.Sequential([
        iaa.Resize({
            "longer-side": image_size,
            "shorter-side": "keep-aspect-ratio"
        }),
        iaa.PadToFixedSize(width=image_size, height=image_size)
    ])

    seq = iaa.Sequential(
        [
            iaa.Fliplr(0.5),  # horizontal flips
            # iaa.Crop(percent=(0, 0.05)), # random crops
            # Gaussian blur with random sigma between 0 and 0.2.
            iaa.GaussianBlur(sigma=(0, 0.2)),
            # Strengthen or weaken the contrast in each image.
            iaa.LinearContrast((0.8, 1.2)),
            # Add gaussian noise.
            # For 50% of all images, we sample the noise once per pixel.
            # For the other 50% of all images, we sample the noise per pixel AND
            # channel. This can change the color (not only brightness) of the
            # pixels.
            iaa.AdditiveGaussianNoise(
                loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5),
            # Make some images brighter and some darker.
            # In 30% of all cases, we sample the multiplier once per channel,
            # which can end up changing the color of the images.
            iaa.Multiply((0.8, 1.2), per_channel=0.3),
            # Apply affine transformations to each image.
            # Scale/zoom them, translate/move them, rotate them and shear them.
            iaa.Affine(
                scale={
                    "x": (1, 1.1),
                    "y": (1, 1.1)
                },
                #             translate_percent={"x": (-0.1, 0.1), "y": (-0.1, 0.1)},
                rotate=[90, -90, 180, 88, -92, 178, 182],
                shear={
                    "x": (-5, 5),
                    "y": (-5, 5)
                },
                cval=(0, 255)),
        ],
        random_order=True)  # apply augmenters in order

    image, bbs = resize(image=image, bounding_boxes=bbs)
    image, bbs = seq(image=image, bounding_boxes=bbs)
    bbs = bbs.remove_out_of_image().clip_out_of_image()
    return image, bbs
示例#11
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
示例#12
0
    def __call__(self, sample):
        img, annot = sample.image, sample.annotation
        aug = iaa.ShearY(self.v)
        img_aug = aug(image=img)
        annot_aug = []
        if annot is not None:

            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)
            bbs_aug = aug(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 != []:
            for mask in masks:
                mask = SegmentationMapsOnImage(mask, shape=img.shape).arr
                segmap = aug(image=mask)

                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
示例#13
0
    def __call__(self, sample):
        img, annot = sample.image, sample.annotation

        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.Fliplr()
        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])

        sample.image = img_aug
        sample.annotation = annot_aug
示例#14
0
    def __call__(self, sample):
        img, annot = sample['img'], sample['annot']

        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.ShearY(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])
        # 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}
示例#15
0
    def __call__(self, sample):
        img, annot = sample.image, sample.annotation
        aug = iaa.geometric.TranslateY(percent=self.v)
        img_aug = aug(image=img)
        annot_aug = []
        if annot is not None:
            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)
            bbs_aug = aug(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])
        segmap = []

        if masks != []:
            for mask in masks:
                segmap.append(SegmentationMapsOnImage(mask, shape=img.shape))
            mask_aug = aug(segmentation_maps=segmap)
            # reshape back to 2D
            for mask in mask_aug:
                mask.arr = mask.arr[:, :, 0]
            for i, index in enumerate(sample.masks_and_category):
                index[0] = mask_aug[i].arr

        # 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
示例#16
0
    def __call__(self, sample):
        img, annot = sample.image, sample.annotation

        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.pillike.Solarize(threshold=self.v)
        img_aug, bbs_aug = aug(image=(img * 2. - 1.), 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))
        img_aug = (img_aug + 1.) / 2
        sample.image = img_aug
        sample.annotation = annot_aug
示例#17
0
 def __call__(self, sample):
     img, annot = sample.image, sample.annotation
     aug = iaa.ElasticTransformation(alpha=self.alpha, sigma=self.sigma)
     img_aug = aug(image=img)
     annot_aug = []
     if annot is not None:
         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)
         bbs_aug = aug(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
示例#18
0
    def __call__(self, sample):
        img, annot = sample.image, sample.annotation

        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.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))
        sample.image = img_aug
        sample.annotation = annot_aug
def main(args):
    print("Hello world!")
    dataset_name = args.dataset
    dataset = os.path.join('/home/ubuntu/workspace_aihub/data/raw/',
                           dataset_name)

    class_wise = glob(os.path.join(dataset, '*'))
    for per_class_dir in tqdm(class_wise):
        datas = glob(os.path.join(per_class_dir, '*'))
        for file in tqdm(datas):
            if file.split('.')[-1] != 'json':
                continue

            base_name = file.strip('.json')
            image_file = None
            if os.path.isfile(base_name + '.jpg'):
                image_file = base_name + '.jpg'

            if os.path.isfile(base_name + '.JPG'):
                image_file = base_name + '.JPG'

            if image_file is None:
                print('no image')
                continue
            conf = OmegaConf.create()
            image = cv2.imread(image_file)

            if image is None:
                print('no image!')
                continue

            if len(image.shape) != 3:
                print('this image dose not have 3 chennels')

            with open(file) as json_file:
                json_data = json.load(json_file)
                if len(json_data['regions']) != 1:
                    print('this regin has more than 1 bbx')
                    print(json_data['regions'])
                conf['regions'] = json_data['regions']

                x1 = json_data['regions'][0]['boxcorners'][0]
                y1 = json_data['regions'][0]['boxcorners'][1]
                x2 = json_data['regions'][0]['boxcorners'][2]
                y2 = json_data['regions'][0]['boxcorners'][3]

                bbs = BoundingBoxesOnImage(
                    [BoundingBox(x1=x1, y1=y1, x2=x2, y2=y2)],
                    shape=image.shape)

                if image.shape[0] > image.shape[1]:
                    seq = iaa.Sequential([
                        iaa.Resize({
                            "height": 480,
                            "width": "keep-aspect-ratio"
                        }),
                        iaa.CenterPadToFixedSize(
                            height=480,
                            width=640,
                        )
                    ])
                else:
                    seq = iaa.Sequential([
                        iaa.Resize({
                            "width": 640,
                            "height": "keep-aspect-ratio"
                        }),
                        iaa.CenterPadToFixedSize(
                            height=480,
                            width=640,
                        )
                    ])

                #image = np.transpose(image, (1,0,2))
                image_aug, bbs_aug = seq(image=image, bounding_boxes=bbs)
                x1 = int(bbs_aug[0].x1)
                y1 = int(bbs_aug[0].y1)
                x2 = int(bbs_aug[0].x2)
                y2 = int(bbs_aug[0].y2)
                conf['bbox'] = {}
                conf['bbox']['x1'] = x1
                conf['bbox']['y1'] = y1
                conf['bbox']['x2'] = x2
                conf['bbox']['y2'] = y2

                rand_number = random.randint(0, 9)
                if rand_number == 0:
                    mode = 'test2'
                elif rand_number == 1:
                    mode = 'val2'
                else:
                    mode = 'train2'

                base_name = (os.path.basename(base_name)).replace('-', '_')
                label = base_name.split('_')[0]

                dst_path = os.path.join(
                    '/home/ubuntu/workspace_aihub/data/refined', mode, label)

                if not os.path.isdir(dst_path):
                    os.mkdir(dst_path)

                OmegaConf.save(
                    conf,
                    os.path.join(dst_path,
                                 f'{os.path.basename(base_name)}.yaml'))
                cv2.imwrite(
                    os.path.join(dst_path,
                                 f'{os.path.basename(base_name)}.jpg'),
                    image_aug)
示例#20
0
    def __call__(self, sample):
        # , sample['mask_category']
        img, annot = sample.image, sample.annotation

        annot_aug = []
        aug = iaa.Rotate(rotate=self.v)
        if annot is not None:
            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)
            bbs_aug = aug(bounding_boxes=bbs)
            annot_aug = np.array(
                [[bb.x1, bb.y1, bb.x2, bb.y2,
                  np.float32(bb.label)] for bb in bbs_aug])

        img_aug = aug(image=img)

        masks = []
        if sample.masks_and_category is not None:
            for index in sample.masks_and_category:
                masks.append(index[0])
        segmap = []

        if masks != []:
            for mask in masks:
                segmap.append(SegmentationMapsOnImage(mask, shape=img.shape))
            mask_aug = aug(segmentation_maps=segmap)
            # reshape back to 2D

            for mask in mask_aug:
                mask.arr = mask.arr[:, :, 0]

            new_bbox = []

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

                binary_mask = np.array(index[0], np.uint8)

                contours, hierarchy = cv2.findContours(binary_mask,
                                                       cv2.RETR_EXTERNAL,
                                                       cv2.CHAIN_APPROX_NONE)

                if contours == []:
                    continue
                areas = []
                for cnt in contours:
                    area = cv2.contourArea(cnt)
                    areas.append(area)

                idx = areas.index(np.max(areas))
                x, y, w, h = cv2.boundingRect(contours[idx])
                bounding_box = [x, y, x + w, y + h]

                temp = [x, y, x + w, y + h, np.float32(index[1])]
                new_bbox.append(temp)

    # 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
        if masks != []:
            sample.annotation = np.array(new_bbox)
 def __call__(self, input_dataframe):
     return input_dataframe.assign(
         bounding_box=lambda df: df[["x1", "y1", "x2", "y2"]].apply(lambda row: BoundingBox(**row), axis=1)
     )
示例#22
0
 def __call__(self, input_dataframe):
     return (input_dataframe.assign(
         bounding_box=lambda df: df[['x1', 'y1', 'x2', 'y2']].apply(
             lambda row: BoundingBox(**row), axis=1)))
def main(args):
    df = pd.DataFrame()
    df_count = 0
    fold = args.fold

    dataset_name = args.dataset
    dataset = os.path.join('/home/ubuntu/workspace_aihub/data/refined/',
                           dataset_name)
    class_wise = glob(os.path.join(dataset, '*'))

    classes_per_batch = 65

    st = fold * classes_per_batch
    en = fold * classes_per_batch + classes_per_batch

    if fold != 15:
        class_wise = class_wise[st:en]
    else:
        class_wise = class_wise[st:]

    print('# classes:', len(class_wise))

    dst_root = os.path.join('/home/ubuntu/workspace_aihub/data/refined/v3/',
                            dataset_name)
    if not os.path.isdir(dst_root):
        os.mkdir(dst_root)

    dst_root = os.path.join(dst_root, str(fold))
    if not os.path.isdir(dst_root):
        os.mkdir(dst_root)

    for per_class_dir in tqdm(class_wise):
        datas = glob(os.path.join(per_class_dir, '*'))
        for file in tqdm(datas):
            if file.split('.')[-1] != 'yaml':
                continue
            base_name = file.strip('.yaml')

            image_file = base_name + '.jpg'
            # print(image_file)
            image = cv2.imread(image_file)
            config = OmegaConf.load(file)
            if image is None:
                print('no image!')
                continue

            x1 = config.bbox.x1
            y1 = config.bbox.y1
            x2 = config.bbox.x2
            y2 = config.bbox.y2

            bbs = BoundingBoxesOnImage(
                [BoundingBox(x1=x1, y1=y1, x2=x2, y2=y2)], shape=image.shape)

            transform = get_transform(image)

            image_aug, bbs_aug = transform(image=image, bounding_boxes=bbs)

            x1 = int(bbs_aug[0].x1)
            y1 = int(bbs_aug[0].y1)
            x2 = int(bbs_aug[0].x2)
            y2 = int(bbs_aug[0].y2)
            config['bbox'] = {}
            config['bbox']['x1'] = x1
            config['bbox']['y1'] = y1
            config['bbox']['x2'] = x2
            config['bbox']['y2'] = y2

            label = config['regions'][0]['tags'][1].split(':')[-1]
            dst_path = os.path.join(dst_root, label)
            if not os.path.isdir(dst_path):
                os.mkdir(dst_path)

            OmegaConf.save(
                config,
                os.path.join(dst_path, f'{os.path.basename(base_name)}.yaml'))
            cv2.imwrite(
                os.path.join(dst_path, f'{os.path.basename(base_name)}.jpg'),
                image_aug)

            df.loc[df_count, 'bbox_x1'] = x1
            df.loc[df_count, 'bbox_y1'] = y1
            df.loc[df_count, 'bbox_x2'] = x2
            df.loc[df_count, 'bbox_y2'] = y2
            df.loc[df_count, 'image_width'] = image_aug.shape[1]
            df.loc[df_count, 'image_height'] = image_aug.shape[0]
            df.loc[df_count, 'class'] = label
            df.loc[df_count, 'yaml_path'] = os.path.join(
                dst_path, f'{os.path.basename(base_name)}.yaml')
            df.loc[df_count, 'image_path'] = os.path.join(
                dst_path, f'{os.path.basename(base_name)}.jpg')

            df_count += 1

    df.to_csv(f'{dataset_name}_dataframe_fold{fold}.csv')
    def _get_x_y(self, indices: List[int], autoscale: bool = True, use_masks: bool = True, do_preprocessing: bool = False, downscale: bool = False):

        batch_of_input_images = []
        batch_of_target_masks = []
        batch_of_target_labels = []

        # Iterate over num batches
        for batch_item in indices:

            r = random()

            # Load image, masks and labels
            image = self.load_image(batch_item)

            masks, labels = self.load_mask(batch_item, as_box=not use_masks)

            initial_shape = image.shape
            initial_width = image.shape[1]
            initial_height = image.shape[0]
            indicated_initial_width = self.__image_info[batch_item]['width']
            indicated_initial_height = self.__image_info[batch_item]['height']

            masks = np.multiply(masks, initial_width / indicated_initial_width)
            # print('Actual width: ', initial_width, 'indicated:', indicated_initial_width)

            has_crop = False

            # Apply augmentations if specified
            if self.augmentation:
                import imgaug

                MASK_AUGMENTERS = ["Sequential", "SomeOf", "OneOf", "Sometimes",
                                   "Fliplr", "Flipud", "CropAndPad",
                                   "Affine", "PiecewiseAffine"]

                def hook(images, augmenter, parents, default):
                    """Determines which augmenters to apply to masks."""
                    return augmenter.__class__.__name__ in MASK_AUGMENTERS

                # Store shapes before augmentation to compare
                image_shape = image.shape
                mask_shape = masks.shape

                # start_total = start = time()
                # print('======================')
                # print('Starting Augmentation at 0.0')

                # Make augmenters deterministic to apply similarly to images and masks
                for child_augmenter in self.augmentation.get_all_children():
                    if isinstance(child_augmenter, CropToFixedSize):
                        has_crop = True
                        min_side = min(*image.shape[:2])
                        child_augmenter.size = (min_side, min_side)

                det = self.augmentation.to_deterministic()
                # print('-- Applying Augmentation to image: {}'.format(det.__class__))
                image = det.augment_image(image.astype(np.uint8))
                # Change mask to np.uint8 because imgaug doesn't support np.bool
                # took = time() - start
                # print('-- Done after ', took)
                # start = time()

                # for mask in masks:
                #     print([(m[0], m[1], m[2], m[3]) for m in masks])
                if use_masks:
                    masks = det.augment_image(masks.astype(np.uint8), hooks=imgaug.HooksImages(activator=hook))
                else:
                    # print('-- Applying Augmentation to mask: {}'.format(det.__class__))


                    bbs = BoundingBoxesOnImage([
                        BoundingBox(x1=m[0], x2=m[2], y1=m[1], y2=m[3]) for m in masks
                    ], shape=initial_shape)

                    aug_boxes = []
                    w, h = image.shape[1], image.shape[0]

                    for b in det.augment_bounding_boxes(bbs).bounding_boxes:
                        x1 = max(0, min(w, b.x1))
                        y1 = max(0, min(h, b.y1))
                        x2 = max(0, min(w, b.x2))
                        y2 = max(0, min(h, b.y2))

                        # Discard boxes with either 0 widht or 0 height
                        if x2 - x1 > 0 and y2 - y1 > 0:
                            aug_boxes.append(
                                [
                                    max(0, min(w, b.x1)),
                                    max(0, min(h, b.y1)),
                                    max(0, min(w, b.x2)),
                                    max(0, min(h, b.y2)),
                                ]
                            )
                    masks = np.array(aug_boxes)
                    # took = time() - start
                    # print('-- Done after\n\n', took)

                # Verify that shapes didn't change
                # assert image.shape == image_shape, "Augmentation shouldn't change image size"
                # assert masks.shape == mask_shape, "Augmentation shouldn't change mask size"
                # Change mask back to bool
                # ret_mask = ret_mask.astype(np.bool)

            # If max image side length was specified, resize image and masks
            if (self.max_image_side_length is not None or self.min_image_side_length is not None) and autoscale:
                old_height, old_width = image.shape[0], image.shape[1]
                ratio = old_height / old_width

                if self.image_scale_mode == 'squash':
                    image = cv2.resize(image, (self.max_image_side_length, self.max_image_side_length))
                    s_w = image.shape[1] / old_width
                    s_h = image.shape[0] / old_height

                    if use_masks:
                        raise RuntimeError('Squashin masks is not implemented')
                    else:
                        masks[:, 0] = np.multiply(masks[:, 0], s_w)
                        masks[:, 2] = np.multiply(masks[:, 2], s_w)
                        masks[:, 1] = np.multiply(masks[:, 1], s_h)
                        masks[:, 3] = np.multiply(masks[:, 3], s_h)

                image, w, scale, p, c = resize_image(
                    image, max_dim=self.max_image_side_length, min_dim=self.min_image_side_length, downscale=downscale
                )

                # box_scale = scale * (initial_width / indicated_initial_width)
                if use_masks:
                    masks = resize_mask(
                        masks, scale=scale, padding=p
                    )
                else:
                    # masks2 = []
                    # print(masks)
                    masks = np.multiply(masks, scale)

                    if self.image_scale_mode == 'square':
                        if p[0][0] > 0:
                            masks[:, 1] += p[0][0]
                            masks[:, 3] += p[0][0]
                        if p[1][0] > 0:
                            masks[:, 0] += p[1][0]
                            masks[:, 2] += p[1][0]
                    # if p[0][0] > 0:
                    #     try:
                    #         masks[:, 1] += p[0][0]
                    #         masks[:, 3] += p[0][0]
                    #     except BaseException:
                    #         print('MAS FAL')
                    #         print(masks.shape)

                    if has_crop:
                        masks[:, 0] += p[1][0]
                        masks[:, 2] += p[1][0]
                        masks[:, 1] += p[0][0]
                        masks[:, 3] += p[0][0]

                #     for mask in masks:
                #         masks2.append(
                #             [mask[0] - w[1], mask[1] - w[0], mask[2] - w[1], mask[3] - w[0]]
                #         )
                #
                #     masks = np.array(masks2)
            if autoscale and self.image_scale_mode == 'just':
                new_width = int((initial_width * scale))
                new_height = int((initial_height * scale))
                remove_width = int((image.shape[1] - new_width) / 2)
                remove_height = int((image.shape[0] - new_height) / 2)

                image = image[remove_height:image.shape[0] - remove_height, remove_width:image.shape[1] - remove_width, :]
                if use_masks:
                    masks = masks[remove_height:image.shape[0] - remove_height, remove_width:masks.shape[1] - remove_width, :]

            if do_preprocessing:
                if self.center_color_to_imagenet:
                    image = image.astype(np.float64)
                    image[..., 0] -= 123.68  # R
                    image[..., 1] -= 116.779  # G
                    image[..., 2] -= 103.939  # B
                else:
                    image /= 255.0

            # Append to batch
            batch_of_input_images.append(image)
            batch_of_target_masks.append(masks)
            batch_of_target_labels.append(labels)

            self._cur_img_idx += 1

        if use_masks:
            batch_of_target_bboxes = np.zeros((1, batch_of_target_masks[0].shape[2], 4))
            for batch, mask_set in enumerate(batch_of_target_masks):

                batch_of_target_bboxes[batch, :mask_set.shape[2]] = extract_bboxes(mask_set)[:mask_set.shape[2]]

                for b, box in enumerate(batch_of_target_bboxes[batch]):
                    y1, x1, y2, x2 = box
                    batch_of_target_bboxes[batch][b] = np.array((x1, y1, x2, y2))
        else:
            batch_of_target_bboxes = batch_of_target_masks

        # batch_of_input_images = np.asarray(batch_of_input_images, dtype=np.float32)

        return batch_of_input_images, batch_of_target_masks, batch_of_target_bboxes, batch_of_target_labels, self._num_classes
示例#25
0
def to_bounding_boxes_on_image(m_image: MetaImage) -> BoundingBoxesOnImage:
    bounding_boxes: List[BoundingBox] = []
    for lbox in m_image.labeled_boxes:
        bounding_boxes.append(
            BoundingBox(lbox.x1, lbox.y1, lbox.x2, lbox.y2, lbox.label))
    return BoundingBoxesOnImage(bounding_boxes, m_image.data.shape)