Skip to content

Instantly share code, notes, and snippets.

@Dsantra92
Last active November 9, 2021 12:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dsantra92/58eadfdf5ad4026cae6e6d169f563011 to your computer and use it in GitHub Desktop.
Save Dsantra92/58eadfdf5ad4026cae6e6d169f563011 to your computer and use it in GitHub Desktop.
Convert a label-me JSON file to a mask array in python
import json
from labelme.utils import shapes_to_label
filepath = "orthophotoNF.json" # Use your own filename
# Load the JSON file
with open(filepath, "r",encoding="utf-8") as f:
ortho_json = json.load(f)
# Get height and width of original image
x, y = ortho_json['imageHeight'], ortho_json['imageWidth']
# These are shapes drawn in labelme with labels and other things
shapes = ortho_json['shapes']
# Extract the labels
labels = set()
for shape in shapes:
labels.add(shape['label'])
# Get dict for mapping labels to unique integer values
labels_to_index = {label: idx + 1 for idx, label in enumerate(labels)}
# Get the mask
mask = shapes_to_label((x,y), shapes, labels_to_index)[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment