def test_normalize_bboxes(): bboxes = [[15, 25, 100, 200], [15, 25, 100, 200, 99]] normalized_bboxes_1 = normalize_bboxes(bboxes, 200, 400) normalized_bboxes_2 = [ normalize_bbox(bboxes[0], 200, 400), normalize_bbox(bboxes[1], 200, 400) ] assert normalized_bboxes_1 == normalized_bboxes_2
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)
def _convert_to_yolo(self, bbox, rows, cols): if self.source_format == 'pascal_voc': (x_min, y_min, x_max, y_max), tail = bbox[:4], bbox[4:] return normalize_bbox(( (x_max + x_min) / 2, (y_max + y_min) / 2, (x_max - x_min), (y_max - y_min) ) + tail, rows, cols) elif self.source_format == 'coco': (x, y, width, height), tail = bbox[:4], bbox[4:] return normalize_bbox(( x + width / 2, y + height / 2, width, height ) + tail, rows, cols) return bbox
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 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)
def apply_to_bboxes(self, bboxes, **params): norm_cap_bboxes = [ normalize_bbox(bbox, rows=self.img_h, cols=self.img_w) for bbox in self.cap_bboxes ] bboxes = np.array(bboxes) norm_cap_bboxes = np.array(norm_cap_bboxes) if len(bboxes) > 0 and len(norm_cap_bboxes) > 0: bbx_result = np.concatenate((bboxes, norm_cap_bboxes), axis=0) elif len(norm_cap_bboxes) > 0: bbx_result = norm_cap_bboxes else: bbx_result = bboxes return bbx_result
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
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
def test_normalize_bbox(bbox, expected): normalized_bbox = normalize_bbox(bbox, 200, 400) assert normalized_bbox == expected