def __iter__(self): return iter([ DatasetItem(id='a/1', subset='a', annotations=[ Bbox(2, 3, 4, 5, label=2, id=1, group=1, attributes={ 'truncated': False, 'difficult': False, 'occluded': True, } ), Bbox(2, 3, 4, 5, label=3, id=2, group=2, attributes={ 'truncated': True, 'difficult': False, 'occluded': False, }, ), ]), DatasetItem(id=2, subset='b', annotations=[ Bbox(5, 4, 6, 5, label=3, id=1, group=1, attributes={ 'truncated': False, 'difficult': True, 'occluded': False, }, ), ]), ])
def test_can_save_dataset_with_no_labels(self): source_dataset = Dataset.from_iterable([ DatasetItem(id='no_label/1', image=np.ones((8, 8, 3)), annotations=[ Bbox(0, 2, 4, 2), Points([ 4.23, 4.32, 5.34, 4.45, 3.54, 3.56, 4.52, 3.51, 4.78, 3.34 ]), ]), DatasetItem(id='no_label/2', image=np.ones((8, 8, 3)), annotations=[ Bbox(2, 2, 4, 2), ]), ], categories=[]) with TestDir() as test_dir: VggFace2Converter.convert(source_dataset, test_dir, save_images=False) parsed_dataset = Dataset.import_from(test_dir, 'vgg_face2') compare_datasets(self, source_dataset, parsed_dataset)
def __iter__(self): return iter([ DatasetItem(id='a/b/1', subset='a', annotations=[ Bbox(2, 3, 4, 5, label=2, id=1, group=1, attributes={ 'truncated': True, 'difficult': False, 'occluded': False, # no attributes here in the label categories } ), Bbox(5, 4, 3, 2, label=self._label('person'), id=2, group=2, attributes={ 'truncated': True, 'difficult': False, 'occluded': False, VOC.VocAction(1).name: True, VOC.VocAction(2).name: True, **{ a.name: False for a in VOC.VocAction if a.value not in {1, 2} } } ), ]), ])
def test_can_import_detection(self): source_dataset = Dataset.from_iterable([ DatasetItem(id='000030_10', subset='training', image=np.ones((10, 10, 3)), annotations=[ Bbox(0, 1, 2, 2, label=0, id=0, attributes={'truncated': True, 'occluded': False}), Bbox(0, 5, 1, 3, label=1, id=1, attributes={'truncated': False, 'occluded': False}), ]), DatasetItem(id='000030_11', subset='training', image=np.ones((10, 10, 3)), annotations=[ Bbox(0, 0, 2, 2, label=1, id=0, attributes={'truncated': True, 'occluded': True}), Bbox(4, 4, 2, 2, label=1, id=1, attributes={'truncated': False, 'occluded': False}), Bbox(6, 6, 1, 3, label=1, id=2, attributes={'truncated': False, 'occluded': True}), ]), ], categories=['Truck', 'Van']) parsed_dataset = Dataset.import_from( osp.join(DUMMY_DATASET_DIR, 'kitti_detection'), 'kitti') compare_datasets(self, source_dataset, parsed_dataset)
def test_can_import(self): expected_dataset = Dataset.from_iterable( [ DatasetItem(id='img0001', subset='test', image=np.ones((5, 5, 3)), annotations=[Bbox(10, 5, 10, 2, label=0)]), DatasetItem(id='img0002', subset='test', image=np.ones((5, 5, 3)), annotations=[ Bbox(11.5, 12, 10.2, 20.5, label=1), ]), DatasetItem(id='img0003', subset='train', image=np.ones((5, 5, 3)), annotations=[ Bbox(6.7, 10.3, 3.3, 4.7, label=0), Bbox(13.7, 20.2, 31.9, 43.4, label=1), ]), DatasetItem(id='img0004', subset='train', image=np.ones((5, 5, 3)), annotations=[ Bbox(1, 2, 1, 2, label=0), ]) ], categories=['helmet', 'person']) dataset = Dataset.import_from(DUMMY_DATASET_DIR, 'vott_csv') compare_datasets(self, expected_dataset, dataset, require_images=True)
def test_can_find_bbox_with_wrong_label(self): detections = 3 class_count = 2 item1 = DatasetItem(id=1, annotations=[ Bbox(i * 10, 10, 10, 10, label=i) for i in range(detections) ]) item2 = DatasetItem(id=2, annotations=[ Bbox(i * 10, 10, 10, 10, label=(i + 1) % class_count) for i in range(detections) ]) iou_thresh = 0.5 comp = DistanceComparator(iou_threshold=iou_thresh) result = comp.match_boxes(item1, item2) matches, mispred, a_greater, b_greater = result self.assertEqual(len(item1.annotations), len(mispred)) self.assertEqual(0, len(a_greater)) self.assertEqual(0, len(b_greater)) self.assertEqual(0, len(matches)) for a_bbox, b_bbox in mispred: self.assertLess(iou_thresh, a_bbox.iou(b_bbox)) self.assertEqual((a_bbox.label + 1) % class_count, b_bbox.label)
def test_can_merge_classes(self): source0 = Dataset.from_iterable([ DatasetItem(1, annotations=[ Label(0), Label(1), Bbox(0, 0, 1, 1, label=1), ]), ], categories=['a', 'b']) source1 = Dataset.from_iterable([ DatasetItem(1, annotations=[ Label(0), Label(1), Bbox(0, 0, 1, 1, label=0), Bbox(0, 0, 1, 1, label=1), ]), ], categories=['b', 'c']) expected = Dataset.from_iterable([ DatasetItem(1, annotations=[ Label(0), Label(1), Label(2), Bbox(0, 0, 1, 1, label=1), Bbox(0, 0, 1, 1, label=2), ]), ], categories=['a', 'b', 'c']) merger = IntersectMerge() merged = merger([source0, source1]) compare_datasets(self, expected, merged, ignored_attrs={'score'})
def _process(self, image): detections = [] for i, roi in enumerate(self.rois): roi_sum = self.roi_value(roi, image) roi_base_sum = self.roi_base_sums[i] first_run = roi_base_sum is None if first_run: roi_base_sum = roi_sum self.roi_base_sums[i] = roi_base_sum cls_conf = roi_sum / roi_base_sum if roi.threshold < roi_sum / roi_base_sum: cls = roi.label detections.append( Bbox(roi.x, roi.y, roi.w, roi.h, label=cls, attributes={'score': cls_conf}) ) if first_run: continue for j in range(self.fp_count): if roi.threshold < cls_conf: cls = roi.label else: cls = (i + j) % self.class_count box = [roi.x, roi.y, roi.w, roi.h] offset = (np.random.rand(4) - 0.5) * self.pixel_jitter detections.append( Bbox(*(box + offset), label=cls, attributes={'score': cls_conf}) ) return detections
def test_can_find_missing_boxes(self): detections = 3 class_count = 2 item1 = DatasetItem(id=1, annotations=[ Bbox(i * 10, 10, 10, 10, label=i) for i in range(detections) if i % 2 == 0 ]) item2 = DatasetItem(id=2, annotations=[ Bbox(i * 10, 10, 10, 10, label=(i + 1) % class_count) for i in range(detections) if i % 2 == 1 ]) iou_thresh = 0.5 comp = DistanceComparator(iou_threshold=iou_thresh) result = comp.match_boxes(item1, item2) matches, mispred, a_greater, b_greater = result self.assertEqual(0, len(mispred)) self.assertEqual(len(item1.annotations), len(a_greater)) self.assertEqual(len(item2.annotations), len(b_greater)) self.assertEqual(0, len(matches))
def append_bbox_voc(annotations, **kwargs): annotations.append( Bbox( 1, 1, 2, 2, label=kwargs["label_id"], id=kwargs["ann_id"] + 1, attributes=kwargs["attributes"], group=kwargs["ann_id"], )) # obj annotations.append( Label(kwargs["label_id"], attributes=kwargs["attributes"])) annotations.append( Bbox( 1, 1, 2, 2, label=kwargs["label_id"] + 3, group=kwargs["ann_id"], )) # part annotations.append( Label(kwargs["label_id"] + 3, attributes=kwargs["attributes"]))
def test_transform_to_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_run_equality_diff(self): dataset1 = Dataset.from_iterable([ DatasetItem(id=100, subset='train', image=np.ones((10, 6, 3)), annotations=[ Bbox(1, 2, 3, 4, label=0), ]), ], categories=['a', 'b']) dataset2 = Dataset.from_iterable([ DatasetItem(id=100, subset='train', image=np.ones((10, 6, 3)), annotations=[ Bbox(1, 2, 3, 4, label=1), Bbox(5, 6, 7, 8, label=2), ]), ], categories=['a', 'b', 'c']) with TestDir() as test_dir: dataset1_url = osp.join(test_dir, 'dataset1') dataset2_url = osp.join(test_dir, 'dataset2') dataset1.export(dataset1_url, 'coco', save_images=True) dataset2.export(dataset2_url, 'voc', save_images=True) result_dir = osp.join(test_dir, 'cmp_result') run(self, 'diff', dataset1_url + ':coco', dataset2_url + ':voc', '-m', 'equality', '-o', result_dir) self.assertEqual({'diff.json'}, set(os.listdir(result_dir)))
def test_can_save_and_load_bboxes(self): expected_dataset = Dataset.from_iterable([ DatasetItem(id='a/b/1', subset='train', image=np.ones((10, 15, 3)), annotations=[ Bbox(1, 3, 6, 10), Bbox(0, 1, 3, 5, attributes={'text': 'word 0'}), ]), DatasetItem(id=2, subset='train', image=np.ones((10, 15, 3)), annotations=[ Polygon([0, 0, 3, 0, 4, 7, 1, 8], attributes={'text': 'word 1'}), Polygon([1, 2, 5, 3, 6, 8, 0, 7]), ]), DatasetItem(id=3, subset='train', image=np.ones((10, 15, 3)), annotations=[ Polygon([2, 2, 8, 3, 7, 10, 2, 9], attributes={'text': 'word_2'}), Bbox(0, 2, 5, 9, attributes={'text': 'word_3'}), ]), ]) with TestDir() as test_dir: self._test_save_and_load( expected_dataset, partial(IcdarTextLocalizationConverter.convert, save_images=True), test_dir, 'icdar_text_localization')
def test_patch_fails_on_inplace_update_without_overwrite(self): dataset = Dataset.from_iterable([ DatasetItem(id=1, image=np.zeros((3, 5, 3)), annotations=[Bbox(1, 2, 3, 4, label=1)]), ], categories=['a', 'b']) patch = Dataset.from_iterable([ DatasetItem(id=2, image=np.zeros((3, 4, 3)), annotations=[Bbox(1, 2, 3, 2, label=1)]), ], categories=['b', 'a', 'c']) with TestDir() as test_dir: dataset_url = osp.join(test_dir, 'dataset1') patch_url = osp.join(test_dir, 'dataset2') dataset.export(dataset_url, 'coco', save_images=True) patch.export(patch_url, 'coco', save_images=True) run(self, 'patch', dataset_url + ':coco', patch_url + ':coco', expected_code=1)
def __iter__(self): yield DatasetItem(id=1, annotations=[ Bbox(2, 3, 4, 5, label=self._label('foreign_label'), id=1), Bbox(1, 2, 3, 4, label=self._label('label'), id=2, group=2, attributes={'act1': True}), Bbox(2, 3, 4, 5, label=self._label('label_part1'), group=2), Bbox(2, 3, 4, 6, label=self._label('label_part2'), group=2), ])
def test_can_import(self): expected_dataset = Dataset.from_iterable( [ DatasetItem(id='n000001/0001_01', subset='train', image=np.ones((10, 15, 3)), annotations=[ Bbox(2, 2, 1, 2, label=0), Points([ 2.787, 2.898, 2.965, 2.79, 2.8, 2.456, 2.81, 2.32, 2.89, 2.3 ], label=0), ]), DatasetItem(id='n000002/0001_01', subset='train', image=np.ones((10, 15, 3)), annotations=[ Bbox(2, 4, 2, 2, label=1), Points([ 2.3, 4.9, 2.9, 4.93, 2.62, 4.745, 2.54, 4.45, 2.76, 4.43 ], label=1) ]), DatasetItem(id='n000002/0002_01', subset='train', image=np.ones((10, 15, 3)), annotations=[ Bbox(1, 3, 1, 1, label=1), Points([ 1.2, 3.8, 1.8, 3.82, 1.51, 3.634, 1.43, 3.34, 1.65, 3.32 ], label=1) ]), DatasetItem( id='n000003/0003_01', subset='test', image=np.ones((10, 15, 3)), annotations=[ Bbox(1, 1, 1, 1, label=2), Points( [0.2, 2.8, 0.8, 2.9, 0.5, 2.6, 0.4, 2.3, 0.6, 2.3], label=2) ]) ], categories={ AnnotationType.label: LabelCategories.from_iterable([('n000001', 'Karl'), ('n000002', 'Jay'), ('n000003', 'Pol')]), }) dataset = Dataset.import_from(DUMMY_DATASET_DIR, 'vgg_face2') compare_datasets(self, expected_dataset, dataset)
def DISABLED_test_roi_nms(): ROI = namedtuple('ROI', ['conf', 'x', 'y', 'w', 'h', 'label']) class_count = 3 noisy_count = 3 rois = [ ROI(0.3, 10, 40, 30, 10, 0), ROI(0.5, 70, 90, 7, 10, 0), ROI(0.7, 5, 20, 40, 60, 2), ROI(0.9, 30, 20, 10, 40, 1), ] pixel_jitter = 10 detections = [] for i, roi in enumerate(rois): detections.append( Bbox(roi.x, roi.y, roi.w, roi.h, label=roi.label, attributes={'score': roi.conf}) ) for j in range(noisy_count): cls_conf = roi.conf * j / noisy_count cls = (i + j) % class_count box = [roi.x, roi.y, roi.w, roi.h] offset = (np.random.rand(4) - 0.5) * pixel_jitter detections.append( Bbox(*(box + offset), label=cls, attributes={'score': cls_conf}) ) import cv2 image = np.zeros((100, 100, 3)) for i, det in enumerate(detections): roi = ROI(det.attributes['score'], *det.get_bbox(), det.label) p1 = (int(roi.x), int(roi.y)) p2 = (int(roi.x + roi.w), int(roi.y + roi.h)) c = (0, 1 * (i % (1 + noisy_count) == 0), 1) cv2.rectangle(image, p1, p2, c) cv2.putText(image, 'd%s-%s-%.2f' % (i, roi.label, roi.conf), p1, cv2.FONT_HERSHEY_SIMPLEX, 0.25, c) cv2.imshow('nms_image', image) cv2.waitKey(0) nms_boxes = RISE.nms(detections, iou_thresh=0.25) print(len(detections), len(nms_boxes)) for i, det in enumerate(nms_boxes): roi = ROI(det.attributes['score'], *det.get_bbox(), det.label) p1 = (int(roi.x), int(roi.y)) p2 = (int(roi.x + roi.w), int(roi.y + roi.h)) c = (0, 1, 0) cv2.rectangle(image, p1, p2, c) cv2.putText(image, 'p%s-%s-%.2f' % (i, roi.label, roi.conf), p1, cv2.FONT_HERSHEY_SIMPLEX, 0.25, c) cv2.imshow('nms_image', image) cv2.waitKey(0)
def test_can_match_items(self): # items 1 and 3 are unique, item 2 is common and should be merged source0 = Dataset.from_iterable([ DatasetItem(1, annotations=[ Label(0), ]), DatasetItem(2, annotations=[ Label(0), ]), ], categories=['a', 'b']) source1 = Dataset.from_iterable([ DatasetItem(2, annotations=[ Label(1), ]), DatasetItem(3, annotations=[ Label(0), ]), ], categories=['a', 'b']) source2 = Dataset.from_iterable([ DatasetItem(2, annotations=[ Label(0), Bbox(1, 2, 3, 4) ]), ], categories=['a', 'b']) expected = Dataset.from_iterable([ DatasetItem(1, annotations=[ Label(0, attributes={'score': 1/3}), ]), DatasetItem(2, annotations=[ Label(0, attributes={'score': 2/3}), Label(1, attributes={'score': 1/3}), Bbox(1, 2, 3, 4, attributes={'score': 1.0}), ]), DatasetItem(3, annotations=[ Label(0, attributes={'score': 1/3}), ]), ], categories=['a', 'b']) merger = IntersectMerge() merged = merger([source0, source1, source2]) compare_datasets(self, expected, merged) self.assertEqual( [ NoMatchingItemError(item_id=('1', DEFAULT_SUBSET_NAME), sources={1, 2}), NoMatchingItemError(item_id=('3', DEFAULT_SUBSET_NAME), sources={0, 2}), ], sorted((e for e in merger.errors if isinstance(e, NoMatchingItemError)), key=lambda e: e.item_id) ) self.assertEqual( [ NoMatchingAnnError(item_id=('2', DEFAULT_SUBSET_NAME), sources={0, 1}, ann=source2.get('2').annotations[1]), ], sorted((e for e in merger.errors if isinstance(e, NoMatchingAnnError)), key=lambda e: e.item_id) )
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 deleted Polygon([1, 1, 2, 2, 3, 4], label=4), # Should be kept PolyLine([1, 3, 4, 2, 5, 6]), Bbox(4, 3, 2, 1, label=5), ]) ], categories={ AnnotationType.label: LabelCategories.from_iterable( f'label{i}' for i in range(6)), AnnotationType.mask: MaskCategories( colormap=mask_tools.generate_colormap(6)), AnnotationType.points: PointsCategories.from_iterable( [(i, [str(i)]) for i in range(6)]) }) 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), PolyLine([1, 3, 4, 2, 5, 6], label=None), Bbox(4, 3, 2, 1, label=2), ]), ], categories={ AnnotationType.label: LabelCategories.from_iterable( ['label0', 'label9', 'label5']), AnnotationType.mask: MaskCategories(colormap={ i: v for i, v in enumerate({ k: v for k, v in mask_tools.generate_colormap(6).items() if k in { 0, 1, 5 } }.values()) }), AnnotationType.points: PointsCategories.from_iterable( [(0, ['0']), (1, ['1']), (2, ['5'])]) }) actual = transforms.RemapLabels(src_dataset, mapping={ 'label1': 'label9', # rename & join with new label9 (from label3) 'label2': 'label0', # rename & join with existing label0 'label3': 'label9', # rename & join with new label9 (from label1) 'label4': '', # delete the label and associated annotations # 'label5' - unchanged }, default='keep') compare_datasets(self, dst_dataset, actual)
def test_can_save_and_load(self): source_dataset = Dataset.from_iterable([ DatasetItem(id=1, subset='train', image=np.ones((8, 8, 3)), annotations=[ Bbox(0, 2, 4, 2, label=2), Bbox(0, 1, 2, 3, label=4), ]), DatasetItem(id=2, subset='train', image=np.ones((10, 10, 3)), annotations=[ Bbox(0, 2, 4, 2, label=2), Bbox(3, 3, 2, 3, label=4), Bbox(2, 1, 2, 3, label=4), ]), DatasetItem(id=3, subset='valid', image=np.ones((8, 8, 3)), annotations=[ Bbox(0, 1, 5, 2, label=2), Bbox(0, 2, 3, 2, label=5), Bbox(0, 2, 4, 2, label=6), Bbox(0, 7, 3, 2, label=7), ]), ], categories={ AnnotationType.label: LabelCategories.from_iterable( 'label_' + str(i) for i in range(10)), }) with TestDir() as test_dir: YoloConverter.convert(source_dataset, test_dir, save_images=True) parsed_dataset = Dataset.import_from(test_dir, 'yolo') compare_datasets(self, source_dataset, parsed_dataset)
def test_inplace_save_writes_only_updated_data_with_direct_changes(self): expected = Dataset.from_iterable([ DatasetItem(1, subset='a', image=np.ones((1, 2, 3)), annotations=[ # Bbox(0, 0, 0, 0, label=1) # won't find removed anns ]), DatasetItem(2, subset='b', image=np.ones((3, 2, 3)), annotations=[ Bbox(0, 0, 0, 0, label=4, id=1, group=1, attributes={ 'truncated': False, 'difficult': False, 'occluded': False, }) ]), ], categories={ AnnotationType.label: LabelCategories.from_iterable( ['background', 'a', 'b', 'c', 'd']), AnnotationType.mask: MaskCategories( colormap=VOC.generate_colormap(5)), }) dataset = Dataset.from_iterable([ DatasetItem(1, subset='a', image=np.ones((1, 2, 3)), annotations=[Bbox(0, 0, 0, 0, label=1)]), DatasetItem(2, subset='b', annotations=[Bbox(0, 0, 0, 0, label=2)]), DatasetItem(3, subset='c', image=np.ones((2, 2, 3)), annotations=[ Bbox(0, 0, 0, 0, label=3), Mask(np.ones((2, 2)), label=1) ]), ], categories=['a', 'b', 'c', 'd']) with TestDir() as path: dataset.export(path, 'voc', save_images=True) os.unlink(osp.join(path, 'Annotations', '1.xml')) os.unlink(osp.join(path, 'Annotations', '2.xml')) os.unlink(osp.join(path, 'Annotations', '3.xml')) dataset.put(DatasetItem(2, subset='b', image=np.ones((3, 2, 3)), annotations=[Bbox(0, 0, 0, 0, label=3)])) dataset.remove(3, 'c') dataset.save(save_images=True) self.assertEqual({'2.xml'}, # '1.xml' won't be touched set(os.listdir(osp.join(path, 'Annotations')))) self.assertEqual({'1.jpg', '2.jpg'}, set(os.listdir(osp.join(path, 'JPEGImages')))) self.assertEqual({'a.txt', 'b.txt'}, set(os.listdir(osp.join(path, 'ImageSets', 'Main')))) compare_datasets(self, expected, Dataset.import_from(path, 'voc'), require_images=True)
def _load_items(self, path): anno_dict = parse_json_file(path) label_categories = self._categories[AnnotationType.label] tags = anno_dict.get('tags', []) for label in tags: label_name = label.get('name') label_idx = label_categories.find(label_name)[0] if label_idx is None: label_idx = label_categories.add(label_name) items = {} for id, asset in anno_dict.get('assets', {}).items(): item_id = osp.splitext(asset.get('asset', {}).get('name'))[0] annotations = [] for region in asset.get('regions', []): tags = region.get('tags', []) if not tags: bbox = region.get('boundingBox', {}) if bbox: annotations.append( Bbox(float(bbox['left']), float(bbox['top']), float(bbox['width']), float(bbox['height']), attributes={'id': region.get('id')})) for tag in region.get('tags', []): label_idx = label_categories.find(tag)[0] if label_idx is None: label_idx = label_categories.add(tag) bbox = region.get('boundingBox', {}) if bbox: annotations.append( Bbox(float(bbox['left']), float(bbox['top']), float(bbox['width']), float(bbox['height']), label=label_idx, attributes={'id': region.get('id')})) items[item_id] = DatasetItem( id=item_id, subset=self._subset, attributes={'id': id}, image=Image(path=osp.join(osp.dirname(path), asset.get('asset', {}).get('path'))), annotations=annotations) return items
def test_can_save_dataset_with_cjk_categories(self): expected = Dataset.from_iterable([ DatasetItem(id=1, subset='train', image=np.ones((4, 4, 3)), annotations=[ Bbox(0, 1, 2, 2, label=0, group=1, id=1, attributes={'is_crowd': False}), ], attributes={'id': 1}), DatasetItem(id=2, subset='train', image=np.ones((4, 4, 3)), annotations=[ Bbox(1, 0, 2, 2, label=1, group=2, id=2, attributes={'is_crowd': False}), ], attributes={'id': 2}), DatasetItem(id=3, subset='train', image=np.ones((4, 4, 3)), annotations=[ Bbox(0, 1, 2, 2, label=2, group=3, id=3, attributes={'is_crowd': False}), ], attributes={'id': 3}), ], categories=["고양이", "ネコ", "猫"]) with TestDir() as test_dir: self._test_save_and_load( expected, partial(DatumaroConverter.convert, save_images=True), test_dir)
def __iter__(self): yield DatasetItem(id=1, annotations=[ Bbox(1, 2, 3, 4, label=self._label('label'), id=1, group=1, attributes={ 'act1': True, 'act2': False, 'truncated': False, 'difficult': False, 'occluded': False, } ), Bbox(2, 3, 4, 5, label=self._label('label_part1'), group=1), Bbox(2, 3, 4, 6, label=self._label('label_part2'), group=1), ])
def test_group_checks(self): dataset = Dataset.from_iterable([ DatasetItem(1, annotations=[ Bbox(0, 0, 0, 0, label=0, group=1), # misses an optional label Bbox(0, 0, 0, 0, label=1, group=1), Bbox(0, 0, 0, 0, label=2, group=2), # misses a mandatory label - error Bbox(0, 0, 0, 0, label=2, group=2), Bbox(0, 0, 0, 0, label=4), # misses an optional label Bbox(0, 0, 0, 0, label=5), # misses a mandatory label - error Bbox(0, 0, 0, 0, label=0), # misses a mandatory label - error Bbox(0, 0, 0, 0, label=3), # not listed - not checked ]), ], categories=['a', 'a_g1', 'a_g2_opt', 'b', 'c', 'c_g1_opt']) merger = IntersectMerge(conf={'groups': [ ['a', 'a_g1', 'a_g2_opt?'], ['c', 'c_g1_opt?'] ]}) merger([dataset, dataset]) self.assertEqual(3, len([e for e in merger.errors if isinstance(e, WrongGroupError)]), merger.errors )