批量更改标签名

多人标注的数据集难免出现打错标签名的情况,或在其他原因需要更改标签名可用此代码批量更改标签名。只需修改三个地方SrcDir(标签和图片所在路径)、SrcLabelName (要改动的标签名)、DstLabelName (要改成的标签名)。
你不清楚都有什么标签名?统计一下喽,传送门https://blog.csdn.net/qq_41585722/article/details/119565225
import os
import imagesize
from tqdm import tqdm
import xml.dom.minidom

def ReadXml(FilePath):
    """输入一个xml文件路径, 将xml文件内所有object目标下的信息返回[[xmin, ymin, xmax , ymax, name], ...]"""
    if os.path.exists(FilePath) is False:
        return None
    dom = xml.dom.minidom.parse(FilePath)
    root_ = dom.documentElement
    object_ = root_.getElementsByTagName('object')
    info = []
    for object_1 in object_:
        name = object_1.getElementsByTagName("name")[0].firstChild.data
        bndbox = object_1.getElementsByTagName("bndbox")[0]
        xmin = int(bndbox.getElementsByTagName("xmin")[0].firstChild.data)
        ymin = int(bndbox.getElementsByTagName("ymin")[0].firstChild.data)
        xmax = int(bndbox.getElementsByTagName("xmax")[0].firstChild.data)
        ymax = int(bndbox.getElementsByTagName("ymax")[0].firstChild.data)
        info.append([xmin, ymin, xmax, ymax, name])
    return info

def WriteXml(infos, W, H):
    FileName = infos[0].split("\\")[-1]
    xml_file = open((infos[0]), 'w')
    xml_file.write('<annotation>\n')
    xml_file.write('    <folder>VOC2007</folder>\n')
    xml_file.write('    <filename>' + FileName + '</filename>\n')
    xml_file.write('    <size>\n')
    xml_file.write('        <width>' + str(W) + '</width>\n')
    xml_file.write('        <height>' + str(H) + '</height>\n')
    xml_file.write('        <depth>3</depth>\n')
    xml_file.write('    </size>\n')
    for img_each_label in infos[1:]:
        spt = img_each_label[4]
        xml_file.write('    <object>\n')
        xml_file.write('        <name>' + str(spt) + '</name>\n')
        xml_file.write('        <pose>Unspecified</pose>\n')
        xml_file.write('        <truncated>0</truncated>\n')
        xml_file.write('        <difficult>0</difficult>\n')
        xml_file.write('        <bndbox>\n')
        xml_file.write('            <xmin>' + str(img_each_label[0]) + '</xmin>\n')
        xml_file.write('            <ymin>' + str(img_each_label[1]) + '</ymin>\n')
        xml_file.write('            <xmax>' + str(img_each_label[2]) + '</xmax>\n')
        xml_file.write('            <ymax>' + str(img_each_label[3]) + '</ymax>\n')
        xml_file.write('        </bndbox>\n')
        xml_file.write('    </object>\n')

    xml_file.write('</annotation>')


SrcDir = r"J:\Temp\800x300\M1\L"  # 标签和图片所在路径
SrcLabelName = ['100', '105', '110', '115', '120', '130', '15', '20', '25', '30', '35', '40', '40 ', '45', '50', '55', '60', '65', '70', '75', '80', '85', '90', '95']  # 要改动的标签名
DstLabelName = 'Limit'  # 要改成的标签名

ReWriteKey = False

for root, dirs, files in os.walk(SrcDir):
    for file in tqdm(files):
        if file[-1] == 'l':
            Infos = ReadXml(root + "\\" + file)
            NewInfos = [root + "\\" + file]
            for Info in Infos:
                if Info[-1] in SrcLabelName:
                    ReWriteKey = True
                    Info[-1] = DstLabelName
                NewInfos.append(Info)
            if ReWriteKey:
                ReWriteKey = False
                W, H = imagesize.get(root + "\\" + file[:-3] + "jpg")
                WriteXml(NewInfos, W, H)

执行效果如下
在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值