Skip to content

Instantly share code, notes, and snippets.

@m-albert
Last active June 30, 2024 16:19
Show Gist options
  • Save m-albert/85856b6f11450a24e208fd6428207681 to your computer and use it in GitHub Desktop.
Save m-albert/85856b6f11450a24e208fd6428207681 to your computer and use it in GitHub Desktop.
<config lang="json">
{
"name": "BioImage.IO Colab Annotator",
"type": "web-python",
"tags": [],
"ui": "",
"version": "0.1.0",
"cover": "",
"description": "Collaborative Annotator for BioImage.IO with Automated Segmentation",
"icon": "extension",
"inputs": null,
"outputs": null,
"api_version": "0.1.8",
"env": "",
"permissions": [],
"requirements": ["kaibu-utils", "imjoy-rpc", "websockets", "ssl"],
"dependencies": []
}
</config>
<script lang="python">
from imjoy_rpc import api
from imjoy_rpc.hypha import connect_to_server
class BioImageIOCrowdSourcing():
def __init__(self):
self.image = None
self.mask = None
self.filename = None
self.newname = None
self.mask_id = None
self.image_id = None
self.annotation_layer = None
async def setup(self):
viewer = await api.createWindow(src="https://kaibu.org/#/app", fullscreen=True)
await api.showMessage("Connecting to server....")
server = await connect_to_server({"server_url": "https://ai.imjoy.io"})
self.biocolab = await server.get_service("biocolab")
## Define image reading and displaying function
async def get_image():
if self.image is not None:
await viewer.remove_layer({"id": self.image_id.id})
await viewer.remove_layer({"id": self.annotation_layer.id})
self.image, self.filename, self.newname = await self.biocolab.get_random_image()
self.image_id = await viewer.view_image(
self.image,
name="image")
# Add the little annotation functionality to the interface
self.annotation_layer = await viewer.add_shapes(
[],
shape_type="polygon",
draw_edge_color="magenta",
name="annotation",
)
## Define image annotation printing into a mask with the name "mask"
# async def create_mask():
# self.features = await self.annotation_layer.get_features()
# if self.mask is not None:
# await viewer.remove_layer({"id": self.mask_id.id})
# self.mask = features_to_mask(self.features, [self.image.shape[0], self.image.shape[1]])
# self.mask_id = await viewer.view_image(
# self.mask,
# name="mask"
# )
# await api.showMessage("Mask created")
async def save_annotation():
self.features = await self.annotation_layer.get_features()
await self.biocolab.save_annotation(self.filename, self.newname, self.features, [self.image.shape[0], self.image.shape[1]])
await api.showMessage("Annotation Saved to " + self.filename)
async def select_option(option):
if option == "Get Image":
await get_image()
elif option == "Save Annotations":
await save_annotation()
await viewer.add_widget(
{
"_rintf": True,
"name": "Control",
"type": "control",
"elements": [
{
"type": "dropdown",
"label": "Options",
"options": ["Get Image", "Save Annotations"],
"callback": select_option,
}
],
})
await api.showMessage("Redy to annotate!")
api.export(BioImageIOCrowdSourcing())
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment