最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python實現(xiàn)xml格式轉txt格式的示例代碼

 更新時間:2022年03月29日 14:40:20   作者:l。Ve  
VOC 的標注是xml格式的,而YOLO是.txt格式,所以要實現(xiàn)VOC數(shù)據(jù)集轉YOLO數(shù)據(jù)集,只能利用代碼實現(xiàn)。所以本文為大家介紹了Python中xml轉txt的示例代碼,需要的可以參考一下

1、前言

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

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

這是xml樹形結構

這是txt格式

總結:

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

2.格式轉化需要用公式轉換

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

x_center :歸一化后的中心點x坐標

y_center : 歸一化后的中心點y坐標

w:歸一化后的目標框寬度

h: 歸一化后的目標況高度

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

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

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、轉換過程

定義兩個文件夾,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):
? ? ? ? #找到文件目錄中每一個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'):
? ? ? ? ? ? ? ? #將類型提取出來,不同目標類型不同,本文僅有一個類別->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、最后結果對比

創(chuàng)建成功

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

到此這篇關于Python實現(xiàn)xml格式轉txt格式的示例代碼的文章就介紹到這了,更多相關Python xml轉txt內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論

青川县| 阜康市| 博乐市| 运城市| 昭苏县| 呼图壁县| 乳山市| 宁阳县| 长治市| 博湖县| 宜城市| 多伦县| 平远县| 临江市| 罗甸县| 和龙市| 巴塘县| 固始县| 冕宁县| 太湖县| 新野县| 怀集县| 博乐市| 武安市| 沈阳市| 福建省| 德安县| 荔波县| 赫章县| 罗江县| 本溪| 南澳县| 丹寨县| 平南县| 鄢陵县| 沁阳市| 天长市| 林口县| 芷江| 油尖旺区| 同心县|