Created
September 19, 2023 14:11
-
-
Save tsikup/4cd7d622af551e3921033924708c1043 to your computer and use it in GitHub Desktop.
Convert QuPath's GeoJson annotations to Json for WholeSlideData library
This file contains 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 os | |
import glob | |
import json | |
import numpy as np | |
LABELS = { | |
'Tissue': 1, | |
'Tumor': 2 | |
} | |
def main(folder): | |
files = glob.glob(os.path.join(folder, "*.geojson")) | |
for src in files: | |
with open(src) as f: | |
data = json.load(f) | |
new_data = [] | |
for ann in data: | |
tmp = dict() | |
name = ann["properties"]["classification"]["name"] | |
tmp["label"] = dict( | |
name = name.lower(), | |
value = LABELS[name] | |
) | |
tmp["coordinates"] = ann['geometry']['coordinates'] | |
new_data.append(tmp) | |
with open(src.replace(".geojson", ".json"), "w") as f: | |
json.dump(new_data, f) | |
if __name__ == "__main__": | |
folder = "./" | |
main(folder) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment