Beispiel #1
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
 def __call__(self, sample):
     image, polygon, labels = sample["image"], sample["polygon"], sample[
         "labels"]
     p = Polygon(polygon)
     image = np.array(image)
     img = p.extract_from_image(image)
     image = Image.fromarray(img)
     sample = {'image': image, 'polygon': polygon, 'labels': labels}
     return sample
Beispiel #3
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")
Beispiel #4
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
Beispiel #5
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))
        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)
Beispiel #6
0
    def augment_card(self, card, hull):
        cardH, cardW, _ = card.shape
        decalX = int((self.imgW - cardW) / 2)
        decalY = int((self.imgH - cardH) / 2)

        img_card = np.zeros((self.imgH, self.imgW, 4), dtype=np.uint8)
        img_card[decalY:decalY + cardH, decalX:decalX + cardW, :] = card

        card = img_card[:, :, :3]
        hull = hull[:, 0, :]

        card_kps_xy = np.float32([[decalX, decalY], [decalX + cardW, decalY],
                                  [decalX + cardW, decalY + cardH],
                                  [decalX, decalY + cardH]])
        kpsoi_card = KeypointsOnImage.from_xy_array(card_kps_xy,
                                                    shape=card.shape)

        # hull is a cv2.Contour, shape : Nx1x2
        kpsoi_hull = [
            ia.Keypoint(x=p[0] + decalX, y=p[1] + decalY)
            for p in hull.reshape(-1, 2)
        ]
        kpsoi_hull = KeypointsOnImage(kpsoi_hull,
                                      shape=(self.imgH, self.imgW, 3))

        # create polygon
        poly_hull = Polygon(kpsoi_hull.keypoints)

        # create empty segmentation map for classes: background and card
        segmap = np.zeros((card.shape[0], card.shape[1], 3), dtype=np.uint8)

        # draw the tree polygon into the second channel
        segmap = poly_hull.draw_on_image(segmap,
                                         color=(0, 255, 0),
                                         alpha=1.0,
                                         alpha_lines=0.0,
                                         alpha_points=0.0)

        # merge the two channels to a single one
        segmap = np.argmax(segmap, axis=2)
        segmap = segmap.astype(np.uint8)
        segmap = SegmentationMapOnImage(segmap, nb_classes=2, shape=card.shape)

        myseq = self.seq.to_deterministic()
        card_aug, segmap_aug = myseq(image=card, segmentation_maps=segmap)
        card_aug, kpsoi_aug = myseq(image=card, keypoints=kpsoi_card)

        return card_aug, kpsoi_aug, segmap_aug
Beispiel #7
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
def Viajson2Polytuples(json_file):
    polycoords = {}

    f = open(json_file)
    data = json.load(f)

    for k, v in data.items():
        regions = []
        if data[k]['regions']:  # if there's a region
            for region in range(len(data[k]['regions'])):
                # extract x,y values
                x_vals = data[k]['regions'][region]['shape_attributes'][
                    'all_points_x']
                y_vals = data[k]['regions'][region]['shape_attributes'][
                    'all_points_y']
                #print("region")
                #print("x:", x_vals)
                #print("y:", y_vals)

                # create as a list of tuples
                coords = [(x_vals[coord], y_vals[coord])
                          for coord in range(len(x_vals))]
                regions.append(Polygon(coords))

            polycoords.update({v['filename']: regions})
        #else:

    return polycoords
    def augment(self):
        data = ParseCoco(path_to_package_images=PATH_TO_PACKAGE_IMAGES,
                         path_to_json=PATH_TO_JSON,
                         type_of_labels=TYPE_OF_LABELS)()
        loop = 0
        while loop != 5:  # Fives times make augmentations
            for filename, polygons in tqdm(data.items()):
                image = imageio.imread(
                    os.path.join(PATH_TO_PACKAGE_IMAGES, filename))
                list_of_poly_objects = []
                for polygon in polygons:
                    poly_object = Polygon(polygon)
                    list_of_poly_objects.append(poly_object)

                psoi = PolygonsOnImage(list_of_poly_objects, shape=image.shape)
                aug = iaa.Sequential([
                    iaa.AdditiveGaussianNoise(scale=(0, 50)),
                    iaa.SigmoidContrast(gain=(3, 10), cutoff=(0.4, 0.6)),
                    # iaa.AddToHueAndSaturation((-50, 50)),
                    iaa.Add((-40, 40)),
                    iaa.Affine(rotate=(-20, 20),
                               translate_percent={
                                   "x": 0.1,
                                   "y": 0.1
                               },
                               scale=(0.5, 1.8)),
                    iaa.Fliplr(1.0)
                ])
                image_aug, psoi_aug = aug(image=image, polygons=psoi)
                # new_name = filename.split('.')[0]
                cv2.imwrite("augmented/{}loop{}.jpg".format(filename, LOOPS),
                            image_aug)
            loop += 1
