常用的標註文件除了採用json格式外,還有xml格式。最近有需要在Python中讀xml文件,記錄下來以備後用。
標註文件內容示例如下:
<?xml version="1.0" encoding="utf-8"?>
<annotations>
<version>1.1</version>
<image id="1" name="0001.jpg" width="1920" height="1080">
<box label="person" occluded="0" xtl="1300.80" ytl="69.89" xbr="1344.74" ybr="213.33">
<attribute name="state">man</attribute>
</box>
<box label="person" occluded="0" xtl="1164.29" ytl="96.66" xbr="1224.44" ybr="253.65">
<attribute name="state">women</attribute>
</box>
</image>
</annotations>
讀xml文件代碼如下:
from xml.dom.minidom import parse
# parse the xml file
xml_data = parse('test.xml')
rootNode = xml_data.documentElement
nodes = rootNode.getElementsByTagName('image')
for node in nodes:
boxes = node.getElementsByTagName('box')
for box in boxes:
xytb = [float(box.getAttribute('xtl')), float(box.getAttribute('ytl')),
float(box.getAttribute('xbr')), float(box.getAttribute('ybr'))]
attribute = str(box.getElementsByTagName('attribute')[0].childNodes[0].data)