Python imgaug.BoundingBox() Examples

The following are 30 code examples of imgaug.BoundingBox(). 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: check_readme_examples.py    From imgaug with MIT License 6 votes vote down vote up
def example_augment_images_and_bounding_boxes():
    print("Example: Augment Images and Bounding Boxes")
    import numpy as np
    import imgaug as ia
    import imgaug.augmenters as iaa

    images = np.zeros((2, 128, 128, 3), dtype=np.uint8)  # two example images
    images[:, 64, 64, :] = 255
    bbs = [
        [ia.BoundingBox(x1=10.5, y1=15.5, x2=30.5, y2=50.5)],
        [ia.BoundingBox(x1=10.5, y1=20.5, x2=50.5, y2=50.5),
         ia.BoundingBox(x1=40.5, y1=75.5, x2=70.5, y2=100.5)]
    ]

    seq = iaa.Sequential([
        iaa.AdditiveGaussianNoise(scale=0.05*255),
        iaa.Affine(translate_px={"x": (1, 5)})
    ])

    images_aug, bbs_aug = seq(images=images, bounding_boxes=bbs) 
Example #2
Source File: test_bbs.py    From imgaug with MIT License 6 votes vote down vote up
def test_is_out_of_image(self):
        bb = ia.BoundingBox(y1=10, x1=20, y2=30, x2=40, label=None)

        subtests = [
            ((100, 100, 3), True, True, False),
            ((100, 100, 3), False, True, False),
            ((100, 100, 3), True, False, False),
            ((20, 100, 3), True, True, True),
            ((20, 100, 3), False, True, False),
            ((20, 100, 3), True, False, True),
            ((100, 30, 3), True, True, True),
            ((100, 30, 3), False, True, False),
            ((100, 30, 3), True, False, True),
            ((1, 1, 3), True, True, True),
            ((1, 1, 3), False, True, True),
            ((1, 1, 3), True, False, False)
        ]

        for shape, partly, fully, expected in subtests:
            with self.subTest(shape=shape, partly=partly, fully=fully):
                observed = bb.is_out_of_image(shape,
                                              partly=partly, fully=fully)
                assert observed is expected 
Example #3
Source File: test_bbs.py    From imgaug with MIT License 6 votes vote down vote up
def test_draw_label_on_image_mocked(self, mock_drawer):
        mock_drawer.return_value = mock_drawer
        image = np.zeros((10, 10, 3), dtype=np.uint8)
        bb = ia.BoundingBox(y1=1, x1=1, y2=3, x2=3)

        result = bb.draw_label_on_image(image)

        kwargs = mock_drawer.call_args_list[0][1]
        assert kwargs["color"] == (0, 255, 0)
        assert kwargs["color_text"] is None
        assert kwargs["color_bg"] is None
        assert np.isclose(kwargs["alpha"], 1.0)
        assert kwargs["size"] == 1
        assert kwargs["size_text"] == 20
        assert kwargs["height"] == 30
        assert kwargs["raise_if_out_of_image"] is False

        assert mock_drawer.draw_on_image.call_count == 1 
Example #4
Source File: test_bbs.py    From imgaug with MIT License 6 votes vote down vote up
def test_draw_label_on_image_mocked_inplace(self, mock_drawer):
        mock_drawer.return_value = mock_drawer
        image = np.zeros((10, 10, 3), dtype=np.uint8)
        bb = ia.BoundingBox(y1=1, x1=1, y2=3, x2=3)

        result = bb.draw_label_on_image(image, copy=False)

        kwargs = mock_drawer.call_args_list[0][1]
        assert kwargs["color"] == (0, 255, 0)
        assert kwargs["color_text"] is None
        assert kwargs["color_bg"] is None
        assert np.isclose(kwargs["alpha"], 1.0)
        assert kwargs["size"] == 1
        assert kwargs["size_text"] == 20
        assert kwargs["height"] == 30
        assert kwargs["raise_if_out_of_image"] is False

        assert mock_drawer.draw_on_image_.call_count == 1 
