SpacetySliceTools/generatorRasterSlicesTools/PredictProcessRaster_AC.py

187 lines
7.9 KiB
Python

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 {} --jmlc {}".format(
spacetySliceEnvPathExEPath,programpath,
srcFolderPath,outMJPortSumTxtPath,outMJPortFolderPath,
MLCShapeFilePath, JLCShapeFilePath, MJLCShapeFilePath
)
if os.system(cmdtxt) ==2:
print("sucess:",cmdtxt)
return "sucess: {}".format(cmdtxt)
else:
print("failed:",cmdtxt)
return "failed: {}".format(cmdtxt)
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")
MLCShapeFilePath=r"D:\TYSAR-德清院\目标点位信息更新\0828目标点位\港口(民船).shp"
JLCShapeFilePath=r"D:\TYSAR-德清院\目标点位信息更新\0828目标点位\军港.shp"
MJLCShapeFilePath=r"D:\TYSAR-德清院\目标点位信息更新\0828目标点位\军民一体港口.shp"
# 港口归属
if processMJPort(srcFolderPath,outMJPortSumTxtPath,outMJPortFolderPath,MLCShapeFilePath,JLCShapeFilePath,MJLCShapeFilePath)==2:
# 逐行处理
MJlines=None
with open(outMJPortSumTxtPath,"r",encoding="utf-8") as f:
MJlines=f.readlines()
MLCName = "MLC" # M
JLCName = "JLC" # J
MJLCName = "MJLC" # JM 混合
NOLCName = "NOLC" # 没有港口
async_results = []
with Pool(processes=6) as pool:
for linestr in MJlines:
if len(linestr)>3:
clsname=linestr.split("\t\t")[0]
tiffpath=linestr.split("\t\t")[1]
rootname=Path(tiffpath).stem
portTxtpath = os.path.join(outMJPortFolderPath, rootname + ".txt")
tarPortFolder=None
if clsname==MLCName:
tarPortFolder=MPortFolder
elif clsname==JLCName:
tarPortFolder=JPortFolder
elif clsname==MJLCName:
tarPortFolder=MJPortFolder
elif clsname==NOLCName:
tarPortFolder=NoPortFolder
else:
continue
async_results.append(preProcessTiFF(tiffpath,portTxtpath,tarPortFolder))
else:
continue
# 处理分块
for pidx in range(len(async_results)):
async_result=async_results[pidx]
print("{}/{}".format(pidx+1,len(async_results)),async_result.get())
writeoutlog(logPath,"{}/{} {}".format(pidx+1,len(async_results),async_result.get()))
pass
else:
pass
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,logPath)
#
# # 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, logPath)
#
# 20250826-不分类 条带模式
srcFolderPath = r"R:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250826-不分类\0-原图"
preFolderPath = r"D:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250826-不分类\A-预处理\AB-图像预处理"
targetfolderPath = r"R:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250826-不分类\A-预处理\AB-图像预处理"
preProcessShipPortTools(srcFolderPath, preFolderPath, targetfolderPath,logPath)