Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add colormap arg to draw_label #217

Merged
merged 1 commit into from
Aug 14, 2018
Merged

Add colormap arg to draw_label #217

merged 1 commit into from
Aug 14, 2018

Conversation

wkentaro
Copy link
Owner

@wkentaro wkentaro commented Aug 14, 2018

For #216

Example

image.jpg

image.json

screen shot 2018-08-15 at 7 19 50

{
  "flags": {},
  "shapes": [
    {
      "label": "sky",
      "line_color": null,
      "fill_color": null,
      "points": [
        [
          1,
          1
        ],
        [
          1,
          386
        ],
        [
          89,
          427
        ],
        [
          80,
          396
        ],
        [
          131,
          361
        ],
        [
          150,
          394
        ],
        [
          205,
          366
        ],
        [
          233,
          365
        ],
        [
          264,
          441
        ],
        [
          476,
          376
        ],
        [
          595,
          422
        ],
        [
          632,
          363
        ],
        [
          723,
          412
        ],
        [
          776,
          373
        ],
        [
          836,
          416
        ],
        [
          895,
          335
        ],
        [
          941,
          402
        ],
        [
          955,
          398
        ],
        [
          957,
          2
        ]
      ]
    },
    {
      "label": "tree",
      "line_color": null,
      "fill_color": null,
      "points": [
        [
          0,
          394
        ],
        [
          1,
          636
        ],
        [
          959,
          639
        ],
        [
          958,
          398
        ],
        [
          937,
          400
        ],
        [
          893,
          343
        ],
        [
          838,
          420
        ],
        [
          772,
          379
        ],
        [
          722,
          417
        ],
        [
          636,
          369
        ],
        [
          596,
          423
        ],
        [
          476,
          381
        ],
        [
          265,
          444
        ],
        [
          229,
          370
        ],
        [
          202,
          371
        ],
        [
          150,
          398
        ],
        [
          130,
          365
        ],
        [
          78,
          398
        ],
        [
          90,
          431
        ]
      ]
    }
  ],
  "lineColor": [
    0,
    255,
    0,
    128
  ],
  "fillColor": [
    255,
    0,
    0,
    128
  ],
  "imagePath": "image.jpg",
  "imageData": null
}

Default Visualization

# use default colormap
labelme_draw_json image.json

screen shot 2018-08-15 at 7 22 33

Custom Visualization

custom_draw_json.py

#!/usr/bin/env python

import argparse
import base64
import json
import os
import sys

import matplotlib.pyplot as plt
import numpy as np

from labelme import utils


PY2 = sys.version_info[0] == 2


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('json_file')
    args = parser.parse_args()

    json_file = args.json_file

    data = json.load(open(json_file))

    if data['imageData']:
        imageData = data['imageData']
    else:
        imagePath = os.path.join(os.path.dirname(json_file), data['imagePath'])
        with open(imagePath, 'rb') as f:
            imageData = f.read()
            imageData = base64.b64encode(imageData).decode('utf-8')
    img = utils.img_b64_to_arr(imageData)

    label_name_to_value = {
        '_background_': 0,
        'sky': 1,
        'tree': 2,
    }
    colormap = np.array([
        (0, 0, 0),
        (0, 0, 1),
        (0, 1, 0),
    ])

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

    label_names = [None] * (max(label_name_to_value.values()) + 1)
    for name, value in label_name_to_value.items():
        label_names[value] = name
    lbl_viz = utils.draw_label(lbl, img, label_names, colormap=colormap)

    plt.subplot(121)
    plt.imshow(img)
    plt.subplot(122)
    plt.imshow(lbl_viz)
    plt.show()


if __name__ == '__main__':
    main()
python custom_draw_json.py image.json

screen shot 2018-08-15 at 7 32 00

@wkentaro wkentaro self-assigned this Aug 14, 2018
@wkentaro wkentaro mentioned this pull request Aug 14, 2018
@wkentaro wkentaro force-pushed the colormap_for_draw_label branch from eb40459 to 05b922a Compare August 14, 2018 23:25
@wkentaro wkentaro merged commit 1191c1d into master Aug 14, 2018
@wkentaro wkentaro deleted the colormap_for_draw_label branch August 14, 2018 23:39
@openXG
Copy link

openXG commented Dec 21, 2018

sorry to bother you about this closed question again.
I don't know how to use the code you gave. I can't find custom_draw_json.py in my file list. Should I build a new py file or just change the code in some files? by the way, I just need some gray labels of two class.
looking forward to your answer. thx!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants