如果想删除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 os
path_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').text
print("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
版权声明:本文为CSDN博主「机器不学习我学习」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/AugustMe/article/details/122358578
暂无评论