網(wǎng)站首頁 編程語言 正文
txt文件轉換為XML
很多目標檢測的模型都是默認需要VOC的文件輸入格式
手上數(shù)據(jù)label是txt文件。
為了避免不必要的bug,還是選擇轉換下格式
將數(shù)據(jù)按VOC形式放置
文件夾 | 內容 |
---|---|
Annotations | 存放生成的XML文件 |
JPEGImages | JPG圖片 |
ImageSets | 標明訓練集測試集的txt文件 |
Labelss | txt格式的Label文件 |
# -*- coding: utf-8 -*-
from xml.dom.minidom import Document
import os
import os.path
from PIL import Image
import importlib
import sys
importlib.reload(sys)
xml_path = "Annotations\\"
img_path = "JPEGImages\\"
ann_path = "Labelss\\"
if not os.path.exists(xml_path):
os.mkdir(xml_path)
def writeXml(tmp, imgname, w, h, objbud, wxml):
doc = Document()
# owner
annotation = doc.createElement('annotation')
doc.appendChild(annotation)
# owner
folder = doc.createElement('folder')
annotation.appendChild(folder)
folder_txt = doc.createTextNode("VOC2007")
folder.appendChild(folder_txt)
filename = doc.createElement('filename')
annotation.appendChild(filename)
filename_txt = doc.createTextNode(imgname)
filename.appendChild(filename_txt)
# ones#
source = doc.createElement('source')
annotation.appendChild(source)
database = doc.createElement('database')
source.appendChild(database)
database_txt = doc.createTextNode("The VOC2007 Database")
database.appendChild(database_txt)
annotation_new = doc.createElement('annotation')
source.appendChild(annotation_new)
annotation_new_txt = doc.createTextNode("PASCAL VOC2007 ")
annotation_new.appendChild(annotation_new_txt)
image = doc.createElement('image')
source.appendChild(image)
image_txt = doc.createTextNode("flickr")
image.appendChild(image_txt)
# onee#
# twos#
size = doc.createElement('size')
annotation.appendChild(size)
width = doc.createElement('width')
size.appendChild(width)
width_txt = doc.createTextNode(str(w))
width.appendChild(width_txt)
height = doc.createElement('height')
size.appendChild(height)
height_txt = doc.createTextNode(str(h))
height.appendChild(height_txt)
depth = doc.createElement('depth')
size.appendChild(depth)
depth_txt = doc.createTextNode("3")
depth.appendChild(depth_txt)
# twoe#
segmented = doc.createElement('segmented')
annotation.appendChild(segmented)
segmented_txt = doc.createTextNode("0")
segmented.appendChild(segmented_txt)
# threes#
object_new = doc.createElement("object")
annotation.appendChild(object_new)
name = doc.createElement('name')
object_new.appendChild(name)
name_txt = doc.createTextNode('cancer')
name.appendChild(name_txt)
pose = doc.createElement('pose')
object_new.appendChild(pose)
pose_txt = doc.createTextNode("Unspecified")
pose.appendChild(pose_txt)
truncated = doc.createElement('truncated')
object_new.appendChild(truncated)
truncated_txt = doc.createTextNode("0")
truncated.appendChild(truncated_txt)
difficult = doc.createElement('difficult')
object_new.appendChild(difficult)
difficult_txt = doc.createTextNode("0")
difficult.appendChild(difficult_txt)
# threes-1#
bndbox = doc.createElement('bndbox')
object_new.appendChild(bndbox)
xmin = doc.createElement('xmin')
bndbox.appendChild(xmin)
#objbud存放[類別,xmin,ymin,xmax,ymax]
xmin_txt = doc.createTextNode(objbud[1])
xmin.appendChild(xmin_txt)
ymin = doc.createElement('ymin')
bndbox.appendChild(ymin)
ymin_txt = doc.createTextNode(objbud[2])
ymin.appendChild(ymin_txt)
xmax = doc.createElement('xmax')
bndbox.appendChild(xmax)
xmax_txt = doc.createTextNode(objbud[3])
xmax.appendChild(xmax_txt)
ymax = doc.createElement('ymax')
bndbox.appendChild(ymax)
ymax_txt = doc.createTextNode(objbud[4])
ymax.appendChild(ymax_txt)
# threee-1#
# threee#
tempfile = tmp + "test.xml"
with open(tempfile, "wb") as f:
f.write(doc.toprettyxml(indent="\t", newl="\n", encoding="utf-8"))
rewrite = open(tempfile, "r")
lines = rewrite.read().split('\n')
newlines = lines[1:len(lines) - 1]
fw = open(wxml, "w")
for i in range(0, len(newlines)):
fw.write(newlines[i] + '\n')
fw.close()
rewrite.close()
os.remove(tempfile)
return
for files in os.walk('E:\ssd_pytorch_cancer\data\cancer_or_not\Labels'):
print(files)
temp = "/temp/"
if not os.path.exists(temp):
os.mkdir(temp)
for file in files[2]:
print(file + "-->start!")
img_name = os.path.splitext(file)[0] + '.jpg'
fileimgpath = img_path + img_name
im = Image.open(fileimgpath)
width = int(im.size[0])
height = int(im.size[1])
filelabel = open(ann_path + file, "r")
lines = filelabel.read().split(' ')
obj = lines[:len(lines)]
filename = xml_path + os.path.splitext(file)[0] + '.xml'
writeXml(temp, img_name, width, height, obj, filename)
os.rmdir(temp)
不過代碼只使用于每個label文件只有一個標注框,可在生成bndbox節(jié)點處加入循環(huán)
總結
原文鏈接:https://blog.csdn.net/weixin_43289424/article/details/106371995
相關推薦
- 2022-07-25 C/C++實現(xiàn)線性順序表的示例代碼_C 語言
- 2023-01-12 keepalived?+?nginx?實現(xiàn)高可用方案_nginx
- 2022-04-28 C#圖形編程GDI+基礎介紹_C#教程
- 2024-02-01 mybatis plus 分頁查詢出現(xiàn)count()
- 2022-04-10 Python實現(xiàn)讀取excel中的圖片功能_python
- 2022-04-05 Python利用prettytable庫輸出好看的表格_python
- 2022-03-30 C語言入門之淺談數(shù)據(jù)類型和變量常量_C 語言
- 2022-05-19 關于python中不同函數(shù)讀取圖片格式的區(qū)別淺析_python
- 最近更新
-
- 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之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支