Example #1
0
    def _read(self):
        if not dir_exists(self.img_dir):
            raise FileNotFoundError('Image directory not found: {!r}'.format(
                self.img_dir))
        if not dir_exists(self.ann_dir):
            raise FileNotFoundError(
                'Annotation directory not found: {!r}'.format(self.ann_dir))

        raw_ann_paths = list_files(self.ann_dir, [ANN_EXT])
        img_paths = list_files(self.img_dir, filter_fn=sly_image.has_valid_ext)

        raw_ann_names = set(os.path.basename(path) for path in raw_ann_paths)
        img_names = [os.path.basename(path) for path in img_paths]

        if len(img_names) == 0 or len(raw_ann_names) == 0:
            raise RuntimeError('Dataset {!r} is empty'.format(self.name))

        # Consistency checks. Every image must have an annotation, and the correspondence must be one to one.
        effective_ann_names = set()
        for img_name in img_names:
            ann_name = _get_effective_ann_name(img_name, raw_ann_names)
            if ann_name is None:
                raise RuntimeError(
                    'Image {!r} in dataset {!r} does not have a corresponding annotation file.'
                    .format(img_name, self.name))
            if ann_name in effective_ann_names:
                raise RuntimeError(
                    'Annotation file {!r} in dataset {!r} matches two different image files.'
                    .format(ann_name, self.name))
            effective_ann_names.add(ann_name)
            self._item_to_ann[img_name] = ann_name
Example #2
0
    def _read(self):
        if not dir_exists(self.img_dir):
            raise FileNotFoundError('Image directory not found: {!r}'.format(
                self.img_dir))
        if not dir_exists(self.ann_dir):
            raise FileNotFoundError(
                'Annotation directory not found: {!r}'.format(self.ann_dir))

        ann_paths = list_files(self.ann_dir, [ANN_EXT])
        img_paths = list_files(self.img_dir, image.SUPPORTED_IMG_EXTS)

        ann_names = set(get_file_name(path) for path in ann_paths)
        img_names = {
            get_file_name(path): get_file_ext(path)
            for path in img_paths
        }

        if len(img_names) == 0 or len(ann_names) == 0:
            raise RuntimeError('Dataset {!r} is empty'.format(self.name))
        if ann_names != set(img_names.keys()):
            raise RuntimeError(
                'File names in dataset {!r} are inconsistent'.format(
                    self.name))

        self._items_exts = img_names
Example #3
0
 def _create(self):
     if dir_exists(self.directory):
         if len(list_files(self.directory)) > 0:
             raise RuntimeError(
                 "Cannot create new project {!r}. Directory {!r} already exists and is not empty"
                 .format(self.name, self.directory))
     else:
         mkdir(self.directory)
     self.set_meta(ProjectMeta())
Example #4
0
 def get_related_images(self, item_name):
     results = []
     path = self.get_related_images_path(item_name)
     if dir_exists(path):
         files = list_files(path, SUPPORTED_IMG_EXTS)
         for file in files:
             img_meta_path = os.path.join(path, get_file_name_with_ext(file)+".json")
             img_meta = {}
             if file_exists(img_meta_path):
                 img_meta = load_json_file(img_meta_path)
             results.append((file, img_meta))
     return results