Esempio n. 1
0
    def test_to_keypoint_image_size_3(self):
        kps = [ia.Keypoint(x=1, y=2), ia.Keypoint(x=3, y=4)]
        kpi = ia.KeypointsOnImage(keypoints=kps, shape=(5, 5, 3))

        image = kpi.to_keypoint_image(size=3)

        kps_mask = np.zeros((5, 5, 2), dtype=np.bool)
        kps_mask[2 - 1:2 + 1 + 1, 1 - 1:1 + 1 + 1, 0] = 1
        kps_mask[4 - 1:4 + 1 + 1, 3 - 1:3 + 1 + 1, 1] = 1
        assert np.all(image[kps_mask] >= 128)
        assert np.all(image[~kps_mask] == 0)
Esempio n. 2
0
    def test_on__wider_image_shape_given_as_array(self):
        kps = [ia.Keypoint(x=1, y=2), ia.Keypoint(x=3, y=4)]
        kpi = ia.KeypointsOnImage(keypoints=kps, shape=(10, 20, 3))

        image = np.zeros((20, 40, 3), dtype=np.uint8)
        kpi2 = kpi.on(image)

        assert kpi2.keypoints[0].x == 2
        assert kpi2.keypoints[0].y == 4
        assert kpi2.keypoints[1].x == 6
        assert kpi2.keypoints[1].y == 8
    def test_to_distance_maps_inverted(self):
        kpi = ia.KeypointsOnImage(
            keypoints=[ia.Keypoint(x=2, y=3)],
            shape=(5, 5, 3))

        distance_map = kpi.to_distance_maps(inverted=True)

        expected = self._get_single_keypoint_distance_map()
        expected_inv = np.divide(np.ones_like(expected), expected+1)
        assert distance_map.shape == (5, 5, 1)
        assert np.allclose(distance_map, expected_inv)
Esempio n. 4
0
    def test_to_distance_maps_two_keypoints_inverted(self):
        kpi = ia.KeypointsOnImage(
            keypoints=[ia.Keypoint(x=2, y=3),
                       ia.Keypoint(x=1, y=0)],
            shape=(4, 4, 3))

        distance_map_inv = kpi.to_distance_maps(inverted=True)

        expected = self._get_two_points_keypoint_distance_map()
        expected_inv = np.divide(np.ones_like(expected), expected + 1)
        assert np.allclose(np.max(distance_map_inv, axis=2), expected_inv)
    def test_to_xy_array(self):
        kps = [ia.Keypoint(x=1, y=2), ia.Keypoint(x=3, y=4)]
        kpi = ia.KeypointsOnImage(keypoints=kps, shape=(5, 5, 3))

        observed = kpi.to_xy_array()

        expected = np.float32([
            [1, 2],
            [3, 4]
        ])
        assert np.allclose(observed, expected)
def create_random_keypoints(size_images, nb_keypoints_per_img):
    result = []
    for _ in sm.xrange(size_images[0]):
        kps = []
        height, width = size_images[1], size_images[2]
        for _ in sm.xrange(nb_keypoints_per_img):
            x = np.random.randint(0, width - 1)
            y = np.random.randint(0, height - 1)
            kps.append(ia.Keypoint(x=x, y=y))
        result.append(ia.KeypointsOnImage(kps, shape=size_images[1:]))
    return result
    def test_on__same_image_size(self):
        kps = [ia.Keypoint(x=1, y=2), ia.Keypoint(x=3, y=4)]
        kpi = ia.KeypointsOnImage(keypoints=kps, shape=(10, 20, 3))

        kpi2 = kpi.on((10, 20, 3))

        assert np.all([
            kp_i.x == kp_j.x and kp_i.y == kp_j.y
            for kp_i, kp_j
            in zip(kpi.keypoints, kpi2.keypoints)
        ])
Esempio n. 8
0
def hull_to_kps(hull, decalX=decalX, decalY=decalY):
    """
        Convert hull to imgaug keypoints
    """
    # hull is a cv2.Contour, shape : Nx1x2
    kps = [
        ia.Keypoint(x=p[0] + decalX, y=p[1] + decalY)
        for p in hull.reshape(-1, 2)
    ]
    kps = ia.KeypointsOnImage(kps, shape=(imgH, imgW, 3))
    return kps
Esempio n. 9
0
    def test_wider_image(self):
        kps = [ia.Keypoint(x=1, y=2), ia.Keypoint(x=3, y=4)]
        kpi = ia.KeypointsOnImage(keypoints=kps, shape=(10, 20, 3))

        kpi2 = self._func(kpi, (20, 40, 3))

        assert kpi2.keypoints[0].x == 2
        assert kpi2.keypoints[0].y == 4
        assert kpi2.keypoints[1].x == 6
        assert kpi2.keypoints[1].y == 8
        assert kpi2.shape == (20, 40, 3)
Esempio n. 10
0
    def test_deepcopy_keypoints_set(self):
        kp1 = ia.Keypoint(x=1, y=2)
        kp2 = ia.Keypoint(x=3, y=4)
        kp3 = ia.Keypoint(x=5, y=6)
        kpsoi = ia.KeypointsOnImage([kp1, kp2], shape=(40, 50, 3))

        kpsoi_copy = kpsoi.deepcopy(keypoints=[kp3])

        assert kpsoi_copy is not kpsoi
        assert kpsoi_copy.shape == (40, 50, 3)
        assert kpsoi_copy.keypoints == [kp3]
Esempio n. 11
0
    def test_to_keypoint_image_size_1(self):
        kps = [ia.Keypoint(x=1, y=2), ia.Keypoint(x=3, y=4)]
        kpi = ia.KeypointsOnImage(keypoints=kps, shape=(5, 5, 3))

        image = kpi.to_keypoint_image(size=1)

        kps_mask = np.zeros((5, 5, 2), dtype=np.bool)
        kps_mask[2, 1, 0] = 1
        kps_mask[4, 3, 1] = 1
        assert np.all(image[kps_mask] == 255)
        assert np.all(image[~kps_mask] == 0)
Esempio n. 12
0
    def test_to_distance_maps_two_keypoints(self):
        # TODO this test could have been done a bit better by simply splitting
        #      the distance maps, one per keypoint, considering the function
        #      returns one distance map per keypoint
        kpi = ia.KeypointsOnImage(
            keypoints=[ia.Keypoint(x=2, y=3), ia.Keypoint(x=1, y=0)],
            shape=(4, 4, 3))

        distance_map = kpi.to_distance_maps()

        expected = self._get_two_points_keypoint_distance_map()
        assert np.allclose(np.min(distance_map, axis=2), expected)
Esempio n. 13
0
    def __getitem__(self, idx):

        sample = super().__getitem__(idx)

        orig_shape = self.imgs[0].shape[:2]
        new_shape = sample['image'].shape

        locs = self.locs2d[self.locs2d['frame'] == idx]
        locs = [
            coord2Pixel(l['x'], l['y'], orig_shape[1], orig_shape[0])
            for _, l in locs.iterrows()
        ]

        keypoints = ia.KeypointsOnImage(
            [ia.Keypoint(x=l[1], y=l[0]) for l in locs],
            shape=(orig_shape[0], orig_shape[1]))

        keypoints = self.reshaper_seg.augment_keypoints(keypoints)

        if (len(locs) > 0):
            obj_prior = [
                make_2d_gauss((new_shape[0], new_shape[1]),
                              self.sig_prior * max(new_shape), (kp.y, kp.x))
                for kp in keypoints.keypoints
            ]
            obj_prior = np.asarray(obj_prior).sum(axis=0)[..., None]
            # offset = np.ones_like(obj_prior) * 0.5
            obj_prior -= obj_prior.min()
            obj_prior /= obj_prior.max()
            # obj_prior *= 0.5
            # obj_prior += offset
        else:
            obj_prior = (np.ones((new_shape[0], new_shape[1])))[..., None]

        sample['prior'] = obj_prior
        sample['loc_keypoints'] = keypoints

        coords = np.array([(np.round(kp.y).astype(int),
                            np.round_(kp.x).astype(int))
                           for kp in keypoints.keypoints])
        if (coords.shape[0] > 0):
            coords[:, 0] = np.clip(coords[:, 0],
                                   a_min=0,
                                   a_max=keypoints.shape[0] - 1)
            coords[:, 1] = np.clip(coords[:, 1],
                                   a_min=0,
                                   a_max=keypoints.shape[1] - 1)

        sample['labels_clicked'] = [
            sample['labels'][i, j, 0] for i, j in coords
        ]

        return sample
