首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在python中读取PASCAL VOC批注

在python中读取PASCAL VOC批注
EN

Stack Overflow用户
提问于 2018-11-15 18:41:50
回答 1查看 12.2K关注 0票数 12

我在xml文件中有这样的注释,它遵循PASCAL VOC约定:

代码语言:javascript
复制
<annotation>
<folder>training</folder>
<filename>chanel1.jpg</filename>
<source>
<database>synthetic initialization</database>
<annotation>PASCAL VOC2007</annotation>
<image>synthetic</image>
<flickrid>none</flickrid>
</source>
<owner>
<flickrid>none</flickrid>
<name>none</name>
</owner>
<size>
<width>640</width>
<height>427</height>
<depth>3</depth>
</size>
<segmented>0</segmented>
<object>
<name>chanel</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>344</xmin>
<ymin>10</ymin>
<xmax>422</xmax>
<ymax>83</ymax>
</bndbox>
</object>
<object>
<name>chanel</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>355</xmin>
<ymin>165</ymin>
<xmax>443</xmax>
<ymax>206</ymax>
</bndbox>
</object>
</annotation>

例如,在Python语言中检索字段filenamebndbox的最干净的方法是什么?

我正在尝试ElementTree,这似乎是官方的Python解决方案,但我无法使其工作。

到目前为止我的代码如下:

代码语言:javascript
复制
from xml.etree import ElementTree as ET
tree = ET.parse("data/all/annotations/" + file)
fn = tree.find('filename').text
boxes = tree.findall('bndbox')

这就产生了

代码语言:javascript
复制
fn == 'chanel1.jpg'
boxes == []

因此,它成功地提取了filename字段,但没有提取bndbox字段。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-18 19:32:53

对于您的问题,这是一个非常简单的解决方案:

这将返回一个嵌套列表中的方框坐标xmin,ymin,xmax,ymax和文件名,一旦我与bndbox标签弄混了(ymin,xmin,...)或者任何其他奇怪的组合,所以这段代码读取的不仅仅是标签的位置。

最后,我更新了代码。多亏了craq和Pritesh Gohil,你是绝对正确的。

希望这能帮上忙。

代码语言:javascript
复制
import xml.etree.ElementTree as ET


def read_content(xml_file: str):

    tree = ET.parse(xml_file)
    root = tree.getroot()

    list_with_all_boxes = []

    for boxes in root.iter('object'):

        filename = root.find('filename').text

        ymin, xmin, ymax, xmax = None, None, None, None

        ymin = int(boxes.find("bndbox/ymin").text)
        xmin = int(boxes.find("bndbox/xmin").text)
        ymax = int(boxes.find("bndbox/ymax").text)
        xmax = int(boxes.find("bndbox/xmax").text)

        list_with_single_boxes = [xmin, ymin, xmax, ymax]
        list_with_all_boxes.append(list_with_single_boxes)

    return filename, list_with_all_boxes

name, boxes = read_content("file.xml")
票数 30
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53317592

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档