【目标检测】批量删除xml中的类别标签
如果想删除xml中指定的一个类别标签,该怎么办?
如下图,xml中包含tim、jkm两个标签,现在只想留jkm,删除tim,怎么实现呢?
请看下面代码:

文件布局如下:

# -*- coding: utf-8 -*-
"""
Created on Fri Jan 7 09:51:51 2022@author: zqq
"""# 批量移除xml标注中的某一个类别标签
import xml.etree.cElementTree as ET
import ospath_root = ['annotations']CLASSES = ["tim"] # 将含有tim的objec删掉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').textprint("name",name)if name in CLASSES: # 这里可以反向写,不在Class的删掉root.remove(child)# 重写tree.write(os.path.join('annotations_new/', axml)) # 记得新建annotations_new文件夹
参考:
https://blog.csdn.net/weixin_44692101/article/details/108502834
https://blog.csdn.net/ai_faker/article/details/109068731
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
