python读取xml文件,VOC格式标注读取

python解析XML的三种方式
https://www.cnblogs.com/xiaobingqianrui/p/8405813.html

主要用xml.etree.ElementTree模块(简称 ET)来解析xml文件,它提供了轻量级的Python式的API。读取VOC数据集大致流程如下:

import xml.etree.ElementTree as ET

tree = ET.parse(file_name)
root = tree.getroot()
# 从size节点中读取宽高
size=root.find('size')
width = float(size.find('width').text)
height = float(size.find('height').text)
print(width,height)
        
for obj in root.iter('object'):
    xml_box = obj.find('bndbox')
    xmin = (int(xml_box.find('xmin').text))
    ymin = (int(xml_box.find('ymin').text))
    xmax = (int(xml_box.find('xmax').text))
    ymax = (int(xml_box.find('ymax').text))
    
	captionList = obj.findall('class') # find只能查找一个,findall可以查找所有的,class名可以换为其他子节点名

Pascal voc 数据集xml格式解析
https://blog.csdn.net/weixin_41278720/article/details/84872064

python xml提取多重标签内容
https://blog.csdn.net/qq_36076233/article/details/78989300

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
读取voc格式的数据集,可以按照以下步骤进行操作: 1. 首先,下载VOC数据集并解压缩。VOC数据集通常包含图像文件和相应的注释文件,其中注释文件可以是xml格式或txt格式。 2. 使用Python中的xml.etree.ElementTree或其他相关库来解析注释文件。如果注释文件是xml格式的,可以使用以下代码进行解析: ```python import xml.etree.ElementTree as ET def parse_annotation(xml_file): tree = ET.parse(xml_file) root = tree.getroot() # 获取图像的宽度和高度 size = root.find('size') width = int(size.find('width').text) height = int(size.find('height').text) # 遍历每个object标签,提取标注框的坐标和类别 boxes = [] labels = [] for obj in root.findall('object'): label = obj.find('name').text bbox = obj.find('bndbox') xmin = int(bbox.find('xmin').text) ymin = int(bbox.find('ymin').text) xmax = int(bbox.find('xmax').text) ymax = int(bbox.find('ymax').text) boxes.append([xmin, ymin, xmax, ymax]) labels.append(label) return boxes, labels, width, height ``` 3. 遍历数据集中的每个图像文件,使用上述函数解析相应的注释文件,并获取图像的宽度、高度、标注框坐标和类别。 ```python import os data_dir = '/path/to/voc_dataset' image_dir = os.path.join(data_dir, 'JPEGImages') annotation_dir = os.path.join(data_dir, 'Annotations') # 遍历图像文件夹中的每个图像文件 for filename in os.listdir(image_dir): image_file = os.path.join(image_dir, filename) annotation_file = os.path.join(annotation_dir, f'{os.path.splitext(filename)[0]}.xml') # 解析注释文件 boxes, labels, width, height = parse_annotation(annotation_file) # 在这里可以做进一步的处理,例如将图像数据和标注框传递给模型进行训练或推理 # ... ``` 这样,你就可以读取并解析voc格式的数据集了。根据具体需求,你可以进一步处理数据并进行后续的操作,如训练模型或进行目标检测等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值