目标检测项目中,使用python+xml.etree.ElementTree修改xml格式标注文件中的类别名称

这篇具有很好参考价值的文章主要介绍了目标检测项目中,使用python+xml.etree.ElementTree修改xml格式标注文件中的类别名称。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

需求:         数据集的数据增强中,有时需要将xml标注文件中的类别做修改为新类别,或者将几个类别合并为一个类别。   解决方法:         使用python+import xml.etree.ElementTree将xml标注文件中的类别名称做修改。代码如下:

import os
import glob
import xml.etree.ElementTree as et

def change_labels(source_dir):
    file_list = glob.glob(os.path.join(source_dir,"*.xml"))
    print(len(file_list))
    index = 1
    for file in file_list:
        print(file,index)
        index += 1
        tree_ = 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").text
        path = 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").text
        height = et.SubElement(size, "height")
        height.text = tree_.find(".//height").text
        depth = et.SubElement(size, "depth")
        depth.text = "3"
        segmented = et.SubElement(root, "segmented")
        segmented.text = "0"

        objects = tree_.findall(".//object")
        anchor = 0
        for object in objects:

            try:
                name_ = object.find("name").text
            except:
                print(file)
                print("-----------------------------------------")
                continue
            
            n = ""
            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 = n
            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

        tree = 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”,这里可根据实际情况自行修改。亲测可用。 

文章来源地址https://www.toymoban.com/news/detail-683868.html

到了这里,关于目标检测项目中,使用python+xml.etree.ElementTree修改xml格式标注文件中的类别名称的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包