Beispiel #1
0
    def convert(self):
        images_pathes = self._get_images_pathes()
        dataset_name = os.path.basename(os.path.normpath(self.dataset_dir))
        out_pr = sly.ProjectStructure(self.settings['res_names']['project'])

        for image_fp in images_pathes:
            image_ext = os.path.splitext(image_fp)[1]
            image_name = os.path.splitext(image_fp)[0]
            dt = {
                "image_ext": image_ext
            }
            out_pr.add_item(dataset_name, image_name, dt)

        out_pr_fs = sly.ProjectFS(self.out_dir, out_pr)
        out_pr_fs.make_dirs()

        res_meta = sly.ProjectMeta()
        print(self.classes)
        for class_name in self.classes:
            res_meta.classes.add({'title': class_name, 'shape': 'bitmap', 'color': sly.gen_new_color()})
        res_meta.to_dir(out_pr_fs.project_path)

        progress = sly.progress_counter_import(out_pr.name, out_pr.image_cnt)
        for sample_info in out_pr_fs:
            self._convert_sample(sample_info)
            progress.iter_done_report()
Beispiel #2
0
    def convert(self):
        # map input structure to output
        out_pr = sly.ProjectStructure(self.settings['res_names']['project'])

        for ds_name, sample_names in self.src_datasets.items():
            for name in sample_names:
                dt = {
                    'src_img_path':
                    osp.join(self._imgs_dir(ds_name), name + '.jpg'),
                    'segm_path':
                    osp.join(self._segm_dir(ds_name), name + '.png'),
                    'inst_path':
                    osp.join(self._inst_dir(ds_name), name + '.png')
                }
                if all((osp.isfile(x) for x in dt.values())):
                    dt['image_ext'] = '.jpg'
                    out_pr.add_item(ds_name, name, dt)

        out_pr_fs = sly.ProjectFS(self.out_dir, out_pr)
        out_pr_fs.make_dirs()

        res_meta = sly.ProjectMeta()
        for class_name, color in self.cls2col.items():
            res_meta.classes.add({
                'title': class_name,
                'shape': 'bitmap',
                'color': sly.color2code(color)
            })
        res_meta.to_dir(out_pr_fs.project_path)

        progress = sly.progress_counter_import(out_pr.name, out_pr.image_cnt)
        for sample_info in out_pr_fs:
            self._convert_sample(sample_info)
            progress.iter_done_report()
Beispiel #3
0
    def convert(self):
        images_pathes = self._get_files_list(self.img_dir)
        masks_pathes = self._get_files_list(self.ann_dir)
        masks_map = {
            os.path.splitext(mask_p)[0]: mask_p
            for mask_p in masks_pathes
        }
        dataset_name = 'ds'
        out_pr = sly.ProjectStructure(self.settings['res_names']['project'])

        for image_fp in images_pathes:
            image_ext = os.path.splitext(image_fp)[1]
            image_id = os.path.splitext(image_fp)[0]
            dt = {"image_ext": ".png", "image_ext_in": image_ext}
            out_pr.add_item(dataset_name, image_id, dt)

        out_pr_fs = sly.ProjectFS(self.out_dir, out_pr)
        out_pr_fs.make_dirs()

        res_meta = sly.ProjectMeta()
        res_meta.classes.add({
            'title': 'untitled',
            'shape': 'bitmap',
            'color': sly.gen_new_color()
        })
        res_meta.to_dir(out_pr_fs.project_path)

        progress = sly.progress_counter_import(out_pr.name, out_pr.image_cnt)
        for sample_info in out_pr_fs:
            self._convert_sample(sample_info, masks_map)
            progress.iter_done_report()
Beispiel #4
0
    def convert(self):
        images_pathes = self._get_images_pathes()
        masks_map = self._get_masks_mapping()
        dataset_name = 'ds'
        out_pr = sly.ProjectStructure(self.settings['res_names']['project'])

        for image_fp in images_pathes:
            base_name = os.path.basename(image_fp)
            image_ext = os.path.splitext(image_fp)[1]
            image_id = os.path.splitext(base_name)[0]
            if base_name.replace(image_ext, '') in masks_map:
                dt = {"image_ext": ".png", "image_orig_path": image_fp}
                out_pr.add_item(dataset_name, image_id, dt)

        out_pr_fs = sly.ProjectFS(self.out_dir, out_pr)
        out_pr_fs.make_dirs()

        res_meta = sly.ProjectMeta()
        res_meta.classes.add({
            'title': 'leaf',
            'shape': 'bitmap',
            'color': sly.gen_new_color()
        })
        res_meta.to_dir(out_pr_fs.project_path)

        progress = sly.progress_counter_import(out_pr.name, out_pr.image_cnt)
        for sample_info in out_pr_fs:
            self._convert_sample(sample_info, masks_map)
            progress.iter_done_report()