Beispiel #10
0
def draw():
    image = imageio.imread('./flip/IMG_8038R.JPG')
    json_file = './flip/json/IMG_8038R.json'
    with open(json_file, 'rb') as fp:
        data = json.load(fp)
        all_point = []
        for shapes in data['shapes']:
            aaa = Polygon(shapes['points'])
            all_point.append(aaa)
        psoi = ia.PolygonsOnImage(all_point, shape=image.shape)
        aug = iaa.Sequential([
            iaa.Affine(scale=(1, 1),
                       rotate=(0, 0),
                       translate_percent=(0, 0),
                       mode=["constant"],
                       cval=0),  # 仿射变换

            # iaa.Fliplr(0.5), # 左右翻转
            # iaa.PerspectiveTransform((0.01, 0.1)), # 透视变换
            # iaa.AddToHueAndSaturation((-20, 20)),  #
            # iaa.LinearContrast((0.8, 1.2), per_channel=0.5),
            # iaa.Sometimes(0.75, iaa.Snowflakes())   # 高斯模糊
        ])
        image_aug, psoi_aug = aug(image=image, polygons=psoi)
        ia.imshow(
            psoi_aug.draw_on_image(image_aug, alpha_face=0.2, size_points=7))
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)
Beispiel #12
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])
Beispiel #13
0
    def kps_to_mask(self, kpsoi):
        poly_card = Polygon(kpsoi.keypoints)

        segmap = np.zeros((self.imgH, self.imgW, 3), dtype=np.uint8)

        # draw the tree polygon into the second channel
        segmap = poly_card.draw_on_image(segmap,
                                         color=(0, 255, 0),
                                         alpha=1.0,
                                         alpha_lines=0.0,
                                         alpha_points=0.0)

        # merge the two channels to a single one
        segmap = np.argmax(segmap, axis=2)
        card_mask = np.stack([segmap] * 3, -1)

        return card_mask
Beispiel #14
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)
Beispiel #15
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)
Beispiel #16
0
 def generate_images(self, path_to_pic: str, shapes: List[Dict]) -> (List, List):
     image = imageio.imread(path_to_pic)
     pols = []
     for p in shapes:
         if p['name'] == 'rect':
             x = p['x']
             y = p['y']
             width = p['width']
             height = p['height']
             pol = Polygon([
                 (x, y),
                 (x + width, y),
                 (x + width, y + height),
                 (x, y + height)
             ])
             pols.append(pol)
         else:
             x_all = p['all_points_x']
             y_all = p['all_points_y']
             points = []
             for i in range(len(x_all)):
                 points.append((x_all[i], y_all[i]))
             pol = Polygon(points)
             pols.append(pol)
     psoi = ia.PolygonsOnImage(pols, shape=image.shape)
     aug_images = []
     psoi_augs = []
     transforms = [iaa.Fliplr(1),
                   iaa.GaussianBlur(5),
                   iaa.SaltAndPepper(0.2),
                   iaa.Multiply(0.6),
                   iaa.Multiply(1.2),
                   iaa.ElasticTransformation(sigma=0.2, alpha=3)]
     for trans in transforms:
         aug_func = iaa.Sequential([
             trans
         ])
         image_aug, psoi_aug = aug_func(image=image, polygons=psoi)
         aug_images.append(image_aug)
         psoi_augs.append(psoi_aug)
     # images = [psoi_aug.draw_on_image(image_aug, alpha_face=0.2, size_points=7), image]
     # ia.imshow(np.hstack(images))
     return aug_images, psoi_augs
Beispiel #17
0
 def __prune_imgaug_polys(cls, imgaug_polys: ImgAugPolygons,
                          img_shape: np.ndarray) -> ImgAugPolygons:
     multipoly_inter_shapely = cls.__get_multipoly_inter_shapely(
         imgaug_polys=imgaug_polys, img_shape=img_shape)
     polys = [
         ImgAugPolygon.from_shapely(shape_obj)
         for shape_obj in multipoly_inter_shapely
         if type(shape_obj) is ShapelyPolygon
     ]
     return ImgAugPolygons(polys, shape=img_shape)