Esempio n. 14
0
def transform(aug, image, anns):
    image_shape = image.shape
    image = aug.augment_image(image)
    new_anns = []
    for ann in anns:
        keypoints = [imgaug.Keypoint(p[0], p[1]) for p in ann['poly']]
        keypoints = aug.augment_keypoints(
            [imgaug.KeypointsOnImage(keypoints, shape=image_shape)])[0].keypoints
        poly = [(min(max(0, p.x), image.shape[1] - 1), min(max(0, p.y), image.shape[0] - 1)) for p in keypoints]
        new_ann = {'poly': poly, 'text': ann['text']}
        new_anns.append(new_ann)
    return image, new_anns
Esempio n. 15
0
 def __key_points(image_shape, point_list):
     """
     feed cnt and return ia.KeypointsOnImage object
     :param point_list: np.array size=(n,1,2)
            image_shape
     :return:
     """
     keypoint_list = []
     for i in range(point_list.shape[0]):
         keypoint_list.append(ia.Keypoint(x=point_list[i, 0, 0], y=point_list[i, 0, 1]))
     return ia.KeypointsOnImage(keypoint_list,
                                shape=ia.quokka(size=image_shape[:2]))
Esempio n. 16
0
def y_to_keypoint(X_data, y_data):
    """
    convert y location to imgaug keypoint class
    """
    keypoints = []
    for idx in range(len(y_data)):
        x = X_data[idx].shape[1] // 2
        s = X_data[idx].shape + (1, )
        keypoint = ia.KeypointsOnImage([ia.Keypoint(x=x, y=y_data[idx])],
                                       shape=s)
        keypoints.append(keypoint)
    return keypoints
Esempio n. 17
0
    def test_deepcopy_shape_set(self):
        kp1 = ia.Keypoint(x=1, y=2)
        kp2 = ia.Keypoint(x=3, y=4)
        kpsoi = ia.KeypointsOnImage([kp1, kp2], shape=(40, 50, 3))

        kpsoi_copy = kpsoi.deepcopy(shape=(40 + 1, 50 + 1, 3))

        assert kpsoi_copy is not kpsoi
        assert kpsoi_copy.shape == (40 + 1, 50 + 1, 3)
        assert len(kpsoi_copy.keypoints) == 2
        assert kpsoi_copy.keypoints[0].coords_almost_equals(kp1)
        assert kpsoi_copy.keypoints[1].coords_almost_equals(kp2)
Esempio n. 18
0
    def test_fill_from_xy_array___list_with_two_coords(self):
        xy = [(0, 0), (1, 2)]
        kps = ia.KeypointsOnImage(
            [ia.Keypoint(10, 20), ia.Keypoint(30, 40)], shape=(2, 2, 3))

        kps = kps.fill_from_xy_array_(xy)

        assert len(kps.keypoints) == 2
        assert kps.keypoints[0].x == 0
        assert kps.keypoints[0].y == 0
        assert kps.keypoints[1].x == 1
        assert kps.keypoints[1].y == 2