Beispiel #5
0
    def convert(self):
        in_datasets = self._find_in_datasets()

        # map input structure to output
        out_pr = sly.ProjectStructure(self.settings['res_names']['project'])

        for ds_name, ds_path in in_datasets:
            img_fnames = sly.ImportImgLister.list_images(ds_path)
            for name_with_ext in img_fnames:
                img_name, img_ext = osp.splitext(name_with_ext)
                src_img_path = osp.join(ds_path, name_with_ext)
                dt = {
                    'src_img_path': src_img_path,
                    'image_ext': img_ext,
                }
                out_pr.add_item(ds_name, img_name, dt)
            logger.info(
                'Found source dataset with raw images: "{}", {} sample(s).'.
                format(ds_name, len(img_fnames)))

        out_pr_fs = sly.ProjectFS(self.out_dir, out_pr)
        out_pr_fs.make_dirs()

        res_meta = sly.ProjectMeta()  # empty
        res_meta.to_dir(out_pr_fs.project_path)

        progress = sly.progress_counter_import(out_pr.name, out_pr.image_cnt)
        for sample_info in out_pr_fs:
            sample_data = sample_info.ia_data
            src_img_path = sample_data['src_img_path']
            sly.copy_file(src_img_path, sample_info.img_path)  # img is ready

            image = Image.open(sample_info.img_path)
            exif_data = pyexiv2.metadata.ImageMetadata(sample_info.img_path)
            exif_data.read()

            if exif_data.get_orientation() != 1:
                logger.debug('Image with flip/rot EXIF',
                             extra={
                                 'orientation': exif_data.get_orientation(),
                                 'image_path': sample_info.img_path
                             })
                image = sly.image_transpose_exif(image)
                image.save(sample_info.img_path)
                exif_data['Exif.Image.Orientation'] = pyexiv2.ExifTag(
                    'Exif.Image.Orientation', 1)
                exif_data.modified = True
                exif_data.write()

            imsize_wh = image.size
            ann = sly.Annotation.new_with_objects(imsize_wh, [])
            sly.json_dump(ann.pack(), sample_info.ann_path)  # ann is ready
            progress.iter_done_report()
Beispiel #6
0
    def convert(self):
        search_fine = osp.join(self.in_dir, "gtFine", "*", "*",
                               "*_gt*_polygons.json")
        files_fine = glob.glob(search_fine)
        files_fine.sort()

        search_imgs = osp.join(self.in_dir, "leftImg8bit", "*", "*",
                               "*_leftImg8bit.png")
        files_imgs = glob.glob(search_imgs)
        files_imgs.sort()

        out_pr = sly.ProjectStructure(self.settings['res_names']['project'])

        for orig_ann_path in files_fine:
            parent_dir, json_fname = osp.split(os.path.abspath(orig_ann_path))
            dataset_name = osp.basename(parent_dir)
            sample_name = json_fname.replace('_gtFine_polygons.json', '')

            orig_img_path = self.json_path_to_image_path(orig_ann_path)

            tag_path = osp.split(parent_dir)[0]
            tag_name = osp.basename(tag_path)  # e.g. train, val, test

            dt = {
                'orig_ann_path': orig_ann_path,
                'orig_img_path': orig_img_path,
                'tag_name': tag_name,
                'image_ext': '.png',  # hard-coded ext (see glob above)
            }

            if all(osp.isfile(x) for x in (orig_img_path, orig_ann_path)):
                out_pr.add_item(dataset_name, sample_name, dt)

        stat_dct = {
            'samples': out_pr.image_cnt,
            'src_ann_cnt': len(files_fine),
            'src_img_cnt': len(files_imgs)
        }
        logger.info('Found img/ann pairs.', extra=stat_dct)
        if stat_dct['samples'] < stat_dct['src_ann_cnt']:
            logger.warn(
                'Found source annotations without corresponding images.',
                extra=stat_dct)

        out_pr_fs = sly.ProjectFS(self.out_dir, out_pr)
        out_pr_fs.make_dirs()

        progress = sly.progress_counter_import(out_pr.name, out_pr.image_cnt)
        ok_cnt = 0
        for s in out_pr_fs:
            log_dct = s._asdict()  # ok, it's documented
            try:
                self._convert_sample(s)
            except AnnConvException:
                logger.warn(
                    'Error occured while processing input sample annotation.',
                    exc_info=True,
                    extra=log_dct)
            except Exception:
                logger.error('Error occured while processing input sample.',
                             exc_info=False,
                             extra=log_dct)
                raise
            else:
                ok_cnt += 1
            progress.iter_done_report()

        logger.info('Processed.',
                    extra={
                        'samples': out_pr.image_cnt,
                        'ok_cnt': ok_cnt
                    })

        res_meta = sly.ProjectMeta()
        for class_name in self.classes:
            res_meta.classes.add({
                'title': class_name,
                'shape': 'polygon',
                'color': sly.gen_new_color()
            })
        res_meta.img_tags.update(self.tags)
        res_meta.to_dir(out_pr_fs.project_path)
        logger.info('Found classes.',
                    extra={
                        'cnt': len(self.classes),
                        'classes': sorted(list(self.classes))
                    })
        logger.info('Created tags.',
                    extra={
                        'cnt': len(self.tags),
                        'tags': sorted(list(self.tags))
                    })