Example #5
Source File: test_data.py    From imgaug with MIT License 6 votes vote down vote up
def test_non_square_vs_square(self):
        kpsoi = iadata.quokka_keypoints()
        img = iadata.quokka()

        patches = []
        for kp in kpsoi.keypoints:
            bb = ia.BoundingBox(x1=kp.x-1, x2=kp.x+2, y1=kp.y-1, y2=kp.y+2)
            patches.append(bb.extract_from_image(img))

        img_square = iadata.quokka(extract="square")
        kpsoi_square = iadata.quokka_keypoints(extract="square")

        assert len(kpsoi.keypoints) == len(kpsoi_square.keypoints)
        assert kpsoi_square.shape == (643, 643, 3)
        for kp, patch in zip(kpsoi_square.keypoints, patches):
            bb = ia.BoundingBox(x1=kp.x-1, x2=kp.x+2, y1=kp.y-1, y2=kp.y+2)
            patch_square = bb.extract_from_image(img_square)
            assert np.average(
                np.abs(
                    patch.astype(np.float32)
                    - patch_square.astype(np.float32)
                )
            ) < 1.0 
Example #6
Source File: test_bbs.py    From imgaug with MIT License 5 votes vote down vote up
def test_intersection_of_non_overlapping_bbs(self):
        bb1 = ia.BoundingBox(y1=10, x1=20, y2=30, x2=40)
        bb2 = ia.BoundingBox(y1=10, x1=41, y2=30, x2=61)

        bb_inter = bb1.intersection(bb2, default=False)

        assert bb_inter is False 
Example #7
Source File: check_bb_augmentation.py    From imgaug with MIT License 5 votes vote down vote up
def main():
    image = data.astronaut()
    image = ia.imresize_single_image(image, (HEIGHT, WIDTH))

    kps = []
    for y in range(NB_ROWS):
        ycoord = BB_Y1 + int(y * (BB_Y2 - BB_Y1) / (NB_COLS - 1))
        for x in range(NB_COLS):
            xcoord = BB_X1 + int(x * (BB_X2 - BB_X1) / (NB_ROWS - 1))
            kp = (xcoord, ycoord)
            kps.append(kp)
    kps = set(kps)
    kps = [ia.Keypoint(x=xcoord, y=ycoord) for (xcoord, ycoord) in kps]
    kps = ia.KeypointsOnImage(kps, shape=image.shape)

    bb = ia.BoundingBox(x1=BB_X1, x2=BB_X2, y1=BB_Y1, y2=BB_Y2)
    bbs = ia.BoundingBoxesOnImage([bb], shape=image.shape)

    seq = iaa.Affine(rotate=45)
    seq_det = seq.to_deterministic()
    image_aug = seq_det.augment_image(image)
    kps_aug = seq_det.augment_keypoints([kps])[0]
    bbs_aug = seq_det.augment_bounding_boxes([bbs])[0]

    image_before = np.copy(image)
    image_before = kps.draw_on_image(image_before)
    image_before = bbs.draw_on_image(image_before)

    image_after = np.copy(image_aug)
    image_after = kps_aug.draw_on_image(image_after)
    image_after = bbs_aug.draw_on_image(image_after)

    ia.imshow(np.hstack([image_before, image_after]))
    imageio.imwrite("bb_aug.jpg", np.hstack([image_before, image_after])) 
Example #8
Source File: test_bbs.py    From imgaug with MIT License 5 votes vote down vote up
def test_extract_from_image_bb_partially_out_of_image(self):
        image = iarandom.RNG(1234).integers(0, 255, size=(10, 10, 3))

        bb = ia.BoundingBox(y1=8, y2=11, x1=8, x2=11)
        image_sub = bb.extract_from_image(image)

        image_pad = np.pad(
            image,
            ((0, 1), (0, 1), (0, 0)),
            mode="constant",
            constant_values=0)  # pad at bottom and right each 1px (black)
        assert np.array_equal(image_sub, image_pad[8:11, 8:11, :]) 
Example #9
Source File: test_bbs.py    From imgaug with MIT License 5 votes vote down vote up
def test_draw_on_image_label_is_str(self):
        # if label is None, no label box should be drawn, only the rectangle
        # box below the label
        image = np.zeros((100, 70, 3), dtype=np.uint8)
        bb = ia.BoundingBox(y1=40, x1=10, y2=50, x2=40, label="Foo")

        image_drawn = bb.draw_on_image(image)

        expected = bb.draw_box_on_image(image)
        expected = bb.draw_label_on_image(expected)
        assert np.array_equal(image_drawn, expected) 
Example #10
Source File: test_bbs.py    From imgaug with MIT License 5 votes vote down vote up
def test_draw_box_on_image_raise_true_and_bb_fully_outside_image(self):
        image, bb, bb_mask = self._get_standard_draw_box_on_image_vars()
        bb = ia.BoundingBox(y1=-5, x1=-5, y2=-1, x2=-1)

        with self.assertRaises(Exception) as context:
            _ = bb.draw_box_on_image(
                image, color=[255, 255, 255], alpha=1.0, size=1, copy=True,
                raise_if_out_of_image=True)

        assert "Cannot draw bounding box" in str(context.exception) 
Example #11
Source File: test_bbs.py    From imgaug with MIT License 5 votes vote down vote up
def test_draw_box_on_image_raise_true_but_bb_partially_inside_image(self):
        image, bb, bb_mask = self._get_standard_draw_box_on_image_vars()
        bb = ia.BoundingBox(y1=-1, x1=-1, y2=1, x2=1)

        _ = bb.draw_box_on_image(
            image, color=[255, 255, 255], alpha=1.0, size=1, copy=True,
            raise_if_out_of_image=True) 
Example #12
Source File: test_bbs.py    From imgaug with MIT License 5 votes vote down vote up
def test_draw_box_on_image_bb_outside_of_image_and_very_small(self):
        image, bb, bb_mask = self._get_standard_draw_box_on_image_vars()
        bb = ia.BoundingBox(y1=-1, x1=-1, y2=1, x2=1)
        bb_mask = np.zeros(image.shape[0:2], dtype=np.bool)
        bb_mask[0:1+1, 1] = True
        bb_mask[1, 0:1+1] = True

        image_bb = bb.draw_box_on_image(
            image, color=[255, 255, 255], alpha=1.0, size=1, copy=True,
            raise_if_out_of_image=False)

        assert np.all(image_bb[bb_mask] == [255, 255, 255])
        assert np.all(image_bb[~bb_mask] == [0, 0, 0]) 
Example #13
Source File: test_bbs.py    From imgaug with MIT License 5 votes vote down vote up
def test_extract_from_image(self):
        image = iarandom.RNG(1234).integers(0, 255, size=(10, 10, 3))
        bb = ia.BoundingBox(y1=1, y2=3, x1=1, x2=3)
        image_sub = bb.extract_from_image(image)
        assert np.array_equal(image_sub, image[1:3, 1:3, :]) 
Example #14
Source File: test_bbs.py    From imgaug with MIT License 5 votes vote down vote up
def test_compute_out_of_image_area__partially_ooi(self):
        bb = ia.BoundingBox(y1=10, x1=-20, y2=30, x2=40)
        image_shape = (100, 200, 3)
        area_ooi = bb.compute_out_of_image_area(image_shape)
        assert np.isclose(area_ooi, (0-(-20))*(30-10)) 
Example #15
Source File: test_bbs.py    From imgaug with MIT License 5 votes vote down vote up
def test_compute_out_of_image_area__fully_inside(self):
        bb = ia.BoundingBox(y1=10.1, x1=20.2, y2=30.3, x2=40.4)
        image_shape = (100, 200, 3)
        area_ooi = bb.compute_out_of_image_area(image_shape)
        assert np.isclose(area_ooi, 0.0) 
Example #16
Source File: test_bbs.py    From imgaug with MIT License 5 votes vote down vote up
def test_iou_of_partially_overlapping_bbs(self):
        bb1 = ia.BoundingBox(y1=10, x1=10, y2=20, x2=20)
        bb2 = ia.BoundingBox(y1=15, x1=15, y2=25, x2=25)

        iou = bb1.iou(bb2)

        area_union = 10 * 10 + 10 * 10 - 5 * 5
        area_intersection = 5 * 5
        iou_expected = area_intersection / area_union
        assert np.isclose(iou, iou_expected) 
Example #17
Source File: test_bbs.py    From imgaug with MIT License 5 votes vote down vote up
def test_iou_of_non_overlapping_bbs(self):
        bb1 = ia.BoundingBox(y1=10, x1=20, y2=30, x2=40)
        bb2 = ia.BoundingBox(y1=10, x1=41, y2=30, x2=61)

        iou = bb1.iou(bb2)

        assert np.isclose(iou, 0.0) 
Example #18
Source File: test_bbs.py    From imgaug with MIT License 5 votes vote down vote up
def test_union(self):
        bb1 = ia.BoundingBox(y1=10, x1=20, y2=30, x2=40)
        bb2 = ia.BoundingBox(y1=10, x1=39, y2=30, x2=59)

        bb_union = bb1.union(bb2)

        assert bb_union.x1 == 20
        assert bb_union.x2 == 59
        assert bb_union.y1 == 10
        assert bb_union.y2 == 30 
