Exemplo n.º 1
0
    def crop(self, rect):
        '''
        Crop the current Polyline with a given rectangle, if polyline cat't be cropped it generate exception error
        :param rect: Rectangle class object
        :return: list of Polyline class objects
        '''
        try:
            clipping_window = [[rect.top, rect.left], [rect.top, rect.right],
                               [rect.bottom, rect.right],
                               [rect.bottom, rect.left]]
            clipping_window_shpl = ShapelyPolygon(clipping_window)

            exterior = self.exterior_np
            intersections_polygon = LineString(exterior).intersection(
                clipping_window_shpl)
            mapping_shpl = mapping(intersections_polygon)
        except Exception:
            logger.warn('Line cropping exception, shapely.', exc_info=False)
            raise

        res_lines_pts = shapely_figure_to_coords_list(mapping_shpl)

        # tiny hack to combine consecutive segments
        lines_combined = []
        for simple_l in res_lines_pts:
            if len(lines_combined) > 0:
                prev = lines_combined[-1]
                if prev[-1] == simple_l[0]:
                    lines_combined[-1] = list(prev) + list(simple_l[1:])
                    continue
            lines_combined.append(simple_l)

        return [
            Polyline(row_col_list_to_points(line)) for line in lines_combined
        ]
Exemplo n.º 2
0
    def crop(self, rect):
        try:
            clipping_window = [[rect.left, rect.top], [rect.right, rect.top],
                               [rect.right, rect.bottom],
                               [rect.left, rect.bottom]]
            clipping_window_shpl = ShapelyPolygon(clipping_window)

            exterior = self.exterior_np[:, ::-1]
            intersections_polygon = LineString(exterior).intersection(
                clipping_window_shpl)
            mapping_shpl = mapping(intersections_polygon)
        except Exception:
            logger.warn('Line cropping exception, shapely.', exc_info=False)
            raise

        res_lines_pts = shapely_figure_to_coords_list(mapping_shpl)

        # tiny hack to combine consecutive segments
        lines_combined = []
        for simple_l in res_lines_pts:
            if len(lines_combined) > 0:
                prev = lines_combined[-1]
                if prev[-1] == simple_l[0]:
                    lines_combined[-1] = list(prev) + list(simple_l[1:])
                    continue
            lines_combined.append(simple_l)

        return [
            Polyline(row_col_list_to_points(line)) for line in lines_combined
        ]
Exemplo n.º 3
0
    def convert(self, new_geometry, contour_radius=0, approx_epsilon=None):
        from supervisely_lib.geometry.any_geometry import AnyGeometry
        if type(self) == new_geometry or new_geometry == AnyGeometry:
            return [self]

        allowed_transforms = self.allowed_transforms()
        if new_geometry not in allowed_transforms:
            raise NotImplementedError("from {!r} to {!r}".format(
                self.geometry_name(), new_geometry.geometry_name()))

        from supervisely_lib.geometry.bitmap import Bitmap
        from supervisely_lib.geometry.rectangle import Rectangle
        from supervisely_lib.geometry.polygon import Polygon
        from supervisely_lib.geometry.helpers import geometry_to_bitmap, geometry_to_polygon

        res = []
        if new_geometry == Bitmap:
            res = geometry_to_bitmap(self, radius=contour_radius)
        elif new_geometry == Rectangle:
            res = [self.to_bbox()]
        elif new_geometry == Polygon:
            res = geometry_to_polygon(self, approx_epsilon=approx_epsilon)

        if len(res) == 0:
            logger.warn(
                'Can not convert geometry {} to {} because geometry to convert is very small'
                .format(self.geometry_name(), new_geometry.geometry_name()))
        return res
Exemplo n.º 4
0
    def _get_ann(self, segm_path, inst_path, log_dct):
        # segmentation_img = self._read_img(segm_path)  # unused

        instance_img = self._read_img_unch(inst_path)
        colored_img = instance_img

        col2coord = get_col2coord(instance_img)
        curr_col2cls = {
            col: self.cls_names[int(
                col // 256)]  # some dirty hack to determine class correctly
            for col, coord in col2coord.items()
        }
        # _instance_ color -> class name

        imsize_wh = colored_img.shape[:2][::-1]
        figures = []
        for color, class_name in curr_col2cls.items():
            mask = colored_img == color  # exact match for 1d uint16
            objs = sly.FigureBitmap.from_mask(class_name, imsize_wh, (0, 0),
                                              mask)
            figures.extend(objs)
            colored_img[mask] = 0  # to check missing colors, see below

        if np.sum(colored_img) > 0:
            logger.warn(
                'Not all objects or classes are captured from source segmentation.',
                extra=log_dct)

        ann = sly.Annotation.new_with_objects(imsize_wh, figures)
        return ann
Exemplo n.º 5
0
def main():
    sly.task_verification(check_in_graph)

    logger.info('DTL started')
    helper = sly.DtlHelper()
    net = Net(helper.graph, helper.in_project_metas, helper.paths.results_dir)
    helper.save_res_meta(net.get_result_project_meta())

    # is_archive = net.is_archive()
    results_counter = 0
    for pr_name, pr_dir in helper.in_project_dirs.items():
        root_path, project_name = sly.ProjectFS.split_dir_project(pr_dir)
        project_fs = sly.ProjectFS.from_disk(root_path, project_name, by_annotations=True)
        progress = sly.progress_counter_dtl(pr_name, project_fs.image_cnt)
        for sample in project_fs:
            try:
                img_desc = sly.ImageDescriptor(sample)
                ann = sly.json_load(sample.ann_path)
                data_el = (img_desc, ann)
                export_output_generator = net.start(data_el)
                for res_export in export_output_generator:
                    logger.trace("image processed", extra={'img_name': res_export[0][0].get_img_name()})
                    results_counter += 1
            except Exception:
                ex = {
                    'project_name': sample.project_name,
                    'ds_name': sample.ds_name,
                    'image_name': sample.image_name
                }
                logger.warn('Image was skipped because some error occured', exc_info=True, extra=ex)
            progress.iter_done_report()

    logger.info('DTL finished', extra={'event_type': EventType.DTL_APPLIED, 'new_proj_size': results_counter})
Exemplo n.º 6
0
    def exec(self, uid, log_dct, f, *args, **kwargs):
        try:
            return f(*args, **kwargs)
        except Exception as e:
            self._lock.acquire()
            if uid not in self._failed_uids:
                self._failed_uids.add(uid)
                logger.warn('Sample processing error.',
                            exc_info=True,
                            extra={
                                **log_dct, 'exc_str': str(e)
                            })
            fail_cnt = len(self._failed_uids)
            self._lock.release()

            if fail_cnt > self.fails_allowed:
                raise RuntimeError(
                    'Too many errors occurred while processing samples. '
                    'Allowed: {}.'.format(self.fails_allowed))
Exemplo n.º 7
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))
                    })