Пример #1
0
    def load_from_dir(cls,
                      img_dir: str,
                      json_dir: str,
                      show_pbar: bool = True) -> NDDS_Frame_Handler:
        check_dir_exists(json_dir)
        check_dir_exists(img_dir)

        img_pathlist = get_valid_image_paths(img_dir)
        json_path_list = [
            path for path in get_all_files_of_extension(dir_path=json_dir,
                                                        extension='json')
            if not get_filename(path).startswith('_')
        ]
        json_path_list.sort()
        handler = NDDS_Frame_Handler()
        if show_pbar:
            pbar = tqdm(total=len(json_path_list), unit='ann(s)', leave=True)
            pbar.set_description(f'Loading {cls.__name__}')
        for json_path in json_path_list:
            check_file_exists(json_path)
            json_rootname = get_rootname_from_path(json_path)
            matching_img_path = None
            matching_cs_img_path = None
            matching_depth_img_path = None
            matching_is_img_path = None
            for img_path in img_pathlist:
                img_rootname = '.'.join(get_filename(img_path).split('.')[:-1])
                if img_rootname == json_rootname:
                    matching_img_path = img_path
                elif img_rootname == f'{json_rootname}.cs':
                    matching_cs_img_path = img_path
                elif img_rootname == f'{json_rootname}.depth':
                    matching_depth_img_path = img_path
                elif img_rootname == f'{json_rootname}.is':
                    matching_is_img_path = img_path
                if matching_img_path and matching_cs_img_path and matching_depth_img_path and matching_is_img_path:
                    break
            if matching_img_path is None:
                logger.error(
                    f"Couldn't find image file that matches rootname of {get_filename(json_path)} in {img_dir}"
                )
                raise FileNotFoundError
            frame = NDDS_Frame(
                img_path=matching_img_path,
                ndds_ann=NDDS_Annotation.load_from_path(json_path),
                cs_img_path=matching_cs_img_path,
                depth_img_path=matching_depth_img_path,
                is_img_path=matching_is_img_path)
            handler.append(frame)
            if show_pbar:
                pbar.update()
        return handler
Пример #2
0
 def init_save_dir(self):
     make_dir_if_not_exists(self._save_dir)
     if self._clear:
         delete_all_files_in_dir(self._save_dir, ask_permission=False)
     else:
         self._existing_extensions = list(set([get_extension_from_path(path) for path in get_valid_image_paths(self._save_dir)]))
     self._first = False