SpacetySliceTools/tools/FilterLabelEmptyFile.py

79 lines
2.6 KiB
Python

# 统计结果
import numpy as np
import os
from glob import glob
from pathlib import Path
from multiprocessing import Pool
import argparse
import shutil
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 statictNumber(inlabelfilepath):
with open(inlabelfilepath,"r",encoding="utf-8") as f:
lines = f.readlines()
lineresult=[]
for l in lines:
if len(l) > 10:
lineresult.append(l)
return len(lineresult)
def FilterLabelEmptyFile(inlabelfilepath,outlabelfolder):
labelfilepaths=find_txt_files_pathlib(inlabelfolder)
for labelfilepath in labelfilepaths:
rootname=Path(labelfilepath).stem
tiffname=os.path.splitext(labelfilepath)[0]+".tiff"
pngname = os.path.splitext(labelfilepath)[0] + ".png"
labelnum=statictNumber(labelfilepath)
if(labelnum>=1):
outPath=os.path.join(outlabelfolder,rootname+".txt")
outPath2 = os.path.join(outlabelfolder, rootname + ".tiff")
outPath3 = os.path.join(outlabelfolder, rootname + ".png")
shutil.copy(labelfilepath,outPath)
# shutil.move(labelfilepath,outPath)
# if not os.path.exists(tiffname):
# continue
# shutil.move(tiffname, outPath2)
# shutil.copy(tiffname, outPath2)
# shutil.move(pngname, outPath3)
# shutil.copy(pngname, outPath3)
def getParams():
parser = argparse.ArgumentParser()
parser.add_argument('-i','--inlabelfolder',type=str,default=r'R:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250910-不分类\C-人工检查\BB-202509190345-s3-祝鑫良 part1', help='输入tiff的bin文件')
parser.add_argument('-o', '--outlabelfolder',type=str,default=r'R:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250910-不分类\C-人工检查\删除空标注\S3', help='切片文件夹地址')
args = parser.parse_args()
return args
if __name__ == '__main__':
args = getParams()
inlabelfolder = args.inlabelfolder
outlabelfolder = args.outlabelfolder
print('inlabelfolder:', inlabelfolder)
print('outcsvpath:', outlabelfolder)
FilterLabelEmptyFile(inlabelfolder,outlabelfolder)