def test_polygons_to_masks(self): source_dataset = Dataset.from_iterable([ DatasetItem(id=1, image=np.zeros((5, 10, 3)), annotations=[ Polygon([0, 0, 4, 0, 4, 4]), Polygon([5, 0, 9, 0, 5, 5]), ]), ]) target_dataset = Dataset.from_iterable([ DatasetItem(id=1, image=np.zeros((5, 10, 3)), annotations=[ Mask( np.array([ [0, 0, 0, 0, 0, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], ]), ), Mask( np.array([ [0, 1, 1, 1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], ]), ), ]), ]) actual = transforms.PolygonsToMasks(source_dataset) compare_datasets(self, target_dataset, actual)
def __iter__(self): return iter([ DatasetItem(id=1, subset='train', image=np.ones((4, 4, 3)), annotations=[ Polygon([0, 1, 2, 1, 2, 3, 0, 3], attributes={ 'is_crowd': False }, label=2, group=1, id=1), ]), DatasetItem(id=2, subset='train', image=np.ones((4, 4, 3)), annotations=[ Mask(np.array([ [0, 1, 0, 0], [0, 1, 0, 0], [0, 1, 1, 1], [0, 0, 0, 0]], ), attributes={ 'is_crowd': True }, label=4, group=3, id=3), ]), DatasetItem(id=3, subset='val', image=np.ones((4, 4, 3)), annotations=[ Mask(np.array([ [0, 0, 0, 0], [1, 1, 1, 0], [1, 1, 0, 0], [0, 0, 0, 0]], ), attributes={ 'is_crowd': True }, label=4, group=3, id=3), ]), ])
def test_annotation_comparison(self): a = Dataset.from_iterable([ DatasetItem(id=1, annotations=[ Caption('hello'), # unmatched Caption('world', group=5), Label(2, attributes={ 'x': 1, 'y': '2', }), Bbox(1, 2, 3, 4, label=4, z_order=1, attributes={ 'score': 1.0, }), Bbox(5, 6, 7, 8, group=5), Points([1, 2, 2, 0, 1, 1], label=0, z_order=4), Mask(label=3, z_order=2, image=np.ones((2, 3))), ]), ], categories=['a', 'b', 'c', 'd']) b = Dataset.from_iterable([ DatasetItem(id=1, annotations=[ Caption('world', group=5), Label(2, attributes={ 'x': 1, 'y': '2', }), Bbox(1, 2, 3, 4, label=4, z_order=1, attributes={ 'score': 1.0, }), Bbox(5, 6, 7, 8, group=5), Bbox(5, 6, 7, 8, group=5), # unmatched Points([1, 2, 2, 0, 1, 1], label=0, z_order=4), Mask(label=3, z_order=2, image=np.ones((2, 3))), ]), ], categories=['a', 'b', 'c', 'd']) comp = ExactComparator() matched, unmatched, _, _, errors = comp.compare_datasets(a, b) self.assertEqual(6, len(matched), matched) self.assertEqual(2, len(unmatched), unmatched) self.assertEqual(0, len(errors), errors)
def test_crop_covered_segments(self): source_dataset = Dataset.from_iterable([ DatasetItem( id=1, image=np.zeros((5, 5, 3)), annotations=[ # The mask is partially covered by the polygon Mask(np.array( [[0, 0, 1, 1, 1], [0, 0, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 0, 0], [1, 1, 1, 0, 0]], ), z_order=0), Polygon([1, 1, 4, 1, 4, 4, 1, 4], z_order=1), ]), ]) target_dataset = Dataset.from_iterable([ DatasetItem(id=1, image=np.zeros((5, 5, 3)), annotations=[ Mask(np.array([[0, 0, 1, 1, 1], [0, 0, 0, 0, 1], [1, 0, 0, 0, 1], [1, 0, 0, 0, 0], [1, 1, 1, 0, 0]], ), z_order=0), Polygon([1, 1, 4, 1, 4, 4, 1, 4], z_order=1), ]), ]) actual = transforms.CropCoveredSegments(source_dataset) compare_datasets(self, target_dataset, actual)
def __iter__(self): return iter([ DatasetItem(id=1, subset='train', image=np.ones((16, 16, 3)), annotations=[ Bbox(0, 4, 4, 8, label=0, group=2, id=0, attributes={ 'occluded': False, 'username': '', } ), Polygon([0, 4, 4, 4, 5, 6], label=1, id=1, attributes={ 'occluded': True, 'username': '', 'a1': 'qwe', 'a2': True, 'a3': 123, } ), Mask(np.array([[0, 1], [1, 0], [1, 1]]), group=2, id=2, attributes={ 'occluded': False, 'username': '******' } ), Bbox(1, 2, 3, 4, group=1, id=3, attributes={ 'occluded': False, 'username': '', }), Mask(np.array([[0, 0], [0, 0], [1, 1]]), group=1, id=4, attributes={ 'occluded': True, 'username': '' } ), ] ), ])
def test_can_import_masks(self): expected_dataset = Dataset.from_iterable([ DatasetItem(id='1', subset='train', image=np.ones((2, 5, 3)), annotations=[ Mask(group=0, image=np.array([[0, 1, 1, 0, 0], [0, 0, 0, 0, 0]]), attributes={ 'index': 0, 'color': '108 225 132', 'text': 'F', 'center': '0 1' }), Mask(group=1, image=np.array([[0, 0, 0, 1, 0], [0, 0, 0, 1, 0]]), attributes={ 'index': 1, 'color': '82 174 214', 'text': 'T', 'center': '1 3' }), Mask(group=1, image=np.array([[0, 0, 0, 0, 0], [0, 0, 0, 0, 1]]), attributes={ 'index': 2, 'color': '241 73 144', 'text': 'h', 'center': '1 4' }), ] ), ]) dataset = Dataset.import_from( osp.join(DUMMY_DATASET_DIR, 'text_segmentation'), 'icdar') compare_datasets(self, expected_dataset, dataset)
def test_can_save_and_load_image_with_arbitrary_extension(self): expected = Dataset.from_iterable([ DatasetItem('q/1', image=Image(path='q/1.JPEG', data=np.zeros((4, 3, 3))), annotations=[ Mask(np.array([[0, 1, 0, 0, 0]]), label=0, attributes={'track_id': 1}), ]), DatasetItem('a/b/c/2', image=Image(path='a/b/c/2.bmp', data=np.zeros((3, 4, 3))), annotations=[ Mask(np.array([[0, 1, 0, 0, 0]]), label=0, attributes={'track_id': 1}), ]), ], categories=['a']) with TestDir() as test_dir: self._test_save_and_load(expected, partial(MotsPngConverter.convert, save_images=True), test_dir, require_images=True)
def test_boxes_to_masks(self): source_dataset = Dataset.from_iterable([ DatasetItem(id=1, image=np.zeros((5, 5, 3)), annotations=[ Bbox(0, 0, 3, 3, z_order=1), Bbox(0, 0, 3, 1, z_order=2), Bbox(0, 2, 3, 1, z_order=3), ]), ]) target_dataset = Dataset.from_iterable([ DatasetItem( id=1, image=np.zeros((5, 5, 3)), annotations=[ Mask(np.array( [[1, 1, 1, 0, 0], [1, 1, 1, 0, 0], [1, 1, 1, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]], ), z_order=1), Mask(np.array( [[1, 1, 1, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]], ), z_order=2), Mask(np.array( [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [1, 1, 1, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]], ), z_order=3), ]), ]) actual = transforms.BoxesToMasks(source_dataset) compare_datasets(self, target_dataset, actual)
def __iter__(self): yield DatasetItem(id=1, image=np.ones((1, 5, 3)), annotations=[ Mask(image=np.array([[1, 1, 0, 1, 0]]), label=self._label('label_1')), Mask(image=np.array([[0, 0, 1, 0, 1]]), label=self._label('label_2')), ])
def _load_annotations(self, item_id): item_annotations = [] class_mask = None segm_path = osp.join(self._dataset_dir, VocPath.SEGMENTATION_DIR, item_id + VocPath.SEGM_EXT) if osp.isfile(segm_path): inverse_cls_colormap = \ self._categories[AnnotationType.mask].inverse_colormap class_mask = lazy_mask(segm_path, inverse_cls_colormap) instances_mask = None inst_path = osp.join(self._dataset_dir, VocPath.INSTANCES_DIR, item_id + VocPath.SEGM_EXT) if osp.isfile(inst_path): instances_mask = lazy_mask(inst_path, _inverse_inst_colormap) if instances_mask is not None: compiled_mask = CompiledMask(class_mask, instances_mask) if class_mask is not None: label_cat = self._categories[AnnotationType.label] instance_labels = compiled_mask.get_instance_labels( class_count=len(label_cat.items)) else: instance_labels = { i: None for i in range(compiled_mask.instance_count) } for instance_id, label_id in instance_labels.items(): image = compiled_mask.lazy_extract(instance_id) attributes = {} if label_id is not None: actions = { a: False for a in label_cat.items[label_id].attributes } attributes.update(actions) item_annotations.append( Mask(image=image, label=label_id, attributes=attributes, group=instance_id)) elif class_mask is not None: log.warn("item '%s': has only class segmentation, " "instance masks will not be available" % item_id) class_mask = class_mask() classes = np.unique(class_mask) for label_id in classes: image = self._lazy_extract_mask(class_mask, label_id) item_annotations.append(Mask(image=image, label=label_id)) return item_annotations
def test_remap_labels(self): src_dataset = Dataset.from_iterable( [ DatasetItem( id=1, annotations=[ # Should be remapped Label(1), Bbox(1, 2, 3, 4, label=2), Mask(image=np.array([1]), label=3), # Should be kept Polygon([1, 1, 2, 2, 3, 4], label=4), PolyLine([1, 3, 4, 2, 5, 6]) ]) ], categories={ AnnotationType.label: LabelCategories.from_iterable('label%s' % i for i in range(5)), AnnotationType.mask: MaskCategories(colormap=mask_tools.generate_colormap(5)), }) dst_dataset = Dataset.from_iterable( [ DatasetItem(id=1, annotations=[ Label(1), Bbox(1, 2, 3, 4, label=0), Mask(image=np.array([1]), label=1), Polygon([1, 1, 2, 2, 3, 4], label=2), PolyLine([1, 3, 4, 2, 5, 6], label=None) ]), ], categories={ AnnotationType.label: LabelCategories.from_iterable(['label0', 'label9', 'label4']), AnnotationType.mask: MaskCategories( colormap={ k: v for k, v in mask_tools.generate_colormap(5).items() if k in {0, 1, 3, 4} }) }) actual = transforms.RemapLabels(src_dataset, mapping={ 'label1': 'label9', 'label2': 'label0', 'label3': 'label9', }, default='keep') compare_datasets(self, dst_dataset, actual)
def __iter__(self): return iter([ DatasetItem(id='кириллица с пробелом', image=np.ones((1, 5, 3)), annotations=[ Mask(image=np.array([[1, 0, 0, 1, 0]]), label=0), Mask(image=np.array([[0, 1, 1, 0, 1]]), label=3), ]), ])
def __iter__(self): return iter([ DatasetItem(id=1, subset='a', annotations=[ Mask(image=np.array([[0, 0, 1, 0, 0]]), label=4, group=1), Mask(image=np.array([[1, 1, 0, 0, 0]]), label=3, group=2), Mask(image=np.array([[0, 0, 0, 1, 0]]), label=3, group=3), ]), ])
def __iter__(self): return iter([ DatasetItem(id=1, subset='a', annotations=[ # overlapping masks, the first should be truncated # the second and third are different instances Mask(image=np.array([[0, 0, 0, 1, 0]]), label=3, z_order=3), Mask(image=np.array([[0, 1, 1, 1, 0]]), label=4, z_order=1), Mask(image=np.array([[1, 1, 0, 0, 0]]), label=3, z_order=2), ]), ])
def __iter__(self): return iter([ DatasetItem(id=1, subset='a', image=np.ones((1, 5, 3)), annotations=[ Mask(image=np.array([[0, 0, 0, 1, 0]]), label=0), Mask(image=np.array([[0, 1, 1, 0, 0]]), label=3), Mask(image=np.array([[1, 0, 0, 0, 1]]), label=4), ]), ])
def test_can_crop_covered_segments(self): source_dataset = Dataset.from_iterable([ DatasetItem( id=1, image=np.zeros((5, 5, 3)), annotations=[ Mask(np.array( [[0, 0, 1, 1, 1], [0, 0, 1, 1, 1], [1, 1, 0, 1, 1], [1, 1, 1, 0, 0], [1, 1, 1, 0, 0]], ), label=2, id=1, z_order=0), Polygon([1, 1, 4, 1, 4, 4, 1, 4], label=1, id=2, z_order=1), ]), ], categories=[ str(i) for i in range(10) ]) target_dataset = Dataset.from_iterable([ DatasetItem(id=1, image=np.zeros((5, 5, 3)), annotations=[ Mask(np.array([[0, 0, 1, 1, 1], [0, 0, 0, 0, 1], [1, 0, 0, 0, 1], [1, 0, 0, 0, 0], [1, 1, 1, 0, 0]], ), attributes={'is_crowd': True}, label=2, id=1, group=1), Polygon([1, 1, 4, 1, 4, 4, 1, 4], label=1, id=2, group=2, attributes={'is_crowd': False}), ], attributes={'id': 1}), ], categories=[ str(i) for i in range(10) ]) with TestDir() as test_dir: self._test_save_and_load(source_dataset, partial(CocoInstancesConverter.convert, crop_covered=True), test_dir, target_dataset=target_dataset)
def __iter__(self): return iter([ DatasetItem(id='q/1', image=Image(path='q/1.JPEG', data=np.zeros((4, 3, 3)))), DatasetItem(id='a/b/c/2', image=Image(path='a/b/c/2.bmp', data=np.ones((1, 5, 3))), annotations=[ Mask(np.array([[0, 0, 0, 1, 0]]), label=self._label('a')), Mask(np.array([[0, 1, 1, 0, 0]]), label=self._label('b')), ]) ])
def test_transform_labels(self): src_dataset = Dataset.from_iterable( [ DatasetItem(id=1, annotations=[ Label(1), Bbox(1, 2, 3, 4, label=2), Bbox(1, 3, 3, 3), Mask(image=np.array([1]), label=3), Polygon([1, 1, 2, 2, 3, 4], label=4), PolyLine([1, 3, 4, 2, 5, 6], label=5) ]) ], categories=['label%s' % i for i in range(6)]) dst_dataset = Dataset.from_iterable([ DatasetItem( id=1, annotations=[Label(1), Label(2), Label(3), Label(4), Label(5)]), ], categories=[ 'label%s' % i for i in range(6) ]) actual = transforms.AnnsToLabels(src_dataset) compare_datasets(self, dst_dataset, actual)
def test_shapes_to_boxes(self): source_dataset = Dataset.from_iterable([ DatasetItem(id=1, image=np.zeros((5, 5, 3)), annotations=[ Mask(np.array([[0, 0, 1, 1, 1], [0, 0, 0, 0, 1], [1, 0, 0, 0, 1], [1, 0, 0, 0, 0], [1, 1, 1, 0, 0]], ), id=1), Polygon([1, 1, 4, 1, 4, 4, 1, 4], id=2), PolyLine([1, 1, 2, 1, 2, 2, 1, 2], id=3), Points([2, 2, 4, 2, 4, 4, 2, 4], id=4), ]), ]) target_dataset = Dataset.from_iterable([ DatasetItem(id=1, image=np.zeros((5, 5, 3)), annotations=[ Bbox(0, 0, 4, 4, id=1), Bbox(1, 1, 3, 3, id=2), Bbox(1, 1, 1, 1, id=3), Bbox(2, 2, 2, 2, id=4), ]), ]) actual = transforms.ShapesToBoxes(source_dataset) compare_datasets(self, target_dataset, actual)
def test_can_convert_polygons_to_mask(self): source_dataset = Dataset.from_iterable([ DatasetItem(id=1, image=np.zeros((6, 10, 3)), annotations=[ Polygon([0, 0, 4, 0, 4, 4], label=3, id=4, group=4), Polygon([5, 0, 9, 0, 5, 5], label=3, id=4, group=4), ] ), ], categories=[str(i) for i in range(10)]) target_dataset = Dataset.from_iterable([ DatasetItem(id=1, image=np.zeros((6, 10, 3)), annotations=[ Mask(np.array([ [0, 1, 1, 1, 0, 1, 1, 1, 1, 0], [0, 0, 1, 1, 0, 1, 1, 1, 0, 0], [0, 0, 0, 1, 0, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], # only internal fragment (without the border), # but not everywhere... ), attributes={ 'is_crowd': True }, label=3, id=4, group=4), ], attributes={'id': 1} ), ], categories=[str(i) for i in range(10)]) with TestDir() as test_dir: self._test_save_and_load(source_dataset, partial(CocoInstancesConverter.convert, segmentation_mode='mask'), test_dir, target_dataset=target_dataset)
def test_mask_to_polygons(self): source = Dataset.from_iterable([ DatasetItem(id=1, image=np.zeros((5, 10, 3)), annotations=[ Mask( np.array([ [0, 1, 1, 1, 0, 1, 1, 1, 1, 0], [0, 0, 1, 1, 0, 1, 1, 1, 0, 0], [0, 0, 0, 1, 0, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], ]), ), ]), ]) expected = Dataset.from_iterable([ DatasetItem(id=1, image=np.zeros((5, 10, 3)), annotations=[ Polygon([3.0, 2.5, 1.0, 0.0, 3.5, 0.0, 3.0, 2.5]), Polygon([5.0, 3.5, 4.5, 0.0, 8.0, 0.0, 5.0, 3.5]), ]), ]) actual = transforms.MasksToPolygons(source) compare_datasets(self, expected, actual)
def test_can_save_masks(self): test_dataset = Dataset.from_iterable( [ DatasetItem(id=1, subset='train', image=np.ones((4, 5, 3)), annotations=[ Mask(image=np.array([ [1, 0, 0, 1], [0, 1, 1, 0], [0, 1, 1, 0], [1, 0, 0, 1], ]), label=1), ], attributes={'source_id': ''}), ], categories={ AnnotationType.label: LabelCategories.from_iterable('label_' + str(label) for label in range(10)), }) with TestDir() as test_dir: self._test_save_and_load( test_dataset, partial(TfDetectionApiConverter.convert, save_masks=True), test_dir)
def test_can_import_mots_annotations_with_splited_masks(self): #https://github.com/openvinotoolkit/cvat/issues/3360 format_name = 'MOTS PNG 1.0' source_dataset = Dataset.from_iterable([ DatasetItem(id='image_0', annotations=[ Mask(np.array([[1, 1, 1, 0, 1, 1, 1]] * 5), label=0, attributes={'track_id': 0}) ]) ], categories=['label_0']) with tempfile.TemporaryDirectory() as temp_dir: dataset_dir = osp.join(temp_dir, 'dataset') source_dataset.export(dataset_dir, 'mots_png') dataset_path = osp.join(temp_dir, 'annotations.zip') make_zip_archive(dataset_dir, dataset_path) images = self._generate_task_images(1, size=(5, 7)) task = { 'name': 'test', "overlap": 0, "segment_size": 100, "labels": [{ 'name': 'label_0' }] } task.update() task = self._create_task(task, images) dm.task.import_task_annotations(task['id'], dataset_path, format_name) self._test_can_import_annotations(task, format_name)
def test_can_import_instances(self): expected_dataset = Dataset.from_iterable([ DatasetItem(id='000000000001', image=np.ones((10, 5, 3)), subset='val', attributes={'id': 1}, annotations=[ Polygon([0, 0, 1, 0, 1, 2, 0, 2], label=0, id=1, group=1, attributes={ 'is_crowd': False, 'x': 1, 'y': 'hello' }), Mask(np.array([[1, 0, 0, 1, 0]] * 5 + [[1, 1, 1, 1, 0]] * 5), label=0, id=2, group=2, attributes={'is_crowd': True}), ]), ], categories=[ 'TEST', ]) dataset = Dataset.import_from( osp.join(DUMMY_DATASET_DIR, 'coco_instances'), 'coco') compare_datasets(self, expected_dataset, dataset)
def __iter__(self): return iter([ DatasetItem( id='2007_000001', subset='train', image=Image(path='2007_000001.jpg', size=(10, 20)), annotations=[ Label(self._label(l.name)) for l in VOC.VocLabel if l.value % 2 == 1 ] + [ Bbox( 1, 2, 2, 2, label=self._label('cat'), attributes={ 'pose': VOC.VocPose(1).name, 'truncated': True, 'difficult': False, 'occluded': False, }, id=1, group=1, ), Bbox( 4, 5, 2, 2, label=self._label('person'), attributes={ 'truncated': False, 'difficult': False, 'occluded': False, **{ a.name: a.value % 2 == 1 for a in VOC.VocAction } }, id=2, group=2, ), Bbox(5.5, 6, 2, 2, label=self._label(VOC.VocBodyPart(1).name), group=2), Mask( image=np.ones([5, 10]), label=self._label(VOC.VocLabel(2).name), group=1, ), ]), DatasetItem(id='2007_000002', subset='test', image=np.ones((10, 20, 3))), ])
def test_merge_instance_segments(self): source_dataset = Dataset.from_iterable([ DatasetItem(id=1, image=np.zeros((5, 5, 3)), annotations=[ Mask(np.array([ [0, 0, 1, 1, 1], [0, 0, 0, 0, 1], [1, 0, 0, 0, 1], [1, 0, 0, 0, 0], [1, 1, 1, 0, 0]], ), z_order=0, group=1), Polygon([1, 1, 4, 1, 4, 4, 1, 4], z_order=1, group=1), Polygon([0, 0, 0, 2, 2, 2, 2, 0], z_order=1), ] ), ]) target_dataset = Dataset.from_iterable([ DatasetItem(id=1, image=np.zeros((5, 5, 3)), annotations=[ Mask(np.array([ [0, 0, 1, 1, 1], [0, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 0], [1, 1, 1, 0, 0]], ), z_order=0, group=1), Mask(np.array([ [1, 1, 0, 0, 0], [1, 1, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]], ), z_order=1), ] ), ]) actual = transforms.MergeInstanceSegments(source_dataset, include_polygons=True) compare_datasets(self, target_dataset, actual)
def __iter__(self): return iter([ DatasetItem( id=1, image=np.zeros((5, 5, 3)), annotations=[ Mask(np.array([[0, 0, 1, 1, 1], [0, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 0], [1, 1, 1, 0, 0]], ), z_order=0, group=1), Mask(np.array([[1, 1, 0, 0, 0], [1, 1, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]], ), z_order=1), ]), ])
def __iter__(self): return iter([ DatasetItem(id=100, subset='train', image=np.ones((10, 6, 3)), annotations=[ Caption('hello', id=1), Caption('world', id=2, group=5), Label(2, id=3, attributes={ 'x': 1, 'y': '2', }), Bbox(1, 2, 3, 4, label=4, id=4, z_order=1, attributes={ 'score': 1.0, }), Bbox(5, 6, 7, 8, id=5, group=5), Points([1, 2, 2, 0, 1, 1], label=0, id=5, z_order=4), Mask(label=3, id=5, z_order=2, image=np.ones((2, 3))), ]), DatasetItem(id=21, subset='train', annotations=[ Caption('test'), Label(2), Bbox(1, 2, 3, 4, 5, id=42, group=42) ]), DatasetItem(id=2, subset='val', annotations=[ PolyLine([1, 2, 3, 4, 5, 6, 7, 8], id=11, z_order=1), Polygon([1, 2, 3, 4, 5, 6, 7, 8], id=12, z_order=4), ]), DatasetItem(id=42, subset='test', attributes={ 'a1': 5, 'a2': '42' }), DatasetItem(id=42), DatasetItem(id=43, image=Image(path='1/b/c.qq', size=(2, 4))), ])
def test_can_save_masks(self): source = Dataset.from_iterable([ DatasetItem(id=1, subset='a', image=np.ones((5, 1)), annotations=[ # overlapping masks, the first should be truncated # the first and third are different instances Mask(np.array([[0, 0, 0, 1, 0]]), label=3, z_order=3, attributes={'track_id': 1}), Mask(np.array([[0, 1, 1, 1, 0]]), label=2, z_order=1, attributes={'track_id': 2}), Mask(np.array([[1, 1, 0, 0, 0]]), label=3, z_order=2, attributes={'track_id': 3}), ]), DatasetItem(id=2, subset='a', image=np.ones((5, 1)), annotations=[ Mask(np.array([[1, 0, 0, 0, 0]]), label=3, attributes={'track_id': 2}), ]), DatasetItem(id=3, subset='b', image=np.ones((5, 1)), annotations=[ Mask(np.array([[0, 1, 0, 0, 0]]), label=0, attributes={'track_id': 1}), ]), ], categories=['a', 'b', 'c', 'd']) target = Dataset.from_iterable([ DatasetItem(id=1, subset='a', image=np.ones((5, 1)), annotations=[ Mask(np.array([[0, 0, 0, 1, 0]]), label=3, attributes={'track_id': 1}), Mask(np.array([[0, 0, 1, 0, 0]]), label=2, attributes={'track_id': 2}), Mask(np.array([[1, 1, 0, 0, 0]]), label=3, attributes={'track_id': 3}), ]), DatasetItem(id=2, subset='a', image=np.ones((5, 1)), annotations=[ Mask(np.array([[1, 0, 0, 0, 0]]), label=3, attributes={'track_id': 2}), ]), DatasetItem(id=3, subset='b', image=np.ones((5, 1)), annotations=[ Mask(np.array([[0, 1, 0, 0, 0]]), label=0, attributes={'track_id': 1}), ]), ], categories=['a', 'b', 'c', 'd']) with TestDir() as test_dir: self._test_save_and_load(source, partial(MotsPngConverter.convert, save_images=True), test_dir, target_dataset=target)
def __iter__(self): return iter([ DatasetItem(id=1, subset='a', annotations=[ Mask(image=bit(x, y, shape=[10, 10]), label=self._label(VOC.VocLabel(3).name), group=10 * y + x + 1 ) for y in range(10) for x in range(10) ]), ])