使用 Python 读取 XML 文件,获取所有类别。
# coding=utf-8
# @Author: FSJohn
# @Date: 2022.3.21
import xml.etree.ElementTree as ET
import os
Annotations_path = r'Annotations' # VOC 目录中 Annotations 的路径
xml_file_list = os.listdir(Annotations_path)
classes = []
for xml in xml_file_list:
xml_file = os.path.join(os.path.join(os.getcwd(), Annotations_path, xml))
doc = ET.parse(xml_file)
root = doc.getroot()
obj = root.find('object')
name = obj.find('name')
classes.append(name.text)
print("测试集类别:")
for i in set(classes):
print(i)
print(f"类别数量:{len(set(classes))}")