Example #1
0
def test_denormalize_bboxes():
    bboxes = [[0.0375, 0.125, 0.25, 1.0], [0.0375, 0.125, 0.25, 1.0, 99]]
    denormalized_bboxes_1 = denormalize_bboxes(bboxes, 200, 400)
    denormalized_bboxes_2 = [
        denormalize_bbox(bboxes[0], 200, 400),
        denormalize_bbox(bboxes[1], 200, 400)
    ]
    assert denormalized_bboxes_1 == denormalized_bboxes_2
Example #2
0
def crop_bbox_by_coords(bbox, crop_coords, crop_height, crop_width, rows, cols):
    """Crop a bounding box using the provided coordinates of bottom-left and top-right corners in pixels and the
    required height and width of the crop.
    """
    bbox = denormalize_bbox(bbox, rows, cols)
    x_min, y_min, x_max, y_max = bbox
    x1, y1, x2, y2 = crop_coords
    cropped_bbox = [x_min - x1, y_min - y1, x_max - x1, y_max - y1]
    return normalize_bbox(cropped_bbox, crop_height, crop_width)
Example #3
0
 def apply_to_bbox(self,
                   bbox,
                   pad_top=0,
                   pad_bottom=0,
                   pad_left=0,
                   pad_right=0,
                   rows=0,
                   cols=0,
                   **params):
     x_min, y_min, x_max, y_max = denormalize_bbox(bbox, rows, cols)
     bbox = x_min + pad_left, y_min + pad_top, x_max + pad_left, y_max + pad_top
     return normalize_bbox(bbox, rows + pad_top + pad_bottom,
                           cols + pad_left + pad_right)
	def _convert_to_coco(self, bbox, rows, cols):
		if self.source_format == 'pascal_voc':
			(x1, y1, x2, y2), tail = bbox[:4], bbox[4:]
			return (x1, y1, x2 - x1, y2 - y1) + tail

		elif self.source_format == 'yolo':
			_bbox, tail = np.array(bbox[:4]), bbox[4:]
			if np.any((_bbox <= 0) | (_bbox > 1)):
				raise ValueError("In YOLO format all labels must be float "
				                 "and in range (0, 1]")

			x, y, width, height = np.round(denormalize_bbox(bbox, rows, cols))

			x_min = x - width / 2 + 1
			y_min = y - height / 2 + 1

			return (x_min, y_min, width, height) + tail

		return bbox
	def _convert_to_pascal_voc(self, bbox, rows, cols):
		(x, y, width, height), tail = bbox[:4], bbox[4:]

		if self.source_format == 'coco':
			return (x, y, x + width, y + height) + tail

		elif self.source_format == 'yolo':
			_bbox = np.array(bbox[:4])
			if np.any((_bbox <= 0) | (_bbox > 1)):
				raise ValueError("In YOLO format all labels must be float "
				                 "and in range (0, 1]")

			x, y, width, height = np.round(denormalize_bbox(bbox, rows, cols))

			x_min = x - width / 2 + 1
			x_max = x_min + width
			y_min = y - height / 2 + 1
			y_max = y_min + height

			return (x_min, y_min, x_max, y_max) + tail

		return bbox
Example #6
0
    def apply_to_bbox(self, bbox, **params):
        """
        Removes the bounding boxes which are covered by the applied cutout       
        :param bbox: A single bounding box coordinates in pascal_voc format
        :returns transformed bbox's coordinates
        """

        # Denormalize the bbox coordinates
        bbox = denormalize_bbox(bbox, self.img_height, self.img_width)
        x_min, y_min, x_max, y_max = tuple(map(int, bbox))

        bbox_size = (x_max - x_min) * (y_max - y_min)  # width * height
        overlapping_size = np.sum(
            (self.image[y_min:y_max, x_min:x_max, 0] == self.fill_value)
            & (self.image[y_min:y_max, x_min:x_max, 1] == self.fill_value)
            & (self.image[y_min:y_max, x_min:x_max, 2] == self.fill_value))

        # Remove the bbox if it has more than some threshold of content is inside the cutout patch
        if overlapping_size / bbox_size > self.bbox_removal_threshold:
            return normalize_bbox((0, 0, 0, 0), self.img_height,
                                  self.img_width)

        return normalize_bbox(bbox, self.img_height, self.img_width)
Example #7
0
def test_denormalize_normalize_bbox(bbox):
    denormalized_bbox = denormalize_bbox(bbox, 200, 400)
    normalized_bbox = normalize_bbox(denormalized_bbox, 200, 400)
    assert normalized_bbox == bbox
Example #8
0
def test_normalize_denormalize_bbox(bbox):
    normalized_bbox = normalize_bbox(bbox, 200, 400)
    denormalized_bbox = denormalize_bbox(normalized_bbox, 200, 400)
    assert denormalized_bbox == bbox
Example #9
0
def test_denormalize_bbox(bbox, expected):
    denormalized_bbox = denormalize_bbox(bbox, 200, 400)
    assert denormalized_bbox == expected