網(wǎng)站首頁 編程語言 正文
txt文件轉(zhuǎn)換為XML
很多目標(biāo)檢測(cè)的模型都是默認(rèn)需要VOC的文件輸入格式
手上數(shù)據(jù)label是txt文件。
為了避免不必要的bug,還是選擇轉(zhuǎn)換下格式
將數(shù)據(jù)按VOC形式放置
文件夾 | 內(nèi)容 |
---|---|
Annotations | 存放生成的XML文件 |
JPEGImages | JPG圖片 |
ImageSets | 標(biāo)明訓(xùn)練集測(cè)試集的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)
不過代碼只使用于每個(gè)label文件只有一個(gè)標(biāo)注框,可在生成bndbox節(jié)點(diǎn)處加入循環(huán)
總結(jié)
原文鏈接:https://blog.csdn.net/weixin_43289424/article/details/106371995
相關(guān)推薦
- 2022-07-15 go語言實(shí)現(xiàn)銀行卡號(hào)Luhn校驗(yàn)_Golang
- 2022-07-09 docker安裝nginx并部署前端項(xiàng)目的全過程_docker
- 2022-05-18 C語言初識(shí)動(dòng)態(tài)內(nèi)存管理malloc?calloc?realloc?free函數(shù)_C 語言
- 2022-09-17 Python?pandas?重命名索引和列名稱的實(shí)現(xiàn)_python
- 2021-12-01 C語言中單目操作符++、–的實(shí)例講解_C 語言
- 2023-01-18 Python中的裝飾器使用_python
- 2022-08-25 .net?core中高效的動(dòng)態(tài)內(nèi)存管理方案_實(shí)用技巧
- 2022-05-10 @requestmapping獲取請(qǐng)求參數(shù)
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支