Esempio n. 19
0
    def test_augmentations_with_seed_match_for_images_and_keypoints(self):
        augseq = iaa.AddElementwise((0, 255))
        image = np.zeros((10, 10, 1), dtype=np.uint8)
        # keypoints here will not be changed by augseq, but they will induce
        # deterministic mode to start in augment_batches() as each batch
        # contains images AND keypoints
        kps = ia.KeypointsOnImage([ia.Keypoint(x=2, y=0)], shape=(10, 10, 1))
        batch = ia.Batch(images=np.uint8([image, image]), keypoints=[kps, kps])
        batches = [batch.deepcopy() for _ in sm.xrange(60)]

        # seed=1
        with multicore.Pool(augseq, processes=2, maxtasksperchild=30,
                            seed=1) as pool:
            batches_aug1 = pool.map_batches(batches, chunksize=2)
        # seed=1
        with multicore.Pool(augseq, processes=2, seed=1) as pool:
            batches_aug2 = pool.map_batches(batches, chunksize=1)
        # seed=2
        with multicore.Pool(augseq, processes=2, seed=2) as pool:
            batches_aug3 = pool.map_batches(batches, chunksize=1)

        assert len(batches_aug1) == 60
        assert len(batches_aug2) == 60
        assert len(batches_aug3) == 60

        for batches_aug in [batches_aug1, batches_aug2, batches_aug3]:
            for batch in batches_aug:
                for keypoints_aug in batch.keypoints_aug:
                    assert keypoints_aug.keypoints[0].x == 2
                    assert keypoints_aug.keypoints[0].y == 0

        for b1, b2, b3 in zip(batches_aug1, batches_aug2, batches_aug3):
            # images were augmented
            assert not np.array_equal(b1.images_unaug, b1.images_aug)
            assert not np.array_equal(b2.images_unaug, b2.images_aug)
            assert not np.array_equal(b3.images_unaug, b3.images_aug)

            # original images still the same
            assert np.array_equal(b1.images_unaug, batch.images_unaug)
            assert np.array_equal(b2.images_unaug, batch.images_unaug)
            assert np.array_equal(b3.images_unaug, batch.images_unaug)

            # augmentations for same seed are the same
            assert np.array_equal(b1.images_aug, b2.images_aug)

            # augmentations for different seeds are different
            assert not np.array_equal(b1.images_aug, b3.images_aug)

        # make sure that batches for the two pools with same seed did not
        # repeat within results (only between the results of the two pools)
        for batches_aug in [batches_aug1, batches_aug2, batches_aug3]:
            self._assert_each_augmentation_not_more_than_once(batches_aug)
Esempio n. 20
0
 def __call__(self, img, ploys):
     aug = self.aug.to_deterministic()
     origin_shape = img.shape
     img = aug.augment_image(img)
     poly_list = []
     for poly in ploys:
         keypoints = [imgaug.Keypoint(p[0], p[1]) for p in poly]
         keypoints = aug.augment_keypoints(
             [imgaug.KeypointsOnImage(keypoints,
                                      shape=origin_shape)])[0].keypoints
         poly = [[p.x, p.y] for p in keypoints]
         poly_list.append(np.array(poly))
     return img, poly_list
