统计数据集的TXT、XML及JSON标注文件中各类别/每个标签的数量

在计算机视觉和深度学习领域,标注文件是模型训练的重要组成部分。无论是图像分类、目标检测还是图像分割,正确的标注能够显著提升模型的性能。在实际应用中,我们需要快速了解每个类别的样本数量,以便进行数据分析、平衡类别分布或优化模型训练。

以下是各个格式的文件代码,输出均按照标签数量从多到少排序,其中txt输入需要修改列表中标签。

统计YOLO格式的TXT文件

python 复制代码
import os
from collections import Counter

string_table = ['hat','nohat']  #按顺序修改为类别列表
folder_path = r' '  #修改为txt文件夹
category_counter = Counter()

for filename in os.listdir(folder_path):
    if filename.endswith('.txt'):
        file_path = os.path.join(folder_path, filename)
        with open(file_path, 'r') as file:
            for line in file:
                category_index = int(line.split()[0])
                if category_index < len(string_table):
                    category = string_table[category_index]
                    category_counter[category] += 1
print("各类别数量:")
for category in string_table:
    count = category_counter[category]
    print(f"{category}: {count}")

统计VOC格式的XML文件

python 复制代码
import os
import xml.etree.ElementTree as ET

class_count = {}
folder_path = r' '  # 此处修改为xml文件夹
for filename in os.listdir(folder_path):
	if filename.endswith('.xml'):
		tree = ET.parse(os.path.join(folder_path, filename))
		root = tree.getroot()
		for obj in root.findall('object'):
			name = obj.find('name').text
			if name in class_count:
				class_count[name] += 1
			else:
				class_count[name] = 1
sorted_class_count = sorted(class_count.items(), key=lambda x: x[1], reverse=True)
print("各类别数量:")
for name, count in sorted_class_count:
	print(f"{name}: {count}")

统计JSON文件

python 复制代码
import os
import json
from collections import Counter

json_folder = r' '  # 修改JSON文件夹路径
json_files = [f for f in os.listdir(json_folder) if f.endswith('.json')]
category_counter = Counter()
for json_file in json_files:
    with open(os.path.join(json_folder, json_file), 'r') as f:
        data = json.load(f)
    for shape in data['shapes']:
        category = shape['label']
        category_counter[category] += 1
sorted_category_count = sorted(category_counter.items(), key=lambda x: x[1], reverse=True)
print("各类别数量:")
for category, count in sorted_category_count:
    print(f"{category}: {count}")
相关推荐
spencer_tseng2 天前
[j2cache ehcache.xml]/tmp/.ehcache-diskstore.lock (Permission denied)
xml·linux·python·ehcache·[j2cache
DevOpenClub3 天前
PDF 多格式解析如何避免混用输出:TEXT、HTML、XML 与 TAG 数据契约
xml·前端·数据库·pdf·html
Java小白笔记3 天前
MyBatis XML Mapper 标签与 CRUD 原理实战
xml·数据库·mybatis
风萧萧19994 天前
JAVA :JSONObject转换为XML
xml·java·python
马优晨6 天前
pom.xml 中 jquery webjars 依赖解释
xml·前端·jquery·jquery webjars·webjars依赖
楚识科技7 天前
定制 OCR 服务:非标证件与表格的 XML 字段映射实践
xml·ocr
光泽雨8 天前
JSON /XML转换
xml·json
我命由我123459 天前
UI 设计 7 种排列方式
xml·学习·ui·html·产品运营·产品经理·学习方法
ly768914 天前
XML 从入门到实践:语法、命名空间、XPath、XSD 与 Java 安全解析
xml·java·python