本实验中使用的数据集为Nucls数据集中的cav标注格式,对齐进行了转换:
from xml.dom.minidom import Document
import os
import cv2
import csv
def makexml(txtPath, xmlPath, picPath): # txt所在文件夹路径,xml文件保存路径,图片所在文件夹路径
"""此函数用于将yolo格式txt标注文件转换为voc格式xml标注文件
在自己的标注图片文件夹下建三个子文件夹,分别命名为picture、txt、xml
"""
files = os.listdir(txtPath)
for i, name in enumerate(files):
xmlBuilder = Document()
annotation = xmlBuilder.createElement("annotation") # 创建annotation标签
xmlBuilder.appendChild(annotation)
txtFile = open(os.path.join(txtPath, name))
#txtList = txtFile.readlines()
txtList = csv.reader(txtFile)
img = cv2.imread(os.path.join(picPath, name[0:-4] + ".png"))
Pheight, Pwidth, Pdepth = img.shape
folder = xmlBuilder.createElement("folder") # folder标签
foldercontent = xmlBuilder.createTextNode("VOC2007")
folder.appendChild(foldercontent)
annotation.appendChild(folder