Add a function which can load xml file
This commit is contained in:
parent
e85c4568b9
commit
ed787d4f3a
25
labelImg.py
25
labelImg.py
@ -22,6 +22,7 @@ from labelDialog import LabelDialog
|
||||
from colorDialog import ColorDialog
|
||||
from labelFile import LabelFile, LabelFileError
|
||||
from toolBar import ToolBar
|
||||
from pascal_voc_io import PascalVocReader
|
||||
|
||||
__appname__ = 'labelImg'
|
||||
|
||||
@ -139,6 +140,9 @@ class MainWindow(QMainWindow, WindowMixin):
|
||||
changeSavedir = action('&Change default saved Annotation dir', self.changeSavedir,
|
||||
'Ctrl+r', 'open', u'Change default saved Annotation dir')
|
||||
|
||||
openAnnotation = action('&Open Annotation', self.openAnnotation,
|
||||
'Ctrl+q', 'openAnnotation', u'Open Annotation')
|
||||
|
||||
openNextImg = action('&Next Image', self.openNextImg,
|
||||
'n', 'next', u'Open Next')
|
||||
|
||||
@ -263,7 +267,7 @@ class MainWindow(QMainWindow, WindowMixin):
|
||||
labelList=labelMenu)
|
||||
|
||||
addActions(self.menus.file,
|
||||
(open, opendir,changeSavedir, self.menus.recentFiles, save, saveAs, close, None, quit))
|
||||
(open, opendir,changeSavedir, openAnnotation, self.menus.recentFiles, save, saveAs, close, None, quit))
|
||||
addActions(self.menus.help, (help,))
|
||||
addActions(self.menus.view, (
|
||||
labels, advancedMode, None,
|
||||
@ -797,6 +801,22 @@ class MainWindow(QMainWindow, WindowMixin):
|
||||
self.statusBar().showMessage('%s . Annotation will be saved to %s' %('Change saved folder', self.defaultSaveDir))
|
||||
self.statusBar().show()
|
||||
|
||||
def openAnnotation(self, _value=False):
|
||||
path = os.path.dirname(unicode(self.filename))\
|
||||
if self.filename else '.'
|
||||
|
||||
formats = ['*.%s' % unicode(fmt).lower()\
|
||||
for fmt in QImageReader.supportedImageFormats()]
|
||||
filters = "Open Annotation XML file (%s)" % \
|
||||
' '.join(formats + ['*.xml'])
|
||||
filename = unicode(QFileDialog.getOpenFileName(self,
|
||||
'%s - Choose a xml file' % __appname__, path, filters))
|
||||
|
||||
tVocParseReader = PascalVocReader(filename)
|
||||
shapes = tVocParseReader.getShapes()
|
||||
self.loadLabels(shapes)
|
||||
return
|
||||
|
||||
def openDir(self, _value=False):
|
||||
if not self.mayContinue():
|
||||
return
|
||||
@ -834,8 +854,6 @@ class MainWindow(QMainWindow, WindowMixin):
|
||||
if filename:
|
||||
self.loadFile(filename)
|
||||
|
||||
|
||||
|
||||
def openNextImg(self, _value=False):
|
||||
# Proceding next image without dialog if having any label
|
||||
if self.autoSaving is True and self.defaultSaveDir is not None:
|
||||
@ -1039,7 +1057,6 @@ class Settings(object):
|
||||
def inverted(color):
|
||||
return QColor(*[255 - v for v in color.getRgb()])
|
||||
|
||||
|
||||
def read(filename, default=None):
|
||||
try:
|
||||
with open(filename, 'rb') as f:
|
||||
|
||||
@ -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))
|
||||
Loading…
x
Reference in New Issue
Block a user