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)
def test_get_box_coord(self): """ Tests that bounding box successfully returns the array of the box in coordinates version. """ box = BoundingBox(15, 16, 17, 18) self.assertEqual(box.get_box_coord(), [[15, 16], [32, 16], [15, 34], [32, 34]])
def analyze_bytes(self, image_bytes): """ Analyzes an incoming set of raw bytes corresponding to an image file and returns the bounding boxes detected. Supported extensions: Windows bitmaps - *.bmp, *.dib JPEG files - *.jpeg, *.jpg, *.jpe JPEG 2000 files - *.jp2 Portable Network Graphics - *.png WebP - *.webp Portable image format - *.pbm, *.pgm, *.ppm *.pxm, *.pnm (always supported) Sun rasters - *.sr, *.ras TIFF files - *.tiff, *.tif OpenEXR Image files - *.exr Radiance HDR - *.hdr, *.pic :param image_bytes: array of bytes of an image. :return: list of bounding boxes detected inside the image. """ response = self._request("PUT", data=image_bytes, is_binary=True)['bounding_boxes'] bounding_boxes = [] for bbox in response: bbox_elements = [ int(element) for element in bbox.replace("[", "").replace( "]", "").split(",") ] bounding_boxes.append(BoundingBox(*bbox_elements)) return bounding_boxes
def test_expand(self): """ Tests the expansion of bounding box. """ box1 = BoundingBox(3, 3, 100, 100) box1.expand() # expand a default of 20% self.assertEqual( box1.get_box(), [-7, -7, 120, 120]) # If you see something weird here,
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)
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))
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))
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])
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)
def test_area(self): """ Tests that bounding box knows its area. """ box = BoundingBox(10, 10, 20, 20) self.assertEqual(box.get_area(), 400)
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])
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]")
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])
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])