Skip to content

Commit

Permalink
Preserve other keys on shape level
Browse files Browse the repository at this point in the history
  • Loading branch information
Grzegorz Ruciński authored and wkentaro committed Feb 8, 2020
1 parent a62194b commit 9bc8a75
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
12 changes: 8 additions & 4 deletions labelme/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -1039,7 +1039,8 @@ def loadLabels(self, shapes):
points = shape['points']
shape_type = shape['shape_type']
flags = shape['flags']
group_id = shape.get('group_id')
group_id = shape['group_id']
other_data = shape['other_data']

shape = Shape(
label=label,
Expand All @@ -1058,6 +1059,7 @@ def loadLabels(self, shapes):
default_flags[key] = False
shape.flags = default_flags
shape.flags.update(flags)
shape.other_data = other_data

s.append(shape)
self.loadShapes(s)
Expand All @@ -1074,13 +1076,15 @@ def saveLabels(self, filename):
lf = LabelFile()

def format_shape(s):
return dict(
data = s.other_data.copy()
data.update(dict(
label=s.label.encode('utf-8') if PY2 else s.label,
points=[(p.x(), p.y()) for p in s.points],
group_id=s.group_id,
shape_type=s.shape_type,
flags=s.flags
)
flags=s.flags,
))
return data

shapes = [format_shape(shape) for shape in self.labelList.shapes]
flags = {}
Expand Down
12 changes: 11 additions & 1 deletion labelme/label_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ def load(self, filename):
'imageHeight',
'imageWidth',
]
shape_keys = [
'label',
'points',
'group_id',
'shape_type',
'flags',
]
try:
with open(filename, 'rb' if PY2 else 'r') as f:
data = json.load(f)
Expand Down Expand Up @@ -103,7 +110,10 @@ def load(self, filename):
points=s['points'],
shape_type=s.get('shape_type', 'polygon'),
flags=s.get('flags', {}),
group_id=s.get('group_id')
group_id=s.get('group_id'),
other_data={
k: v for k, v in s.items() if k not in shape_keys
}
)
for s in data['shapes']
]
Expand Down
1 change: 1 addition & 0 deletions labelme/shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def __init__(self, label=None, line_color=None, shape_type=None,
self.selected = False
self.shape_type = shape_type
self.flags = flags
self.other_data = {}

self._highlightIndex = None
self._highlightMode = self.NEAR_VERTEX
Expand Down
4 changes: 3 additions & 1 deletion tests/labelme_tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,11 @@ def test_MainWindow_annotate_jpg(qtbot):
]
shapes = [dict(
label=label,
group_id=None,
points=points,
shape_type='polygon',
flags={}
flags={},
other_data={}
)]
win.loadLabels(shapes)
win.saveFile()
Expand Down

0 comments on commit 9bc8a75

Please sign in to comment.