Last active
November 9, 2021 12:01
-
-
Save Dsantra92/58eadfdf5ad4026cae6e6d169f563011 to your computer and use it in GitHub Desktop.
Convert a label-me JSON file to a mask array in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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