python中的txt文件轉(zhuǎn)換為XML
txt文件轉(zhuǎn)換為XML
很多目標(biāo)檢測的模型都是默認(rèn)需要VOC的文件輸入格式
手上數(shù)據(jù)label是txt文件。
為了避免不必要的bug,還是選擇轉(zhuǎn)換下格式
將數(shù)據(jù)按VOC形式放置

| 文件夾 | 內(nèi)容 |
|---|---|
| Annotations | 存放生成的XML文件 |
| JPEGImages | JPG圖片 |
| ImageSets | 標(biāo)明訓(xùn)練集測試集的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é)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
深度學(xué)習(xí)詳解之初試機(jī)器學(xué)習(xí)
機(jī)器學(xué)習(xí)可應(yīng)用在各個(gè)方面,本篇將在系統(tǒng)性進(jìn)入機(jī)器學(xué)習(xí)方向前,初步認(rèn)識機(jī)器學(xué)習(xí),利用線性回歸預(yù)測波士頓房價(jià),讓我們一起來看看吧2021-04-04
如何使用matplotlib讓你的數(shù)據(jù)更加生動
數(shù)據(jù)可視化用于以更直接的表示方式顯示數(shù)據(jù),并且更易于理解,下面這篇文章主要給大家介紹了關(guān)于如何使用matplotlib讓你的數(shù)據(jù)更加生動的相關(guān)資料,需要的朋友可以參考下2021-11-11
Python Django框架url反向解析實(shí)現(xiàn)動態(tài)生成對應(yīng)的url鏈接示例
這篇文章主要介紹了Python Django框架url反向解析實(shí)現(xiàn)動態(tài)生成對應(yīng)的url鏈接,結(jié)合實(shí)例形式分析了Django框架URL反向解析具體原理與應(yīng)用操作技巧,需要的朋友可以參考下2019-10-10
五個(gè)Pandas?實(shí)戰(zhàn)案例帶你分析操作數(shù)據(jù)
pandas是基于NumPy的一種工具,該工具是為了解決數(shù)據(jù)分析任務(wù)而創(chuàng)建的。Pandas納入了大量庫和一些標(biāo)準(zhǔn)的數(shù)據(jù)模型,提供了高效操作大型數(shù)據(jù)集的工具。pandas提供大量快速便捷地處理數(shù)據(jù)的函數(shù)和方法。你很快就會發(fā)現(xiàn),它是使Python強(qiáng)大而高效的數(shù)據(jù)分析環(huán)境的重要因素之一2022-01-01
我們知道python只定義了6種數(shù)據(jù)類型,字符串,整數(shù),浮點(diǎn)數(shù),列表,元組,字典。但是C語言中有些字節(jié)型的變量,在python中該如何實(shí)現(xiàn)呢?這點(diǎn)頗為重要,特別是要在網(wǎng)絡(luò)上進(jìn)行數(shù)據(jù)傳輸?shù)脑挕?/div> 2014-06-06最新評論

