按照下面的格式,一个一个的删除:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# 批量移除xml标注中的某一个类别标签 import xml.etree.cElementTree as ET import os path_root = ['VOC2007/Annotations'] CLASSES = ["crowd"] # 填入想要删去的类别,每次删去一个类 for anno_path in path_root: xml_list = os.listdir(anno_path) for axml in xml_list: path_xml = os.path.join(anno_path, axml) tree = ET.parse(path_xml) root = tree.getroot() print("root",root) for child in root.findall('object'): name = child.find('name').text print("name",name) if name in CLASSES: # 这里可以反向写,不在Class的删掉 root.remove(child) # 重写 tree.write(os.path.join('VOC2007/Annotations_new', axml)) # 记得新建annotations_new文件夹 |
本文最后更新于2022年5月1日,已超过 1 年没有更新,如果文章内容或图片资源失效,请留言反馈,我们会及时处理,谢谢!