python 批量修改 labelImg 生成的xml文件的方法

系统 2086 0

概述

自己在用labelImg打好标签后,想只用其中几类训练,不想训练全部类别,又不想重新打标生成.xml文件,因此想到这个办法:直接在.xml文件中删除原有的不需要的标签类及其属性。

打标时标签名出现了大小写(工程量大时可能会手滑),程序中有改写标签值为小写的过程,因为我做py-faster-rcnn 训练时,标签必须全部为小写。

以如下的.xml文件为例,我故意把标签增加了大写

            
              
                test.jpg
              
              
                C:\Users\yasin\Desktop\test
              
              
                
                  Unknown
                
              
              
                
                  400
                
                
                  300
                
                
                  3
                
              
              
                0
              
              
                
                  People
                
                
                  Unspecified
                
                
                  0
                
                
                  0
                
                
                  
                    80
                  
                  
                    69
                  
                  
                    144
                  
                  
                    89
                  
                
              
              
                
                  CAT
                
                
                  Unspecified
                
                
                  0
                
                
                  0
                
                
                  
                    40
                  
                  
                    69
                  
                  
                    143
                  
                  
                    16
                  
                
              
              
                
                  dog
                
                
                  Unspecified
                
                
                  0
                
                
                  0
                
                
                  
                    96
                  
                  
                    82
                  
                  
                    176
                  
                  
                    87
                  
                
              
            
          

具体实现

假如我们只想保留图片上的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类),并把保留类别的打标改成了小写:

            
              
                test.jpg
              
              
                C:\Users\yasin\Desktop\test
              
              
                
                  Unknown
                
              
              
                
                  400
                
                
                  300
                
                
                  3
                
              
              
                0
              
              
                
                  people
                
                
                  Unspecified
                
                
                  0
                
                
                  0
                
                
                  
                    80
                  
                  
                    69
                  
                  
                    144
                  
                  
                    89
                  
                
              
              
                
                  cat
                
                
                  Unspecified
                
                
                  0
                
                
                  0
                
                
                  
                    40
                  
                  
                    69
                  
                  
                    143
                  
                  
                    16
                  
                
              
            
          

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论