Skip to content

Instantly share code, notes, and snippets.

@tsikup
Created September 19, 2023 14:11
Show Gist options
  • Save tsikup/4cd7d622af551e3921033924708c1043 to your computer and use it in GitHub Desktop.
Save tsikup/4cd7d622af551e3921033924708c1043 to your computer and use it in GitHub Desktop.
Convert QuPath's GeoJson annotations to Json for WholeSlideData library
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