我在xml文件中有这样的注释,它遵循PASCAL VOC约定:
<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语言中检索字段filename和bndbox的最干净的方法是什么?
我正在尝试ElementTree,这似乎是官方的Python解决方案,但我无法使其工作。
到目前为止我的代码如下:
from xml.etree import ElementTree as ET
tree = ET.parse("data/all/annotations/" + file)
fn = tree.find('filename').text
boxes = tree.findall('bndbox')这就产生了
fn == 'chanel1.jpg'
boxes == []因此,它成功地提取了filename字段,但没有提取bndbox字段。
发布于 2018-12-18 19:32:53
对于您的问题,这是一个非常简单的解决方案:
这将返回一个嵌套列表中的方框坐标xmin,ymin,xmax,ymax和文件名,一旦我与bndbox标签弄混了(ymin,xmin,...)或者任何其他奇怪的组合,所以这段代码读取的不仅仅是标签的位置。
最后,我更新了代码。多亏了craq和Pritesh Gohil,你是绝对正确的。
希望这能帮上忙。
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")https://stackoverflow.com/questions/53317592
复制相似问题