代碼
# -*- coding: utf-8 -*-
try:
import xml.etree.cElementTree as ET
except ImportError:
import xml.etree.ElementTree as ET
import os
def GetAnnotBoxLoc(AnotPath):
tree = ET.ElementTree(file=AnotPath)
root = tree.getroot()
ObjSize=root.findall('size')
assert(len(ObjSize)==1)
fwidth = float(ObjSize[0].find("width").text)
fheight = float(ObjSize[0].find("height").text)
ObjectSet=root.findall('object')
ObjBndBoxSet={}
for Object in ObjectSet:
ObjName=Object.find('name').text
BndBox=Object.find('bndbox')
x1 = float(BndBox.find('xmin').text)#-1
y1 = float(BndBox.find('ymin').text)#-1
x2 = float(BndBox.find('xmax').text)#-1
y2 = float(BndBox.find('ymax').text)#-1
BndBoxLoc=[x1/fwidth,y1/fheight,x2/fwidth,y2/fheight]
if ObjBndBoxSet.has_key(ObjName):
ObjBndBoxSet[ObjName].append(BndBoxLoc)
else:
ObjBndBoxSet[ObjName]=[BndBoxLoc]
return ObjBndBoxSet
def ConvertXml2Txt(fileInput,fileOutput,name2Lable):
objBndBoxSet = GetAnnotBoxLoc(fileInput)
fp = open(fileOutput,"w+")
for objk in objBndBoxSet:
label = name2Lable[objk]
objv = objBndBoxSet[objk]
for v in objv:
fp.writelines("%d %.6f %.6f %.6f %.6f\n"%(label,v[0],v[1],v[2],v[3]))
fp.close()
# main
if __name__== '__main__':
path = r"../voc/Annotations"
name2Lable = {"aeroplane": 1,"bicycle": 2,"bird": 3,"boat": 4,"bottle": 5,"bus": 6,"car": 7,"cat": 8,"chair": 9,"cow": 10,
"diningtable": 11,"dog": 12,"horse": 13,"motorbike":14,"person": 15,"pottedplant": 16,"sheep": 17,"sofa": 18,"train": 19,"tvmonitor": 20}
for root,dirs,files in os.walk(path):
for file in files:
splitSeg = file.split('.')
if splitSeg[len(splitSeg)-1] == "xml":
fileInput = os.path.join(root,file)
fileOutput = os.path.join(root,file.replace('xml','txt'))
print("Process %s"%(fileInput))
ConvertXml2Txt(fileInput,fileOutput,name2Lable)
輸出
//2007_000346.txt
15 0.274000 0.208000 0.994000 1.000000
15 0.178000 0.538667 0.258000 0.658667
15 0.144000 0.557333 0.222000 0.690667
5 0.248000 0.285333 0.460000 0.914667
參考
核心代碼參考 https://blog.csdn.net/qq_38109843/article/details/85323777