需求:
数据集的数据增强中,有时需要将xml标注文件中的类别做修改为新类别,或者将几个类别合并为一个类别。

解决方法:
使用python+import xml.etree.ElementTree将xml标注文件中的类别名称做修改。代码如下:

import osimport globimport xml.etree.ElementTree as etdef change_labels(source_dir):file_list = glob.glob(os.path.join(source_dir,"*.xml"))print(len(file_list))index = 1for file in file_list:print(file,index)index += 1tree_ = et.ElementTree()tree_.parse(file)root = et.Element("annotation")folder = et.SubElement(root, "folder")folder.text = "images"filename = et.SubElement(root, "filename")filename.text = tree_.find(".//filename").textpath = et.SubElement(root, "path")path.text = "/home/Desktop/labels"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 = tree_.find(".//width").textheight = et.SubElement(size, "height")height.text = tree_.find(".//height").textdepth = et.SubElement(size, "depth")depth.text = "3"segmented = et.SubElement(root, "segmented")segmented.text = "0"objects = tree_.findall(".//object")anchor = 0for object in objects:try:name_ = object.find("name").textexcept:print(file)print("-----------------------------------------")continuen = ""if name_ == "van" or name_ == "bus" or name_ == "truck":n = "car"else:n = name_object_ = et.SubElement(root, "object")name = et.SubElement(object_, "name")name.text = npose = 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").textymin = et.SubElement(bndbox, "ymin")ymin.text = object.find(".//ymin").textxmax = et.SubElement(bndbox, "xmax")xmax.text = object.find(".//xmax").textymax = et.SubElement(bndbox, "ymax")ymax.text = object.find(".//ymax").texttree = et.ElementTree(root)tree.write(file,encoding="utf-8")if __name__ == '__main__':source_dir = "/home/Desktop/labels/Annotations"flip_images(source_dir)

使用以上代码需要修改原图像和标注文件所在文件夹路径(source_dir),也需要修改类别,上述代码将”van”、“bus”、“truck”修改为“car”,这里可根据实际情况自行修改。亲测可用。