Esempio n. 21
0
    def test_fill_from_augmented_normalized_batch(self):
        batch = ia.UnnormalizedBatch(
            images=np.zeros((1, 2, 2, 3), dtype=np.uint8),
            heatmaps=[np.zeros((2, 2, 1), dtype=np.float32)],
            segmentation_maps=[np.zeros((2, 2, 1), dtype=np.int32)],
            keypoints=[[(0, 0)]],
            bounding_boxes=[[ia.BoundingBox(0, 0, 1, 1)]],
            polygons=[[ia.Polygon([(0, 0), (1, 0), (1, 1)])]],
            line_strings=[[ia.LineString([(0, 0), (1, 0)])]])
        batch_norm = ia.Batch(
            images=np.zeros((1, 2, 2, 3), dtype=np.uint8),
            heatmaps=[
                ia.HeatmapsOnImage(np.zeros((2, 2, 1), dtype=np.float32),
                                   shape=(2, 2, 3))
            ],
            segmentation_maps=[
                ia.SegmentationMapsOnImage(np.zeros((2, 2, 1), dtype=np.int32),
                                           shape=(2, 2, 3))
            ],
            keypoints=[
                ia.KeypointsOnImage([ia.Keypoint(0, 0)], shape=(2, 2, 3))
            ],
            bounding_boxes=[
                ia.BoundingBoxesOnImage([ia.BoundingBox(0, 0, 1, 1)],
                                        shape=(2, 2, 3))
            ],
            polygons=[
                ia.PolygonsOnImage([ia.Polygon([(0, 0), (1, 0), (1, 1)])],
                                   shape=(2, 2, 3))
            ],
            line_strings=[
                ia.LineStringsOnImage([ia.LineString([(0, 0), (1, 0)])],
                                      shape=(2, 2, 3))
            ])
        batch_norm.images_aug = batch_norm.images_unaug
        batch_norm.heatmaps_aug = batch_norm.heatmaps_unaug
        batch_norm.segmentation_maps_aug = batch_norm.segmentation_maps_unaug
        batch_norm.keypoints_aug = batch_norm.keypoints_unaug
        batch_norm.bounding_boxes_aug = batch_norm.bounding_boxes_unaug
        batch_norm.polygons_aug = batch_norm.polygons_unaug
        batch_norm.line_strings_aug = batch_norm.line_strings_unaug

        batch = batch.fill_from_augmented_normalized_batch(batch_norm)

        assert batch.images_aug.shape == (1, 2, 2, 3)
        assert ia.is_np_array(batch.heatmaps_aug[0])
        assert ia.is_np_array(batch.segmentation_maps_aug[0])
        assert batch.keypoints_aug[0][0] == (0, 0)
        assert batch.bounding_boxes_aug[0][0].x1 == 0
        assert batch.polygons_aug[0][0].exterior[0][0] == 0
        assert batch.line_strings_aug[0][0].coords[0][0] == 0
Esempio n. 22
0
def chapter_examples_keypoints_simple():
    import imgaug as ia
    from imgaug import augmenters as iaa

    ia.seed(1)

    image = ia.quokka(size=(256, 256))
    keypoints = ia.KeypointsOnImage([
        ia.Keypoint(x=65, y=100),
        ia.Keypoint(x=75, y=200),
        ia.Keypoint(x=100, y=100),
        ia.Keypoint(x=200, y=80)
    ],
                                    shape=image.shape)

    seq = iaa.Sequential([
        iaa.Multiply(
            (1.2, 1.5)),  # change brightness, doesn't affect keypoints
        iaa.Affine(rotate=10, scale=(
            0.5, 0.7
        ))  # rotate by exactly 10deg and scale to 50-70%, affects keypoints
    ])

    # Make our sequence deterministic.
    # We can now apply it to the image and then to the keypoints and it will
    # lead to the same augmentations.
    # IMPORTANT: Call this once PER BATCH, otherwise you will always get the
    # exactly same augmentations for every batch!
    seq_det = seq.to_deterministic()

    # augment keypoints and images
    image_aug = seq_det.augment_images([image])[0]
    keypoints_aug = seq_det.augment_keypoints([keypoints])[0]

    # print coordinates before/after augmentation (see below)
    for i in range(len(keypoints.keypoints)):
        before = keypoints.keypoints[i]
        after = keypoints_aug.keypoints[i]
        print("Keypoint %d: (%d, %d) -> (%d, %d)" %
              (i, before.x, before.y, after.x, after.y))

    # image with keypoints before/after augmentation (shown below)
    image_before = keypoints.draw_on_image(image, size=7)
    image_after = keypoints_aug.draw_on_image(image_aug, size=7)

    # ------------

    save("examples_keypoints",
         "simple.jpg",
         grid([image_before, image_after], cols=2, rows=1),
         quality=90)
