Example #1
0
def confirm_folder(path, mode):
    # make_dir_if_not_exists(path)
    if mode == 'save':
        if dir_exists(path):
            delete_dir(path)
            make_dir(path)
        else:
            make_dir(path)
Example #2
0
 def confirm_folder(path, mode):
     '''Deletes the directory if exist and create new directory.'''
     # make_dir_if_not_exists(path)
     if mode == 'save':
         if dir_exists(path):
             delete_dir(path)
             make_dir(path)
         else:
             make_dir(path)
Example #3
0
    def run(self):
        if not path_exists(self.ndds_dir):
            printj.red(f"Input ndds_dir path does not exist./n{self.ndds_dir}")
        make_dir_if_not_exists(dir_path=self.output_dir)
        coco_data_dir = os.path.join(
            self.output_dir,
            f"{get_filename_from_path(self.ndds_dir)}_coco_data")
        delete_dir_if_exists(dir_path=coco_data_dir)
        make_dir(dir_path=coco_data_dir)

        ndds_dataset = NDDS_Dataset.load_from_dir(json_dir=self.ndds_dir)
Example #4
0
    def infer(self, input_type: Union[str, int],
              output_type: Union[str, int],
              input_path: Union[str, List[str]],
              output_path: Union[str, List[str]],
              show_max_score_only: bool = False,
              show_class_label: bool = True,
              show_class_label_score_only: bool = False,
              show_keypoint_label: bool = True,
              show_legends: bool = False,
              show_bbox: bool = True,
              show_keypoints: bool = True,
              show_segmentation: bool = True,
              color_bbox: list = None,
              transparent_mask: bool = True,
              transparency_alpha: float = 0.3,
              ignore_keypoint_idx=None,
              gt_path: Union[str, List[str]] = None,
              result_json_path: str = None):
        """

        Valid options for,
        === 
        input_type:
        --- 
        ["image", "image_list", "image_directory", "image_directories_list", "video",
        "video_list", "video_directory" ]

        output_type: 
        ---
        ["return_summary", "show_image", "write_image", "write_video" ]

        Returns
        ---
        The inference result of all data formats.
        """
        self.counter = infinite_sequence()
        check_value(input_type,
                    check_from=["image", "image_list", "image_directory", "image_directories_list", "video",
                                "video_list", "video_directory"])
        check_value(value=output_type, check_from=[
                    "show_image", "write_image", "write_video"])
        self.gt_path = gt_path
        if self.gt_path:
            check_file_exists(gt_path)
            with open(gt_path) as json_file:
                self.gt_data = json.load(json_file)
        if result_json_path is None:
            if dir_exists(output_path):
                self.result_json_path = f'{output_path}/result.json'
            else:
                _p = output_path.split('.')
                _output_path = '.'.join(_p[:-1])
                self.result_json_path = f'{_output_path}.json'

        predict_image = partial(self._infer_image,
                                show_max_score_only=show_max_score_only,
                                show_class_label=show_class_label,
                                show_class_label_score_only=show_class_label_score_only,
                                show_keypoint_label=show_keypoint_label,
                                show_bbox=show_bbox, show_keypoints=show_keypoints, show_segmentation=show_segmentation,
                                color_bbox=color_bbox,
                                transparent_mask=transparent_mask, transparency_alpha=transparency_alpha,
                                ignore_keypoint_idx=ignore_keypoint_idx,
                                show_legends=show_legends
                                # gt_path=gt_path,
                                )
        if input_type == "image":
            if file_exists(input_path):
                output = predict_image(input_path)
            else:
                raise Error
            if output_type == "return_summary":
                return self.pred_dataset
            elif output_type == "show_image":
                show_image(output)
            elif output_type == "write_image":
                cv2.imwrite(f'{output_path}', output)
            elif output_type == "write_video":
                raise NotImplementedError
            else:
                raise Exception
        elif input_type == "image_list":
            for image_path in tqdm(input_path, colour='#66cc66'):
                output = predict_image(image_path)
                if output_type == "show_image":
                    if show_image(output):
                        break
                elif output_type == "write_image":
                    make_dir_if_not_exists(output_path)
                    filename = f.get_filename(image_path)
                    cv2.imwrite(f'{output_path}/{filename}', output)
                elif output_type == "write_video":
                    raise NotImplementedError
                else:
                    raise Exception
        elif input_type == "image_directory":
            image_path_list = f.dir_contents_path_list_with_extension(
                dirpath=input_path,
                extension=[".png", ".jpg", ".jpeg"])
            for image_path in tqdm(image_path_list, colour='#66cc66'):
                output = predict_image(image_path)
                # output = self.draw_gt(gt_path, gt_data, image_path, output)
                if output_type == "show_image":
                    if show_image(output):
                        break
                elif output_type == "write_image":
                    make_dir_if_not_exists(output_path)
                    filename = f.get_filename(image_path)
                    cv2.imwrite(f'{output_path}/{filename}', output)
                elif output_type == "write_video":
                    raise NotImplementedError
                else:
                    raise Exception
        elif input_type == "image_directories_list":
            for image_directory in tqdm(input_path):
                image_path_list = f.dir_contents_path_list_with_extension(
                    dirpath=input_path,
                    extension=[".png", ".jpg", ".jpeg"])
                for image_path in tqdm(image_path_list, colour='#66cc66'):
                    output = predict_image(image_path)
                    if output_type == "show_image":
                        if show_image(output):
                            break
                    elif output_type == "write_image":
                        filename = f.get_filename(image_path)
                        directory_name = f.get_directory_name(image_path)
                        if f.dir_exists(f'{output_path}/{directory_name}'):
                            f.delete_all_files_in_dir(
                                f'{output_path}/{directory_name}')
                        else:
                            f.make_dir(f'{output_path}/{directory_name}')
                        cv2.imwrite(
                            f'{output_path}/{directory_name}/{filename}', output)
                    elif output_type == "write_video":
                        raise NotImplementedError
                    else:
                        raise Exception
        elif input_type == "video":
            raise NotImplementedError
        elif input_type == "video_list":
            raise NotImplementedError
        elif input_type == "video_directory":
            raise NotImplementedError
        else:
            raise Exception
        # printj.cyan(self.pred_dataset)
        self.write_predictions_json()
        if self.gt_path:
            # print(self.df)
            # pip install openpyxl
            self.df.to_excel(os.path.abspath(
                f'{result_json_path}/../test_data.xlsx'))