python 批量修改 labelImg 生成的xml文件的方法
作者:Miscellaneous0712 时间:2022-09-03 12:04:23
概述
自己在用labelImg打好标签后,想只用其中几类训练,不想训练全部类别,又不想重新打标生成.xml文件,因此想到这个办法:直接在.xml文件中删除原有的不需要的标签类及其属性。
打标时标签名出现了大小写(工程量大时可能会手滑),程序中有改写标签值为小写的过程,因为我做py-faster-rcnn 训练时,标签必须全部为小写。
以如下的.xml文件为例,我故意把标签增加了大写
<annotation verified="yes">
<filename>test.jpg</filename>
<path>C:\Users\yasin\Desktop\test</path>
<source>
<database>Unknown</database>
</source>
<size>
<width>400</width>
<height>300</height>
<depth>3</depth>
</size>
<segmented>0</segmented>
<object>
<name>People</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>80</xmin>
<ymin>69</ymin>
<xmax>144</xmax>
<ymax>89</ymax>
</bndbox>
</object>
<object>
<name>CAT</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>40</xmin>
<ymin>69</ymin>
<xmax>143</xmax>
<ymax>16</ymax>
</bndbox>
</object>
<object>
<name>dog</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>96</xmin>
<ymin>82</ymin>
<xmax>176</xmax>
<ymax>87</ymax>
</bndbox>
</object>
</annotation>
具体实现
假如我们只想保留图片上的people和cat类,其他都删除,代码如下:
from xml.etree.ElementTree import ElementTree
from os import walk, path
def read_xml(in_path):
tree = ElementTree()
tree.parse(in_path)
return tree
def write_xml(tree, out_path):
tree.write(out_path, encoding="utf-8", xml_declaration=True)
def find_nodes(tree, path):
return tree.findall(path)
def del_node_by_target_classes(nodelist, target_classes_lower, tree_root):
for parent_node in nodelist:
children = parent_node.getchildren()
if (parent_node.tag == "object" and children[0].text.lower() not in target_classes_lower):
tree_root.remove(parent_node)
elif (parent_node.tag == "object" and children[0].text.lower() in target_classes_lower):
children[0].text = children[0].text.lower()
def get_fileNames(rootdir):
data_path = []
prefixs = []
for root, dirs, files in walk(rootdir, topdown=True):
for name in files:
pre, ending = path.splitext(name)
if ending != ".xml":
continue
else:
data_path.append(path.join(root, name))
prefixs.append(pre)
return data_path, prefixs
if __name__ == "__main__":
# get all the xml paths, prefixes if not used here
paths_xml, prefixs = get_fileNames("/home/yasin/old_labels/")
target_classes = ["PEOPLE", "CAT"] # target flags you want to keep
target_classes_lower = []
for i in range(len(target_classes)):
target_classes_lower.append(target_classes[i].lower()) # make sure your target is lowe-case
# print(target_classes_lower)
for i in range(len(paths_xml)):
# rename and save the corresponding xml
tree = read_xml(paths_xml[i])
# get tree node
tree_root = tree.getroot()
# get parent nodes
del_parent_nodes = find_nodes(tree, "./")
# get target classes and delete
target_del_node = del_node_by_target_classes(del_parent_nodes, target_classes_lower, tree_root)
# save output xml, 000001.xml
write_xml(tree, "/home/yasin/new_labels/{}.xml".format("%06d" % i))
按照上述代码,示例.xml变为如下.xml,可以看出我们删除了除people和cat类的类别(即dog类),并把保留类别的打标改成了小写:
<?xml version='1.0' encoding='utf-8'?>
<annotation verified="yes">
<filename>test.jpg</filename>
<path>C:\Users\yasin\Desktop\test</path>
<source>
<database>Unknown</database>
</source>
<size>
<width>400</width>
<height>300</height>
<depth>3</depth>
</size>
<segmented>0</segmented>
<object>
<name>people</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>80</xmin>
<ymin>69</ymin>
<xmax>144</xmax>
<ymax>89</ymax>
</bndbox>
</object>
<object>
<name>cat</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>40</xmin>
<ymin>69</ymin>
<xmax>143</xmax>
<ymax>16</ymax>
</bndbox>
</object>
</annotation>
来源:https://blog.csdn.net/zhou4411781/article/details/96650819
标签:python,labelImg,xml文件
0
投稿
猜你喜欢
python 中文编码乱码问题的解决
2021-03-06 16:43:58
sqlserver2005 TSql新功能学习总结(数据类型篇)
2024-01-28 10:46:07
Python从列表推导到zip()函数的5种技巧总结
2023-07-31 00:57:11
python 生成目录树及显示文件大小的代码
2022-10-24 18:30:27
python入门之算法学习
2021-05-16 19:38:19
网站发布后Bootstrap框架引用woff字体无法正常显示的解决方法
2023-08-13 00:26:58
网页设计标准尺寸参考
2007-12-29 20:42:00
MySQL中row_number的实现过程
2024-01-23 15:08:54
详解Django模板层过滤器和继承的问题
2023-02-08 06:28:04
python可视化分析的实现(matplotlib、seaborn、ggplot2)
2021-10-20 13:59:21
python采集百度百科的方法
2023-01-12 03:48:35
python爬虫利用代理池更换IP的方法步骤
2023-10-04 05:50:38
分享Pytest fixture参数传递的几种方式
2023-06-15 01:25:28
一文搞懂Pandas数据透视的4个函数的使用
2022-03-26 02:01:53
python实现简单ftp客户端的方法
2023-12-14 20:07:31
Linux下如何实现Mysql定时任务
2024-01-19 09:41:50
Python遍历文件夹 处理json文件的方法
2022-02-19 16:08:40
查找备注(text,ntext)类型字段为空的方法
2008-08-02 12:47:00
SQL语句学习
2024-01-18 15:07:00
Python+Pygame实现之走四棋儿游戏的实现
2023-08-29 21:34:55