我在这样的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
。
答案 0 :(得分:3)
对于您的问题而言,这是一个非常简单的解决方案:
这将以嵌套列表[xmin,ymin,xmax,ymax]和文件名返回框坐标 一旦我对bndbox标签(ymin,xmin,...)或其他任何奇怪的组合感到困惑,那么这段代码不仅读取位置,而且读取标签。
希望有帮助...
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
for box in boxes.findall("bndbox"):
ymin = int(box.find("ymin").text)
xmin = int(box.find("xmin").text)
ymax = int(box.find("ymax").text)
xmax = int(box.find("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")
答案 1 :(得分:0)
以下是详细的文档和源代码:https://github.com/trinath503/Python/tree/master/Generate_Pascal_VOC_Files
注意:您需要在指定的formart中传递数据
pascal_voc_data = '''
[
{
"folder":"folder",
"filename": "1.jpg",
"path":"path",
"source":{"database":"database"},
"size":{"width":256,"height":256,"depth":3},
"segmented":0,
"objects":[{"name":"name","pose":"pose","truncated":"truncated","occluded":"occluded","bndbox":{"xmin":3,"xmax":33,"ymin":3,"ymax":33}},{"name":"name","pose":"pose","truncated":"truncated","occluded":"occluded","bndbox":{"xmin":3,"xmax":33,"ymin":3,"ymax":33}}]
},
{
"folder":"folder",
"filename": "2.jpg",
"path":"path",
"source":{"database":"database"},
"size":{"width":256,"height":256,"depth":3},
"segmented":0,
"objects":[{"name":"name","pose":"pose","truncated":"truncated","occluded":"occluded","bndbox":{"xmin":3,"xmax":33,"ymin":3,"ymax":33}},{"name":"name","pose":"pose","truncated":"truncated","occluded":"occluded","bndbox":{"xmin":3,"xmax":33,"ymin":3,"ymax":33}}]
}
]
'''