Python imgaug.Keypoint() Examples

The following are 30 code examples of imgaug.Keypoint(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module imgaug , or try the search function .
Example #1
Source File: test_kps.py    From imgaug with MIT License 6 votes vote down vote up
def test_draw_on_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 = 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=[0, 255, 0], size=3, copy=True,
            raise_if_out_of_image=False)
        kps_mask_size3 = np.copy(kps_mask)
        kps_mask_size3[2-1:2+1+1, 1-1:1+1+1] = 1
        kps_mask_size3[4-1:4+1+1, 3-1:3+1+1] = 1

        assert np.all(image_kps[kps_mask_size3] == [0, 255, 0])
        assert np.all(image_kps[~kps_mask_size3] == [10, 10, 10]) 
Example #2
Source File: test_kps.py    From imgaug with MIT License 6 votes vote down vote up
def test_draw_on_image_copy_is_false(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

        image2 = np.copy(image)
        image_kps = kpi.draw_on_image(
            image2, color=[0, 255, 0], size=1, copy=False,
            raise_if_out_of_image=False)

        assert np.all(image2 == image_kps)
        assert np.all(image_kps[kps_mask] == [0, 255, 0])
        assert np.all(image_kps[~kps_mask] == [10, 10, 10])
        assert np.all(image2[kps_mask] == [0, 255, 0])
        assert np.all(image2[~kps_mask] == [10, 10, 10]) 
Example #3
Source File: check_background_augmentation.py    From imgaug with MIT License 6 votes vote down vote up
def load_images(n_batches=10, sleep=0.0):
    batch_size = 4
    astronaut = data.astronaut()
    astronaut = ia.imresize_single_image(astronaut, (64, 64))
    kps = ia.KeypointsOnImage([ia.Keypoint(x=15, y=25)], shape=astronaut.shape)
    counter = 0
    for i in range(n_batches):
        batch_images = []
        batch_kps = []
        for b in range(batch_size):
            astronaut_text = ia.draw_text(astronaut, x=0, y=0, text="%d" % (counter,), color=[0, 255, 0], size=16)
            batch_images.append(astronaut_text)
            batch_kps.append(kps)
            counter += 1
        batch = ia.Batch(
            images=np.array(batch_images, dtype=np.uint8),
            keypoints=batch_kps
        )
        yield batch
        if sleep > 0:
            time.sleep(sleep) 
Example #4
Source File: test_kps.py    From imgaug with MIT License 6 votes vote down vote up
def test_draw_on_image_alpha_is_50_percent(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=[0, 255, 0], alpha=0.5, size=1, copy=True,
            raise_if_out_of_image=False)

        bg_plus_color_at_alpha = [int(0.5*10+0),
                                  int(0.5*10+0.5*255),
                                  int(10*0.5+0)]
        assert np.all(image_kps[kps_mask] == bg_plus_color_at_alpha)
        assert np.all(image_kps[~kps_mask] == [10, 10, 10]) 
Example #5
Source File: test_kps.py    From imgaug with MIT License 6 votes vote down vote up
def test_draw_on_image_one_kp_at_bottom_right_corner(self):
        kps = [ia.Keypoint(x=1, y=2), ia.Keypoint(x=3, y=4)]
        kpi = ia.KeypointsOnImage(
            keypoints=kps + [ia.Keypoint(x=5, y=5)],
            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=[0, 255, 0], size=1, copy=True,
            raise_if_out_of_image=False)

        assert np.all(image_kps[kps_mask] == [0, 255, 0])
        assert np.all(image_kps[~kps_mask] == [10, 10, 10]) 
Example #6
Source File: test_kps.py    From imgaug with MIT License 6 votes vote down vote up
def test_draw_on_image_one_kp_at_bottom_right_corner_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=5, y=5)],
            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

        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) 
Example #7
Source File: test_kps.py    From imgaug with MIT License 5 votes vote down vote up
def test_inplaceness(self):
        kps = [ia.Keypoint(x=1, y=2), ia.Keypoint(x=3, y=4)]
        kpi = ia.KeypointsOnImage(keypoints=kps, shape=(5, 5, 3))
        kpi2 = self._func(kpi, x=0, y=0)

        if self._is_inplace:
            assert kpi is kpi2
        else:
            assert kpi is not kpi2 
