0
点赞
收藏
分享

微信扫一扫

Python批量获取VOC测试集的类别

无愠色 2022-03-21 阅读 75

使用 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))}")
举报

相关推荐

0 条评论