import xmltodict
import os
# VOC xml文件所在文件夹
annotation_dir="./labels_voc/"
label_list = list()
# 逐一处理xml文件
for file in os.listdir(annotation_dir):
annotation_path = os.path.join(annotation_dir,file)
# 读取xml文件
with open(annotation_path,'r') as f:
xml_str = f.read()
#转为字典
xml_dic = xmltodict.parse(xml_str)
# 获取label并去重加入到label_list
objects = xml_dic["annotation"]["object"]
if isinstance(objects,list): # xml文件中包含多个object
for obj in objects:
label = obj['name']
if label not in label_list:
label_list.append(label)
else:# xml文件中只包含1个object
obj = objects
label = object_['name']
if label not in label_list:
label_list.append(label)
print(label_list)
评论 (0)