Add a function which can load xml file

This commit is contained in:
tzutalin
2015-12-09 21:29:26 +08:00
parent e85c4568b9
commit ed787d4f3a
3 changed files with 61 additions and 5 deletions
+1 -1
View File
@@ -3,7 +3,7 @@ import os.path
import numpy
import cv2
import sys
from pascal_voc_writer import PascalVocWriter
from pascal_voc_io import PascalVocWriter
from base64 import b64encode, b64decode
class LabelFileError(Exception):
@@ -100,6 +100,45 @@ class PascalVocWriter:
out_file.write(self.prettify(root))
out_file.close()
class PascalVocReader:
def __init__(self, filepath):
## shapes type:
## [labbel, [(x1,y1), (x2,y2), (x3,y3), (x4,y4)], color, color]
self.shapes=[]
self.filepath = filepath
self.parseXML()
def getShapes(self):
return self.shapes
def addShape(self, label, rect):
xmin = rect[0]
ymin = rect[1]
xmax = rect[2]
ymax = rect[3]
points = [(xmin,ymin), (xmin,ymax), (xmax, ymax), (xmax, ymin)]
self.shapes.append((label, points, None, None))
def parseXML(self):
assert self.filepath.endswith('.xml'), "Unsupport file format"
xmltree = ElementTree.parse(self.filepath).getroot()
filename = xmltree.find('filename').text
for object_iter in xmltree.findall('object'):
rects = []
bndbox = object_iter.find("bndbox")
rects.append([int(it.text) for it in bndbox])
label = object_iter.find('name').text
for rect in rects:
self.addShape(label, rect)
return True
# tempParseReader = PascalVocReader('test.xml')
# print tempParseReader.getShapes()
"""
# Test
tmp = PascalVocWriter('temp','test', (10,20,3))