机器学习数据集xml和jpg同时命名
借鉴的代码
import numpy as np
import glob
import os
import xml.etree.ElementTree as ET
import xml.dom.minidom
'''
第一步,将xml文件和图片重新命名
'''
def getDirImageNum(path):
bmpDirImagesNum = 0
for bmpfile in os.listdir(path):
if os.path.splitext(bmpfile)[1] == '.jpg':
bmpDirImagesNum += 1
return bmpDirImagesNum
def getDirXmlNum(path):
xmlDirXmlNum = 0
for xmlfile in os.listdir(path):
if os.path.splitext(xmlfile)[1] == '.xml':
xmlDirXmlNum += 1
return xmlDirXmlNum
inputpath1 = 'D:/jpg_new/JPEGImages/'
inputpath2 = 'D:/jpg_new/Annotations/'
outpath1 = 'D:/jpg_new/jpg_new/'
outpath2 = 'D:/jpg_new/xml_new/'
file_name = os.listdir(inputpath1)
error = []
for item in file_name:
print(item)
o_imap = inputpath1 + item.split('.')[0] + ".jpg"
o_xmlp = inputpath2 + item.split('.')[0] + ".xml"
i = getDirImageNum(outpath1)
if os.path.exists(o_imap)and os.path.exists(o_xmlp):
i = i + 1
new_name = '0' + format(str(i), '0>5s') + '.jpg'
dst1 = os.path.join(os.path.abspath(outpath1), new_name)
os.rename(o_imap, dst1)
dst2 = os.path.join(os.path.abspath(outpath2), '0' + format(str(i), '0>5s') + '.xml')
try:
dom = xml.dom.minidom.parse(o_xmlp)
root = dom.documentElement
root.getElementsByTagName('folder')[0].firstChild.data = "VOC2007"
root.getElementsByTagName('filename')[0].firstChild.data = new_name
with open(o_xmlp, 'w') as fh:
dom.writexml(fh)
os.rename(o_xmlp, dst2)
print('converting %s to %s ...' % (o_xmlp, dst2))
except:
error.append(new_name)
continue
print(len(error))
代码出处