Ver Fonte

Add labelme_draw_label_png

Kentaro Wada há 7 anos atrás
pai
commit
3253db7859

BIN
examples/tutorial/.readme/draw_label_png.jpg


+ 8 - 0
examples/tutorial/README.md

@@ -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 - 0
labelme/cli/__init__.py

@@ -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 - 0
labelme/cli/draw_label_png.py

@@ -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()

+ 5 - 2
labelme/utils.py

@@ -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')
@@ -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))
 
@@ -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

+ 1 - 0
setup.py

@@ -91,6 +91,7 @@ setup(
         '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',
         ],