import numpy as np import os from glob import glob from pathlib import Path from multiprocessing import Pool ######################################################## # 函数区 ######################################################## spacetySliceEnvPathExEPath=r"D:\ProgramData\miniconda3\envs\spacetySliceEnv\python.exe" def find_tif_files_pathlib(directory): """ 使用pathlib.Path.rglob递归查找指定目录下所有.tif和.tiff文件 参数: directory (str): 要搜索的根目录路径 返回: list: 包含所有找到的.tif/.tiff文件完整路径的列表 """ path = Path(directory) # 使用rglob递归匹配所有.tif和.tiff文件 tif_files = list(path.rglob('*.tif')) + list(path.rglob('*.tiff')) # 将Path对象转换为字符串路径 return [str(file) for file in tif_files] def find_shp_files_pathlib(directory): """ 使用pathlib.Path.rglob递归查找指定目录下所有.tif和.tiff文件 参数: directory (str): 要搜索的根目录路径 返回: list: 包含所有找到的.tif/.tiff文件完整路径的列表 """ path = Path(directory) # 使用rglob递归匹配所有.tif和.tiff文件 tif_files = list(path.rglob('*.shp')) # 将Path对象转换为字符串路径 return [str(file) for file in tif_files] def preProcessTiFF(tiffpath,preFolderPath): if not os.path.exists(preFolderPath): os.makedirs(preFolderPath) file_path = Path(tiffpath) directory_path = str(file_path.resolve().parent.name) rootname=Path(tiffpath).stem outpngpath=os.path.join(preFolderPath,"I"+directory_path+"_"+rootname+'.png') programpath=r"R:\TYSAR-德清院\A-预处理-未标注\A0-算法版本\AA\SpacetySliceDataTools\tools\SpacetyTIFFDataStretch2PNG_AB.py" cmdtxt=r"{} {} -i {} -o {} --filemode --SquareRoot".format(spacetySliceEnvPathExEPath,programpath,tiffpath,outpngpath) if os.system(cmdtxt) ==2: print("sucess:",cmdtxt) return "sucess: {}".format(cmdtxt) else: print("failed:",cmdtxt) return "failed: {}".format(cmdtxt) pass def preProcessLabelshapefile(shpfilepath,preFolderPath): if not os.path.exists(preFolderPath): os.makedirs(preFolderPath) file_path = Path(shpfilepath) directory_path = str(file_path.resolve().parent.name) rootname = Path(shpfilepath).stem txtpath=os.path.join(preFolderPath,r"I"+directory_path+"_"+rootname+'.txt') programpath=r"R:\TYSAR-德清院\A-预处理-未标注\A0-算法版本\AA\SpacetySliceDataTools\tools\shapefile2dota.py" cmdtxt=r"{} {} -i {} -o {}".format(spacetySliceEnvPathExEPath,programpath,shpfilepath,txtpath) if os.system(cmdtxt) ==2: print("sucess:",cmdtxt) return "sucess: {}".format(cmdtxt) else: print("failed:",cmdtxt) return "failed: {}".format(cmdtxt) pass ######################################################## # 流程执行区 ######################################################## if __name__ == '__main__': # 1. 预处理流程 async_results = [] with Pool() as pool: # 聚束模式 srcFolderPath = r"D:\Annotation_Y\港口\聚束模式" tiffPaths = find_tif_files_pathlib(srcFolderPath) shpPaths = find_shp_files_pathlib(srcFolderPath) preFolderPath = r"D:\Annotation_Y\预处理\聚束模式" for tiffpath in tiffPaths: async_results.append( pool.apply_async(preProcessTiFF, (tiffpath,preFolderPath,)) ) for shppath in shpPaths: async_results.append( pool.apply_async(preProcessLabelshapefile, (shppath,preFolderPath,)) ) # 条带模式 srcFolderPath = r"D:\Annotation_Y\港口\条带模式" tiffPaths = find_tif_files_pathlib(srcFolderPath) shpPaths = find_shp_files_pathlib(srcFolderPath) preFolderPath = r"D:\Annotation_Y\预处理\条带模式" for tiffpath in tiffPaths: async_results.append( pool.apply_async(preProcessTiFF, (tiffpath,preFolderPath,)) ) for shppath in shpPaths: async_results.append( pool.apply_async(preProcessLabelshapefile, (shppath,preFolderPath,)) ) for async_result in async_results: print(async_result.get())