Skip to content

Commit

Permalink
Fix utils.labelme_shapes_to_label
Browse files Browse the repository at this point in the history
- Make 'background' label name have '0' label value
  • Loading branch information
wkentaro committed Apr 10, 2018
1 parent c754161 commit 1e53764
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 14 deletions.
3 changes: 2 additions & 1 deletion examples/tutorial/apc2016_obj3_json/info.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
label_names:
- background
- _background_
- shelf
- highland_6539_self_stick_notes
- mead_index_cards
- kong_air_dog_squeakair_tennis_ball
Binary file modified examples/tutorial/apc2016_obj3_json/label.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion examples/tutorial/apc2016_obj3_json/label_names.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
background
_background_
shelf
highland_6539_self_stick_notes
mead_index_cards
kong_air_dog_squeakair_tennis_ball
Binary file modified examples/tutorial/apc2016_obj3_json/label_viz.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 16 additions & 4 deletions labelme/cli/draw_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,22 @@ def main():

data = json.load(open(json_file))

img = utils.img_b64_to_array(data['imageData'])
lbl, lbl_names = utils.labelme_shapes_to_label(img.shape, data['shapes'])

captions = ['%d: %s' % (l, name) for l, name in enumerate(lbl_names)]
img = utils.img_b64_to_arr(data['imageData'])

label_name_to_value = {'_background_': 0}
for shape in data['shapes']:
label_name = shape['label']
if label_name in label_name_to_value:
label_value = label_name_to_value[label_name]
else:
label_value = len(label_name_to_value)
label_name_to_value[label_name] = label_value

lbl = utils.shapes_to_label(
img.shape, data['shapes'], label_name_to_value)

captions = ['{}: {}'.format(lv, ln)
for ln, lv in label_name_to_value.items()]
lbl_viz = utils.draw_label(lbl, img, captions)

plt.subplot(121)
Expand Down
30 changes: 24 additions & 6 deletions labelme/cli/json_to_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,40 @@ def main():

data = json.load(open(json_file))

img = utils.img_b64_to_array(data['imageData'])
lbl, lbl_names = utils.labelme_shapes_to_label(img.shape, data['shapes'])

captions = ['%d: %s' % (l, name) for l, name in enumerate(lbl_names)]
img = utils.img_b64_to_arr(data['imageData'])

label_name_to_value = {'_background_': 0}
for shape in data['shapes']:
label_name = shape['label']
if label_name in label_name_to_value:
label_value = label_name_to_value[label_name]
else:
label_value = len(label_name_to_value)
label_name_to_value[label_name] = label_value

# label_values must be dense
label_values, label_names = [], []
for ln, lv in sorted(label_name_to_value.items(), key=lambda x: x[1]):
label_values.append(lv)
label_names.append(ln)
assert label_values == list(range(len(label_values)))

lbl = utils.shapes_to_label(img.shape, data['shapes'], label_name_to_value)

captions = ['{}: {}'.format(lv, ln)
for ln, lv in label_name_to_value.items()]
lbl_viz = utils.draw_label(lbl, img, captions)

PIL.Image.fromarray(img).save(osp.join(out_dir, 'img.png'))
PIL.Image.fromarray(lbl).save(osp.join(out_dir, 'label.png'))
PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, 'label_viz.png'))

with open(osp.join(out_dir, 'label_names.txt'), 'w') as f:
for lbl_name in lbl_names:
for lbl_name in label_names:
f.write(lbl_name + '\n')

warnings.warn('info.yaml is being replaced by label_names.txt')
info = dict(label_names=lbl_names)
info = dict(label_names=label_names)
with open(osp.join(out_dir, 'info.yaml'), 'w') as f:
yaml.safe_dump(info, f, default_flow_style=False)

Expand Down
4 changes: 2 additions & 2 deletions labelme/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def shapes_to_label(img_shape, shapes, label_name_to_value, type='class'):
cls = np.zeros(img_shape[:2], dtype=np.int32)
if type == 'instance':
ins = np.zeros(img_shape[:2], dtype=np.int32)
instance_names = ['__background__']
instance_names = ['_background_']
for shape in shapes:
polygons = shape['points']
label = shape['label']
Expand All @@ -162,7 +162,7 @@ def labelme_shapes_to_label(img_shape, shapes):
warnings.warn('labelme_shapes_to_label is deprecated, so please use '
'shapes_to_label.')

label_name_to_value = {}
label_name_to_value = {'_background_': 0}
for shape in shapes:
label_name = shape['label']
if label_name in label_name_to_value:
Expand Down

0 comments on commit 1e53764

Please sign in to comment.