用labelImg工具制作好xml文件后,需要读取其中img路径和坐标点,生成一个label.txt
<annotation>
<folder>big</folder>
<filename>img_7.jpg</filename>
<path>E:/FDDB/2003/01/02/big/img_7.jpg</path>
<source>
<database>Unknown</database>
</source>
<size>
<width>367</width>
<height>450</height>
<depth>3</depth>
</size>
<segmented>0</segmented>
<object>
<name>face</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>116</xmin>
<ymin>21</ymin>
<xmax>220</xmax>
<ymax>167</ymax>
</bndbox>
</object>
</annotation>
python脚本,python2.7,在img的路径中最好不要加上中文字符,否则提取或者存入txt的时候,会比较麻烦
# coding=utf-8
# 读xml文件中的一个rect
import xml.etree.ElementTree as ET
import sys reload(sys)
sys.setdefaultencoding('utf-8') xml_path='./img_7.xml'
tree = ET.parse(xml_path)
rect={}
line=""
root = tree.getroot()
for name in root.iter('path'):
rect['path'] = name.text
for ob in root.iter('object'):
for bndbox in ob.iter('bndbox'):
# for l in bndbox:
# print(l.text)
for xmin in bndbox.iter('xmin'):
rect['xmin'] = xmin.text
for ymin in bndbox.iter('ymin'):
rect['ymin'] = ymin.text
for xmax in bndbox.iter('xmax'):
rect['xmax'] = xmax.text
for ymax in bndbox.iter('ymax'):
rect['ymax'] = ymax.text
line = rect['path'] + "\t"+ rect['xmin']+ "\t"+rect['ymin']+"\t"+rect['xmax']+"\t"+rect['ymax'] f1 = open('img_7.txt', 'w')
f1.write(line)
即可将xml中的img路径和,xmin,ymin,xman,ymax数据写入到一个txt文档中。