def boxes_postprocessing(self, data): rows, cols = data['image'].shape[:2] data['bboxes'] = filter_bboxes(data['bboxes'], rows, cols, self.min_area, self.min_visibility) if self.bbox_format == 'albumentations': check_bboxes(data['bboxes']) else: data['bboxes'] = convert_bboxes_from_albumentations(data['bboxes'], self.bbox_format, rows, cols, check_validity=True) if self.label_fields: for idx, field in enumerate(self.label_fields): field_values = [] for bbox in data['bboxes']: field_values.append(bbox[4 + idx]) data[field] = field_values data['bboxes'] = [bbox[:4] for bbox in data['bboxes']] return data
def boxes_preprocessing(self, data): if 'bboxes' not in data: raise Exception('Please name field with bounding boxes `bboxes`') if self.label_fields: for field in self.label_fields: bboxes_with_added_field = [] for bbox, field_value in zip(data['bboxes'], data[field]): bboxes_with_added_field.append(list(bbox) + [field_value]) data['bboxes'] = bboxes_with_added_field rows, cols = data['image'].shape[:2] if self.bbox_format == 'albumentations': check_bboxes(data['bboxes']) else: data['bboxes'] = convert_bboxes_to_albumentations(data['bboxes'], self.bbox_format, rows, cols, check_validity=True) return data
def test_check_bboxes_with_end_greater_that_start(): with pytest.raises(ValueError) as exc_info: check_bboxes([[0.8, 0.5, 0.7, 0.6, 99], [0.1, 0.5, 0.8, 1.0]]) message = "x_max is less than or equal to x_min for bbox [0.8, 0.5, 0.7, 0.6, 99]." assert str(exc_info.value) == message
def test_check_bboxes_with_values_greater_than_one(): with pytest.raises(ValueError) as exc_info: check_bboxes([[0.2, 0.5, 1.5, 0.6, 99], [0.1, 0.5, 0.8, 1.0]]) message = "Expected x_max for bbox [0.2, 0.5, 1.5, 0.6, 99] to be in the range [0.0, 1.0], got 1.5." assert str(exc_info.value) == message
def test_check_bboxes_with_values_less_than_zero(): with pytest.raises(ValueError) as exc_info: check_bboxes([[0.2, 0.5, 0.5, 0.6, 99], [-0.1, 0.5, 0.8, 1.0]]) message = "Expected x_min for bbox [-0.1, 0.5, 0.8, 1.0] to be in the range [0.0, 1.0], got -0.1." assert str(exc_info.value) == message
def test_check_bboxes_with_correct_values(): try: check_bboxes([[0.1, 0.5, 0.8, 1.0], [0.2, 0.5, 0.5, 0.6, 99]]) except Exception as e: pytest.fail("Unexpected Exception {!r}".format(e))