目标检测标注的图片xml文件中的二级标签转为字典

本文介绍了一个用于解析XML格式图片标注文件的Python函数。通过递归方式将XML结构转换为易于操作的字典形式,便于进一步的数据处理和分析。重点讲解了如何处理XML中的二级标签,如folder, filename, path等。

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

输出图片标注xml文件的二级标签
folder
filename
path
source
size
segmented
object

import xml.dom.minidom as xmldom
import sys
from lxml import etree
import tensorflow as tf

sys.setrecursionlimit(1000000)
def recursive_parse_xml_to_dict(xml):
  
  if not xml:
    return {xml.tag: xml.text}
  result = {}
  for child in xml:
    child_result = recursive_parse_xml_to_dict(child)
    if child.tag != 'object':
      result[child.tag] = child_result[child.tag]
    else:
      if child.tag not in result:
        result[child.tag] = []
      result[child.tag].append(child_result[child.tag])
  return {xml.tag: result}


with tf.io.gfile.GFile("000001.xml", 'r') as fid:
  xml_str = fid.read()
xml = etree.fromstring(xml_str)  
dict0 = recursive_parse_xml_to_dict(xml)['annotation']
for i in dict0:
  print(i)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hacker_wooder

谢谢您的赞赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值