如何將yolo格式轉(zhuǎn)化為voc格式:txt轉(zhuǎn)xml(親測(cè)有效)
1.文件目錄如下所示:

對(duì)以上目錄的解釋:
1.dataset下面的image文件夾:里面裝的是數(shù)據(jù)集的原圖片

2.dataset下面的label文件夾:里面裝的是圖片對(duì)應(yīng)得yolo格式標(biāo)簽

3.dataset下面的Annotations文件夾:這是一個(gè)空文件夾,里面要裝得是即將要生成得voc格式標(biāo)簽
2.轉(zhuǎn)換代碼如下所示
新建一個(gè)convert.py文件,然后將下面代碼復(fù)制進(jìn)去
注意:文件夾的格式要與我的一樣才行
from xml.dom.minidom import Document
import os
import cv2
# def makexml(txtPath, xmlPath, picPath): # txt所在文件夾路徑,xml文件保存路徑,圖片所在文件夾路徑
def makexml(picPath, txtPath, xmlPath): # txt所在文件夾路徑,xml文件保存路徑,圖片所在文件夾路徑
"""此函數(shù)用于將yolo格式txt標(biāo)注文件轉(zhuǎn)換為voc格式xml標(biāo)注文件
"""
dic = {'0': "pedestrian", # 創(chuàng)建字典用來對(duì)類型進(jìn)行轉(zhuǎn)換
'1': "people", # 此處的字典要與自己的classes.txt文件中的類對(duì)應(yīng),且順序要一致
'2': "bicycle",
'3': "car",
'4': "van",
'5': "truck",
'6': "tricycle",
'7': "awning-tricycle",
'8': "bus",
'9': "motor",
}
files = os.listdir(txtPath)
for i, name in enumerate(files):
xmlBuilder = Document()
annotation = xmlBuilder.createElement("annotation") # 創(chuàng)建annotation標(biāo)簽
xmlBuilder.appendChild(annotation)
txtFile = open(txtPath + name)
txtList = txtFile.readlines()
img = cv2.imread(picPath + name[0:-4] + ".jpg")
Pheight, Pwidth, Pdepth = img.shape
folder = xmlBuilder.createElement("folder") # folder標(biāo)簽
foldercontent = xmlBuilder.createTextNode("driving_annotation_dataset")
folder.appendChild(foldercontent)
annotation.appendChild(folder) # folder標(biāo)簽結(jié)束
filename = xmlBuilder.createElement("filename") # filename標(biāo)簽
filenamecontent = xmlBuilder.createTextNode(name[0:-4] + ".jpg")
filename.appendChild(filenamecontent)
annotation.appendChild(filename) # filename標(biāo)簽結(jié)束
size = xmlBuilder.createElement("size") # size標(biāo)簽
width = xmlBuilder.createElement("width") # size子標(biāo)簽width
widthcontent = xmlBuilder.createTextNode(str(Pwidth))
width.appendChild(widthcontent)
size.appendChild(width) # size子標(biāo)簽width結(jié)束
height = xmlBuilder.createElement("height") # size子標(biāo)簽height
heightcontent = xmlBuilder.createTextNode(str(Pheight))
height.appendChild(heightcontent)
size.appendChild(height) # size子標(biāo)簽height結(jié)束
depth = xmlBuilder.createElement("depth") # size子標(biāo)簽depth
depthcontent = xmlBuilder.createTextNode(str(Pdepth))
depth.appendChild(depthcontent)
size.appendChild(depth) # size子標(biāo)簽depth結(jié)束
annotation.appendChild(size) # size標(biāo)簽結(jié)束
for j in txtList:
oneline = j.strip().split(" ")
object = xmlBuilder.createElement("object") # object 標(biāo)簽
picname = xmlBuilder.createElement("name") # name標(biāo)簽
namecontent = xmlBuilder.createTextNode(dic[oneline[0]])
picname.appendChild(namecontent)
object.appendChild(picname) # name標(biāo)簽結(jié)束
pose = xmlBuilder.createElement("pose") # pose標(biāo)簽
posecontent = xmlBuilder.createTextNode("Unspecified")
pose.appendChild(posecontent)
object.appendChild(pose) # pose標(biāo)簽結(jié)束
truncated = xmlBuilder.createElement("truncated") # truncated標(biāo)簽
truncatedContent = xmlBuilder.createTextNode("0")
truncated.appendChild(truncatedContent)
object.appendChild(truncated) # truncated標(biāo)簽結(jié)束
difficult = xmlBuilder.createElement("difficult") # difficult標(biāo)簽
difficultcontent = xmlBuilder.createTextNode("0")
difficult.appendChild(difficultcontent)
object.appendChild(difficult) # difficult標(biāo)簽結(jié)束
bndbox = xmlBuilder.createElement("bndbox") # bndbox標(biāo)簽
xmin = xmlBuilder.createElement("xmin") # xmin標(biāo)簽
mathData = int(((float(oneline[1])) * Pwidth + 1) - (float(oneline[3])) * 0.5 * Pwidth)
xminContent = xmlBuilder.createTextNode(str(mathData))
xmin.appendChild(xminContent)
bndbox.appendChild(xmin) # xmin標(biāo)簽結(jié)束
ymin = xmlBuilder.createElement("ymin") # ymin標(biāo)簽
mathData = int(((float(oneline[2])) * Pheight + 1) - (float(oneline[4])) * 0.5 * Pheight)
yminContent = xmlBuilder.createTextNode(str(mathData))
ymin.appendChild(yminContent)
bndbox.appendChild(ymin) # ymin標(biāo)簽結(jié)束
xmax = xmlBuilder.createElement("xmax") # xmax標(biāo)簽
mathData = int(((float(oneline[1])) * Pwidth + 1) + (float(oneline[3])) * 0.5 * Pwidth)
xmaxContent = xmlBuilder.createTextNode(str(mathData))
xmax.appendChild(xmaxContent)
bndbox.appendChild(xmax) # xmax標(biāo)簽結(jié)束
ymax = xmlBuilder.createElement("ymax") # ymax標(biāo)簽
mathData = int(((float(oneline[2])) * Pheight + 1) + (float(oneline[4])) * 0.5 * Pheight)
ymaxContent = xmlBuilder.createTextNode(str(mathData))
ymax.appendChild(ymaxContent)
bndbox.appendChild(ymax) # ymax標(biāo)簽結(jié)束
object.appendChild(bndbox) # bndbox標(biāo)簽結(jié)束
annotation.appendChild(object) # object標(biāo)簽結(jié)束
f = open(xmlPath + name[0:-4] + ".xml", 'w')
xmlBuilder.writexml(f, indent='\t', newl='\n', addindent='\t', encoding='utf-8')
f.close()
if __name__ == "__main__":
picPath = "dataset/image/" # 圖片所在文件夾路徑,后面的/一定要帶上
txtPath = "dataset/label/" # txt所在文件夾路徑,后面的/一定要帶上
xmlPath = "dataset/Annotations/" # xml文件保存路徑,后面的/一定要帶上
makexml(picPath, txtPath, xmlPath)3.需要修改的地方-標(biāo)簽字典
如果你要轉(zhuǎn)換得標(biāo)簽內(nèi)容與上面標(biāo)簽字典得內(nèi)容不同得話,請(qǐng)按需求修改成你自己的標(biāo)簽

