Python 解析labelme标注的xml文件(bbounding box)

本文介绍了一种方法,用于将Labelme工具标注的XML文件转换为TensorFlow所需的TFRecord格式,便于物体检测模型训练。文章详细展示了如何解析XML文件,提取图像尺寸和bounding box坐标,并将其整理为一维数组,方便进一步的数据处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

搭配我之前写的生成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)

              

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值