目标检测txt标注文件(Yolo格式)转为xml标注文件(coco格式)
from xml.dom.minidom import Document
import os
import cv2
def makexml(picPath, txtPath, xmlPath):
"""此函数用于将yolo格式txt标注文件转换为voc格式xml标注文件
在自己的标注图片文件夹下建三个子文件夹,分别命名为picture、txt、xml
"""
dic = {
'0': "person",
'1': "rider",
'2': "car",
'3': "truck",
'4': "bus",
'5': "train",
'6': "motorcycle",
'7': "bicycle",
}
files = os.listdir(txtPath)
for i, name in enumerate(files):
xmlBuilder = Document()
annotation = xmlBuilder.createElement("annotation")
xmlBuilder.appendChild(annotation)
txtFile = open(txtPath +'/'+ name)
txtList = txtFile.readlines()
for root,dirs,filename in os.walk(picPath):
img = cv2.imread(root+ '/'+filename[i])
Pheight, Pwidth, Pdepth = img.shape
folder = xmlBuilder.createElement