1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
| from xml.dom.minidom import Document import os import cv2 from tqdm import tqdm
def makexml(picPath, txtPath, xmlPath, datasetName): """此函数用于将yolo格式txt标注文件转换为voc格式xml标注文件 在自己的标注图片文件夹下建三个子文件夹,分别命名为picture、txt、xml """ dic = {'0': "Pickable"} files = os.listdir(txtPath) for i, name in enumerate(tqdm(files)): if name == 'classes.txt': continue xmlBuilder = Document() annotation = xmlBuilder.createElement("annotation") xmlBuilder.appendChild(annotation) txtFile = open(os.path.join(txtPath, name)) txtList = txtFile.readlines() img = cv2.imread(os.path.join(picPath, name.replace('txt', 'jpg'))) Pheight, Pwidth, Pdepth = img.shape
folder = xmlBuilder.createElement("folder") foldercontent = xmlBuilder.createTextNode(datasetName) folder.appendChild(foldercontent) annotation.appendChild(folder)
filename = xmlBuilder.createElement("filename") filenamecontent = xmlBuilder.createTextNode(name[0:-4] + ".jpg") filename.appendChild(filenamecontent) annotation.appendChild(filename)
size = xmlBuilder.createElement("size") width = xmlBuilder.createElement("width") widthcontent = xmlBuilder.createTextNode(str(Pwidth)) width.appendChild(widthcontent) size.appendChild(width)
height = xmlBuilder.createElement("height") heightcontent = xmlBuilder.createTextNode(str(Pheight)) height.appendChild(heightcontent) size.appendChild(height)
depth = xmlBuilder.createElement("depth") depthcontent = xmlBuilder.createTextNode(str(Pdepth)) depth.appendChild(depthcontent) size.appendChild(depth)
annotation.appendChild(size)
for j in txtList: oneline = j.strip().split(" ") object = xmlBuilder.createElement("object") picname = xmlBuilder.createElement("name") namecontent = xmlBuilder.createTextNode(dic[oneline[0]]) picname.appendChild(namecontent) object.appendChild(picname)
pose = xmlBuilder.createElement("pose") posecontent = xmlBuilder.createTextNode("Unspecified") pose.appendChild(posecontent) object.appendChild(pose)
truncated = xmlBuilder.createElement("truncated") truncatedContent = xmlBuilder.createTextNode("0") truncated.appendChild(truncatedContent) object.appendChild(truncated)
difficult = xmlBuilder.createElement("difficult") difficultcontent = xmlBuilder.createTextNode("0") difficult.appendChild(difficultcontent) object.appendChild(difficult)
bndbox = xmlBuilder.createElement("bndbox") xmin = xmlBuilder.createElement("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)
ymin = xmlBuilder.createElement("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)
xmax = xmlBuilder.createElement("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)
ymax = xmlBuilder.createElement("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)
object.appendChild(bndbox)
annotation.appendChild(object)
f = open(os.path.join(xmlPath, name.replace('txt', 'xml')), 'w') xmlBuilder.writexml(f, indent='\t', newl='\n', addindent='\t', encoding='utf-8') f.close()
if __name__ == "__main__": picPath = r"G:\CBB-Program\TKK-10\TKK-SDD\VOCdevkit\VOC2007\JPEGImages/" txtPath = r"G:\CBB-Program\TKK-10\TKK-SDD\VOCdevkit\VOC2007\111/" xmlPath = r"G:\CBB-Program\TKK-10\TKK-SDD\VOCdevkit\VOC2007\Annotations/" datasetName = r'Pickable' os.makedirs(xmlPath) if not os.path.exists(xmlPath) else None makexml(picPath, txtPath, xmlPath, datasetName)
|