按照下面的格式,一个一个的删除:
|
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文件夹 |






最新评论
站长您好,亚马逊云咨询推广资源,望建立联系,可邮件,谢谢。
换友情链接吗?
看你的站做的挺不错的
恭喜!!太强了,硕博连读啊
雁过留毛,人过留名。
看不懂但大受震撼
每天都在战争,希望2026和平.
ZeroTier 看过多篇帖子,目前群辉、飞牛os、win11 、安卓、ubuntu ≥18.04 顺利通过,这篇相对于简单 实用、特来感谢 🙄