4.需要修改的地方-文件夾路徑
如果你的文件夾路徑跟我上面的不一樣的話,那么在這里修改成你對(duì)應(yīng)的文件夾路徑

5.運(yùn)行你剛剛創(chuàng)建的convert.py文件,就生成xml格式的標(biāo)簽了

6.使用labelimg驗(yàn)證一下轉(zhuǎn)換之后的格式
先打開圖片和標(biāo)簽所在的文件夾

在這里輸入cmd


打開命令行窗口

先激活虛擬環(huán)境,輸入命令:
activate yolo

然后使用labelimg驗(yàn)證
labelimg image
在選擇標(biāo)簽文件夾的時(shí)候選擇剛才生成的voc格式標(biāo)簽的文件夾

然后進(jìn)入頁(yè)面就是這個(gè)樣子

說明轉(zhuǎn)換格式成功啦!??!
到此這篇關(guān)于如何將yolo格式轉(zhuǎn)化為voc格式:txt轉(zhuǎn)xml(親測(cè)有效)的文章就介紹到這了,更多相關(guān)yolo格式轉(zhuǎn)化為voc格式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
利用Python程序讀取Excel創(chuàng)建折線圖
這篇文章主要介紹了利用Python程序讀取Excel創(chuàng)建折線圖,文章通過圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09
jupyter notebook使用argparse傳入list參數(shù)
這篇文章主要介紹了jupyter notebook使用argparse傳入list參數(shù),jupyter notebook其實(shí)是可以使用 argparse來調(diào)用參數(shù)的,只要把參數(shù)轉(zhuǎn)為list即可,下面來看看具體的實(shí)現(xiàn)過程吧2022-01-01

