TXT的文件格式为 wmin,hmin,wmax,hmax
最终转换出来的效果为
代码:
from xml.dom import minidom
import cv2
txt_dirtory=r'F:\9.txt'
jpg_dirtory=r'F:\9.jpg'
img_name=jpg_dirtory.split('\\')[-1]
floder=jpg_dirtory.split('\\')[-2]
im = cv2.imread(jpg_dirtory)
w = im.shape[1]
h = im.shape[0]
d = im.shape[2]
# print w,h,d
doc = minidom.Document() #创建DOM树对象
annotation = doc.createElement('annotation') #创建子节点
doc.appendChild(annotation) #annotation作为doc树的子节点
folder = doc.createElement('folder')
folder.appendChild(doc.createTextNode(floder)) #文本节点作为floder的子节点
annotation.appendChild(folder) #folder作为annotation的子节点
filename = doc.createElement('filename')
filename.appendChild(doc.createTextNode(img_name))
annotation.appendChild(filename)
filename = doc.createElement('path')
filename.appendChild(doc.createTextNode(jpg_dirtory))
annotation.appendChild(filename)
source = doc.createElement('source')
database = doc.createElement('database')
database.appendChild(doc.createTextNode("Unknown"))
source.appendChild(database)
# annotation2 = doc.createElement('annotation')
# annotation2.appendChild(doc.createTextNode("ICDAR POD2017"))
# source.appendChild(annotation2)
# image = doc.createElement('image')
# image.appendChild(doc.createTextNode("image"))
# source.appendChild(image)
# flickrid = doc.createElement('flickrid')
# flickrid.appendChild(doc.createTextNode("NULL"))
# source.appendChild(flickrid)
annotation.appendChild(source)
# owner = doc.createElement('owner')
# flickrid = doc.createElement('flickrid')
# flickrid.appendChild(doc.createTextNode("NULL"))
# owner.appendChild(flickrid)
# na = doc.createElement('name')
# na.appendChild(doc.createTextNode("cxm"))
# owner.appendChild(na)
# annotation.appendChild(owner)
size = doc.createElement('size')
width = doc.createElement('width')
width.appendChild(doc.createTextNode("%d" % w))
size.appendChild(width)
height = doc.createElement('height')
height.appendChild(doc.createTextNode("%d" % h))
size.appendChild(height)
depth = doc.createElement('depth')
depth.appendChild(doc.createTextNode("%d" % d))
size.appendChild(depth)
annotation.appendChild(size)
segmented = doc.createElement('segmented')
segmented.appendChild(doc.createTextNode("0"))
annotation.appendChild(segmented)
txtLabel = open(txt_dirtory, 'r')
boxes = [txtLabel.readline()]
for box in boxes:
# print(box)
box = box.split(' ')
# print(box)
object = doc.createElement('object')
nm = doc.createElement('name')
nm.appendChild(doc.createTextNode('0'))
object.appendChild(nm)
pose = doc.createElement('pose')
pose.appendChild(doc.createTextNode("Unspecified"))
object.appendChild(pose)
truncated = doc.createElement('truncated')
truncated.appendChild(doc.createTextNode("1"))
object.appendChild(truncated)
difficult = doc.createElement('difficult')
difficult.appendChild(doc.createTextNode("0"))
object.appendChild(difficult)
bndbox = doc.createElement('bndbox')
xmin = doc.createElement('xmin')
xmin.appendChild(doc.createTextNode(box[0]))
bndbox.appendChild(xmin)
ymin = doc.createElement('ymin')
ymin.appendChild(doc.createTextNode(box[1]))
bndbox.appendChild(ymin)
xmax = doc.createElement('xmax')
xmax.appendChild(doc.createTextNode(box[2]))
bndbox.appendChild(xmax)
ymax = doc.createElement('ymax')
ymax.appendChild(doc.createTextNode(box[3]))
bndbox.appendChild(ymax)
object.appendChild(bndbox)
annotation.appendChild(object)
savefile = open(r'F:\9.xml', 'w')
savefile.write(doc.toprettyxml())
savefile.close()
参考内容:
txt版本的标签转换成PASCAL VOC2007 格式的XML标签
Python minidom模块(DOM写入和解析XML)