Beispiel #18
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)
Beispiel #19
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
Beispiel #21
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")
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)
Beispiel #23
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
def create_random_polygon(height, width, seed):
    rs = np.random.RandomState(seed)
    nb_points = rs.randint(3, 50)
    coords = rs.rand(nb_points, 2)
    coords = (coords * 2 - 0.5)  # allow coords outside of the image plane
    coords[:, 0] *= width
    coords[:, 1] *= height
    poly = Polygon(coords)
    if poly.is_valid:
        return poly

    new_seed = rs.randint(ia.SEED_MIN_VALUE, ia.SEED_MAX_VALUE)
    return create_random_polygon(height, width, new_seed)
    def __getitem__(self, idx):
        image = Image.open(os.path.join(
            self.root, self.image_filenames[idx])).convert('RGB')

        image = np.asarray(image)

        if self.nums is not None:
            segmap = np.zeros((image.shape[0], image.shape[1], 1),
                              dtype=np.int32)

            nums = self.nums[idx]

            for num in nums:
                poly = Polygon(num['box'])
                segmap = poly.draw_on_image(segmap, color=2)

            segmap = SegmentationMapsOnImage(segmap, shape=image.shape)

            if self.transforms:
                image, segmap = self.transforms(image=image,
                                                segmentation_maps=segmap)

            image = to_tensor(image)
            image = normalize(image, MEAN, STD)

            segmap = segmap.arr
            segmap = to_tensor(segmap.astype(np.float32))

            return {'image': image, 'mask': segmap}

        else:
            if self.transforms:
                image = self.transforms(image=image)

            image = to_tensor(image)
            image = normalize(image, MEAN, STD)

            return {'image': image}
    def __call__(self, img, boxes, masks, labels, filename):
        #print("####")
        #print(type(img), type(boxes), type(masks[0]), len(masks), np.unique(masks[0]))
        #print(img.shape, boxes.shape, masks[0].shape)
        bboxes = []
        for box in boxes[:, :4]:
            x1, y1, x2, y2 = box
            bboxes.append(ia.BoundingBox(x1=x1, y1=y1, x2=x2, y2=y2))

        # mask를 위해서 polygon 만들기
        points = []
        for gt_mask in masks:
            contours, ct = cv2.findContours(gt_mask, cv2.RETR_LIST,
                                            cv2.CHAIN_APPROX_SIMPLE)
            for c in contours:
                # 각도 있는 다각형
                rc = cv2.minAreaRect(contours[0])
                points.append(cv2.boxPoints(rc))
                #cv2.dravwContours(im, [box], 0, (0,255,0),3)
                # 각도 없는 사각형
                #rect = cv2.boundingRect(c)
                #x,y,w,h = rect
                #cv2.rectangle(im,(x,y),(x+w,y+h),(0,255,0),2)
                #cv2.putText(im,'Moth Detected',(x+w+10,y+h),0,0.3,(0,255,0))

        mulpoly = [Polygon(point) for point in points]
        psoi = ia.PolygonsOnImage(mulpoly, shape=img.shape)

        image_aug, bbs_aug, poly_aug = self.seq(images=[img],
                                                bounding_boxes=[bboxes],
                                                polygons=[psoi])
        image_aug, bbs_aug, poly_aug = image_aug[0], bbs_aug[0], poly_aug[0]

        # ia.BoundingBox 된 boxes 원래 형태로 돌려놓기
        bbs = []
        for bb in bbs_aug:
            bbs.append(bb.coords.reshape(4))
        bbs_aug = np.array(bbs)

        # polygon이 된 mask를 원래 형태로 돌려놓기
        masks_aug = []
        for poly in poly_aug:
            img = Image.new('L', (image_aug.shape[0], image_aug.shape[0]), 0)
            ImageDraw.Draw(img).polygon(poly.coords, outline=1, fill=1)
            masks_aug.append(np.array(img))

        #print(type(image_aug), type(bbs_aug), type(masks_aug[0]), len(masks_aug), np.unique(masks_aug[0]))
        #print(image_aug.shape, bbs_aug.shape, len(masks_aug), masks_aug[0].shape)

        return image_aug, bbs_aug, masks_aug, labels
Beispiel #27
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_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]])
Beispiel #29
0
    def to_polygon(self):
        """Convert this bounding box to a polygon covering the same area.

        Returns
        -------
        imgaug.augmentables.polys.Polygon
            The bounding box converted to a polygon.

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

        return Polygon([(self.x1, self.y1), (self.x2, self.y1),
                        (self.x2, self.y2), (self.x1, self.y2)],
                       label=self.label)
Beispiel #30
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