3d 点云格式转换 binary ascii binary_compressed

import shutil
from pathlib import Path
from traceback import format_exc

from log_color.log_color import Logger
from progress.bar import IncrementalBar

from pypcd.pypcd import PointCloud

id_type = {
    1: "ascii",
    2: "binary",
    3: "binary_compressed"
}


class ProcessData:
    def __init__(self, src, dst):
        self.src = Path(src)
        self.dst = Path(dst)
        if self.dst.exists():
            self.logger = Logger(name=str(self.dst.joinpath("30小时点云格式转换工具.log")), data_type="")
        else:
            self.logger = Logger(name="30小时点云格式转换工具.log")

    def start(self):
        self.logger.info("""1: ascii
2: binary
3: binary_compressed""")
        pcd_type_id = int(input("请选择目标格式(回车默认为3):") or 2)
        pcd_type = id_type.get(pcd_type_id, "binary")
        self.process_data(pcd_type)

    def process_data(self, pcd_type):
        pcd_file_list = list(self.src.rglob("*.pcd"))
        suffix = '%(percent)d%% [%(index)d/%(max)d in %(elapsed)ds (eta:%(eta_td)s)]'
        with IncrementalBar("进度:", max=len(pcd_file_list), suffix=suffix) as bar:
            for file in self.src.rglob("*.*"):
                try:
                    output_file = self.dst.joinpath(file.relative_to(self.src))
                    output_file.parent.mkdir(parents=True, exist_ok=True)
                    if file.suffix == ".pcd":
                        self.convert(file, output_file, pcd_type)
                        bar.next()
                    else:
                        shutil.copyfile(file, output_file)
                except Exception as e:
                    self.logger.error(f"{file}运行失败,跳过这个文件。{e}\n{format_exc()}")

    @staticmethod
    def convert(file, output_file, pcd_type):
        pcd = PointCloud.from_path(file)
        pcd.save_pcd(output_file, pcd_type)

    def check_path_exist(self):
        """
        :return:True/False
        """
        for input_path in self.__dict__.values():
            if isinstance(input_path, Path) and (not input_path.exists() or input_path == ""):
                self.logger.error(f"{input_path}不存在或者为空")
                return False
        return True


if __name__ == '__main__':
    while True:
        print("******** 开始 ********")
        input_folder = input("请输入源文件夹:").strip("\"")
        output_folder = input("请输入结果保存文件夹:").strip("\"")
        pd = ProcessData(src=input_folder, dst=output_folder)
        try:
            if pd.check_path_exist():
                pd.start()
            else:
                continue
        except Exception as ee:
            pd.logger.error(f"{format_exc()}:{ee}")
        print("\r******** 结束 ********")
        c = input("请输入q(不区分大小写)回车退出,按其他任意键回车继续:")
        if c.lower() == "q":
            break


posted @ 2022-12-28 16:38  不能说的秘密  阅读(363)  评论(0编辑  收藏  举报