網(wǎng)站首頁 編程語言 正文
1、前言
最近學習Yolo v5是遇見了個問題,找的數(shù)據(jù)集全是xml文件,VOC 的標注是 xml 格式的,而YOLO是.txt格式,那么問題就來了,手動提取肯定是不可能的,那只能借用程序解決咯。
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 :歸一化后的中心點x坐標
y_center : 歸一化后的中心點y坐標
w:歸一化后的目標框?qū)挾?/p>
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、轉(zhuǎn)換過程
定義兩個文件夾,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、最后結(jié)果對比
創(chuàng)建成功
與真實數(shù)據(jù)對比誤差很小
原文鏈接:https://blog.csdn.net/qq_43604989/article/details/123781947
相關(guān)推薦
- 2022-06-02 React中的Props類型校驗和默認值詳解_React
- 2022-05-13 Linux操作系統(tǒng)筆記——GCC編譯器
- 2023-01-31 MongoDB?聚合查詢詳解_MongoDB
- 2022-07-07 python計算階乘的兩個函數(shù)用法_python
- 2022-06-01 C語言線性表中順序表超詳細理解_C 語言
- 2021-12-02 使用gin框架搭建簡易服務的實現(xiàn)方法_Golang
- 2022-12-07 C++?兩個vector對象拼接方式_C 語言
- 2022-04-20 iOS實現(xiàn)多控制器切換效果_IOS
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支