欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

CV项目——遍历文件夹下标签txt文件统计各属性图片张数和标签个数

程序员文章站 2022-07-10 21:33:59
import ospath = "../data_labels/" # to changetxt_files= os.listdir(path) #得到文件夹下的所有文件名称txt_files = [x for x in txt_files if x.endswith('.txt')]label_image_count_dict = dict() # 每个 label 出现的图片个数label_count_dict = dict() # 每个 label 出现多少次# todo: 每...
import os

path = "../data_labels/"  # to change
txt_files= os.listdir(path)  #得到文件夹下的所有文件名称
txt_files = [x for x in txt_files if x.endswith('.txt')]
label_image_count_dict = dict()  # 每个 label 出现的图片个数
label_count_dict = dict()  # 每个 label 出现多少次

# todo: 每个 label, 保存一张出现这个 label 的图

for file in txt_files:  #遍历文件夹

    abs_txt_file = os.path.join(path, file)
    tmp_label_set = set()  # 记录当前标注文档出现的 label
    print(abs_txt_file)
    with open(abs_txt_file, "r",encoding='utf-8') as f:  #打开文件
        for line in f.readlines():
            split_line = line.split()
            if len(split_line) == 5:
                tmp_label = split_line[0]
                tmp_label_set.add(tmp_label)
                if tmp_label not in label_count_dict:
                    label_count_dict[tmp_label] = 0
                label_count_dict[tmp_label] = label_count_dict[tmp_label] + 1
    for y in tmp_label_set:
        if y not in label_image_count_dict:
            label_image_count_dict[y] = 0
        label_image_count_dict[y] = label_image_count_dict[y] + 1

print(label_image_count_dict)
print(label_count_dict)

本文地址:https://blog.csdn.net/mndlgzzd/article/details/107358891