import xml.etree.ElementTree as ET
def parseXml(filename):
xml_file = open(filename)
tree = ET.parse(xml_file)
root = tree.getroot()
w = int(root.find('size').find('width').text)
h = int(root.find('size').find('height').text)
print(w,h)
lb = []
for obj in root.iter('object'):
name = obj.find('name').text
xmlbox = obj.find('bndbox')
x1 = int(xmlbox.find('xmin').text)
x2 = int(xmlbox.find('xmax').text)
y1 = int(xmlbox.find('ymin').text)
y2 = int(xmlbox.find('ymax').text)
center_x = (x1+x2)*0.5
center_y = (y1+y2)*0.5
cw = x2 - x1
ch = y2 - y1
if name=='name':
info = [0,center_x/float(w),center_y / float(h),cw / float(w),ch / float(h)]
else:
info = [1, center_x / float(w), center_y / float(h), cw / float(w), ch / float(h)]
lb.append(info)
return lb
if __name__=='__main__':
print(parseXml('vocface/016102.xml'))
05-03
2185
01-29
2202
04-10
409