Skip to content

Instantly share code, notes, and snippets.

@oeway
Created May 30, 2022 08:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oeway/251ccd3d75b6c544045e2e16cf8628b9 to your computer and use it in GitHub Desktop.
Save oeway/251ccd3d75b6c544045e2e16cf8628b9 to your computer and use it in GitHub Desktop.
<docs lang="markdown">
[TODO: write documentation for this plugin.]
</docs>
<config lang="json">
{
"name": "CellPoseDemo",
"type": "web-python",
"version": "0.1.0",
"description": "A demo plugin for running cell-pose segmentation",
"tags": [],
"ui": "",
"cover": "",
"inputs": null,
"outputs": null,
"flags": [],
"icon": "extension",
"api_version": "0.1.8",
"env": "",
"permissions": [],
"requirements": ["pyotritonclient", "kaibu-utils"],
"dependencies": []
}
</config>
<script lang="python">
import io
import numpy as np
from pyotritonclient import execute
from kaibu_utils import fetch_image
from imjoy import api
class ImJoyPlugin():
async def setup(self):
image = await fetch_image('https://static.imjoy.io/img/img02.png')
image = image.astype('float32')
print("example image downloaded: ", image.shape)
self.image = image
async def run(self, ctx):
viewer = await api.createWindow(src="https://kaibu.org/#/app")
await viewer.view_image(self.image.astype('uint8'), type="itk-vtk", name="input-image")
async def form_submit_callback(fields):
diameter = int(fields['Diameter'])
await viewer.set_loader(True)
try:
# run inference
param = {'diameter': diameter, 'model_type': 'cyto'}
results = await execute([self.image.transpose(2, 0, 1), param],
server_url='https://hypha.imjoy.io/triton',
model_name='cellpose-python',
decode_bytes=True)
mask = results['mask']
await viewer.view_image(mask, type="itk-vtk", name="output-mask", opacity=0.4)
await api.showMessage('cellpose prediction completed!')
except Exception as exp:
await api.alert("Failed to run prediction: " + str(exp))
finally:
await viewer.set_loader(False)
await viewer.add_widget(
{
"_rintf": True,
"name": "CellPose Segmentation",
"type": "form",
"form_submit_callback": form_submit_callback,
"fields": [
{
"label": "Diameter",
"type": "number",
"value": 30,
"rules": {
"min": 1,
"max": 400
}
}
],
})
api.export(ImJoyPlugin())
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment