Exemple #1
0
 def test_augment_polygons__kernel_size_is_two__no_keep_size(self):
     from imgaug.augmentables.polys import Polygon, PolygonsOnImage
     ps = [Polygon([(1.5, 1.5), (5.5, 1.5), (5.5, 5.5)])]
     psoi = PolygonsOnImage(ps, shape=(6, 6, 3))
     expected = PolygonsOnImage([
         Polygon([(1.5/2, 1.5/2), (5.5/2, 1.5/2), (5.5/2, 5.5/2)])
     ], shape=(3, 3, 3))
     self._test_augment_cbaoi__kernel_size_is_two__no_keep_size(
         psoi, expected, "augment_polygons")
    def test_empty_polygons(self):
        from imgaug.augmentables.polys import PolygonsOnImage
        psoi = PolygonsOnImage([], shape=(5, 6, 3))
        aug = self.augmenter(3, keep_size=False)

        psoi_aug = aug.augment_polygons(psoi)

        expected = psoi.deepcopy()
        expected.shape = (2, 2, 3)
        assert_cbaois_equal(psoi_aug, expected)
Exemple #3
0
def aug_img_anns(img, anns):
    augments = AUGS.to_deterministic()
    polygons = []
    kps = []
    for ann in anns:
        polygons.append(
            Polygon(
                np.float32(ann['segmentation'][0]).reshape(-1, 2),
                ann['category_id']))
        if ann.get('keypoints'):
            for ki in range(ann['num_keypoints']):
                kps.append(
                    Polygon([[
                        ann['keypoints'][ki * 3], ann['keypoints'][ki * 3 + 1]
                    ]],
                            label=(ki, len(polygons) - 1,
                                   ann['keypoints'][ki * 3 + 2])))
    polygons = PolygonsOnImage(polygons, img.shape)
    kps = PolygonsOnImage(kps, img.shape)
    img = augments.augment_image(img)
    polygons = augments.augment_polygons(polygons).polygons
    kps = augments.augment_polygons(kps).polygons
    kps_on_instance = [[] for i in range(len(polygons))]
    for kp in kps:
        kps_on_instance[kp.label[1]].append(kp)
    map(lambda x: x.sort(key=lambda kp: kp.label[0]), kps_on_instance)
    anns = []
    for p, kps in zip(polygons, kps_on_instance):
        seg = p.exterior.reshape(-1).tolist()
        xs = seg[::2]
        ys = seg[1::2]
        if len([x for x in xs if x >= img.shape[1] - 1 or x <= 0]) > 0:
            continue
        if len([y for y in ys if y >= img.shape[0] - 1 or y <= 0]) > 0:
            continue
        x1 = min(xs)
        y1 = min(ys)
        w = max(xs) - x1
        h = max(ys) - y1
        anns.append({
            'area': w * h,
            'bbox': [x1, y1, w, h],
            'category_id': p.label,
            'iscrowd': 0,
            'segmentation': [seg]
        })
        if len(kps):
            anns[-1]['keypoints'] = []
            anns[-1]['num_keypoints'] = len(kps)
            for kp in kps:
                anns[-1]['keypoints'].append(float(kp.exterior[0][0]))
                anns[-1]['keypoints'].append(float(kp.exterior[0][1]))
                anns[-1]['keypoints'].append(kp.label[2])

    return img, anns
Exemple #4
0
    def test_polygon_alignment(self):
        from imgaug.augmentables.polys import Polygon, PolygonsOnImage
        polys = [Polygon([(10, 10), (30, 10), (30, 30)])]
        psoi = PolygonsOnImage(polys, shape=(40, 40, 1))
        psoi_empty = PolygonsOnImage([], shape=(40, 40, 1))

        self._test_cbaoi_alignment(psoi, psoi_empty, [[(10 / 2, 10 / 2),
                                                       (30 / 2, 10 / 2),
                                                       (30 / 2, 30 / 2)]],
                                   [[(10, 10), (30, 10),
                                     (30, 30)]], "augment_polygons")
Exemple #5
0
def quokka_polygons(size=None, extract=None):
    """
    Returns example polygons on the standard example quokke image.

    The result contains one polygon, covering the quokka's outline.

    Added in 0.5.0. (Moved from ``imgaug.imgaug``.)

    Parameters
    ----------
    size : None or float or tuple of int or tuple of float, optional
        Size of the output image on which the polygons are placed. If ``None``,
        then the polygons are not projected to any new size (positions on the
        original image are used). ``float`` s lead to relative size changes,
        ``int`` s to absolute sizes in pixels.

    extract : None or 'square' or tuple of number or imgaug.augmentables.bbs.BoundingBox or imgaug.augmentables.bbs.BoundingBoxesOnImage
        Subarea to extract from the image. See :func:`~imgaug.imgaug.quokka`.

    Returns
    -------
    imgaug.augmentables.polys.PolygonsOnImage
        Example polygons on the quokka image.

    """
    # TODO get rid of this deferred import
    from imgaug.augmentables.polys import Polygon, PolygonsOnImage

    left, top = 0, 0
    if extract is not None:
        bb_extract = _quokka_normalize_extract(extract)
        left = bb_extract.x1
        top = bb_extract.y1
    with open(_QUOKKA_ANNOTATIONS_FP, "r") as f:
        json_dict = json.load(f)
    polygons = []
    for poly_json in json_dict["polygons"]:
        polygons.append(
            Polygon([(point["x"] - left, point["y"] - top)
                     for point in poly_json["keypoints"]])
        )
    if extract is not None:
        shape = (bb_extract.height, bb_extract.width, 3)
    else:
        shape = (643, 960, 3)
    psoi = PolygonsOnImage(polygons, shape=shape)
    if size is not None:
        shape_resized = _compute_resized_shape(shape, size)
        psoi = psoi.on(shape_resized)
    return psoi
    def test_augment_polygons__kernel_size_is_two__no_keep_size(self):
        from imgaug.augmentables.polys import Polygon, PolygonsOnImage
        ps = [Polygon([(1.5, 1.5), (5.5, 1.5), (5.5, 5.5)])]
        psoi = PolygonsOnImage(ps, shape=(6, 6, 3))
        aug = self.augmenter(2, keep_size=False)

        psoi_aug = aug.augment_polygons(psoi)

        expected = PolygonsOnImage([
            Polygon([(1.5 / 2, 1.5 / 2), (5.5 / 2, 1.5 / 2),
                     (5.5 / 2, 5.5 / 2)])
        ],
                                   shape=(3, 3, 3))
        assert_cbaois_equal(psoi_aug, expected)
Exemple #7
0
 def get_data(self, idx):
     img = cv2.imread(self.data[idx][0])
     anns = self.data[idx][1]
     polygons = []
     for ann_id, ann in enumerate(anns):
         for seg in ann['segmentation']:
             if len(seg) > 4:
                 polygons.append(
                     Polygon(
                         np.float32(seg).reshape(-1, 2), {
                             'category_id':
                             self.classes_hash[ann['category_id']],
                             'instance_id':
                             ann_id
                         }))
             elif len(seg) == 2:
                 # point
                 polygons.append(
                     Polygon(
                         np.float32([
                             seg[0] - POINTS_WH / 2, seg[1] - POINTS_WH / 2,
                             seg[0] - POINTS_WH / 2, seg[1] + POINTS_WH / 2,
                             seg[0] + POINTS_WH / 2, seg[1] + POINTS_WH / 2,
                             seg[0] + POINTS_WH / 2, seg[1] - POINTS_WH / 2
                         ]).reshape(-1, 2), {
                             'category_id':
                             self.classes_hash[ann['category_id']],
                             'instance_id':
                             ann_id
                         }))
             else:
                 print('segmentation with 2 points is not supported: {}'.
                       format(seg))
     polygons = PolygonsOnImage(polygons, img.shape)
     return img, polygons
Exemple #8
0
    def augmentation(self):

        polygons = []
        for polygon in self.y:
            polygons.append(Polygon(polygon['points']))
            polygon['points'] = []

        pps = PolygonsOnImage(polygons, shape=self.x.shape)

        # Augment.
        self.x, pps = self.augmentationseq(image=self.x, polygons=pps)
        # self.x = cv2.cvtColor(self.x,cv2.COLOR_RGB2GRAY)

        # print("{}".format(self.x.shape))

        for i, polygon in enumerate(self.y):

            for (x, y) in pps[i]:

                if x <= 0:
                    x = 0
                if y <= 0:
                    y = 0
                if x >= WIDTH:
                    x = WIDTH - 1
                if y >= HEIGHT:
                    y = HEIGHT - 1

                polygon['points'].append([x, y])
def test_aug():
    im=cv2.imread('/media/wsl/SB@data/dataset/瓶盖分类/dataset/单字检测/聪明盖/0_0.jpg')
    bboxes=read_labelme('/media/wsl/SB@data/dataset/瓶盖分类/dataset/单字检测/聪明盖/0_0.json')
    poly_on_img=PolygonsOnImage([Polygon(bbox) for bbox in bboxes],shape=im.shape)
    
    st = lambda aug: iaa.Sometimes(1, aug)
    seq = iaa.Sequential([
        #st(iaa.Pad(percent=((0, 0.2), (0, 0.2), (0, 0.2), (0, 0.2)), keep_size=False)),
        #
        # # st(iaa.Crop(percent=([0.0, 0.3], [0.00, 0.1], [0.0, 0.3], [0.0, 0.1]), keep_size=False)),
        st(iaa.Affine(scale=(0.9, 1.0), rotate=(-45, 45), shear=(-5, 5), translate_px={"x": (-16, 16), "y": (-10, 10)},
                      fit_output=True)),
        st(iaa.Add(value=(-10, 10), per_channel=True)),
        # st(iaa.PerspectiveTransform((0,0.1),fit_output=True)),
        # st(iaa.MultiplyAndAddToBrightness(mul=(0.6, 1.5), add=(0, 30))),
        st(iaa.ChangeColorTemperature(kelvin=(3000, 9100))),
        st(iaa.Sharpen(0, 0.1)),
        st(iaa.GaussianBlur((0, 1))),
        st(iaa.AddToHueAndSaturation((-2, 2))),
        st(iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.2), per_channel=True)),  # add gaussian noise to images
    ])
    for i in range(10):
        imgs_aug,poly_on_img_aug=seq(image=im,polygons=poly_on_img)
        
        res=poly_on_img_aug.draw_on_image(imgs_aug)
        a=poly_on_img_aug.to_xy_array()
        print(a.shape)
        cv2.imshow('a',res)
        cv2.waitKey(0)
Exemple #10
0
    def augment_image(self, img, gt, completed_groups):
        # Note: Running as groups directly is cheaper than running individually using run_augment()

        if self.shuffle:  # TODO: Move to top-level augmentor?
            random.shuffle(self.augmentors)

        polygons = [
            Polygon(element['points'], element['label'])
            for element in gt["data"]
        ]
        polygons = PolygonsOnImage(polygons, shape=img.shape)

        for aug in self.augmentors:
            if random.random() < aug.p and len(
                    gt["augs_done"]) < self.max_augmentations_per_image:
                if aug.name in self.augname2groups:
                    if self.augname2groups[aug.name].intersection(
                            completed_groups):
                        continue
                    else:
                        completed_groups.update(self.augname2groups[aug.name])

                img, polygons = aug(image=img, polygons=polygons)
                gt["augs_done"].append(aug.name)

        # Put back polygons into GT
        for element, pg in zip(gt["data"], polygons):
            element['points'] = [pt.tolist() for pt in pg.exterior]

        return img, gt
Exemple #11
0
    def __remove_out_of_image(cls,
                              img_aug_polys: ImgAugPolygons,
                              fully: bool = True,
                              partly: bool = False) -> ImgAugPolygons:
        result = img_aug_polys.copy()
        result.remove_out_of_image(fully=fully, partly=partly)

        return result
Exemple #12
0
    def _test_augment_polygons__kernel_size_differs(self, shape, shape_exp):
        from imgaug.augmentables.polys import Polygon, PolygonsOnImage
        polys = [Polygon([(1.5, 5.5), (5.5, 1.5), (5.5, 5.5)])]
        psoi = PolygonsOnImage(polys, shape=shape)
        aug = self.augmenter(
            (iap.Deterministic(3), iap.Deterministic(2)),
            keep_size=False)

        psoi_aug = aug.augment_polygons(psoi)

        expected = PolygonsOnImage(
            [Polygon([
                ((1.5/shape[1])*shape_exp[1], (5.5/shape[0])*shape_exp[0]),
                ((5.5/shape[1])*shape_exp[1], (1.5/shape[0])*shape_exp[0]),
                ((5.5/shape[1])*shape_exp[1], (5.5/shape[0])*shape_exp[0])
            ])],
            shape=shape_exp)
        assert_cbaois_equal(psoi_aug, expected)
Exemple #13
0
    def test_augment_polygons__kernel_size_is_two__keep_size(self):
        from imgaug.augmentables.polys import Polygon, PolygonsOnImage
        polys = [Polygon([(0, 0), (2, 0), (2, 2)])]
        psoi = PolygonsOnImage(polys, shape=(6, 6, 3))
        aug = self.augmenter(2, keep_size=True)

        psoi_aug = aug.augment_polygons(psoi)

        assert_cbaois_equal(psoi_aug, psoi)
    def test_empty_polygons(self):
        from imgaug.augmentables.polys import PolygonsOnImage
        psoi = PolygonsOnImage([], shape=(5, 6, 3))
        aug = self.augmenter(3, keep_size=False)

        psoi_aug = aug.augment_polygons(psoi)

        assert psoi_aug.shape == (2, 2, 3)
        assert len(psoi_aug.polygons) == 0
Exemple #15
0
 def get_data(self, idx):
     img = cv2.imread(self.data[idx][0])
     polygons = []
     for c, xmin, ymin, xmax, ymax in self.data[idx][1]:
         polygons.append(
             Polygon(
                 np.float32(
                     [xmin, ymin, xmin, ymax, xmax, ymax, xmax,
                      ymin]).reshape(-1, 2), c))
     polygons = PolygonsOnImage(polygons, img.shape)
Exemple #16
0
 def get_data(self, idx):
     img = cv2.imread(self.data[idx][0])
     anns = self.data[idx][1]
     polygons = []
     for ann in anns:
         polygons.append(
             Polygon(
                 np.float32(ann['segmentation']).reshape(-1, 2),
                 ann['category_id']))
     polygons = PolygonsOnImage(polygons, img.shape)
     return img, polygons
    def __call__(self, *args, **kwargs) -> typing.Tuple[np.ndarray, typing.List[Polygon]]:

        if self.is_training:
            resize = iaa.Resize(size=dict(longer_side=self.long_sizes,
                                          width='keep-aspect-ratio'))
            rotate = iaa.Rotate(rotate=self.angles, fit_output=True)
            resize_height = iaa.Resize(size=dict(height=self.height_ratios,
                                                 width='keep'))
            crop = iaa.CropToFixedSize(width=self.cropped_size[0], height=self.cropped_size[1])
            fix_resize = iaa.Resize(size=self.output_size)


            # blur = iaa.GaussianBlur()
            # blur = iaa.Sometimes(p=self.blur_prob,
            #                      then_list=blur)

            brightness = iaa.MultiplyBrightness((0.5, 1.5))
            brightness = iaa.Sometimes(self.color_jitter_prob, then_list=brightness)

            saturation = iaa.MultiplySaturation((0.5, 1.5))
            saturation = iaa.Sometimes(self.color_jitter_prob, then_list=saturation)

            contrast = iaa.LinearContrast(0.5)
            contrast = iaa.Sometimes(self.color_jitter_prob, then_list=contrast)

            hue = iaa.MultiplyHue()
            hue = iaa.Sometimes(self.color_jitter_prob, then_list=hue)

            augs = [resize,
                    rotate,
                    resize_height,
                    crop,
                    fix_resize,
                    brightness,
                    saturation,
                    contrast,
                    hue]
            ia = iaa.Sequential(augs)
        else:
            fix_resize = iaa.Resize(size=self.output_size)
            ia = iaa.Sequential([fix_resize])

        image = args[0]
        polygons = args[1]

        polygon_list = []
        for i in range(polygons.shape[0]):
            polygon_list.append(Polygon(polygons[i].tolist()))

        polygons_on_image = PolygonsOnImage(polygon_list, shape=image.shape)

        image_aug, polygons_aug = ia(image=image, polygons=polygons_on_image)

        return image_aug, polygons_aug.polygons
    def aug(self,im,bboxes,viz=True):
        assert len(bboxes.shape)==3
        assert bboxes.shape[1]==4

        poly_on_img = PolygonsOnImage([Polygon(bbox) for bbox in bboxes], shape=im.shape)
        if viz:
            res = poly_on_img.draw_on_image(im)
            cv2.imshow('ori', res)
            cv2.waitKey(0)
        imgs_aug, poly_on_img_aug = self.seq(image=im, polygons=poly_on_img,)
        res_bboxes=poly_on_img_aug.to_xy_array()
        if bboxes.shape[0]*bboxes.shape[1]!=res_bboxes.shape[0]:
            print('aug error','before:',bboxes.shape,'  after:',res_bboxes.shape)
            return im,bboxes
        #print('before:',bboxes.shape,'  after:',res_bboxes.shape)
        res_bboxes=np.reshape(res_bboxes,bboxes.shape)
        if viz:
            res = poly_on_img_aug.draw_on_image(imgs_aug)
            cv2.imshow('a', res)
            cv2.waitKey(0)
        return imgs_aug,res_bboxes
Exemple #19
0
    def run_augment(aug, img, gt):
        polygons = [
            Polygon(element['points'], element['label']) for element in gt
        ]
        polygons = PolygonsOnImage(polygons, shape=img.shape)

        img, polygons = aug(image=img, polygons=polygons)

        # Put back polygons into GT
        for element, pg in zip(gt, polygons):
            element['points'] = [pt.tolist() for pt in pg.exterior]

        return img, gt
Exemple #20
0
    def test_polygon_alignment(self):
        from imgaug.augmentables.polys import Polygon, PolygonsOnImage
        aug = self.augmenter((1, 2), keep_size=False)
        image = np.zeros((40, 40, 1), dtype=np.uint8)

        polys = [Polygon([(10, 10), (30, 10), (30, 30)])]
        psoi = PolygonsOnImage(polys, shape=image.shape)
        psoi_empty = PolygonsOnImage([], shape=image.shape)

        images_batch = [image, image, image, image]
        psoi_batch = [psoi, psoi, psoi_empty, psoi]

        nb_iterations = 10
        for _ in sm.xrange(nb_iterations):
            images_aug, psois_aug = aug(images=images_batch,
                                        polygons=psoi_batch)

            for index in [0, 1, 3]:
                image_aug = images_aug[index]
                psoi_aug = psois_aug[index]

                assert image_aug.shape == psoi_aug.shape

                if image_aug.shape == (20, 20, 1):
                    assert np.allclose(psoi_aug.items[0].coords,
                                       [(10 / 2, 10 / 2), (30 / 2, 10 / 2),
                                        (30 / 2, 30 / 2)])
                else:
                    assert np.allclose(psoi_aug.items[0].coords, [(10, 10),
                                                                  (30, 10),
                                                                  (30, 30)])

            for index in [2]:
                image_aug = images_aug[index]
                psoi_aug = psois_aug[index]

                assert psoi_aug.shape == image_aug.shape
                assert len(psoi_aug.polygons) == 0
    def test_augment_polygons__kernel_size_differs(self):
        from imgaug.augmentables.polys import Polygon, PolygonsOnImage
        polys = [Polygon([(1.5, 5.5), (5.5, 1.5), (5.5, 5.5)])]
        psoi = PolygonsOnImage(polys, shape=(6, 6, 3))
        aug = self.augmenter(
            (iap.Deterministic(3), iap.Deterministic(2)),
            keep_size=False)

        psoi_aug = aug.augment_polygons(psoi)

        assert psoi_aug.shape == (2, 3, 3)
        assert np.allclose(psoi_aug.polygons[0].exterior,
                           [[(1.5/6)*3, (5.5/6)*2],
                            [(5.5/6)*3, (1.5/6)*2],
                            [(5.5/6)*3, (5.5/6)*2]])
Exemple #22
0
    def get_data(self, idx):
        img = cv2.imread(self.data[idx][0])
        anns = self.data[idx][1]
        polygons = []
        for ann in anns:
            polygons.append(
                Polygon(
                    np.float32(ann['segmentation']).reshape(-1, 2),
                    ann['category_id']))
        polygons = PolygonsOnImage(polygons, img.shape)

        h, w, c = img.shape

        # augment
        if self.det_augments is not None:
            augments = self.det_augments.to_deterministic()
            img = augments.augment_image(img)
            polygons = augments.augment_polygons(polygons)
        
        for i in range(len(polygons.polygons)):
            polygon = random.choice(polygons.polygons)
            p = polygon.exterior.reshape(-1, 2).astype(np.int32)
            # p = p.clip(0, 1)
            if p[:, 0].min() < 0 or p[:, 1].min() < 0 or p[:, 0].max(
            ) >= img.shape[1] or p[:, 1].max() >= img.shape[0] or p[:, 0].max(
            ) - p[:, 0].min() < 50 or p[:, 1].max() - p[:, 1].min() < 50:
                continue
            break
        x1 = random.randint(p[:, 0].min() - 100, p[:, 0].min())
        x1 = max(0, x1)
        x2 = random.randint(p[:, 0].max(), p[:, 0].max() + 100)
        x2 = min(img.shape[1], x2)
        y1 = random.randint(p[:, 1].min() - 100, p[:, 1].min())
        y1 = max(0, y1)
        y2 = random.randint(p[:, 1].max(), p[:, 1].max() + 100)
        y2 = min(img.shape[0], y2)

        cimg = img[y1:y2, x1:x2]
        if cimg.size > 0:
            img = cimg
        p[:, 0] -= x1
        p[:, 1] -= y1
        seg = np.zeros([img.shape[0], img.shape[1]], dtype=np.uint8)
        seg = cv2.fillPoly(seg, [p], polygon.label + 1, 0)
        seg = SegmentationMapsOnImage(seg, shape=img.shape)
        return img, seg
def test_transform():
    data_root = '/home/luning/dev/data/SynthText800k/detection/'
    img_fn = 'desert_78_83'
    gt_fn = data_root + 'gt.mat'
    targets = {}
    targets = sio.loadmat(gt_fn,
                          targets,
                          squeeze_me=True,
                          struct_as_record=False,
                          variable_names=['imnames', 'wordBB', 'txt'])

    imageNames = targets['imnames']
    wordBBoxes = targets['wordBB']
    transcripts = targets['txt']

    mask = [True if img_fn in i else False for i in imageNames]
    index = np.where(mask)[0][0]
    print(index)
    img_fn = imageNames[index]

    img_fn = data_root + '/imgs/' + img_fn

    img = cv2.imread(img_fn)
    boxes = wordBBoxes[index]
    transcripts = transcripts[index]
    transcripts = [word for line in transcripts for word in line.split()]

    boxes = np.expand_dims(boxes, axis=2) if (boxes.ndim == 2) else boxes
    _, _, numOfWords = boxes.shape
    # boxes = boxes.reshape([8, numOfWords]).T  # num_words * 8
    # boxes = boxes.reshape([numOfWords, 4, 2])
    boxes = boxes.transpose((2, 1, 0))

    polys = []
    for i in range(numOfWords):
        # box = boxes[:, :, i].T
        box = boxes[i]
        polys.append(Polygon(box.tolist()))
    polys_on_image = PolygonsOnImage(polygons=polys, shape=img.shape)

    image_before_aug = polys_on_image.draw_on_image(img)
    cv2.imwrite('before_aug.jpg', image_before_aug)

    transform = Transform()

    image_aug, polygons_aug = transform(img, boxes)

    polygons_on_image = PolygonsOnImage(polygons=polygons_aug,
                                        shape=image_aug.shape)
    image_aug = polygons_on_image.draw_on_image(image_aug)

    cv2.imwrite('aug.jpg', image_aug)
def main():
    nb_checked = 0

    augs = iaa.SomeOf((1, None), [
        iaa.Resize({
            "height": (1, 100),
            "width": (1, 100)
        }),
        iaa.Affine(scale=(0.01, 2.0),
                   rotate=(-360, 360),
                   shear=(-360, 360),
                   translate_px={
                       "x": (-50, 50),
                       "y": (-50, 50)
                   }),
        iaa.PerspectiveTransform((0.01, 0.2))
    ])

    height, width = 100, 200

    while True:
        poly = create_random_polygon(height, width, nb_checked)
        psoi = PolygonsOnImage([poly], shape=(height, width, 3))
        psoi_aug = augs.augment_polygons(psoi)

        if not poly.is_valid or not psoi_aug.polygons[0].is_valid:
            print("poly:     ", poly, poly.is_valid)
            print("poly_aug: ", psoi_aug.polygons[0],
                  psoi_aug.polygons[0].is_valid)

        assert poly.is_valid
        assert psoi_aug.polygons[0].is_valid

        nb_checked += 1
        if nb_checked % 100 == 0:
            print("Checked %d..." % (nb_checked, ))
        if nb_checked > 100000:
            break
Exemple #25
0
def AugmentData(d, input_dir, augmentations_dict, new_image_id, new_ann_id):
	TOTAL_IMAGES = len(d['images'])
	TOTAL_AUG = len(augmentations_dict.items())
	CURRENT_AUG_ITER = 1
	print('Applying a total of {} augmentations to a total of {} images'.format(TOTAL_AUG, TOTAL_IMAGES))
	for aug in augmentations_dict:
		prefix = augmentations_dict[aug]
		print('APPLYING AUGMENTATION {}. PREFIX = {}'.format(CURRENT_AUG_ITER, prefix))
		CURRENT_AUG_ITER += 1
		for CURRENT_IMAGE_ITER in range(TOTAL_IMAGES):
			print('Working on Image {}'.format(CURRENT_IMAGE_ITER+1))
			image_name = (d['images'][CURRENT_IMAGE_ITER]['file_name'])
			image_id = d['images'][CURRENT_IMAGE_ITER]['id']
			width = d['images'][CURRENT_IMAGE_ITER]['width']
			height = d['images'][CURRENT_IMAGE_ITER]['height']
			image = imageio.imread(os.path.join(input_dir, image_name))
			annotations = []
			for c in d['annotations']:
				if c['image_id']==image_id:
					annotations.append(c)
			segments = {i:[] for i in range(len(annotations))}
			categories = {}
			ids = {}
			bboxes = {}
			areas = {}
			for i in range(0, len(annotations)):
				for j in range(0, len(annotations[i]['segmentation'])):
					current_seg = []
					for k in range(0, len(annotations[i]['segmentation'][j]), 2):
						[x, y] = [annotations[i]['segmentation'][j][k], annotations[i]['segmentation'][j][k+1]]
						current_seg = current_seg + [[x, y]]
					segments[i] = segments[i] + [current_seg]
				categories[i] = annotations[i]['category_id']
				ids[i] = annotations[i]['id']
				bboxes[i] = convertBboxStyle(annotations[i]['bbox'])
				areas[i] = annotations[i]['area']
			num_annotations = len(annotations)
			POLYGONS = []

			for i in segments:
				for L in segments[i]:
					POLYGONS.append(Polygon(L))

			psoi = PolygonsOnImage(POLYGONS, shape=image.shape)
			bbsoi = BoundingBoxesOnImage(
				[BoundingBox(x1 = bboxes[i][0], y1= bboxes[i][1], x2 = bboxes[i][2], y2=bboxes[i][3]) for i in bboxes],
				shape=image.shape
			)

			image_aug, psoi_aug = aug(image=image, polygons=psoi)
			_, bbsoi_aug = aug(image=image, bounding_boxes = bbsoi)
			bboxes_converted = bbsoi_aug.items #List of bounding boxes

			#is there a need to do this?
			#not really sure. May remove this later.
			polygons_converted = [psoi_aug.polygons[i].exterior for i in range(len(psoi_aug.polygons))]
			
			#Generating coco_entry for the same.
			image_data = {
				"id":new_image_id,
				"width": width,
				"height": height,
				"file_name": '{}{}'.format(prefix,image_name),
				"license": 0
			}

			annotations_data = []
			polygon_index = 0
			for i in range(num_annotations):
				#to retreive: segments, bboxes
				num_segments = len(segments[i])
				SEGMENTATION = []
				AREA = 0
				for j in range(num_segments):
					#p = polygons_converted[polygon_index + j]
					current_polygon = psoi_aug.polygons[polygon_index +j]
					if(current_polygon.is_valid):
						if(current_polygon.is_fully_within_image(image_aug)):
							polygons_list = [current_polygon]
							print("WITHIN")
						else:
							try:
								print("NOTWITHIN")
								polygons_list = current_polygon.clip_out_of_image(image_aug) #now this is a list of polyogns that the current polygon may have split into.
							except AssertionError:
								print("Assertion Error occured. Ignoring. Can't guarantee that final dataset will be accurate.")
								polygons_list = [current_polygon]
						#add this to the SEGMENTATION
						s = []
						for P in polygons_list:
							AREA += P.area
							p = P.exterior
							for k in range(len(p)):
								[x,y] = p[k]
								s.append(int(x))
								s.append(int(y))

							s.append(s[0])
							s.append(s[1])
							SEGMENTATION = SEGMENTATION + [s]
					else:
						print("Polygon Invalid, skiiping: ", current_polygon)
				polygon_index = polygon_index + j + 1
				bboxes_converted[i].clip_out_of_image_(image_aug)
				#bbox = [bboxes_converted[i].x1_int, bboxes_converted[i].y1_int, int(bboxes_converted[i].width), int(bboxes_converted[i].height)]
				#bbox = convertBboxStyle(bbox, "s")
				bbox = getBoundingBox(SEGMENTATION)
				if(len(SEGMENTATION) > 0):
					A = {
						"id":new_ann_id,
						"image_id": new_image_id,
						"category_id": categories[i],
						"segmentation": (SEGMENTATION),
						"area": int(AREA),
						"bbox": (bbox),
						"iscrowd": 0
					}
					annotations_data.append(A)
					new_ann_id += 1
			new_image_id += 1
			d['images'].append(image_data)
			d['annotations'] += annotations_data
			imageio.imwrite(os.path.join(input_dir, '{}{}'.format(prefix, image_name)), image_aug)
	return d
Exemple #26
0
 def test_empty_polygons(self):
     from imgaug.augmentables.polys import PolygonsOnImage
     cbaoi = PolygonsOnImage([], shape=(5, 6, 3))
     self._test_empty_cbaoi(cbaoi, "augment_polygons")
Exemple #27
0
 def test_augment_polygons__kernel_size_is_two__keep_size(self):
     from imgaug.augmentables.polys import Polygon, PolygonsOnImage
     polys = [Polygon([(0, 0), (2, 0), (2, 2)])]
     psoi = PolygonsOnImage(polys, shape=(6, 6, 3))
     self._test_augment_cbaoi__kernel_size_is_two__keep_size(
         psoi, "augment_polygons")
Exemple #28
0
 def _test_augment_polygons__kernel_size_is_noop(self, kernel_size):
     from imgaug.augmentables.polys import Polygon, PolygonsOnImage
     ps = [Polygon([(1, 1), (2, 1), (2, 2)])]
     psoi = PolygonsOnImage(ps, shape=(6, 6, 3))
     self._test_augment_cbaoi__kernel_size_is_noop(kernel_size, psoi,
                                                   "augment_polygons")
Exemple #29
0
def normalize_polygons(inputs, shapes=None):
    # TODO get rid of this deferred import
    from imgaug.augmentables.polys import Polygon, PolygonsOnImage

    shapes = _preprocess_shapes(shapes)
    ntype = estimate_polygons_norm_type(inputs)
    _assert_exactly_n_shapes_partial = functools.partial(
        _assert_exactly_n_shapes,
        from_ntype=ntype, to_ntype="List[PolygonsOnImage]",
        shapes=shapes)

    if ntype == "None":
        return None
    elif ntype in ["array[float]", "array[int]", "array[uint]"]:
        _assert_single_array_ndim(inputs, 4, "(N,#polys,#points,2)",
                                  "PolygonsOnImage")
        _assert_single_array_last_dim_exactly(inputs, 2, "PolygonsOnImage")
        _assert_exactly_n_shapes_partial(n=len(inputs))
        return [
            PolygonsOnImage(
                [Polygon(poly_points) for poly_points in attr_i],
                shape=shape)
            for attr_i, shape
            in zip(inputs, shapes)
        ]
    elif ntype == "Polygon":
        _assert_exactly_n_shapes_partial(n=1)
        return [PolygonsOnImage([inputs], shape=shapes[0])]
    elif ntype == "PolygonsOnImage":
        return [inputs]
    elif ntype == "iterable[empty]":
        return None
    elif ntype in ["iterable-array[float]",
                   "iterable-array[int]",
                   "iterable-array[uint]"]:
        _assert_many_arrays_ndim(inputs, 3, "(#polys,#points,2)",
                                 "PolygonsOnImage")
        _assert_many_arrays_last_dim_exactly(inputs, 2, "PolygonsOnImage")
        _assert_exactly_n_shapes_partial(n=len(inputs))
        return [
            PolygonsOnImage([Polygon(poly_points) for poly_points in attr_i],
                            shape=shape)
            for attr_i, shape
            in zip(inputs, shapes)
        ]
    elif ntype == "iterable-tuple[number,size=2]":
        _assert_exactly_n_shapes_partial(n=1)
        return [PolygonsOnImage([Polygon(inputs)], shape=shapes[0])]
    elif ntype == "iterable-Keypoint":
        _assert_exactly_n_shapes_partial(n=1)
        return [PolygonsOnImage([Polygon(inputs)], shape=shapes[0])]
    elif ntype == "iterable-Polygon":
        _assert_exactly_n_shapes_partial(n=1)
        return [PolygonsOnImage(inputs, shape=shapes[0])]
    elif ntype == "iterable-PolygonsOnImage":
        return inputs
    elif ntype == "iterable-iterable[empty]":
        return None
    elif ntype in ["iterable-iterable-array[float]",
                   "iterable-iterable-array[int]",
                   "iterable-iterable-array[uint]"]:
        _assert_many_arrays_ndim(inputs, 2, "(#points,2)", "PolygonsOnImage")
        _assert_many_arrays_last_dim_exactly(inputs, 2, "PolygonsOnImage")
        _assert_exactly_n_shapes_partial(n=len(inputs))
        return [
            PolygonsOnImage(
                [Polygon(poly_points) for poly_points in attr_i],
                shape=shape)
            for attr_i, shape
            in zip(inputs, shapes)
        ]
    elif ntype == "iterable-iterable-tuple[number,size=2]":
        _assert_exactly_n_shapes_partial(n=1)
        return [
            PolygonsOnImage([Polygon(attr_i) for attr_i in inputs],
                            shape=shapes[0])
        ]
    elif ntype == "iterable-iterable-Keypoint":
        _assert_exactly_n_shapes_partial(n=1)
        return [
            PolygonsOnImage([Polygon(attr_i) for attr_i in inputs],
                            shape=shapes[0])
        ]
    elif ntype == "iterable-iterable-Polygon":
        _assert_exactly_n_shapes_partial(n=len(inputs))
        return [
            PolygonsOnImage(attr_i, shape=shape)
            for attr_i, shape
            in zip(inputs, shapes)
        ]
    elif ntype == "iterable-iterable-iterable[empty]":
        return None
    else:
        assert ntype in ["iterable-iterable-iterable-tuple[number,size=2]",
                         "iterable-iterable-iterable-Keypoint"]
        _assert_exactly_n_shapes_partial(n=len(inputs))
        return [
            PolygonsOnImage(
                [Polygon(poly_points) for poly_points in attr_i],
                shape=shape)
            for attr_i, shape
            in zip(inputs, shapes)
        ]
Exemple #30
0
    def mapper(self, dataset_dict, train_type: str = 'kpt'):
        if train_type != 'kpt':
            for item in dataset_dict["annotations"]:
                if 'keypoints' in item:
                    del item['keypoints']

        image = utils.read_image(dataset_dict["file_name"], format="BGR")
        img_h, img_w = image.shape[:2]
        num_pixels = img_w * img_h

        ann_dict = Detectron2_Annotation_Dict.from_dict(dataset_dict)
        bbox_list = [ann.bbox for ann in ann_dict.annotations]
        # if train_type == 'seg':
        #     printj.purple(len(ann_dict.annotations))
        #     for ann in ann_dict.annotations:
        #         seg = ann.segmentation
        #     mask = seg.to_mask()
        #     tranformed = self.aug(mask=mask)
        #     mask = tranformed['mask']
        #     image = tranformed['image']
            
        # else:
        image = self.aug(image=np.array(image))['image']
        seq_aug_for_no_seg = almost_always(iaa.Sequential(
            [
                # iaa.Rot90(ia.ALL, keep_size=False)
            ]
        ))
        seq_aug_for_seg = sometimes(iaa.Sequential(
            [
                iaa.Rot90(ia.ALL, keep_size=False),
                iaa.Affine(
                    scale={"x": (0.8, 1.2), "y": (0.8, 1.2)},
                    translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)},
                    rotate=(-180, 180),
                    order=[0, 1],
                    # cval=(0, 255),
                    cval=255,
                    mode=ia.ALL
                )
            ]
        ))
        imgaug_kpts = KeypointsOnImage(keypoints=[], shape=image.shape)
        imgaug_bboxes = BoundingBoxesOnImage(
            bounding_boxes=[], shape=image.shape)
        imgaug_polys = PolygonsOnImage(polygons=[], shape=image.shape)

        num_ann = len(ann_dict.annotations)
        num_kpts = None
        seg_len_list = []
        for ann in ann_dict.annotations:
            if num_kpts is None:
                num_kpts = len(ann.keypoints)
            if len(ann.keypoints.to_imgaug(img_shape=image.shape).keypoints) != len(ann_dict.annotations[0].keypoints):
                printj.red(
                    f'len(ann.keypoints.to_imgaug(img_shape=image.shape).keypoints) == {len(ann.keypoints.to_imgaug(img_shape=image.shape).keypoints)} != {len(ann_dict.annotations[0].keypoints)} == len(ann_dict.annotations[0].keypoints)')
                raise Exception
            imgaug_kpts.keypoints.extend(
                ann.keypoints.to_imgaug(img_shape=image.shape).keypoints)
            if ann.bbox.to_imgaug() is None:
                printj.red(f'ann.bbox.to_imgaug() is None')
                printj.red(f'ann.bbox: {ann.bbox}')
                raise Exception
            imgaug_bboxes.bounding_boxes.append(ann.bbox.to_imgaug())
            if ann.segmentation.to_imgaug(img_shape=image.shape).polygons is None:
                printj.red(
                    f'ann.segmentation.to_imgaug(img_shape=image.shape).polygons is None')
                printj.red(f'ann.segmentation:\n{ann.segmentation}')
                raise Exception
            seg_len_list.append(len(ann.segmentation))

            imgaug_polys.polygons.extend(
                ann.segmentation.to_imgaug(img_shape=image.shape).polygons)
        if len(imgaug_polys.polygons) > 0:
            if num_kpts > 0:
                image, imgaug_kpts_aug, imgaug_polys_aug = seq_aug_for_seg(
                    image=image, keypoints=imgaug_kpts, polygons=imgaug_polys)
            else:
                image, imgaug_polys_aug = seq_aug_for_seg(
                    image=image, polygons=imgaug_polys)
                imgaug_kpts_aug = None
            imgaug_bboxes_aug = None
        else:
            if num_kpts > 0:
                image, imgaug_kpts_aug, imgaug_bboxes_aug = seq_aug_for_no_seg(
                    image=image, keypoints=imgaug_kpts, bounding_boxes=imgaug_bboxes)
            else:
                image, imgaug_bboxes_aug = seq_aug_for_no_seg(
                    image=image, bounding_boxes=imgaug_bboxes)
                imgaug_kpts_aug = None
            imgaug_polys_aug = None

        kpts_aug0 = Keypoint2D_List.from_imgaug(
            imgaug_kpts=imgaug_kpts_aug) if num_kpts > 0 else Keypoint2D_List()
        kpts_aug_list = kpts_aug0.to_numpy(demarcation=True)[:, :2].reshape(
            num_ann, num_kpts, 2) if num_kpts > 0 else []
        kpts_aug_list = [[[x, y, 2] for x, y in kpts_aug]
                         for kpts_aug in kpts_aug_list]
        kpts_aug_list = [Keypoint2D_List.from_list(
            kpts_aug, demarcation=True) for kpts_aug in kpts_aug_list]

        if imgaug_polys_aug is not None and imgaug_bboxes_aug is None:
            poly_aug_list = [Polygon.from_imgaug(
                imgaug_polygon) for imgaug_polygon in imgaug_polys_aug.polygons]
            poly_aug_list_list = unflatten_list(
                poly_aug_list, part_sizes=seg_len_list)
            seg_aug_list = [Segmentation(poly_aug_list)
                            for poly_aug_list in poly_aug_list_list]
            bbox_aug_list = [seg_aug.to_bbox() for seg_aug in seg_aug_list]
            # Adjust BBoxes when Segmentation BBox does not contain all keypoints
            for i in range(len(bbox_aug_list)):
                kpt_points_aug = [
                    kpt_aug.point for kpt_aug in kpts_aug_list[i]] if num_kpts > 0 else []
                kpt_points_aug_contained = [kpt_point_aug.within(
                    bbox_aug_list[i]) for kpt_point_aug in kpt_points_aug]
                if len(kpt_points_aug) > 0:
                    if not np.any(np.array(kpt_points_aug_contained)):
                        printj.red(
                            f"Keypoints not contained in corresponding bbox.")
                    elif not np.all(np.array(kpt_points_aug_contained)):
                        pass
                    else:
                        break
        elif imgaug_polys_aug is None and imgaug_bboxes_aug is not None:
            bbox_aug_list = [BBox.from_imgaug(
                bbox_aug) for bbox_aug in imgaug_bboxes_aug.bounding_boxes]
            seg_aug_list = [None] * len(bbox_aug_list)
        else:
            printj.red(f'Unexpected error')
            raise Exception

        if num_kpts > 0:
            for ann, kpts_aug, bbox_aug, seg_aug in zip(ann_dict.annotations, kpts_aug_list, bbox_aug_list, seg_aug_list):
                ann.keypoints = kpts_aug
                ann.bbox = bbox_aug
                ann.segmentation = seg_aug if seg_aug is not None else Segmentation.from_list([
                ])
        else:
            for ann, bbox_aug, seg_aug in zip(ann_dict.annotations, bbox_aug_list, seg_aug_list):
                ann.keypoints = Keypoint2D_List()
                ann.bbox = bbox_aug
                ann.segmentation = seg_aug if seg_aug is not None else Segmentation.from_list([
                ])

        dataset_dict = ann_dict.to_dict()

        image, transforms = T.apply_transform_gens([], image)

        annots = []
        for item in dataset_dict["annotations"]:
            if 'keypoints' in item and num_kpts == 0:
                del item['keypoints']
            elif 'keypoints' in item:
                item['keypoints'] = np.array(
                    item['keypoints']).reshape(-1, 3).tolist()
            annots.append(item)
        dataset_dict["image"] = torch.as_tensor(
            image.transpose(2, 0, 1).astype("float32"))
        instances = utils.annotations_to_instances(annots, image.shape[:2])
        dataset_dict["instances"] = utils.filter_empty_instances(
            instances, by_box=True, by_mask=False)

        # if True:
        #     vis_img = image.copy()
        #     bbox_list = [BBox.from_list(vals) for vals in dataset_dict["instances"].gt_boxes.tensor.numpy().tolist()]
        #     seg_list = [Segmentation([Polygon.from_list(poly.tolist(), demarcation=False) for poly in seg_polys]) for seg_polys in dataset_dict["instances"].gt_masks.polygons]
        #     kpts_list = [Keypoint2D_List.from_numpy(arr, demarcation=True) for arr in dataset_dict["instances"].gt_keypoints.tensor.numpy()] if hasattr(dataset_dict["instances"], 'gt_keypoints') else []
        #     for seg in seg_list:
        #         vis_img = draw_segmentation(img=vis_img, segmentation=seg, transparent=True)
        #     for bbox in bbox_list:
        #         vis_img = draw_bbox(img=vis_img, bbox=bbox)
        #     for kpts in kpts_list:
        #         vis_img = draw_keypoints(img=vis_img, keypoints=kpts.to_numpy(demarcation=True)[:, :2].tolist(), radius=6)
        #     aug_visualizer.step(vis_img)

        return dataset_dict