Python實(shí)現(xiàn)xml格式轉(zhuǎn)txt格式的示例代碼
1、前言
最近學(xué)習(xí)Yolo v5是遇見了個(gè)問(wèn)題,找的數(shù)據(jù)集全是xml文件,VOC 的標(biāo)注是 xml 格式的,而YOLO是.txt格式,那么問(wèn)題就來(lái)了,手動(dòng)提取肯定是不可能的,那只能借用程序解決咯。
2、分析xml、txt數(shù)據(jù)
這是xml樹形結(jié)構(gòu)
這是txt格式
總結(jié):
1.提取object->name、bndbox->xmin,ymin,xmax,ymin
2.格式轉(zhuǎn)化需要用公式轉(zhuǎn)換
YOLO數(shù)據(jù)集txt格式:
x_center :歸一化后的中心點(diǎn)x坐標(biāo)
y_center : 歸一化后的中心點(diǎn)y坐標(biāo)
w:歸一化后的目標(biāo)框?qū)挾?/p>
h: 歸一化后的目標(biāo)況高度
(此處歸一化指的是除以圖片寬和高)
VOC數(shù)據(jù)集xml格式
yolo的四個(gè)數(shù)據(jù) | xml->txt公式 |
---|---|
x_center | ((x_min+x_max)/2-1)/w_image |
y_center | ((y_min+y_max)/2-1)/h_image |
w | (x_max-x_min)/w_image |
h | (y_max-y_min)/h_image |
3、轉(zhuǎn)換過(guò)程
定義兩個(gè)文件夾,train放xml數(shù)據(jù), labels放txt數(shù)據(jù)。
代碼解析:
import os import xml.etree.ElementTree as ET import io find_path = './train/' ? ?#xml所在的文件 savepath='./labels/' ? #保存文件 class Voc_Yolo(object): ? ? def __init__(self, find_path): ? ? ? ? self.find_path = find_path ? ? def Make_txt(self, outfile): ? ? ? ? out = open(outfile,'w')? ? ? ? ? print("創(chuàng)建成功:{}".format(outfile)) ? ? ? ? return out ? ? def Work(self, count): ? ? #找到文件路徑 ? ? ? ? for root, dirs, files in os.walk(self.find_path): ? ? ? ? #找到文件目錄中每一個(gè)xml文件 ? ? ? ? ? ? for file in files: ? ? ? ? ? ? #記錄處理過(guò)的文件 ? ? ? ? ? ? ? ? count += 1 ? ? ? ? ? ? ? ? #輸入、輸出文件定義 ? ? ? ? ? ? ? ? input_file = find_path + file ? ? ? ? ? ? ? ? outfile = savepath+file[:-4]+'.txt' ? ? ? ? ? ? ? ? #新建txt文件,確保文件正常保存 ? ? ? ? ? ? ? ? out = self.Make_txt(outfile) ? ? ? ? ? ? ? ? #分析xml樹,取出w_image、h_image ? ? ? ? ? ? ? ? tree=ET.parse(input_file) ? ? ? ? ? ? ? ? root=tree.getroot() ? ? ? ? ? ? ? ? size=root.find('size') ? ? ? ? ? ? ? ? w_image=float(size.find('width').text) ? ? ? ? ? ? ? ? h_image=float(size.find('height').text) ? ? ? ? ? ? ? ? #繼續(xù)提取有效信息來(lái)計(jì)算txt中的四個(gè)數(shù)據(jù) ? ? ? ? ? ? ? ? for obj in root.iter('object'): ? ? ? ? ? ? ? ? #將類型提取出來(lái),不同目標(biāo)類型不同,本文僅有一個(gè)類別->0 ? ? ? ? ? ? ? ? ? ? classname=obj.find('name').text ? ? ? ? ? ? ? ? ? ? cls_id = classname ? ? ? ? ? ? ? ? ? ? xmlbox=obj.find('bndbox') ? ? ? ? ? ? ? ? ? ? x_min=float(xmlbox.find('xmin').text) ? ? ? ? ? ? ? ? ? ? x_max=float(xmlbox.find('xmax').text) ? ? ? ? ? ? ? ? ? ? y_min=float(xmlbox.find('ymin').text) ? ? ? ? ? ? ? ? ? ? y_max=float(xmlbox.find('ymax').text) ? ? ? ? ? ? ? ? ? ? #計(jì)算公式 ? ? ? ? ? ? ? ? ? ? x_center=((x_min+x_max)/2-1)/w_image ? ? ? ? ? ? ? ? ? ? y_center=((y_min+y_max)/2-1)/h_image ? ? ? ? ? ? ? ? ? ? w=(x_max-x_min)/w_image ? ? ? ? ? ? ? ? ? ? h=(y_max-y_min)/h_image ? ? ? ? ? ? ? ? ? ? #文件寫入 ? ? ? ? ? ? ? ? ? ? out.write(str(cls_id)+" "+str(x_center)+" "+str(y_center)+" "+str(w)+" "+str(h)+'\n') ? ? ? ? ? ? ? ? out.close() ? ? ? ? return count if __name__ == "__main__": ? ? data = Voc_Yolo(find_path) ? ? number = data.Work(0) ? ? print(number)
4、最后結(jié)果對(duì)比
創(chuàng)建成功
與真實(shí)數(shù)據(jù)對(duì)比誤差很小
到此這篇關(guān)于Python實(shí)現(xiàn)xml格式轉(zhuǎn)txt格式的示例代碼的文章就介紹到這了,更多相關(guān)Python xml轉(zhuǎn)txt內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python實(shí)現(xiàn)畫出e指數(shù)函數(shù)的圖像
今天小編就為大家分享一篇python實(shí)現(xiàn)畫出e指數(shù)函數(shù)的圖像,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11python中datetime模塊中strftime/strptime函數(shù)的使用
這篇文章主要介紹了python中datetime模塊中strftime/strptime函數(shù)的使用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07python 請(qǐng)求服務(wù)器的實(shí)現(xiàn)代碼(http請(qǐng)求和https請(qǐng)求)
本篇文章主要介紹了python 請(qǐng)求服務(wù)器的實(shí)現(xiàn)代碼(http請(qǐng)求和https請(qǐng)求),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05python實(shí)現(xiàn)三種隨機(jī)請(qǐng)求頭方式
這篇文章主要介紹了python實(shí)現(xiàn)三種隨機(jī)請(qǐng)求頭方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01python實(shí)現(xiàn)停車管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)停車管理系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11Pandas 數(shù)據(jù)處理,數(shù)據(jù)清洗詳解
今天小編就為大家分享一篇Pandas 數(shù)據(jù)處理,數(shù)據(jù)清洗詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07