Beispiel #7
0
def main():
    logger.info('Hello world.')

    # It isn't necessary, but let's suppose that our data will be stored as for Supervisely task:
    # input in '/sly_task_data/data` and results in '/sly_task_data/results'.
    # So TaskPaths provides the paths.
    task_paths = sly.TaskPaths()

    in_pr_dir = task_paths.project_dir  # the paths includes project name

    in_pr_meta = sly.ProjectMeta.from_dir(in_pr_dir)
    # Now we've read meta of input project.
    logger.info('Input project meta: {} class(es).'.format(
        len(in_pr_meta.classes)))

    in_pr_fs = sly.ProjectFS.from_disk(
        *sly.ProjectFS.split_dir_project(in_pr_dir))
    # Now we've read project structure.
    logger.info(
        'Input project: "{}" contains {} dataset(s) and {} image(s).'.format(
            in_pr_fs.pr_structure.name, len(in_pr_fs.pr_structure.datasets),
            in_pr_fs.image_cnt))

    # It's convenient to create output project structure and store source file paths in ia_data.
    out_pr_structure = sly.ProjectStructure(
        'my_new_project')  # rename project... just for fun
    for item_descr in in_pr_fs:  # iterate over input project
        new_ia_data = {
            'src_ann_path': item_descr.ann_path,
            'src_img_path': item_descr.img_path,
            **item_descr.ia_data  # contains 'image_ext' which is required to write images
        }
        out_pr_structure.add_item(item_descr.ds_name, item_descr.image_name,
                                  new_ia_data)
    # ProjectFS will provide out file paths
    out_pr_fs = sly.ProjectFS(task_paths.results_dir, out_pr_structure)

    # We will add the rectangle to each annotation.
    new_class_title = 'new-region'
    rect_to_add = sly.Rect(left=20, top=20, right=50, bottom=100)

    # Ok, start processing.
    out_pr_fs.make_dirs()  # create all directories required for writing
    for item_descr in out_pr_fs:  # iterate over output project
        logger.info('Processing sample',
                    extra={
                        'dataset': item_descr.ds_name,
                        'image_name': item_descr.image_name
                    })

        # Copy image unchanged.
        sly.copy_file(item_descr.ia_data['src_img_path'], item_descr.img_path)

        # Read annotation.
        ann_packed = sly.json_load(item_descr.ia_data['src_ann_path'])
        ann = sly.Annotation.from_packed(ann_packed, in_pr_meta)

        # Add new figure to the annotation.
        # Method to construct figures returns iterable of new figures.
        # (e.g., line cropped with image bounds may produce some lines), but here we'll get not more than one figure
        # ...or no figures if image is less than 20x20.
        new_figures = sly.FigureRectangle.from_rect(new_class_title,
                                                    ann.image_size_wh,
                                                    rect_to_add)
        ann['objects'].extend(new_figures)

        # Save annotation.
        sly.json_dump(ann.pack(), item_descr.ann_path)

    # OK, and don't forget to create and save output project meta.
    # We'll save given data and add new class with shape "rectangle".
    out_pr_meta = deepcopy(in_pr_meta)
    out_pr_meta.classes.add({
        'title': new_class_title,
        'shape': 'rectangle',
        'color': '#FFFF00'
    })
    # Then store the meta.
    out_pr_meta.to_dir(out_pr_fs.project_path)

    logger.info('Done.')