时间:2021-05-22
概述
自己在用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 ElementTreefrom os import walk, pathdef read_xml(in_path): tree = ElementTree() tree.parse(in_path) return treedef 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, prefixsif __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>以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
  因项目需要根据指定格式的文件生成XML标注文件,可以方便使用LabelImg打开进行编辑和查看。其原始文件默认使用逗号进行分隔,如下
本文实例讲述了python写xml文件的操作的方法,分享给大家供大家参考。具体方法如下:要生成的xml文件格式如下:samplexmlthingmaxiaoju
(1)pom.xml文件修改war,默认是jar包,节点中增加springboot,即生成war包的名字,完整pom.xml文件内容如下:?1234567891
本文实例讲述了java生成XML的方法。分享给大家供大家参考,具体如下:下拉框的生成,我是通过javascript读取xml文件生成的。Xml文件是根据数据库生
今天写一个在windows下批量修改文件名的python脚本,用到文件的遍历。用python进行文件遍历有多种方法,这里列举并说明一下。os.path.walk