Example #8
Source File: test_kps.py    From imgaug with MIT License 5 votes vote down vote up
def test_shift_by_negative_1_on_x_axis(self):
        kps = [ia.Keypoint(x=1, y=2), ia.Keypoint(x=3, y=4)]
        kpi = ia.KeypointsOnImage(keypoints=kps, shape=(5, 5, 3))

        kpi2 = self._func(kpi, x=-1)

        assert kpi2.keypoints[0].x == 1 - 1
        assert kpi2.keypoints[0].y == 2
        assert kpi2.keypoints[1].x == 3 - 1
        assert kpi2.keypoints[1].y == 4 
Example #9
Source File: test_kps.py    From imgaug with MIT License 5 votes vote down vote up
def test_shift_by_1_on_y_axis(self):
        kps = [ia.Keypoint(x=1, y=2), ia.Keypoint(x=3, y=4)]
        kpi = ia.KeypointsOnImage(keypoints=kps, shape=(5, 5, 3))

        kpi2 = self._func(kpi, y=1)

        assert kpi2.keypoints[0].x == 1
        assert kpi2.keypoints[0].y == 2 + 1
        assert kpi2.keypoints[1].x == 3
        assert kpi2.keypoints[1].y == 4 + 1 
Example #10
Source File: test_kps.py    From imgaug with MIT License 5 votes vote down vote up
def test_shift_by_negative_1_on_y_axis(self):
        kps = [ia.Keypoint(x=1, y=2), ia.Keypoint(x=3, y=4)]
        kpi = ia.KeypointsOnImage(keypoints=kps, shape=(5, 5, 3))

        kpi2 = self._func(kpi, y=-1)

        assert kpi2.keypoints[0].x == 1
        assert kpi2.keypoints[0].y == 2 - 1
        assert kpi2.keypoints[1].x == 3
        assert kpi2.keypoints[1].y == 4 - 1 
Example #11
Source File: test_kps.py    From imgaug with MIT License 5 votes vote down vote up
def test_to_keypoints_on_image(self):
        kps = ia.KeypointsOnImage([ia.Keypoint(0, 0), ia.Keypoint(1, 2)],
                                  shape=(1, 2, 3))
        kps.deepcopy = mock.MagicMock()
        kps.deepcopy.return_value = "foo"

        kps_cp = kps.to_keypoints_on_image()

        assert kps.deepcopy.call_count == 1
        assert kps_cp == "foo" 
Example #12
Source File: test_kps.py    From imgaug with MIT License 5 votes vote down vote up
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) 
Example #13
Source File: test_kps.py    From imgaug with MIT License 5 votes vote down vote up
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 
Example #14
Source File: test_kps.py    From imgaug with MIT License 5 votes vote down vote up
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) 
Example #15
Source File: test_kps.py    From imgaug with MIT License 5 votes vote down vote up
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) 
Example #16
Source File: test_kps.py    From imgaug with MIT License 5 votes vote down vote up
def test_to_distance_maps(self):
        kpi = ia.KeypointsOnImage(
            keypoints=[ia.Keypoint(x=2, y=3)],
            shape=(5, 5, 3))

        distance_map = kpi.to_distance_maps()

        expected = self._get_single_keypoint_distance_map()
        assert distance_map.shape == (5, 5, 1)
        assert np.allclose(distance_map, expected) 
Example #17
Source File: test_kps.py    From imgaug with MIT License 5 votes vote down vote up
def test_shape_is_array(self):
        image = np.zeros((10, 20, 3), dtype=np.uint8)
        kps = [ia.Keypoint(x=1, y=2), ia.Keypoint(x=3, y=4)]
        with assertWarns(self, ia.DeprecationWarning):
            kpi = ia.KeypointsOnImage(
                keypoints=kps,
                shape=image
            )
        assert kpi.shape == (10, 20, 3) 
Example #18
Source File: test_kps.py    From imgaug with MIT License 5 votes vote down vote up
def test_shift_by_1_on_x_axis(self):
        kps = [ia.Keypoint(x=1, y=2), ia.Keypoint(x=3, y=4)]
        kpi = ia.KeypointsOnImage(keypoints=kps, shape=(5, 5, 3))

        kpi2 = self._func(kpi, x=1)

        assert kpi2.keypoints[0].x == 1 + 1
        assert kpi2.keypoints[0].y == 2
        assert kpi2.keypoints[1].x == 3 + 1
        assert kpi2.keypoints[1].y == 4 
