具體參考:https://blog.csdn.net/qq_43161211/article/details/122958255(如侵權现横,速刪)
1.數據目錄格式如下:
其中JPEGImages為圖片數據所在的目錄贞远,YOLO為yolo格式的標簽數據所在目錄,Annotations為生成的voc格式數據標簽目錄(程序運行前這是一個空目錄)
2.把對應文件、圖片放到對應目錄下
3.執(zhí)行yolo_to_voc.py:
本人使用的圖片格式為bmp苞七,jpg等格式需修改程序中的bmp->jpg
from xml.dom.minidom import Document
import os
import cv2
# def makexml(txtPath, xmlPath, picPath): # txt所在文件夾路徑器予,xml文件保存路徑,圖片所在文件夾路徑
def makexml(picPath, txtPath, xmlPath): # txt所在文件夾路徑浙芙,xml文件保存路徑登刺,圖片所在文件夾路徑
"""此函數用于將yolo格式txt標注文件轉換為voc格式xml標注文件
在自己的標注圖片文件夾下建三個子文件夾,分別命名為picture茁裙、txt塘砸、xml
"""
dic = {'0': "Person", # 創(chuàng)建字典用來對類型進行轉換
'1': "Car", # 此處的字典要與自己的classes.txt文件中的類對應,且順序要一致
}
files = os.listdir(txtPath)
for i, name in enumerate(files):
xmlBuilder = Document()
annotation = xmlBuilder.createElement("annotation") # 創(chuàng)建annotation標簽
xmlBuilder.appendChild(annotation)
txtFile = open(txtPath + name)
txtList = txtFile.readlines()
img = cv2.imread(picPath + name[0:-4] + ".bmp")
Pheight, Pwidth, Pdepth = img.shape
folder = xmlBuilder.createElement("folder") # folder標簽
foldercontent = xmlBuilder.createTextNode("driving_annotation_dataset")
folder.appendChild(foldercontent)
annotation.appendChild(folder) # folder標簽結束
filename = xmlBuilder.createElement("filename") # filename標簽
filenamecontent = xmlBuilder.createTextNode(name[0:-4] + ".bmp")
filename.appendChild(filenamecontent)
annotation.appendChild(filename) # filename標簽結束
size = xmlBuilder.createElement("size") # size標簽
width = xmlBuilder.createElement("width") # size子標簽width
widthcontent = xmlBuilder.createTextNode(str(Pwidth))
width.appendChild(widthcontent)
size.appendChild(width) # size子標簽width結束
height = xmlBuilder.createElement("height") # size子標簽height
heightcontent = xmlBuilder.createTextNode(str(Pheight))
height.appendChild(heightcontent)
size.appendChild(height) # size子標簽height結束
depth = xmlBuilder.createElement("depth") # size子標簽depth
depthcontent = xmlBuilder.createTextNode(str(Pdepth))
depth.appendChild(depthcontent)
size.appendChild(depth) # size子標簽depth結束
annotation.appendChild(size) # size標簽結束
for j in txtList:
oneline = j.strip().split(" ")
object = xmlBuilder.createElement("object") # object 標簽
picname = xmlBuilder.createElement("name") # name標簽
namecontent = xmlBuilder.createTextNode(dic[oneline[0]])
picname.appendChild(namecontent)
object.appendChild(picname) # name標簽結束
pose = xmlBuilder.createElement("pose") # pose標簽
posecontent = xmlBuilder.createTextNode("Unspecified")
pose.appendChild(posecontent)
object.appendChild(pose) # pose標簽結束
truncated = xmlBuilder.createElement("truncated") # truncated標簽
truncatedContent = xmlBuilder.createTextNode("0")
truncated.appendChild(truncatedContent)
object.appendChild(truncated) # truncated標簽結束
difficult = xmlBuilder.createElement("difficult") # difficult標簽
difficultcontent = xmlBuilder.createTextNode("0")
difficult.appendChild(difficultcontent)
object.appendChild(difficult) # difficult標簽結束
bndbox = xmlBuilder.createElement("bndbox") # bndbox標簽
xmin = xmlBuilder.createElement("xmin") # xmin標簽
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標簽結束
ymin = xmlBuilder.createElement("ymin") # ymin標簽
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標簽結束
xmax = xmlBuilder.createElement("xmax") # xmax標簽
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標簽結束
ymax = xmlBuilder.createElement("ymax") # ymax標簽
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標簽結束
object.appendChild(bndbox) # bndbox標簽結束
annotation.appendChild(object) # object標簽結束
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 = "/home/slave110/code/VOCdevkit/VOC2007/JPEGImages/" # 圖片所在文件夾路徑晤锥,后面的/一定要帶上
txtPath = "/home/slave110/code/VOCdevkit/VOC2007/YOLO/" # txt所在文件夾路徑掉蔬,后面的/一定要帶上
xmlPath = "/home/slave110/code/VOCdevkit/VOC2007/Annotations/" # xml文件保存路徑,后面的/一定要帶上
makexml(picPath, txtPath, xmlPath)