def main():
    image = ia.quokka(size=0.5)
    kps = [
        ia.KeypointsOnImage([
            ia.Keypoint(x=245, y=203),
            ia.Keypoint(x=365, y=195),
            ia.Keypoint(x=313, y=269)
        ],
                            shape=(image.shape[0] * 2, image.shape[1] * 2))
    ]
    kps[0] = kps[0].on(image.shape)
    print("image shape:", image.shape)

    augs = [
        iaa.PerspectiveTransform(scale=0.01, name="pt001", keep_size=True),
        iaa.PerspectiveTransform(scale=0.1, name="pt01", keep_size=True),
        iaa.PerspectiveTransform(scale=0.2, name="pt02", keep_size=True),
        iaa.PerspectiveTransform(scale=0.3, name="pt03", keep_size=True),
        iaa.PerspectiveTransform(scale=(0, 0.3),
                                 name="pt00to03",
                                 keep_size=True)
    ]

    print("original", image.shape)
    ia.imshow(kps[0].draw_on_image(image))

    print("-----------------")
    print("Random aug per image")
    print("-----------------")
    for aug in augs:
        images_aug = []
        for _ in range(16):
            aug_det = aug.to_deterministic()
            img_aug = aug_det.augment_image(image)
            kps_aug = aug_det.augment_keypoints(kps)[0]
            img_aug_kps = kps_aug.draw_on_image(img_aug)
            img_aug_kps = np.pad(img_aug_kps, ((1, 1), (1, 1), (0, 0)),
                                 mode="constant",
                                 constant_values=255)
            #print(aug.name, img_aug_kps.shape, img_aug_kps.shape[1]/img_aug_kps.shape[0])
            images_aug.append(img_aug_kps)
            #ia.imshow(img_aug_kps)
        print(aug.name)
        ia.imshow(ia.draw_grid(images_aug))

    print("----------------")
    print("6 channels")
    print("----------------")
    image6 = np.dstack([image, image])
    image6_aug = augs[1].augment_image(image6)
    ia.imshow(np.hstack([image6_aug[..., 0:3], image6_aug[..., 3:6]]))
Esempio n. 24
0
def do_aug(img, img_name, dets_bb, split_num):
    image_shape = img.shape
    keypoints = []
    keypoints_on_images = []
    for i in range(dets_bb.shape[0]):
        x = dets_bb[i][0]
        y = dets_bb[i][1]
        keypoints.append(ia.Keypoint(x=x, y=y))

        x = dets_bb[i][2]
        y = dets_bb[i][3]
        keypoints.append(ia.Keypoint(x=x, y=y))

    keypoints_on_images.append(ia.KeypointsOnImage(keypoints, shape=img.shape))

    #     seq_det = iaa.Sequential([iaa.Affine(scale=(0.5,1.5)),iaa.Add((-40, 40))])
    seq_det = iaa.Sequential([iaa.Add((-40, 40))])

    images_aug = seq_det.augment_image(img)
    keypoints_aug = seq_det.augment_keypoints(keypoints_on_images)
    im_name = img_name + '_sp_' + str(split_num) + '.jpg'
    image_txt = img_name + '_sp_' + str(split_num) + '_gt.txt'
    #print("Image Name::",image_txt)
    figname = os.path.join(split_img, im_name)
    txt_name = os.path.join(split_ann, image_txt)
    #print("text_name::",txt_name)
    cv2.imwrite(figname, images_aug)

    #keypoints = keypoints_aug[0].keypoints
    #print(keypoints)

    file_arr = np.loadtxt(txt_name, dtype='str', ndmin=2)
    #print(txt_name)#,file_arr,file_arr.shape[0])
    file_new_arr = []
    for box_num in range(file_arr.shape[0]):
        row = list(file_arr[box_num])


#         print(keypoints[2*box_num].x,keypoints[2*box_num].y,keypoints[2*box_num + 1].x,keypoints[2*box_num + 1].y,image_shape[1],image_shape[0])
#print(row)
#         if((keypoints[2*box_num].x > -10) and (keypoints[2*box_num].y > -10) and (keypoints[2*box_num + 1].x < image_shape[1] + 10) and (keypoints[2*box_num + 1].y < image_shape[0] + 10) ):
#             row[0]= keypoints[2*box_num].x
#             row[1]= keypoints[2*box_num].y
#             row[2]= keypoints[2*box_num + 1].x - keypoints[2*box_num].x
#             row[3]= keypoints[2*box_num + 1].y - keypoints[2*box_num].y
#             file_new_arr.append(row)
    file_new_arr = np.array(file_arr, ndmin=2)
    if (file_new_arr.shape[0] > 0 and file_new_arr.shape[1] > 0):
        #print(file_new_arr)
        np.savetxt(txt_name, file_new_arr, delimiter=" ", fmt="%s")
