# 统计结果
import numpy as np
import os
from glob import glob
from pathlib import Path
from multiprocessing import Pool
import argparse




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 StaticProcess(inlabelfolder,outcsvpath):
    labelfilepaths=find_txt_files_pathlib(inlabelfolder)
    with open(outcsvpath,"w",encoding="utf-8") as f:
        f.write("切片名,军用飞机,民用飞机,军民一体飞机,军用轮船,民用轮船,民用机场,军用机场,军民一体机场,军用港口,民用港口,军民一体港口,总计\n")
        f.write("标注,military_airplane, civil_airplane,mix_airpllane,military_ship,civil_ship,civil_airport,military_airport,mix_airport,military_harbor,civil_harbor,mix_harbor,Total\n")

        for labelfilepath in labelfilepaths:
            rastername=Path(labelfilepath).stem
            JFJ=0
            MFJ=0
            JMFJ=0
            JLC=0
            MLC=statictNumber(labelfilepath)
            MJC=0
            JJC=0
            JMJC=0
            JGK=0
            MGK=0
            JMGK=0
            Total=JFJ+MFJ+JMFJ+JLC+MLC+MJC+JJC+JMJC+JGK+MGK+JMGK
            f.write(
                "{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}\n".format(
                    # SAR影像名,StudentID,work_time,JFJ,MFJ,JMFJ,JLC,MLC,MJC,JJC,JMJC,JGK,MGK,JMGK,LD,DD,SB,HL,FL,JS,HP,SY,Total
                    rastername,JFJ,MFJ,JMFJ,JLC,MLC,MJC,JJC,JMJC,JGK,MGK,JMGK,Total
                ))


def getParams():
    parser = argparse.ArgumentParser()
    parser.add_argument('-i','--inlabelfolder',type=str,default=r'R:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250910-不分类\C-人工检查\删除空标注', help='输入tiff的bin文件')
    parser.add_argument('-o', '--outcsvpath',type=str,default=r'D:\TYSAR-德清院\切片对应目标的个数信息表_0919.csv', help='切片文件夹地址')
    args = parser.parse_args()
    return args


if __name__ == '__main__':
    args = getParams()
    inlabelfolder = args.inlabelfolder
    outcsvpath = args.outcsvpath
    print('inlabelfolder:', inlabelfolder)
    print('outcsvpath:', outcsvpath)
    StaticProcess(inlabelfolder,outcsvpath)