Example #19
Source File: test_bbs.py    From imgaug with MIT License 5 votes vote down vote up
def test_height_floats(self):
        bb = ia.BoundingBox(y1=10.1, x1=20.2, y2=30.3, x2=40.4)
        assert np.isclose(bb.height, 30.3 - 10.1) 
Example #20
Source File: test_bbs.py    From imgaug with MIT License 5 votes vote down vote up
def test_intersection(self):
        bb1 = ia.BoundingBox(y1=10, x1=20, y2=30, x2=40)
        bb2 = ia.BoundingBox(y1=10, x1=39, y2=30, x2=59)

        bb_inter = bb1.intersection(bb2)

        assert bb_inter.x1 == 39
        assert bb_inter.x2 == 40
        assert bb_inter.y1 == 10
        assert bb_inter.y2 == 30 
Example #21
Source File: test_bbs.py    From imgaug with MIT License 5 votes vote down vote up
def test_xy_int_properties_floats(self):
        bb = ia.BoundingBox(y1=10.1, x1=20.2, y2=30.6, x2=40.7)
        assert bb.y1_int == 10
        assert bb.x1_int == 20
        assert bb.y2_int == 31
        assert bb.x2_int == 41 
Example #22
Source File: test_bbs.py    From imgaug with MIT License 5 votes vote down vote up
def test_extract_from_image_bb_partially_out_of_image_no_channels(self):
        image = iarandom.RNG(1234).integers(0, 255, size=(10, 10))

        bb = ia.BoundingBox(y1=8, y2=11, x1=8, x2=11)
        image_sub = bb.extract_from_image(image)

        image_pad = np.pad(
            image,
            ((0, 1), (0, 1)),
            mode="constant",
            constant_values=0)  # pad at bottom and right each 1px (black)
        assert np.array_equal(image_sub, image_pad[8:11, 8:11]) 
Example #23
Source File: test_bbs.py    From imgaug with MIT License 5 votes vote down vote up
def test_width(self):
        bb = ia.BoundingBox(y1=10, x1=20, y2=30, x2=40)
        assert bb.width == 40 - 20 
Example #24
Source File: test_bbs.py    From imgaug with MIT License 5 votes vote down vote up
def test_contains(self):
        bb = ia.BoundingBox(y1=1, x1=2, y2=1+4, x2=2+5, label=None)
        assert bb.contains(ia.Keypoint(x=2.5, y=1.5)) is True
        assert bb.contains(ia.Keypoint(x=2, y=1)) is True
        assert bb.contains(ia.Keypoint(x=0, y=0)) is False 
Example #25
Source File: test_bbs.py    From imgaug with MIT License 5 votes vote down vote up
def test_area_floats(self):
        bb = ia.BoundingBox(y1=10.1, x1=20.2, y2=30.3, x2=40.4)
        assert np.isclose(bb.area, (30.3-10.1) * (40.4-20.2)) 
Example #26
Source File: test_bbs.py    From imgaug with MIT License 5 votes vote down vote up
def test_center_y_floats(self):
        bb = ia.BoundingBox(y1=10.1, x1=20.2, y2=30.3, x2=40.4)
        expected = 10.1 + (30.3 - 10.1)/2
        assert np.isclose(bb.center_y, expected) 
Example #27
Source File: test_bbs.py    From imgaug with MIT License 5 votes vote down vote up
def test_center_y(self):
        bb = ia.BoundingBox(y1=10, x1=20, y2=30, x2=40)
        expected = 10 + (30 - 10)/2
        assert np.isclose(bb.center_y, expected) 
Example #28
Source File: test_bbs.py    From imgaug with MIT License 5 votes vote down vote up
def test_center_x_floats(self):
        bb = ia.BoundingBox(y1=10.1, x1=20.2, y2=30.3, x2=40.4)
        expected = 20.2 + (40.4 - 20.2)/2
        assert np.isclose(bb.center_x, expected) 
Example #29
Source File: test_bbs.py    From imgaug with MIT License 5 votes vote down vote up
def test_center_x(self):
        bb = ia.BoundingBox(y1=10, x1=20, y2=30, x2=40)
        expected = 20 + (40 - 20)/2
        assert np.isclose(bb.center_x, expected) 
Example #30
Source File: test_bbs.py    From imgaug with MIT License 5 votes vote down vote up
def test_width_floats(self):
        bb = ia.BoundingBox(y1=10.1, x1=20.2, y2=30.3, x2=40.4)
        assert np.isclose(bb.width, 40.4 - 20.2)