Esempio n. 25
0
    def _test_clip_remove_frac(cls, func, inplace):
        item1 = ia.Keypoint(x=5, y=1)
        item2 = ia.Keypoint(x=15, y=1)
        cbaoi = ia.KeypointsOnImage([item1, item2], shape=(10, 10, 3))

        cbaoi_reduced = func(cbaoi)

        assert len(cbaoi_reduced.items) == 1
        assert np.allclose(cbaoi_reduced.to_xy_array(), [item1.xy])
        if inplace:
            assert cbaoi_reduced is cbaoi
        else:
            assert cbaoi_reduced is not cbaoi
            assert len(cbaoi.items) == 2
Esempio n. 26
0
    def test_draw_on_image_keypoint_is_outside_of_image_and_raise_true(self):
        kps = [ia.Keypoint(x=1, y=2), ia.Keypoint(x=3, y=4)]
        kpi = ia.KeypointsOnImage(keypoints=kps + [ia.Keypoint(x=100, y=100)],
                                  shape=(5, 5, 3))
        image = np.zeros((5, 5, 3), dtype=np.uint8) + 10

        with self.assertRaises(Exception) as context:
            _ = kpi.draw_on_image(image,
                                  color=[0, 255, 0],
                                  size=1,
                                  copy=True,
                                  raise_if_out_of_image=True)

        assert "Cannot draw keypoint" in str(context.exception)
def generate_data(classes, dataset='LINEMOD'):
    if dataset=='LINEMOD':

        cam_K = np.reshape([572.4114, 0.0, 325.2611, 0.0, 573.57043, 242.04899, 0.0, 0.0, 1.0], (3, 3))
        mesh_base = '../data/LINEMOD_original/models'

        fg_image_dataset = []
        fg_mask_dataset = []
        fg_corners_dataset = []

        for cls in classes:
            mesh_name = os.path.join(mesh_base, cls + '.ply')
            mesh = trimesh.load(mesh_name)
            corners_3d = trimesh.bounds.corners(mesh.bounding_box.bounds)
            corners_3d = np.insert(corners_3d, 0, np.average(corners_3d, axis=0), axis=0)

            trainset_filename = '../data/LINEMOD_original/training_range/' + cls + '.txt'
            train_set = np.loadtxt(trainset_filename, dtype=int, ndmin=1)
            print('Number of real training images as training images in {:s} class is: {:d}'.format(cls, train_set.shape[0]))

            fg_rgbbase = '../data/LINEMOD_original/objects/' + cls + '/rgb'
            fg_maskbase = '../data/LINEMOD_original/objects/' + cls + '/mask'
            fg_posebase = '../data/LINEMOD_original/objects/' + cls + '/pose'

            fg_image_class = []
            fg_mask_class = []
            fg_corners_class = []

            for count, fg_idx in enumerate(train_set):
                fg_idx = '{:04d}'.format(fg_idx)
                fg_imname = os.path.join(fg_rgbbase, fg_idx + '.jpg')
                fg_maskname = os.path.join(fg_maskbase, fg_idx + '.png')
                fg_posename = os.path.join(fg_posebase, fg_idx + '.txt')

                pose_mat = np.loadtxt(fg_posename, dtype=float, ndmin=2)
                corners_2d_np = corners_transform(corners_3d, cam_K, pose_mat[0:3, :])
                corners_2d = [ia.Keypoint(x=point[0], y=point[1]) for point in corners_2d_np.transpose()]
                corners_2d = ia.KeypointsOnImage(corners_2d, shape=cv2.imread(fg_imname).shape)

                fg_image_class.append(cv2.imread(fg_imname))
                fg_mask_class.append(cv2.imread(fg_maskname) / 255)
                fg_corners_class.append(corners_2d)

            fg_image_dataset.append(fg_image_class)
            fg_mask_dataset.append(fg_mask_class)
            fg_corners_dataset.append(fg_corners_class)

        return fg_image_dataset, fg_mask_dataset, fg_corners_dataset
    else:
        return None
