From c689b2d84da87e1ea8ad34c74324b10c12d44988 Mon Sep 17 00:00:00 2001 From: chenzenghui <3045316072@qq.com> Date: Fri, 26 Sep 2025 12:17:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=B8=AF=E5=8F=A3=E5=88=87?= =?UTF-8?q?=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../LabelPortShipRasterSlice.py | 1 - .../PortRasterSlice_Batch.py | 135 ++++++++++++++++++ 2 files changed, 135 insertions(+), 1 deletion(-) delete mode 100644 LabelPortShipRasterSlice/LabelPortShipRasterSlice.py create mode 100644 LabelPortShipRasterSlice/PortRasterSlice_Batch.py diff --git a/LabelPortShipRasterSlice/LabelPortShipRasterSlice.py b/LabelPortShipRasterSlice/LabelPortShipRasterSlice.py deleted file mode 100644 index 0aba17d..0000000 --- a/LabelPortShipRasterSlice/LabelPortShipRasterSlice.py +++ /dev/null @@ -1 +0,0 @@ -import os \ No newline at end of file diff --git a/LabelPortShipRasterSlice/PortRasterSlice_Batch.py b/LabelPortShipRasterSlice/PortRasterSlice_Batch.py new file mode 100644 index 0000000..215a4b6 --- /dev/null +++ b/LabelPortShipRasterSlice/PortRasterSlice_Batch.py @@ -0,0 +1,135 @@ +import numpy as np +import os +from glob import glob +from pathlib import Path +from multiprocessing import Pool +######################################################## +# 函数区 +######################################################## + +spacetySliceEnvPathExEPath=r"d:\ProgramData\anaconda3\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('*.tiff'))+list(path.rglob('*.tif')) + # 将Path对象转换为字符串路径 + return [str(file) for file in tif_files] + + +def find_txt_files_pathlib(directory): + """ + 使用pathlib.Path.rglob递归查找指定目录下所有.tif和.tiff文件 + + 参数: + directory (str): 要搜索的根目录路径 + + 返回: + list: 包含所有找到的.tif/.tiff文件完整路径的列表 + """ + path = Path(directory) + # 使用rglob递归匹配所有.tif和.tiff文件 + tif_files = list(path.rglob('*.txt')) + # 将Path对象转换为字符串路径 + return [str(file) for file in tif_files] + + +def matchTiff_Txt(tiffpaths,txtPaths): + match_results={} + tiffID=0 + for tid in range(len(tiffpaths)): + tiffID=tiffID+1 + match_results[tiffID]={"tiff":tiffpaths[tid],"txt":[]} + rootname=Path(tiffpaths[tid]).stem + for txtpath in txtPaths: + txtrootname=Path(txtpath).stem + if txtrootname.startswith(rootname): + match_results[tiffID]["txt"].append(txtpath) + + return match_results + +def sliceTiFFAndTxt(match_meta,outfolderpath): + resultTxt="" + programpath = r"R:\TYSAR-德清院\A-预处理-未标注\A0-算法版本\AA\SpacetySliceDataTools\LabelPortShipRasterSlice\DataSamplePortSliceRaster_AA.py" + tiffPath=match_meta["tiff"] + for txtpath in match_meta["txt"]: + cmdtxt = r"{} {} -i {} -l {} -o {}".format(spacetySliceEnvPathExEPath, programpath, + tiffPath, + txtpath, + outfolderpath + ) + + if os.system(cmdtxt) == 2: + resultTxt="{}\nsucess: {}".format(resultTxt,txtpath) + print("sucess:", cmdtxt) + + else: + resultTxt = "{}\nfailed: {}".format(resultTxt, txtpath) + print("failed:", cmdtxt) + + + +if __name__ == '__main__': + preFolderPath = r"R:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250813-不分类\0-原图" + dotafolderPath=r"D:\港口\港口dota" + outfolderPath=r"D:\港口\切片结果" + tiffPaths = find_tif_files_pathlib(preFolderPath) + txtPaths = find_txt_files_pathlib(dotafolderPath) + match_results=matchTiff_Txt(tiffPaths,txtPaths) + + for tiffID in match_results: + match_meta=match_results[tiffID] + sliceTiFFAndTxt(match_meta,outfolderPath) + + preFolderPath = r"R:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250818-不分类\0-原图" + dotafolderPath=r"D:\港口\港口dota" + outfolderPath=r"D:\港口\切片结果" + tiffPaths = find_tif_files_pathlib(preFolderPath) + txtPaths = find_txt_files_pathlib(dotafolderPath) + match_results=matchTiff_Txt(tiffPaths,txtPaths) + + for tiffID in match_results: + match_meta=match_results[tiffID] + sliceTiFFAndTxt(match_meta,outfolderPath) + + preFolderPath = r"R:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250826-不分类\0-原图" + dotafolderPath=r"D:\港口\港口dota" + outfolderPath=r"D:\港口\切片结果" + tiffPaths = find_tif_files_pathlib(preFolderPath) + txtPaths = find_txt_files_pathlib(dotafolderPath) + match_results=matchTiff_Txt(tiffPaths,txtPaths) + + for tiffID in match_results: + match_meta=match_results[tiffID] + sliceTiFFAndTxt(match_meta,outfolderPath) + + preFolderPath = r"R:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250903-不分类\0-原图" + dotafolderPath=r"D:\港口\港口dota" + outfolderPath=r"D:\港口\切片结果" + tiffPaths = find_tif_files_pathlib(preFolderPath) + txtPaths = find_txt_files_pathlib(dotafolderPath) + match_results=matchTiff_Txt(tiffPaths,txtPaths) + + for tiffID in match_results: + match_meta=match_results[tiffID] + sliceTiFFAndTxt(match_meta,outfolderPath) + + preFolderPath = r"R:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250910-不分类\0-原图" + dotafolderPath=r"D:\港口\港口dota" + outfolderPath=r"D:\港口\切片结果" + tiffPaths = find_tif_files_pathlib(preFolderPath) + txtPaths = find_txt_files_pathlib(dotafolderPath) + match_results=matchTiff_Txt(tiffPaths,txtPaths) + + for tiffID in match_results: + match_meta=match_results[tiffID] + sliceTiFFAndTxt(match_meta,outfolderPath) \ No newline at end of file