赞
踩
- import os
- import glob
- import xml.etree.ElementTree as et
-
- def delete_bbox(dir_path,labels):
- file_list = glob.glob(os.path.join(dir_path, "*.xml"))
- index = 0
- for file in file_list:
- print(file,index)
- index += 1
- tree_ = et.parse(file)
- root_ = tree_.getroot()
-
- root = et.Element("annotation")
- folder = et.SubElement(root, "folder")
- folder.text = "images"
- filename = et.SubElement(root, "filename")
- filename.text = root_.find(".//filename").text
- source = et.SubElement(root, "source")
- database = et.SubElement(source, "database")
- database.text = "Unknown"
- size = et.SubElement(root, "size")
- width = et.SubElement(size, "width")
- width.text = root_.find(".//width").text
- height = et.SubElement(size, "height")
- height.text = root_.find(".//height").text
- depth = et.SubElement(size, "depth")
- depth.text = "3"
- segmented = et.SubElement(root, "segmented")
- segmented.text = "0"
-
- for object in root_.iter("object"):
- name_ = object.find("name").text
- if name_ in labels:
- object_ = et.SubElement(root, "object")
- name = et.SubElement(object_,"name")
- name.text = name_
- pose = et.SubElement(object_,"pose")
- pose.text = "Unspecified"
- truncated = et.SubElement(object_,"truncated")
- truncated.text = "0"
- difficult = et.SubElement(object_,"difficult")
- difficult.text = "0"
- bndbox = et.SubElement(object_,"bndbox")
- xmin = et.SubElement(bndbox,"xmin")
- xmin.text = object.find(".//xmin").text
- ymin = et.SubElement(bndbox,"ymin")
- ymin.text = object.find(".//ymin").text
- xmax = et.SubElement(bndbox,"xmax")
- xmax.text = object.find(".//xmax").text
- ymax = et.SubElement(bndbox,"ymax")
- ymax.text = object.find(".//ymax").text
- pretty_xml(root, ' ', '\n')
- tree = et.ElementTree(root)
- tree.write(file, encoding="utf-8")
-
- def pretty_xml(element, indent, newline, level=0): # elemnt为传进来的Elment类,参数indent用于缩进,newline用于换行
- if element: # 判断element是否有子元素
- if (element.text is None) or element.text.isspace(): # 如果element的text没有内容
- element.text = newline + indent * (level + 1)
- else:
- element.text = newline + indent * (level + 1) + element.text.strip() + newline + indent * (level + 1)
- # else: # 此处两行如果把注释去掉,Element的text也会另起一行
- # element.text = newline + indent * (level + 1) + element.text.strip() + newline + indent * level
- temp = list(element) # 将element转成list
- for subelement in temp:
- if temp.index(subelement) < (len(temp) - 1): # 如果不是list的最后一个元素,说明下一个行是同级别元素的起始,缩进应一致
- subelement.tail = newline + indent * (level + 1)
- else: # 如果是list的最后一个元素, 说明下一行是母元素的结束,缩进应该少一个
- subelement.tail = newline + indent * level
- pretty_xml(subelement, indent, newline, level=level + 1) # 对子元素进行递归操作
-
- if __name__ == '__main__':
- dir_path = "./Annotations" # xml标注文件所在文件夹
- labels = ["red","green","blue"] # 需要保留的标签类别
- delete_bbox(dir_path,labels)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。