使用模型訓(xùn)練自定義數(shù)據(jù)集之前,在用在網(wǎng)上搜索得到的圖片制作數(shù)據(jù)集時,即使批量修改圖片名稱后,在使用labelimg標注得到的xml文件中,圖片名稱還是網(wǎng)絡(luò)上圖片原本的名稱,這時需要對其進行批量修改。
<annotation>
<folder>測試圖片</folder>
<filename>ae2f50b6a937df1e1a72f9bcc45b172d.jpg</filename>
<path>F:\項目圖像數(shù)據(jù)集\ae2f50b6a937df1e1a72f9bcc45b172d.jpg</path>
<source>
<database>Unknown</database>
</source>
<size>
<width>800</width>
<height>800</height>
<depth>3</depth>
</size>
<segmented>0</segmented>
<object>
<name>class1</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>631</xmin>
<ymin>275</ymin>
<xmax>714</xmax>
<ymax>509</ymax>
</bndbox>
</object>
</annotation>
然后先修改路徑,將xml文件對應(yīng)圖片的真實路徑替換。這里圖片的名稱是采用12位數(shù)字排序的。
import xml.dom.minidom
import os
path = r'D:\test\xmltest\xml_source' # xml文件存放路徑
sv_path = r'D:\test\xmltest\xml_save' # 修改后的xml文件存放路徑
files = os.listdir(path)
cnt = 0
for xmlFile in files:
dom = xml.dom.minidom.parse(os.path.join(path, xmlFile)) # 打開xml文件,送到dom解析
root = dom.documentElement # 得到文檔元素對象
item = root.getElementsByTagName('path') # 獲取path這一node名字及相關(guān)屬性值
for i in item:
i.firstChild.data = f'D:/test/xmltest/xml_source/' + str(cnt).zfill(12) + '.jpg' # xml文件對應(yīng)的圖片路徑
with open(os.path.join(sv_path, xmlFile), 'w', encoding='utf-8') as fh:
dom.writexml(fh)
cnt += 1
修改后變成這樣。
<?xml version="1.0" ?><annotation>
<folder>測試圖片</folder>
<filename>ae2f50b6a937df1e1a72f9bcc45b172d.jpg</filename>
<path>D:/test/xmltest/JPEGimage/000000000000.jpg</path>
<source>
<database>Unknown</database>
</source>
<size>
<width>800</width>
<height>800</height>
<depth>3</depth>
</size>
<segmented>0</segmented>
<object>
<name>class1</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>631</xmin>
<ymin>275</ymin>
<xmax>714</xmax>
<ymax>509</ymax>
</bndbox>
</object>
</annotation>
接下來修改圖片名稱。
import xml.dom.minidom
import os
path = r'D:\test\xmltest\xml_source' # xml文件存放路徑
sv_path = r'D:\test\xmltest\xml_save' # 修改后的xml文件存放路徑
files = os.listdir(path)
for xmlFile in files:
dom = xml.dom.minidom.parse(os.path.join(path, xmlFile)) # 打開xml文件,送到dom解析
root = dom.documentElement # 得到文檔元素對象
names = root.getElementsByTagName('filename')
a, b = os.path.splitext(xmlFile) # 分離出文件名a
for n in names:
n.firstChild.data = a + '.jpg'
with open(os.path.join(sv_path, xmlFile), 'w', encoding='utf-8') as fh:
dom.writexml(fh)
xml文件中的圖片名稱和已經(jīng)修改好了。
<?xml version="1.0" ?><annotation>
<folder>測試圖片</folder>
<filename>000000000000.jpg</filename>
<path>D:/test/xmltest/JPEGimage/000000000000.jpg</path>
<source>
<database>Unknown</database>
</source>
<size>
<width>800</width>
<height>800</height>
<depth>3</depth>
</size>
<segmented>0</segmented>
<object>
<name>class1</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>631</xmin>
<ymin>275</ymin>
<xmax>714</xmax>
<ymax>509</ymax>
</bndbox>
</object>
</annotation>
得到的xml文件會顯示版本號,如果直接用xml文件訓(xùn)練可能會報錯,所以還需刪除<?xml version="1.0" ?>。如果需要刪除或者替換其他屬性,也可在此修改。
# -*- coding:utf-8 -*-
# 將a替換成b
import os
xmldir = r'D:\test\xmltest\xml_source'
savedir = r'D:\test\xmltest\xml_save'
xmllist = os.listdir(xmldir)
for xml in xmllist:
if '.xml' in xml:
fo = open(savedir + '/' + '{}'.format(xml), 'w', encoding='utf-8')
print('{}'.format(xml))
fi = open(xmldir + '/' + '{}'.format(xml), 'r', encoding='utf-8')
content = fi.readlines()
for line in content:
# line = line.replace('a', 'b') # 例:將a替換為b
line = line.replace('<?xml version="1.0" ?>', '')
line = line.replace('<folder>測試圖片</folder>', '<folder>車輛圖片</folder>')
line = line.replace('<name>class1</name>', '<name>class2</name>')
fo.write(line)
fo.close()
print('替換成功')
# 如通b為空字符串,就是刪除
大功告成。
<annotation>
<folder>車輛圖片</folder>
<filename>000000000000.jpg</filename>
<path>D:/test/xmltest/JPEGimage/000000000000.jpg</path>
<source>
<database>Unknown</database>
</source>
<size>
<width>800</width>
<height>800</height>
<depth>3</depth>
</size>
<segmented>0</segmented>
<object>
<name>class2</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>631</xmin>
<ymin>275</ymin>
<xmax>714</xmax>
<ymax>509</ymax>
</bndbox>
</object>
</annotation>
參考代碼文章來源:http://www.zghlxwxcb.cn/news/detail-414349.html
python批量修改xml文件的屬性(filename/path) - 代碼先鋒網(wǎng)python批量修改xml文件的屬性(filename/path),代碼先鋒網(wǎng),一個為軟件開發(fā)程序員提供代碼片段和技術(shù)文章聚合的網(wǎng)站。https://www.codeleading.com/article/67672212062/Python: 文件夾下xml內(nèi)容批量替換、刪除_南石北岸生的博客-CSDN博客_python替換xml 內(nèi)容?功能:對文件夾下的所有xml進行批量替換或刪除。#-*- coding:utf-8 -*-#將a替換成bimport osxmldir=''savedir=''xmllist=os.listdir(xmldir)for xml in xmllist: if '.xml' in xml: fo=open(savedir+'/'+'new_{}'.for...https://blog.csdn.net/gusui7202/article/details/85194806文章來源地址http://www.zghlxwxcb.cn/news/detail-414349.html
到了這里,關(guān)于Python批量修改、刪除、替換xml文件內(nèi)容(labelimg標注)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!