多人标注的数据集难免出现打错标签名的情况,或在其他原因需要更改标签名可用此代码批量更改标签名。只需修改三个地方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)
执行效果如下