Esempio n. 28
0
    def test_draw_on_image_single_int_as_color(self):
        kps = [ia.Keypoint(x=1, y=2), ia.Keypoint(x=3, y=4)]
        kpi = ia.KeypointsOnImage(keypoints=kps, shape=(5, 5, 3))
        image = np.zeros((5, 5, 3), dtype=np.uint8) + 10

        kps_mask = np.zeros(image.shape[0:2], dtype=np.bool)
        kps_mask[2, 1] = 1
        kps_mask[4, 3] = 1

        image_kps = kpi.draw_on_image(
            image, color=255, size=1, copy=True,
            raise_if_out_of_image=False)

        assert np.all(image_kps[kps_mask] == [255, 255, 255])
        assert np.all(image_kps[~kps_mask] == [10, 10, 10])
Esempio n. 29
0
 def test_string_conversion(self):
     kps = [ia.Keypoint(x=1, y=2), ia.Keypoint(x=3, y=4)]
     kpi = ia.KeypointsOnImage(keypoints=kps, shape=(5, 5, 3))
     expected = (
         "KeypointsOnImage(["
         "Keypoint(x=1.00000000, y=2.00000000), "
         "Keypoint(x=3.00000000, y=4.00000000)"
         "], shape=(5, 5, 3)"
         ")"
     )
     assert (
         kpi.__repr__()
         == kpi.__str__()
         == expected
     )
Esempio n. 30
0
def example_keypoints():
    print("Example: Keypoints")
    import imgaug as ia
    #from imgaug import augmenters as iaa
    import augmenters as iaa
    from scipy import misc
    import random
    images = np.random.randint(0, 50, (4, 128, 128, 3), dtype=np.uint8)

    # Generate random keypoints.
    # The augmenters expect a list of imgaug.KeypointsOnImage.
    keypoints_on_images = []
    for image in images:
        height, width = image.shape[0:2]
        keypoints = []
        for _ in range(4):
            x = random.randint(0, width - 1)
            y = random.randint(0, height - 1)
            keypoints.append(ia.Keypoint(x=x, y=y))
        keypoints_on_images.append(
            ia.KeypointsOnImage(keypoints, shape=image.shape))

    seq = iaa.Sequential(
        [iaa.GaussianBlur((0, 3.0)),
         iaa.Affine(scale=(0.5, 0.7))])
    seq_det = seq.to_deterministic(
    )  # call this for each batch again, NOT only once at the start

    # augment keypoints and images
    images_aug = seq_det.augment_images(images)
    keypoints_aug = seq_det.augment_keypoints(keypoints_on_images)

    # Example code to show each image and print the new keypoints coordinates
    for img_idx, (image_before, image_after, keypoints_before,
                  keypoints_after) in enumerate(
                      zip(images, images_aug, keypoints_on_images,
                          keypoints_aug)):
        image_before = keypoints_before.draw_on_image(image_before)
        image_after = keypoints_after.draw_on_image(image_after)
        misc.imshow(np.concatenate((image_before, image_after),
                                   axis=1))  # before and after
        for kp_idx, keypoint in enumerate(keypoints_after.keypoints):
            keypoint_old = keypoints_on_images[img_idx].keypoints[kp_idx]
            x_old, y_old = keypoint_old.x, keypoint_old.y
            x_new, y_new = keypoint.x, keypoint.y
            print(
                "[Keypoints for image #%d] before aug: x=%d y=%d | after aug: x=%d y=%d"
                % (img_idx, x_old, y_old, x_new, y_new))