Example #19
Source File: test_kps.py    From imgaug with MIT License 5 votes vote down vote up
def test_shift_by_zero_on_both_axis(self):
        kps = [ia.Keypoint(x=1, y=2), ia.Keypoint(x=3, y=4)]
        kpi = ia.KeypointsOnImage(keypoints=kps, shape=(5, 5, 3))
        kpi2 = self._func(kpi, x=0, y=0)
        assert kpi2.keypoints[0].x == 1
        assert kpi2.keypoints[0].y == 2
        assert kpi2.keypoints[1].x == 3
        assert kpi2.keypoints[1].y == 4 
Example #20
Source File: test_kps.py    From imgaug with MIT License 5 votes vote down vote up
def test_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 = self._func(kpi, 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
        assert kpi2.shape == image.shape 
Example #21
Source File: test_kps.py    From imgaug with MIT License 5 votes vote down vote up
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) 
Example #22
Source File: test_kps.py    From imgaug with MIT License 5 votes vote down vote up
def test_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 = self._func(kpi, (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)
        ])
        assert kpi2.shape == (10, 20, 3) 
Example #23
Source File: test_kps.py    From imgaug with MIT License 5 votes vote down vote up
def test_with_list_of_keypoints(self):
        kps = [ia.Keypoint(x=1, y=2), ia.Keypoint(x=3, y=4)]
        kpsoi = ia.KeypointsOnImage(keypoints=[], shape=(10, 20, 3))
        kpsoi.items = kps
        assert np.all([
            kp_i.x == kp_j.x and kp_i.y == kp_j.y
            for kp_i, kp_j
            in zip(kpsoi.keypoints, kps)
        ]) 
Example #24
Source File: test_kps.py    From imgaug with MIT License 5 votes vote down vote up
def test_string_conversion_floats(self):
        kp = ia.Keypoint(y=1.2, x=2.7)
        assert (
            kp.__repr__()
            == kp.__str__()
            == "Keypoint(x=2.70000000, y=1.20000000)"
        ) 
Example #25
Source File: test_kps.py    From imgaug with MIT License 5 votes vote down vote up
def test_almost_equals(self, mock_cae):
        mock_cae.return_value = "foo"
        kp1 = ia.Keypoint(x=1, y=1.5)
        kp2 = ia.Keypoint(x=1, y=1.5)

        result = kp1.almost_equals(kp2, max_distance=2)

        assert result == "foo"
        mock_cae.assert_called_once_with(kp2, max_distance=2) 
Example #26
Source File: test_kps.py    From imgaug with MIT License 5 votes vote down vote up
def test_coords_almost_equals__tuple_unequal(self):
        kp1 = ia.Keypoint(x=1, y=1.5)
        kp2 = (1, 1.5+1.0)

        equal = kp1.coords_almost_equals(kp2)

        assert not equal 
Example #27
Source File: test_kps.py    From imgaug with MIT License 5 votes vote down vote up
def test_coords_almost_equals__tuple(self):
        kp1 = ia.Keypoint(x=1, y=1.5)
        kp2 = (1, 1.5)

        equal = kp1.coords_almost_equals(kp2)

        assert equal 
Example #28
Source File: test_kps.py    From imgaug with MIT License 5 votes vote down vote up
def test_coords_almost_equals__array_unequal(self):
        kp1 = ia.Keypoint(x=1, y=1.5)
        kp2 = np.float32([1, 1.5+1.0])

        equal = kp1.coords_almost_equals(kp2)

        assert not equal 
Example #29
Source File: test_kps.py    From imgaug with MIT License 5 votes vote down vote up
def test_coords_almost_equals__array(self):
        kp1 = ia.Keypoint(x=1, y=1.5)
        kp2 = np.float32([1, 1.5])

        equal = kp1.coords_almost_equals(kp2)

        assert equal 
Example #30
Source File: test_kps.py    From imgaug with MIT License 5 votes vote down vote up
def test_coords_almost_equals__distance_below_threshold(self):
        kp1 = ia.Keypoint(x=1, y=1.5)
        kp2 = ia.Keypoint(x=1, y=1.5+1e-2)

        equal = kp1.coords_almost_equals(kp2, max_distance=1e-1)

        assert equal