forked from CSAILVision/LabelMeAnnotationTool
/
labelFile.py
293 lines (253 loc) · 10.1 KB
/
labelFile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# -*- coding : utf-8 -*-
#
# Copyright (C) 2011 Michael Pitidis, Hussein Abdulwahid.
#
# This file is part of Labelme.
#
# Labelme is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Labelme is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Labelme. If not, see <http://www.gnu.org/licenses/>.
#
#from PyQt4.QtGui import *
#from PyQt4.QtCore import *
from PySide.QtGui import *
from PySide.QtCore import *
from lib import newIcon
import json
import os.path
from os import environ
import sys
import ntpath
from base64 import b64encode, b64decode
from xml.etree.cElementTree import Element, SubElement, Comment, tostring, ElementTree
import xml.etree.ElementTree as ET
import xml.etree.cElementTree as et
import re
from xmltools import indent
from shape import DEFAULT_LINE_COLOR_LIST, DEFAULT_FILL_COLOR_LIST
from labelDialog import validWriteTypes
# helpers
def getLines(snip):
_linesOK = []
for _line in snip.getiterator('Line'):
if not re.match(ur'^\s*$', _line.get('Value')):
_linesOK.append(_line)
return _linesOK
def pointsFromBox(snip):
if snip.get('Top') != None: # bounding box
top = int(snip.get('Top'))
left = int(snip.get('Left'))
bottom = int(snip.get('Bottom'))
right = int(snip.get('Right'))
return [[left,top], [right,top], [right,bottom], [left,bottom]]
elif snip.get('polygon') != None:
return [[int(x[0]),int(x[1])] for x in [z.split(',') for z in snip.get('polygon').replace('(', '').replace(')', '').split(';')]]
def polyFromPoints(points, tag='BBox'):
ret = []
if str(tag) == u'Polygon':
if points[1][0] < points[0][0]: # couterclockwise?
points = reversed(points)
ret.append((u'polygon',u';'.join(["("+str(int(x))+","+str(int(y))+")" for x,y in points])))
elif str(tag) == 'BBox':
allx = [x for x,y in points]
ally = [y for x,y in points]
left = int(min(allx))
right = int(max(allx))
top = int(min(ally))
bottom = int(max(ally))
ret.append((u'Left', str(left)))
ret.append((u'Top', str(top)))
ret.append((u'Right', str(right)))
ret.append((u'Bottom', str(bottom)))
else:
raise RuntimeError('Unknown tag for polyFromPoints <%s>'%tag)
return ret
def getTop(points):
return min([x[1] for x in points])
BB = QDialogButtonBox
class LabelTagDialog(QDialog):
def __init__(self, text="Enter object label", parent=None):
super(LabelTagDialog, self).__init__(parent)
layout = QVBoxLayout()
self.buttonBox = bb = BB(BB.Ok, Qt.Horizontal, self)
bb.button(BB.Ok).setIcon(newIcon('done'))
#bb.button(BB.Cancel).setIcon(newIcon('undo'))
bb.accepted.connect(self.validate)
#bb.rejected.connect(self.reject)
self.lbl = QLabel('BBox')
# Center align text
self.lbl.setAlignment(Qt.AlignHCenter)
layout.addWidget(self.lbl)
layout.addWidget(bb)
self.combo = QComboBox()
self.combo.addItems([u"Polygon", u"BBox"])
self.connect(self.combo, SIGNAL('activated(QString)'), self.combo_chosen)
self.connect(self.combo, SIGNAL('currentIndexChanged(QString)'), self.combo_chosen)
layout.addWidget(self.combo)
self.setLayout(layout)
def getChoice(self):
return self.lbl.text()
def combo_chosen(self, text):
self.lbl.setText(text)
def validate(self):
self.accept()
#def validate(self):
#if self.edit.text().trimmed():
#self.accept()
#def postProcess(self):
#self.edit.setText(self.edit.text().trimmed())
#def popUp(self, text='', move=True):
#self.edit.setText(text)
#self.edit.setSelection(0, len(text))
#self.edit.setFocus(Qt.PopupFocusReason)
#if move:
#self.move(QCursor.pos())
#return self.edit.text() if self.exec_() else None
###
class LabelFileError(Exception):
pass
class LabelFile(object):
#suffix = '.lif'
suffixes = [u'.xml'] # '.lif' not used
def __init__(self, filename=None):
self.shapes = ()
self.imagePath = None
self.imageData = None
if filename is not None:
self.load(filename)
# RM: TODO make something cleaner
def load(self, filename):
ext = os.path.splitext(filename)[1].lower()
if ext == u'.lif':
self._loadLIF(filename)
elif ext == u'.xml':
self._loadDL(filename)
else:
raise LabelFileError('Unknown label extension %s'%ext)
def _loadLIF(self, filename):
try:
with open(filename, 'rb') as f:
data = json.load(f)
imagePath = data['imagePath']
imageData = b64decode(data['imageData'])
lineColor = data['lineColor']
fillColor = data['fillColor']
shapes = ((s['label'], s['points'], s['line_color'], s['fill_color'])\
for s in data['shapes'])
# Only replace data after everything is loaded.
self.shapes = shapes
self.imagePath = imagePath
self.imageData = imageData
self.lineColor = lineColor
self.fillColor = fillColor
except Exception, e:
raise LabelFileError(e)
def _loadDL(self, filename):
#try:
#RM: just read a DocumentList with a single image file, or just the first one TODO
linesOK = {}
for _, elem in et.iterparse(filename) :
if elem.tag == "SinglePage" :
img = elem.get('FileName')
pars = list(elem.getiterator('Paragraph'))
if len(pars) > 0: # read lines
for k,para in enumerate(pars):
linesOK[k] = []
linesOK[k].append(getLines(para))
else: # try lines only
linesOK['noPara'] = []
linesOK['noPara'].append(getLines(elem))
break # just a single image... TODO
if len(linesOK.keys()) >= 1: # found some lines
self.imagePath = img
if not os.path.isfile(img):
self.imagePath = os.path.join(os.path.split(filename)[0], img)
with open(self.imagePath, "rb") as image_file:
self.imageData = image_file.read()
self.lineColor = DEFAULT_LINE_COLOR_LIST #RM: got from an example, RGB_Alpha
self.fillColor = DEFAULT_FILL_COLOR_LIST #RM: got from an example
# shape is a tuple: see above (s['label'], s['points'], s['line_color'], s['fill_color'])
# points is a list of [x,y] points
shapes = []
if linesOK.has_key('noPara'):
for xl in linesOK['noPara']:
if xl != []:
for line in xl:
shapes.append((line.get('Value'), pointsFromBox(line), DEFAULT_LINE_COLOR_LIST, None))
else:
for k in sorted(linesOK.keys()):
#if pars[k].get('Value') is None:
#paraT = u''
#else:
#paraT = pars[k].get('Value')
#shapes.append((u'__P__:'+paraT+str(len(linesOK[k])), pointsFromBox(pars[k]), [255, 255, 224, 128], [255, 255, 224, 32]))
for xl in linesOK[k]:
if xl != []:
for line in xl:
shapes.append((line.get('Value'), pointsFromBox(line), DEFAULT_LINE_COLOR_LIST, None))
self.shapes = (x for x in shapes)
else:
raise LabelFileError('Did not find any lines in %s'%filename)
#except Exception, e:
#raise LabelFileError(e)
def save(self, filename, shapes, imagePath, imageData,
lineColor=None, fillColor=None):
ext = os.path.splitext(str(filename))[1].lower()
if ext == u'.lif':
self._saveLIF(filename, shapes, imagePath, imageData,
lineColor, fillColor)
elif ext == u'.xml':
self._saveDL(filename, shapes, imagePath, tag ='BBox')
else:
raise LabelFileError('Trying to save with unknown file extension: %s'%ext)
def _saveLIF(self, filename, shapes, imagePath, imageData,
lineColor=None, fillColor=None):
try:
with open(filename, 'wb') as f:
json.dump(dict(
shapes=shapes,
lineColor=lineColor, fillColor=fillColor,
imagePath=imagePath,
imageData=b64encode(imageData)),
f, ensure_ascii=True, indent=2)
except Exception, e:
raise LabelFileError(e)
# RM: we lose the notion of Paragraph and the WT...
def _saveDL(self, filename, shapes, imagePath, tag):
try:
for s in shapes:
if not s['label'][0] in validWriteTypes:
raise LabelFileError('Missing write type in line:\n'+s['label'])
with open(filename, 'wt') as f:
f.write('<?xml version="1.0" encoding="utf-8"?>' + '\n') # Dirty tweak: XML declaration by hand:
f.write('<DocumentList>\n')
sp = Element('SinglePage', {'FileName':imagePath})
for s in sorted(shapes, key=lambda x: getTop(x['points'])):
value = s['label']
line=SubElement(sp, 'Line', {'Value':value})
for prop,val in polyFromPoints(s['points'], tag):
line.set(prop,val)
indent(sp)
ElementTree(sp).write(f, encoding="utf-8")
f.write('</DocumentList>\n')
except Exception, e:
raise LabelFileError(e)
@staticmethod
def isLabelFile(filename):
return os.path.splitext(filename)[1].lower() in LabelFile.suffixes
#f = open('/home/messina/temp/islabel.txt', 'w')
#ext = os.path.splitext(filename)[1].lower()
#alors = ext in LabelFile.suffixes
#print>>f, ext
#print>>f,alors
#f.close()
#return alors