コード例 #1
0
    def process(self, data_el):
        img_desc, ann = data_el
        free_name = self.net.get_free_name(img_desc)
        new_dataset_name = img_desc.get_res_ds_name()

        if self.settings.get('visualize'):
            out_meta = self.net.get_result_project_meta()
            cls_mapping = {}
            for cls_descr in out_meta.classes:
                color_s = cls_descr.get('color')
                if color_s is not None:
                    color = color_utils.hex2rgb(color_s)
                else:
                    color = color_utils.get_random_color()
                cls_mapping[cls_descr['title']] = color

            # hack to draw 'black' regions
            cls_mapping = {
                k: (1, 1, 1) if max(v) == 0 else v
                for k, v in cls_mapping.items()
            }

            vis_img = self.draw_colored_mask(ann, cls_mapping)
            orig_img = img_desc.read_image()
            comb_img = imaging.overlay_images(orig_img, vis_img, 0.5)

            sep = np.array([[[0, 255, 0]]] * orig_img.shape[0], dtype=np.uint8)
            img = np.hstack((orig_img, sep, comb_img))

            img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
            output_img_path = osp.join(self.output_folder, new_dataset_name,
                                       'visualize', free_name + '.png')
            os_utils.ensure_base_path(output_img_path)
            cv2.imwrite(output_img_path, img)

        ann_to_save = deepcopy(ann)
        ann_to_save.normalize_figures()
        packed_ann = ann_to_save.pack()

        dataset_name = img_desc.get_res_ds_name()
        if not self.out_project.datasets.has_key(dataset_name):
            self.out_project.create_dataset(dataset_name)
        out_dataset = self.out_project.datasets.get(dataset_name)

        out_item_name = free_name + img_desc.get_image_ext()

        # net _always_ downloads images
        if img_desc.need_write():
            out_dataset.add_item_np(out_item_name,
                                    img_desc.image_data,
                                    ann=packed_ann)
        else:
            out_dataset.add_item_file(out_item_name,
                                      img_desc.get_img_path(),
                                      ann=packed_ann)

        yield ([img_desc, ann], )
コード例 #2
0
    def process(self, data_el):
        img_desc, ann = data_el
        free_name = self.net.get_free_name(img_desc)
        new_dataset_name = img_desc.get_res_ds_name()

        for out_dir, flag_name, mapping_name in self.odir_flag_mapping:
            if not self.settings[flag_name]:
                continue
            cls_mapping = self.settings[mapping_name]

            # hack to draw 'black' regions
            if flag_name == 'masks_human':
                cls_mapping = {
                    k: (1, 1, 1) if max(v) == 0 else v
                    for k, v in cls_mapping.items()
                }

            img = self.draw_colored_mask(ann, cls_mapping)

            if flag_name == 'masks_human':
                orig_img = img_desc.read_image()
                comb_img = imaging.overlay_images(orig_img, img, 0.5)

                sep = np.array([[[0, 255, 0]]] * orig_img.shape[0],
                               dtype=np.uint8)
                img = np.hstack((orig_img, sep, comb_img))

            img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
            output_img_path = osp.join(self.output_folder, new_dataset_name,
                                       out_dir, free_name + '.png')
            os_utils.ensure_base_path(output_img_path)
            cv2.imwrite(output_img_path, img)

        # net _always_ downloads images
        if self.settings['images'] is True:
            if img_desc.need_write() is True:
                self.pr_writer.write_image(img_desc, free_name)
            else:
                self.pr_writer.copy_image(img_desc, free_name)

        if self.settings['annotations'] is True:
            ann_to_save = deepcopy(ann)
            ann_to_save.normalize_figures()
            packed_ann = ann_to_save.pack()
            self.pr_writer.write_ann(img_desc, packed_ann, free_name)

        yield ([img_desc, ann], )
コード例 #3
0
    def run_inference(self):
        out_project_fs = copy(self.in_project_fs)
        out_project_fs.root_path = self.helper.paths.results_dir
        out_project_fs.make_dirs()

        inference_feeder = InferenceFeederFactory.create(
            self.config, self.helper.in_project_meta,
            self.single_image_inference.train_classes)

        out_pr_meta = inference_feeder.out_meta
        out_pr_meta.to_dir(out_project_fs.project_path)

        ia_cnt = out_project_fs.pr_structure.image_cnt
        progress = progress_counter.progress_counter_inference(cnt_imgs=ia_cnt)

        for sample in self.in_project_fs:
            logger.trace('Will process image',
                         extra={
                             'dataset_name': sample.ds_name,
                             'image_name': sample.image_name
                         })
            ann_packed = json_utils.json_load(sample.ann_path)
            ann = sly_annotation.Annotation.from_packed(
                ann_packed, self.helper.in_project_meta)

            img = cv2.imread(sample.img_path)[:, :, ::-1]
            res_ann = inference_feeder.feed(
                img, ann, self.single_image_inference.inference)

            out_ann_fpath = out_project_fs.ann_path(sample.ds_name,
                                                    sample.image_name)
            res_ann_packed = res_ann.pack()
            json_utils.json_dump(res_ann_packed, out_ann_fpath)

            if self.debug_copy_images:
                out_img_fpath = out_project_fs.img_path(
                    sample.ds_name, sample.image_name)
                os_utils.ensure_base_path(out_img_fpath)
                shutil.copy(sample.img_path, out_img_fpath)

            progress.iter_done_report()

        progress_counter.report_inference_finished()