代码如下:
import os
import xml.etree.ElementTree as ET
dataset_image_dir = 'train/ANNOTATIONS'
dataset_anno_dir = 'train/IMAGES'
# 获取标签内容和数量
tags = {}
# 遍历目录中的每个文件
for filename in os.listdir(dataset_image_dir):
if filename.endswith('.xml'):
# 解析XML文件
tree = ET.parse(os.path.join(dataset_image_dir, filename))
root = tree.getroot()
for obj in root.findall('object'):
name = obj.find('name').text
if name in tags:
tags[name] += 1
else:
tags[name] = 1
for tag, count in tags.items():
print(f'{tag}: {count}')