Skip to content

Commit

Permalink
Add labelme_draw_label_png
Browse files Browse the repository at this point in the history
  • Loading branch information
wkentaro committed Apr 24, 2018
1 parent 4f2f845 commit c88d3e9
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 2 deletions.
Binary file added examples/tutorial/.readme/draw_label_png.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions examples/tutorial/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,11 @@ array([0, 1, 2, 3], dtype=int32)
>>> lbl.shape
(907, 1210)
```

Also, you can see the label PNG file by:

```python
labelme_draw_label_png apc2016_obj3_json/label.png
```

<img src=".readme/draw_label_png.jpg" width="35%" />
1 change: 1 addition & 0 deletions labelme/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# flake8: noqa

from . import draw_json
from . import draw_label_png
from . import json_to_dataset
from . import on_docker
34 changes: 34 additions & 0 deletions labelme/cli/draw_label_png.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import argparse
import logging

import matplotlib.pyplot as plt
import numpy as np
import PIL.Image

from labelme import utils


def main():
logger = logging.Logger('labelme')
logger.setLevel(logging.INFO)

parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('label_png', help='label PNG file')
args = parser.parse_args()

lbl = np.asarray(PIL.Image.open(args.label_png))
if lbl.dtype != np.int32:
logger.warn('We recomment numpy.int32 for the label, but it has: {}'
.format(lbl.dtype))

logger.info('label shape: {}'.format(lbl.shape))
logger.info('unique label values: {}'.format(np.unique(lbl)))

lbl_viz = utils.draw_label(lbl)
plt.imshow(lbl_viz)
plt.show()


if __name__ == '__main__':
main()
7 changes: 5 additions & 2 deletions labelme/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def polygons_to_mask(img_shape, polygons):
return mask


def draw_label(label, img, label_names, colormap=None):
def draw_label(label, img=None, label_names=None, colormap=None):
import matplotlib.pyplot as plt
backend_org = plt.rcParams['backend']
plt.switch_backend('agg')
Expand All @@ -97,6 +97,9 @@ def draw_label(label, img, label_names, colormap=None):
plt.gca().xaxis.set_major_locator(plt.NullLocator())
plt.gca().yaxis.set_major_locator(plt.NullLocator())

if label_names is None:
label_names = [str(l) for l in range(label.max() + 1)]

if colormap is None:
colormap = label_colormap(len(label_names))

Expand Down Expand Up @@ -124,7 +127,7 @@ def draw_label(label, img, label_names, colormap=None):

plt.switch_backend(backend_org)

out_size = (img.shape[1], img.shape[0])
out_size = (label_viz.shape[1], label_viz.shape[0])
out = PIL.Image.open(f).resize(out_size, PIL.Image.BILINEAR).convert('RGB')
out = np.asarray(out)
return out
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
'console_scripts': [
'labelme=labelme.app:main',
'labelme_draw_json=labelme.cli.draw_json:main',
'labelme_draw_label_png=labelme.cli.draw_label_png:main',
'labelme_json_to_dataset=labelme.cli.json_to_dataset:main',
'labelme_on_docker=labelme.cli.on_docker:main',
],
Expand Down

0 comments on commit c88d3e9

Please sign in to comment.