def set_ann_file(self, item_name: str, ann_path: str): if type(ann_path) is not str: raise TypeError( "Annotation path should be a string, not a {}".format( type(ann_path))) dst_ann_path = self.get_ann_path(item_name) copy_file(ann_path, dst_ann_path)
def save_project_as_pascal_voc_detection(save_path, project: Project): # Create root pascal 'datasets' folders for dataset in project.datasets: pascal_dataset_path = os.path.join(save_path, dataset.name) pascal_dataset_relative_path = os.path.relpath(pascal_dataset_path, save_path) 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_dataset_relative_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 _add_img_file(self, item_name, img_path, _validate_img=True, _use_hardlink=False): ''' Add given item file to dataset items directory. Generate exception error if item_name already exists in dataset or item name has unsupported extension :param item_name: str :param img_path: str :param _validate_img: bool :param _use_hardlink: bool ''' # @TODO: deprecated method, should be private and be (refactored, renamed) in future self._check_add_item_name(item_name) dst_img_path = os.path.join(self.item_dir, item_name) if img_path != dst_img_path and img_path is not None: # used only for agent + api during download project + None to optimize internal usage hardlink_done = False if _use_hardlink: try: os.link(img_path, dst_img_path) hardlink_done = True except OSError: pass if not hardlink_done: copy_file(img_path, dst_img_path) if _validate_img: self._validate_added_item_or_die(img_path)
def _copy_file_concurr(cls, src_path, dst_path): try: sly_fs.copy_file(src_path, dst_path) except OSError as e: # may be written by parallel process, skip in that case if not osp.isfile(dst_path): raise e
def set_ann_file(self, item_name: str, ann_path: str): ''' Copy given annotation with given name to dataset annotations dir :param item_name: str :param ann_path: str (Generate exception error if not so) ''' if type(ann_path) is not str: raise TypeError("Annotation path should be a string, not a {}".format(type(ann_path))) dst_ann_path = self.get_ann_path(item_name) copy_file(ann_path, dst_ann_path)
def _add_img_file(self, item_name, img_path): self._check_add_item_name(item_name) dst_img_path = os.path.join(self.img_dir, item_name) if img_path != dst_img_path: # used only for agent + api during download project copy_file(img_path, dst_img_path) # Make sure we actually received a valid image file, clean it up and bail if not so. try: sly_image.validate_format(dst_img_path) except sly_image.UnsupportedImageFormat: os.remove(dst_img_path) raise self._item_to_ann[item_name] = item_name + ANN_EXT
def _add_img_file(self, item_name, img_path, _validate_img=True, _use_hardlink=False): self._check_add_item_name(item_name) dst_img_path = os.path.join(self.img_dir, item_name) if img_path != dst_img_path: # used only for agent + api during download project hardlink_done = False if _use_hardlink: try: os.link(img_path, dst_img_path) hardlink_done = True except OSError: pass if not hardlink_done: copy_file(img_path, dst_img_path) if _validate_img: self._validate_added_image_or_die(img_path)
def _add_img_file(self, item_name, img_path, _validate_img=True, _use_hardlink=False): # @TODO: deprecated method, should be private and be (refactored, renamed) in future self._check_add_item_name(item_name) dst_img_path = os.path.join(self.item_dir, item_name) if img_path != dst_img_path and img_path is not None: # used only for agent + api during download project + None to optimize internal usage hardlink_done = False if _use_hardlink: try: os.link(img_path, dst_img_path) hardlink_done = True except OSError: pass if not hardlink_done: copy_file(img_path, dst_img_path) if _validate_img: self._validate_added_item_or_die(img_path)
def _add_img_file(self, item_name, img_path): self._check_add_item_name(item_name) dst_img_path = os.path.join(self.img_dir, item_name) if img_path != dst_img_path: # used only for agent + api during download project copy_file(img_path, dst_img_path) self._validate_added_image_or_die(dst_img_path)
def _add_img_file(self, item_name, img_path): img_ext = get_file_ext(img_path) dst_img_path = self.deprecated_make_img_path(item_name, img_ext) if img_path != dst_img_path: # used only for agent + api during download project copy_file(img_path, dst_img_path) self._items_exts[item_name] = img_ext
def _add_img_file(self, item_name, img_path): self._check_add_item_name(item_name) dst_img_path = os.path.join(self.img_dir, item_name) if img_path != dst_img_path: # used only for agent + api during download project copy_file(img_path, dst_img_path) self._item_to_ann[item_name] = item_name + ANN_EXT
def _read_obj_impl(self, st_path, dst_path): sly_fs.ensure_base_path(dst_path) sly_fs.copy_file(st_path, dst_path)