搭配我之前写的生成tfrecord的文章,可以直接将labelme标注出来的xml文件解析出来,用于生成bboundingbox的tfrecod文件 import xmltodict import json import xml.dom.minidom as xmldom from functools import reduce import operator import os
def LabelmeXmlGetCoordinate(LabelmeXmlPath): AllcoordinateList = [] for file in [os.path.join(LabelmeXmlPath,item) for item in os.listdir(LabelmeXmlPath)]: fileName = os.path.basename(file).split('.')[0]+'.jpg' DOMTree = xmldom.parse(file) collection = DOMTree.documentElement # 获取尺寸信息 imgSize = collection.getElementsByTagName('size') width = imgSize[0].getElementsByTagName('width')[0].childNodes[0].data height = imgSize[0].getElementsByTagName('height')[0].childNodes[0].data depth = imgSize[0].getElementsByTagName('depth')[0].childNodes[0].data # 为了后面将box信息的列表添加进来后方便降成一维,创建二维数组 coordinateList = [[fileName,width,height,depth]] imgObjects = collection.getElementsByTagName('object') # 获取bboundingbox信息 for imgObject in imgObjects: label = imgObject.getElementsByTagName('name')[0].childNodes[0].data imgBox = imgObject.getElementsByTagName('bndbox')[0] xmin = imgBox.getElementsByTagName('xmin')[0].childNodes[0].data ymin = imgBox.getElementsByTagName('ymin')[0].childNodes[0].data xmax = imgBox.getElementsByTagName('xmax')[0].childNodes[0].data ymax = imgBox.getElementsByTagName('ymax')[0].childNodes[0].data bboxList = [xmin,ymin,xmax,ymax,label] coordinateList.append(bboxList) # 将二维数据变成一维数组 eg:['0acf8.xml', '331', '257', '3', '65', '78', '234', '196', 'a', '258', '122', '300', '171', 'a'] coordinateList = reduce(operator.add, coordinateList) AllcoordinateList.append(coordinateList) print(coordinateList)