|
| 1 | +import os |
| 2 | +from glob import glob |
| 3 | + |
| 4 | +import h5py |
| 5 | +import napari |
| 6 | +from magicgui import magicgui |
| 7 | + |
| 8 | + |
| 9 | +def run_annotation(input_path, output_path): |
| 10 | + with h5py.File(input_path, "r") as f: |
| 11 | + raw = f["raw"][:] |
| 12 | + seg = f["labels/compartments"][:] |
| 13 | + |
| 14 | + v = napari.Viewer() |
| 15 | + v.add_image(raw) |
| 16 | + v.add_labels(seg) |
| 17 | + |
| 18 | + @magicgui(call_button="Save Annotations") |
| 19 | + def save_annotations(): |
| 20 | + seg = v.layers["seg"].data |
| 21 | + |
| 22 | + if os.path.exists(output_path): |
| 23 | + with h5py.File(output_path, "a") as f: |
| 24 | + f["labels/compartments"][:] = seg |
| 25 | + else: |
| 26 | + with h5py.File(output_path, "a") as f: |
| 27 | + f.create_dataset("raw", data=raw, compression="gzip") |
| 28 | + f.create_dataset("labels/compartments", data=seg, compression="gzip") |
| 29 | + |
| 30 | + v.window.add_dock_widget(save_annotations) |
| 31 | + |
| 32 | + napari.run() |
| 33 | + |
| 34 | + |
| 35 | +def main(): |
| 36 | + inputs = sorted(glob("./predictions/**/*.h5", recursive=True)) |
| 37 | + |
| 38 | + output_folder = "./annotations" |
| 39 | + |
| 40 | + for input_path in inputs: |
| 41 | + ds_name, fname = os.path.split(input_path) |
| 42 | + ds_name = os.path.split(ds_name)[1] |
| 43 | + ds_folder = os.path.join(output_folder, ds_name) |
| 44 | + output_path = os.path.join(ds_folder, fname) |
| 45 | + |
| 46 | + if os.path.exists(output_path): |
| 47 | + print("Skipping annotations for", output_path) |
| 48 | + continue |
| 49 | + |
| 50 | + os.makedirs(ds_folder, exist_ok=True) |
| 51 | + run_annotation(input_path, output_path) |
| 52 | + |
| 53 | + |
| 54 | +if __name__ == "__main__": |
| 55 | + main() |
0 commit comments