当一个数据集文件夹只有图片和xml的标签时,但此时我们又想转为txt格式,就不得不填入数据集的所有类别。
思路: 使用xml包读取后,找到存放标签的那个子节点,将其存入集合中。
以下为我的xml文件格式:这里应该了解的是,将我们结构进行对比,然后将代码调整为你需要的
import xml.etree.ElementTree as ET
import os
dir = os.listdir("path_to_annotations") #存放标签的父路径
s = set()
for i in dir:
tree = ET.parse("path_to_annotations"+i) #这里依旧填写父路径
root = tree.getroot()
for child in root:
if child.tag == 'object': #获取节点后,如果节点是object,那么就将其内容添加到set中
s.add(child.find("name").text) #name是装在我标签的节点
print(s)