Ejemplo n.º 1
0
    def test_intersection_with_other_boundingbox(self):
        """
        Tests that bounding box intersects itself with other bounding boxes.
        """
        for rect_set in self.rect_sets:
            rect1 = rect_set[0]
            rect2 = rect_set[1]
            expectedArea = rect_set[2]
            expectedPercentage = rect_set[3]

            box1 = BoundingBox(*rect1)
            box2 = BoundingBox(*rect2)

            intersection = box1.intersect_with(box2)
            number_of_common_pixels = intersection.get_area()

            # Which one is the smaller rectangle?
            area_box1 = box1.get_area()
            area_box2 = box2.get_area()

            lesser_area = min(area_box1, area_box2)
            percentage = round((number_of_common_pixels / lesser_area) * 100,
                               2)

            self.assertEqual(number_of_common_pixels, expectedArea)
            self.assertEqual(percentage, expectedPercentage)
Ejemplo n.º 2
0
    def test_crop_by_bbox(self):
        """
        Tests that the image helper can crop an image successfully by a bounding box.
        """
        with Image.open(self.subject) as im:
            image = im.convert("RGB")

        cropped = image_helper.crop_by_bbox(image, BoundingBox(0,0,15,15))

        self.assertEqual(cropped.size, (15, 15))
Ejemplo n.º 3
0
    def test_get_cropped_faces(self):
        """
        Tests that the image helper can crop an image successfully by multiple bounding boxes.
        """
        with Image.open(self.subject) as im:
            image = im.convert("RGB")

        cropped_list = image_helper.get_cropped_faces(image, [BoundingBox(0,0,15,15), BoundingBox(20,20,45,45)])

        self.assertEqual(cropped_list[0].size, (15, 15))
        self.assertEqual(cropped_list[1].size, (45, 45))
Ejemplo n.º 4
0
    def test_bounding_box_from_string(self):
        """
        Tests the creation a bounding box from a string.
        """
        bbox_string = "22,34,122,432"
        bbox = BoundingBox.from_string(bbox_string)

        self.assertEqual(bbox.get_box(), [22, 34, 122, 432])

        bbox_string = "22, 34,122,432"
        bbox = BoundingBox.from_string(bbox_string)

        self.assertEqual(bbox.get_box(), [22, 34, 122, 432])

        bbox_string = "22, 34, 122, 432"
        bbox = BoundingBox.from_string(bbox_string)

        self.assertEqual(bbox.get_box(), [22, 34, 122, 432])

        bbox_string = "   22, 34, 122, 432   "
        bbox = BoundingBox.from_string(bbox_string)

        self.assertEqual(bbox.get_box(), [22, 34, 122, 432])

        with self.assertRaises(Exception):
            bbox_string = "22,34,122 432"
            bbox = BoundingBox.from_string(bbox_string)

        with self.assertRaises(Exception):
            bbox_string = "22,34  122 432"
            bbox = BoundingBox.from_string(bbox_string)
Ejemplo n.º 5
0
def segment_image(pil_image,
                  segment_width,
                  segment_height,
                  iteration_order=HORIZONTAL):
    """
    Generator of segments of a PIL image. This generator breaks the PIL image into a
    subset of PIL images. Like in a Puzzle, it returns one by one on each iteration_order. The iteration_order order can be
     HORIZONTAL (going from TOP-LEFT to TOP-RIGHT) or VERTICAL (going from TOP-LEFT to BOTTOM-LEFT).
    :param pil_image: Image to segment.
    :param segment_width: the width of the fragment.
    :param segment_height: the height of the fragment.
    :param iteration_order: order of the iteration_order.
    :return:
    """
    width, height = pil_image.size

    horizontal_segment_count = width // segment_width + int(
        width % segment_width > 0)
    vertical_segment_count = height // segment_height + int(
        height % segment_height > 0)

    if iteration_order == VERTICAL:
        for x in range(horizontal_segment_count):
            for y in range(vertical_segment_count):
                bounding_box = BoundingBox(
                    x * segment_width, y * segment_height,
                    min(segment_width, width - x * segment_width),
                    min(segment_height, height - y * segment_height))

                yield crop_by_bbox(pil_image, bounding_box)
    else:
        for y in range(vertical_segment_count):
            for x in range(horizontal_segment_count):
                bounding_box = BoundingBox(
                    x * segment_width, y * segment_height,
                    min(segment_width, width - x * segment_width),
                    min(segment_height, height - y * segment_height))

                yield crop_by_bbox(pil_image, bounding_box)
Ejemplo n.º 6
0
 def test_get_components(self):
     """
     Tests that bounding box isolated components are accessible.
     """
     box = BoundingBox(15, 16, 17, 18)
     self.assertEqual(box.get_x(), 15)
     self.assertEqual(box.get_y(), 16)
     self.assertEqual(box.get_width(), 17)
     self.assertEqual(box.get_height(), 18)
Ejemplo n.º 7
0
    def test_fit_in_size(self):
        """
        Tests that bounding box is able to adapt itself to specified bounds.
        """
        image_size = [300, 300]

        # Case1 all out of bounds
        box1 = BoundingBox(-1, -1, 302, 302)
        box1.fit_in_size(image_size)
        self.assertEqual(box1.get_box(), [0, 0, 300, 300])

        # Case2 top left out of bounds
        box1 = BoundingBox(-1, -1, 301, 301)
        box1.fit_in_size(image_size)
        self.assertEqual(box1.get_box(), [0, 0, 300, 300])

        # Case left out of bounds
        box1 = BoundingBox(-15, 10, 100, 100)
        box1.fit_in_size(image_size)
        self.assertEqual(box1.get_box(), [0, 10, 85, 100])

        # Case top out of bounds
        box1 = BoundingBox(15, -10, 100, 100)
        box1.fit_in_size(image_size)
        self.assertEqual(box1.get_box(), [15, 0, 100, 90])

        # Case width out of bounds
        box1 = BoundingBox(15, 0, 290, 100)
        box1.fit_in_size(image_size)
        self.assertEqual(box1.get_box(), [15, 0, 285, 100])

        # Case height out of bounds
        box1 = BoundingBox(15, 15, 200, 290)
        box1.fit_in_size(image_size)
        self.assertEqual(box1.get_box(), [15, 15, 200, 285])
Ejemplo n.º 8
0
 def test_area(self):
     """
     Tests that bounding box knows its area.
     """
     box = BoundingBox(10, 10, 20, 20)
     self.assertEqual(box.get_area(), 400)
Ejemplo n.º 9
0
 def test_center(self):
     """
     Tests that bounding box knows its center point.
     """
     box = BoundingBox(10, 10, 20, 20)
     self.assertEqual(box.get_center(), [20, 20])
Ejemplo n.º 10
0
 def test_str(self):
     """
     Tests that bounding box describes itself correctly.
     """
     box = BoundingBox(15, 16, 17, 18)
     self.assertEqual(box.__str__(), "[15, 16, 17, 18]")
Ejemplo n.º 11
0
 def test_get_numpy_format(self):
     """
     Tests that bounding box numpy format is correct.
     """
     box = BoundingBox(15, 16, 17, 18)
     self.assertEqual(box.get_numpy_format(), [16, 34, 15, 32])
Ejemplo n.º 12
0
 def test_get_box(self):
     """
     Tests that bounding box successfully returns the array of the box dimensions.
     """
     box = BoundingBox(15, 16, 17, 18)
     self.assertEqual(box.get_box(), [15, 16, 17, 18])