国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Python批量修改、刪除、替換xml文件內(nèi)容(labelimg標注)

這篇具有很好參考價值的文章主要介紹了Python批量修改、刪除、替換xml文件內(nèi)容(labelimg標注)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

使用模型訓(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>

參考代碼

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)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • 【mybatis】mapper.xml中foreach的用法,含批量查詢、插入、修改、刪除方法的使用

    一、xml文件中foreach的主要屬性 foreach元素的屬性主要有 collection,item,index,separator,open,close。 collection: 表示集合,數(shù)據(jù)源 item :表示集合中的每一個元素 index :用于表示在迭代過程中,每次迭代到的位置 separator :表示在迭代時數(shù)據(jù)以什么符號作為分隔符 open :表示該語

    2024年02月12日
    瀏覽(23)
  • linux 再文件夾目錄下,批量替換文件名、文件內(nèi)容字符串

    基本使用命令 具體使用命令方法 1.替換文件內(nèi)容 搜索當前目錄下所有.DTA文件,并將文件中字符串\\\"string1\\\",替換為\\\"string2\\\" 2.替換文件名 搜索當前目錄下所有.DTA文件,并將文件名中字符串\\\"string1\\\"替換為\\\"string2\\\". 3.批量替換文件內(nèi)容 該目錄及子目錄下所有文件,將文件中所有的字符

    2024年02月16日
    瀏覽(98)
  • 批處理批量替換文本內(nèi)容,用bat代碼全篇替換txt文本文件中指定字符信息

    批處理批量全篇替換txt文本文件中指定字符信息,修改三個參數(shù)后即可使用,話不多說直接上代碼: @echo off setlocal EnableDelayedExpansion set path_str=\\\"C:UsersAdministratorDesktop1.txt\\\" set old_str=需要替換的原文本內(nèi)容 set new_str=替換后的文本內(nèi)容 set souerce_path=%path_str% for /f \\\"tokens=1* delims=

    2024年02月11日
    瀏覽(16)
  • 實用VBA:17.大量word文件中的文本內(nèi)容進行批量替換

    實用VBA:17.大量word文件中的文本內(nèi)容進行批量替換

    在工作中可能會遇到需要對大量word文件中的文字內(nèi)容進行批量替換的情況。相比excel的批量處理,個人感覺word文檔中由于包含大量樣式信息,批處理時總感覺有顧慮。一者擔心影響了文檔的格式,誤修改了文檔的樣式,那后果……整過文檔的小伙伴都懂的;二者擔心批處理不

    2024年01月25日
    瀏覽(23)
  • Ansible批量操作(上傳文件、刪除文件&指定文件內(nèi)容、執(zhí)行sh文件等)

    Ansible批量操作(上傳文件、刪除文件&指定文件內(nèi)容、執(zhí)行sh文件等)

    官方網(wǎng)站 https://www.ansible.com/ 一、Ansible 簡介 1、Ansible是新出現(xiàn)的自動化運維工具,完全基于Python開發(fā),集合了眾多運維工具(puppet、chef、func、fabric)的優(yōu)點,實現(xiàn)了批量系統(tǒng)配置、批量程序部署、批量運行命令等功能。 2、Ansible是基于 paramiko 開發(fā)的,并且基于模塊化工作,

    2024年04月16日
    瀏覽(20)
  • python批量處理修改pdf內(nèi)容

    python批量處理修改pdf內(nèi)容

    ? 將PDF轉(zhuǎn)換為Word: 使用pdf2docx庫中的Converter類來進行PDF轉(zhuǎn)換。 convert_pdf_to_docx 函數(shù)接受PDF文件路徑和輸出的Word文檔路徑作為參數(shù)。 通過調(diào)用Converter對象的 convert 方法將PDF轉(zhuǎn)換為Docx格式。 最后調(diào)用 close 方法關(guān)閉Converter對象并保存轉(zhuǎn)換后的文檔。 將Word轉(zhuǎn)換為Excel: 使用docx庫

    2024年01月25日
    瀏覽(22)
  • 如何批量修改刪除html文件中的標簽屬性

    如何批量修改刪除html文件中的標簽屬性

    最近工作中遇到一個問題,一份html文檔因為內(nèi)容里面的樣式標簽過多導(dǎo)致文件整體過大。 這些描述標簽不是必須的,現(xiàn)在需要優(yōu)化刪除掉這些標簽從而減小文件體積。 對于這種批量修改刪除的任務(wù),我們首先想到的就是使用編輯器處理。 編輯html文檔,我使用的是VS Code,它

    2024年02月01日
    瀏覽(46)
  • 常用腳本-持續(xù)更新(文件重命名、視頻抽幀、拆幀、刪除冗余文件、yolo2xml、轉(zhuǎn)換圖片格式、修改xml)

    所有代碼位置 :Learning-Notebook-Codes/Python/常用腳本 腳本路徑: codes/files_rename.py 腳本說明:可以自動重命名某個文件夾下指定類型的文件。 修改前文件名稱: img1.jpg 修改后文件名稱: Le0v1n-20231123-X-0001.jpg 腳本路徑: codes/extract_frames.py 腳本說明:根據(jù)幀間隔對某個文件夾下指定

    2024年02月20日
    瀏覽(24)
  • labelimg標注的VOC格式標簽xml文件和yolo格式標簽txt文件相互轉(zhuǎn)換

    labelimg標注的VOC格式標簽xml文件和yolo格式標簽txt文件相互轉(zhuǎn)換

    目錄 1 labelimg標注VOC格式和yolo格式介紹 1.1 voc格式 1.2 yolo數(shù)據(jù)格式介紹 2 voc格式數(shù)據(jù)和yolo格式數(shù)據(jù)相互轉(zhuǎn)換 2.1 voc轉(zhuǎn)yolo代碼 2.2 yolo轉(zhuǎn)voc格式代碼? ? ? ? ? labelimg標注工具怎么安裝和使用在我的博客中已經(jīng)講解了,有需要可以看看,博客。 ? ? ? ? VOC格式文件保存在和圖像名

    2024年02月02日
    瀏覽(28)
  • notepad++ 批量替換刪除指定字符之后 或者 之前的字符,Notepad+批量替換使用大全

    notepad++ 批量替換刪除指定字符之后 或者 之前的字符,Notepad+批量替換使用大全

    資源寶分享:www.httple.net 注意: 不支持多行表達式 (involving n, r, etc). 1 基本表達式 符號 解釋 . 匹配任意字符,除了新一行(n)。也就是說 “.”可以匹配 r ,當文件中同時含有r and n時,會引起混亂。要匹配所有的字符,使用sS。 (…) 這個匹配一個標簽區(qū)域. 這個標簽可以被

    2024年02月07日
    瀏覽(15)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包