溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python如何實現(xiàn)xml格式轉(zhuǎn)txt格式

發(fā)布時間:2022-03-29 15:32:26 來源:億速云 閱讀:1640 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容介紹了“Python如何實現(xiàn)xml格式轉(zhuǎn)txt格式”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

1、前言

最近學(xué)習(xí)Yolo v5是遇見了個問題,找的數(shù)據(jù)集全是xml文件,VOC 的標(biāo)注是 xml 格式的,而YOLO是.txt格式,那么問題就來了,手動提取肯定是不可能的,那只能借用程序解決咯。

2、分析xml、txt數(shù)據(jù)

這是xml樹形結(jié)構(gòu)

Python如何實現(xiàn)xml格式轉(zhuǎn)txt格式

這是txt格式

Python如何實現(xiàn)xml格式轉(zhuǎn)txt格式

總結(jié):

1.提取object->name、bndbox->xmin,ymin,xmax,ymin

2.格式轉(zhuǎn)化需要用公式轉(zhuǎn)換

YOLO數(shù)據(jù)集txt格式:

x_center :歸一化后的中心點x坐標(biāo)

y_center : 歸一化后的中心點y坐標(biāo)

w:歸一化后的目標(biāo)框?qū)挾?/p>

h: 歸一化后的目標(biāo)況高度

(此處歸一化指的是除以圖片寬和高)

VOC數(shù)據(jù)集xml格式

Python如何實現(xiàn)xml格式轉(zhuǎn)txt格式

yolo的四個數(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)換過程

定義兩個文件夾,train放xml數(shù)據(jù), labels放txt數(shù)據(jù)。

Python如何實現(xiàn)xml格式轉(zhuǎn)txt格式

代碼解析:

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):
        #找到文件目錄中每一個xml文件
            for file in files:
            #記錄處理過的文件
                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ù)提取有效信息來計算txt中的四個數(shù)據(jù)
                for obj in root.iter('object'):
                #將類型提取出來,不同目標(biāo)類型不同,本文僅有一個類別->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)
                    #計算公式
                    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é)果對比

創(chuàng)建成功

Python如何實現(xiàn)xml格式轉(zhuǎn)txt格式

與真實數(shù)據(jù)對比誤差很小

Python如何實現(xiàn)xml格式轉(zhuǎn)txt格式

“Python如何實現(xiàn)xml格式轉(zhuǎn)txt格式”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI