From 4cad12128c85682972ef1d670b41fad667032ec5 Mon Sep 17 00:00:00 2001 From: chenzenghui <3045316072@qq.com> Date: Thu, 25 Sep 2025 20:30:08 +0800 Subject: [PATCH] =?UTF-8?q?MJ=E8=88=B0=E8=88=B9=E5=8C=BA=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SpitShipLabelTools/SpliteShipPort_AB.py | 143 +++++++++++++++ .../PredictProcessRasterMJLCPort_AC.py | 163 ++++++++++++++++++ .../PredictProcessRaster_AC.py | 2 +- .../SpacetyTIFFDataStretch2PNG_AC.py | 4 +- tools/SpliteShipPort_AA.py | 5 +- 5 files changed, 313 insertions(+), 4 deletions(-) create mode 100644 SpitShipLabelTools/SpliteShipPort_AB.py create mode 100644 generatorRasterSlicesTools/PredictProcessRasterMJLCPort_AC.py diff --git a/SpitShipLabelTools/SpliteShipPort_AB.py b/SpitShipLabelTools/SpliteShipPort_AB.py new file mode 100644 index 0000000..4e91ae8 --- /dev/null +++ b/SpitShipLabelTools/SpliteShipPort_AB.py @@ -0,0 +1,143 @@ +from opcode import opname + +from osgeo import ogr +import os +import argparse +from osgeo import ogr, gdal +import os +import argparse +import numpy as np +from scipy.spatial import KDTree +from tools.DotaOperator import DotaObj,readDotaFile,writerDotaFile,createDota +from glob import glob +from pathlib import Path +import shutil +from tqdm import tqdm + + +MLCName="MLC" # M +JLCName="JLC" # J +MJLCName="MJLC" # JM 混合 +NOLCName="NOLC" # 没有港口 + + +def existOrCreate(dirpath): + if not os.path.exists(dirpath): + os.makedirs(dirpath) + + +def find_tifPort_files_pathlib(directory): + path = Path(directory) + # 使用rglob递归匹配所有.tif和.tiff文件 + tif_files = list(path.rglob('sumMJPort.txt')) + # 将Path对象转换为字符串路径 + return [str(file) for file in tif_files] + + +def find_label_files_pathlib(directory): + path = Path(directory) + # 使用rglob递归匹配所有.tif和.tiff文件 + tif_files = list(path.rglob('*.txt')) + # 将Path对象转换为字符串路径 + return [str(file) for file in tif_files] + +def read_MJPortFile(sumMJPortfilepath,deepsdict=None): + lines=None + with open(sumMJPortfilepath,'r',encoding="utf-8") as f: + lines= f.readlines() + if deepsdict is None : + deepsdict={} + for linestr in lines: + if len(linestr)<3: + continue + clsname=linestr.split("\t\t")[0] + tiffpath=linestr.split("\t\t")[1].replace("\n","") + rootname=Path(tiffpath).stem + deepsdict[rootname]=clsname + return deepsdict + + + +def SpliteProcess(srcfolderpath,infolderpath,outfolderpath): + sumMJPortPaths=find_tifPort_files_pathlib(srcfolderpath) + sumMJPortDict={} + print("读取港口归属文件") + for sumMJPortfilepath in tqdm(sumMJPortPaths): + sumMJPortDict=read_MJPortFile(sumMJPortfilepath,sumMJPortDict) + + labeltxtPaths=find_label_files_pathlib(infolderpath) + + JportLabel=os.path.join(outfolderpath,'军港') + MportLabel=os.path.join(outfolderpath,'民港') + MJportLabel=os.path.join(outfolderpath,'混合港') + NoportLabel=os.path.join(outfolderpath,'无港口') + + existOrCreate(JportLabel) + existOrCreate(MportLabel) + existOrCreate(MJportLabel) + existOrCreate(NoportLabel) + + # 软件 + for labeltxtpath in tqdm(labeltxtPaths): + rootname=Path(labeltxtpath).stem + tiffpath=labeltxtpath.replace(".txt",".tif") + if not os.path.exists(tiffpath): + tiffpath=labeltxtpath.replace(".txt",".tiff") + if not os.path.exists(tiffpath): + print("error not fount: ",tiffpath) + rootname=rootname.replace("_image.txt","").replace("_image","") + idx=rootname.rfind("_") + rootname=rootname[:idx] + trgpath=None + + if rootname in sumMJPortDict: + clsname=sumMJPortDict[rootname] + if MLCName == clsname: # M + trgpath=MportLabel + elif JLCName == clsname: # J + trgpath=JportLabel + elif MJLCName == clsname: # JM 混合 + trgpath=MJportLabel + elif NOLCName == clsname: # 没有港口 + trgpath=NoportLabel + clsname="MLC" + else: + print("error: ", Path(labeltxtpath).stem) + continue + + # shutil.copy(labeltxtpath, os.path.join(trgpath, os.path.basename(labeltxtpath))) + newlabelpath=os.path.join(trgpath, os.path.basename(labeltxtpath)) + dotametas=readDotaFile(labeltxtpath) + for dotaid in range(len(dotametas)): + dotametas[dotaid].clsname=clsname + writerDotaFile(dotametas,newlabelpath) + shutil.copy(tiffpath, os.path.join(trgpath, os.path.basename(tiffpath))) + else: + print("error: ", Path(labeltxtpath).stem) + + return True + pass + + +def getParams(): + parser = argparse.ArgumentParser() + parser.add_argument('-s','--srcfolder',type=str,default=r'D:\TYSAR-德清院\TYSAR-条带模式(SM)\港口', help='输入shapefile文件') + parser.add_argument('-i','--inlabelfolder',type=str,default=r'R:\TYSAR-德清院\D-切片成果\TYSAR-条带模式(SM)\港口\切片结果整理', help='输入shapefile文件') + parser.add_argument('-o', '--outfolder',type=str,default=r'R:\TYSAR-德清院\D-切片成果\TYSAR-条带模式(SM)\港口\舰船') + args = parser.parse_args() + return args + +if __name__ == '__main__': + try: + parser = getParams() + srcfolder=parser.srcfolder + infolder=parser.inlabelfolder + outfolder=parser.outfolder + print('srcfolder=',srcfolder) + print('infolder=',infolder) + print('outfolder=',outfolder) + SpliteProcess(srcfolder,infolder,outfolder) + exit(2) + except Exception as e: + print(e) + exit(3) diff --git a/generatorRasterSlicesTools/PredictProcessRasterMJLCPort_AC.py b/generatorRasterSlicesTools/PredictProcessRasterMJLCPort_AC.py new file mode 100644 index 0000000..449bd6c --- /dev/null +++ b/generatorRasterSlicesTools/PredictProcessRasterMJLCPort_AC.py @@ -0,0 +1,163 @@ +import numpy as np +import os +from glob import glob +from pathlib import Path +from multiprocessing import Pool +import hashlib # md5 +import time +import shutil +import datetime +######################################################## +# 函数区 +######################################################## + +spacetySliceEnvPathExEPath=r"d:\ProgramData\anaconda3\envs\spacetySliceEnv\python.exe" + +logPath=r"R:\TYSAR-德清院\A-预处理-未标注\A0-算法版本\AA\SpacetySliceDataTools\log\process.log" + + +def existOrCreate(dirpath): + if not os.path.exists(dirpath): + os.makedirs(dirpath) + +def moveDir(srcfolderPath,targetfolderPath): + try: + shutil.copytree(srcfolderPath,targetfolderPath,dirs_exist_ok=True) + print("sucess: copy ", srcfolderPath,targetfolderPath ) + shutil.rmtree(srcfolderPath) + print("sucess: rmtree ", srcfolderPath) + return "sucess: copy and rmtree from {} to {}".format(srcfolderPath,targetfolderPath),True + except Exception as e: + print("failed: copy ", srcfolderPath, targetfolderPath) + return "failed: copy and rmtree from {} to {}".format(srcfolderPath,targetfolderPath),False + +def writeoutlog(logPath,cmdtxt): + with open(logPath,'a',encoding="utf-8") as f: + f.write(cmdtxt) + f.write("\n") + + +def preProcessTiFF(tiffpath,txtpath,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\generatorRasterSlicesTools\SpacetyTIFFDataStretch2PNG_AC.py" + + # cmdtxt=r"{} {} -i {} -o {} --filemode --SquareRoot".format(spacetySliceEnvPathExEPath,programpath,tiffpath,outpngpath) + cmdtxt=r"{} {} -i {} -p {} -o {} --slicemode --SquareRoot".format( + spacetySliceEnvPathExEPath,programpath, + tiffpath, + txtpath, + preFolderPath + ) # 直接切片 + if os.system(cmdtxt) ==2: + print("sucess:",cmdtxt) + writeoutlog(logPath, "sucess: {}\n".format(cmdtxt)) + return 2 + else: + print("failed:",cmdtxt) + writeoutlog(logPath, "failed: {}\n".format(cmdtxt)) + return 3 + + +def processMJPort(srcFolderPath,outMJPortSumTxtPath,outMJPortFolderPath,MLCShapeFilePath,JLCShapeFilePath,MJLCShapeFilePath): + programpath = r"R:\TYSAR-德清院\A-预处理-未标注\A0-算法版本\AA\SpacetySliceDataTools\generatorRasterSlicesTools\SplitShipPortRasterTools_AC.py" + cmdtxt=r"{} {} -s {} -o {} -f {} -m {} -j {} -jm {}".format( + spacetySliceEnvPathExEPath,programpath, + srcFolderPath,outMJPortSumTxtPath,outMJPortFolderPath, + MLCShapeFilePath, JLCShapeFilePath, MJLCShapeFilePath + ) + if os.system(cmdtxt) ==2: + print("sucess:",cmdtxt) + writeoutlog(logPath, "sucess: {}\n".format(cmdtxt)) + # return "sucess: {}".format(cmdtxt) + return 2 + else: + print("failed:",cmdtxt) + writeoutlog(logPath, "failed: {}\n".format(cmdtxt)) + # return "failed: {}".format(cmdtxt) + return 3 + + +def preProcessShipPortTools(srcFolderPath,targetFolderPath,outTargetFolderPath): + writeoutlog(logPath, "====================================================================================\n") + writeoutlog(logPath, "time: {}".format(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))) + writeoutlog(logPath, "srcFolderPath: {}".format(srcFolderPath)) + writeoutlog(logPath, "targetFolderPath: {}".format(targetFolderPath)) + writeoutlog(logPath, "process start \n") + # + # # 文件接口创建 + # MPortFolder=os.path.join(targetFolderPath,"AC-图像预处理","民港口") + # JPortFolder=os.path.join(targetFolderPath,"AC-图像预处理","军港口") + # MJPortFolder=os.path.join(targetFolderPath,"AC-图像预处理","混合港口") + # NoPortFolder=os.path.join(targetFolderPath,"AC-图像预处理","无港口") + # + # existOrCreate(MPortFolder) + # existOrCreate(JPortFolder) + # existOrCreate(MJPortFolder) + # existOrCreate(NoPortFolder) + + outMJPortSumTxtPath=os.path.join(targetFolderPath,"AC-图像预处理","sumMJPort.txt") + outMJPortFolderPath=os.path.join(targetFolderPath,"AC-图像预处理","sumMJPortFolder") + + existOrCreate(outMJPortFolderPath) + + MLCShapeFilePath=r"D:\TYSAR-德清院\目标点位信息更新\0828目标点位\港口(民船).shp" + JLCShapeFilePath=r"D:\TYSAR-德清院\目标点位信息更新\0828目标点位\军港.shp" + MJLCShapeFilePath=r"D:\TYSAR-德清院\目标点位信息更新\0828目标点位\军民一体港口.shp" + + processMJPortflag=processMJPort(srcFolderPath,outMJPortSumTxtPath,outMJPortFolderPath,MLCShapeFilePath,JLCShapeFilePath,MJLCShapeFilePath) + + writeoutlog(logPath, "\nprocess finished \n") + # moverTip,moveflag=moveDir(targetFolderPath, outTargetFolderPath) + # writeoutlog(logPath, moverTip) + writeoutlog(logPath, "====================================================================================\n") + # if moveflag: + # pass + # else: + # exit(3) + + +if __name__ == '__main__': + + # 20250813-不分类 条带模式 + srcFolderPath = r"R:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250813-不分类\0-原图" + preFolderPath = r"D:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250813-不分类\A-预处理\AB-图像预处理" + targetfolderPath = r"D:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250813-不分类\A-预处理\AB-图像预处理" + preProcessShipPortTools(srcFolderPath, preFolderPath, targetfolderPath) + # + # 20250818 条带模式 + srcFolderPath = r"R:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250818-不分类\0-原图" + preFolderPath = r"D:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250818-不分类\A-预处理\AB-图像预处理" + targetfolderPath = r"R:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250818-不分类\A-预处理\AB-图像预处理" + preProcessShipPortTools(srcFolderPath, preFolderPath,targetfolderPath) + # + # 20250826-不分类 条带模式 + srcFolderPath = r"R:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250826-不分类\0-原图" + preFolderPath = r"D:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250826-不分类\A-预处理" + targetfolderPath = r"R:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250826-不分类\A-预处理" + preProcessShipPortTools(srcFolderPath, preFolderPath,targetfolderPath) + # + # 20250903-不分类条带模式 + srcFolderPath = r"R:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250903-不分类\0-原图" + preFolderPath = r"D:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250903-不分类\A-预处理" + targetfolderPath = r"R:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250903-不分类\A-预处理" + preProcessShipPortTools(srcFolderPath, preFolderPath,targetfolderPath) + # + # 20250910-不分类 条带模式 + srcFolderPath = r"R:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250910-不分类\0-原图" + preFolderPath = r"D:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250910-不分类\A-预处理" + targetfolderPath = r"R:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250910-不分类\A-预处理" + preProcessShipPortTools(srcFolderPath, preFolderPath,targetfolderPath) + + + + + + + + + + diff --git a/generatorRasterSlicesTools/PredictProcessRaster_AC.py b/generatorRasterSlicesTools/PredictProcessRaster_AC.py index 231eedf..2a31495 100644 --- a/generatorRasterSlicesTools/PredictProcessRaster_AC.py +++ b/generatorRasterSlicesTools/PredictProcessRaster_AC.py @@ -140,7 +140,7 @@ def preProcessShipPortTools(srcFolderPath,targetFolderPath,outTargetFolderPath): tarPortFolder=NoPortFolder else: continue - async_results.append(preProcessTiFF(tiffpath,portTxtpath,tarPortFolder)) + # async_results.append(preProcessTiFF(tiffpath,portTxtpath,tarPortFolder)) else: continue # 处理分块 diff --git a/generatorRasterSlicesTools/SpacetyTIFFDataStretch2PNG_AC.py b/generatorRasterSlicesTools/SpacetyTIFFDataStretch2PNG_AC.py index 4284e64..16d6ced 100644 --- a/generatorRasterSlicesTools/SpacetyTIFFDataStretch2PNG_AC.py +++ b/generatorRasterSlicesTools/SpacetyTIFFDataStretch2PNG_AC.py @@ -437,10 +437,10 @@ def stretchSliceProcess(infilepath, outfolder,portfilestr, strechmethod): allImagePath=os.path.join(allpngfolder, rootname+"_all.png") Image.fromarray(im_data).save(allImagePath,compress_level=0) slice_ID=0 - slice_ID=sliceShipDataset(rootname,im_data, src_im_data,im_Geotrans, im_proj, outfolder) + slice_ID=sliceShipDataset(rootname,im_data, src_im_data,im_Geotrans, im_proj, outfolder) # 舰船切片 slice_ID=slice_ID+1 # slice_ID=slicePortDataset(rootname,im_data, src_im_data,im_Geotrans, im_proj, outfolder,slice_ID,portfilestr) - slice_ID=sliceLabelPortDataset(rootname,im_data, src_im_data,im_Geotrans, im_proj, outfolder,slice_ID,portfilestr) + slice_ID=sliceLabelPortDataset(rootname,im_data, src_im_data,im_Geotrans, im_proj, outfolder,slice_ID,portfilestr) # 港口拉伸 print("图像切片与拉伸完成") pass diff --git a/tools/SpliteShipPort_AA.py b/tools/SpliteShipPort_AA.py index b66a019..13530a8 100644 --- a/tools/SpliteShipPort_AA.py +++ b/tools/SpliteShipPort_AA.py @@ -6,11 +6,14 @@ import os import argparse import numpy as np from scipy.spatial import KDTree -from DotaOperator import DotaObj,readDotaFile,writerDotaFile,createDota +from tools.DotaOperator import DotaObj,readDotaFile,writerDotaFile,createDota from glob import glob from pathlib import Path import shutil +def existOrCreate(dirpath): + if not os.path.exists(dirpath): + os.makedirs(dirpath) def find_tif_files_pathlib(directory): path = Path(directory)