def run_evaluation(self): progress = Progress('metric evaluation', self._project_gt.total_items) for ds_name in self._project_gt.datasets.keys(): ds_gt = self._project_gt.datasets.get(ds_name) ds_pred = self._project_pred.datasets.get(ds_name) for sample_name in ds_gt: try: ann_gt = Annotation.load_json_file(ds_gt.get_ann_path(sample_name), self._project_gt.meta) ann_pred = Annotation.load_json_file(ds_pred.get_ann_path(sample_name), self._project_pred.meta) self._metric.add_pair(ann_gt, ann_pred) except ValueError as e: logger.warning('An error has occured ({}). Sample "{}" in dataset "{}" will be skipped' .format(str(e), sample_name, ds_gt.name)) progress.iter_done_report()
def save_project_as_pascal_voc_detection(save_path, project: Project): import pascal_voc_writer # Create root pascal 'datasets' folders for dataset in project.datasets: pascal_dataset_path = os.path.join(save_path, dataset.name) images_dir = os.path.join(pascal_dataset_path, 'JPEGImages') anns_dir = os.path.join(pascal_dataset_path, 'Annotations') lists_dir = os.path.join(pascal_dataset_path, 'ImageSets/Layout') fs_utils.mkdir(pascal_dataset_path) for subdir in ['ImageSets', # Train list, Val list, etc. 'ImageSets/Layout', 'Annotations', 'JPEGImages']: fs_utils.mkdir(os.path.join(pascal_dataset_path, subdir)) samples_by_tags = defaultdict(list) # TRAIN: [img_1, img2, ..] for item_name in dataset: img_path, ann_path = dataset.get_item_paths(item_name) no_ext_name = fs_utils.get_file_name(item_name) pascal_img_path = os.path.join(images_dir, no_ext_name + OUT_IMG_EXT) pascal_ann_path = os.path.join(anns_dir, no_ext_name + XML_EXT) if item_name.endswith(OUT_IMG_EXT): fs_utils.copy_file(img_path, pascal_img_path) else: img = image_utils.read(img_path) image_utils.write(pascal_img_path, img) ann = Annotation.load_json_file(ann_path, project_meta=project.meta) # Read tags for images lists generation for tag in ann.img_tags: samples_by_tags[tag.name].append((no_ext_name ,len(ann.labels))) writer = pascal_voc_writer.Writer(path=pascal_img_path, width=ann.img_size[1], height=ann.img_size[0]) for label in ann.labels: obj_class = label.obj_class rect: Rectangle = label.geometry.to_bbox() writer.addObject(name=obj_class.name, xmin = rect.left, ymin = rect.top, xmax = rect.right, ymax = rect.bottom) writer.save(pascal_ann_path) save_images_lists(lists_dir, samples_by_tags)
def samples_by_tags(required_tags, project): """ Split samples from project by tags :param required_tags: list of tags names :param project: supervisely `Project` class object :return: """ img_annotations_groups = defaultdict(list) for dataset in project: for item_name in dataset: item_paths = dataset.get_item_paths(item_name) ann = Annotation.load_json_file(path=item_paths.ann_path, project_meta=project.meta) img_tags = ann.img_tags for required_tag in required_tags: if img_tags.has_key(required_tag): # TODO migrate to ItemPath objects for img_annotations_groups img_annotations_groups[required_tag].append( (item_paths.img_path, item_paths.ann_path)) return img_annotations_groups
def ensure_samples_nonempty(samples, tag_name, project_meta): """ Args: samples: list of pairs (image path, annotation path). tag_name: tag name for messages. project_meta: input project meta object. Returns: None """ if len(samples) < 1: raise RuntimeError( 'There are no annotations with tag "{}"'.format(tag_name)) for _, ann_path in samples: ann = Annotation.load_json_file(ann_path, project_meta) if len(ann.labels) > 0: return raise RuntimeError( 'There are no objects in annotations with tag "{}"'.format(tag_name))
def load_annotation(self, fpath): # will not resize figures: resize gt instead return Annotation.load_json_file(fpath, self._project_meta)