Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
<docs lang="markdown">
[TODO: write documentation for this plugin.]
<config lang="json">
"name": "live-cell-boundary",
"type": "web-python",
"version": "0.1.0",
"description": "[TODO: describe this plugin with one sentence.]",
"tags": [],
"ui": "",
"cover": "",
"inputs": null,
"outputs": null,
"flags": [],
"icon": "extension",
"api_version": "0.1.8",
"env": "",
"permissions": [],
"requirements": ["pyotritonclient", "pillow"],
"dependencies": []
<script lang="python">
from imjoy import api
import io
from PIL import Image
import numpy as np
from js import fetch
from pyotritonclient import get_config, execute_model
import base64
import pyodide
from io import BytesIO
async def fetch_image(url, name=None, grayscale=False, size=None):
response = await fetch(url)
bytes = await response.arrayBuffer()
bytes = bytes.to_py()
buffer = io.BytesIO(bytes) = name or url.split('?')[0].split('/')[1]
image ='L')
if grayscale:
image = image.convert('L')
if size:
image = image.resize(size=size)
image = np.array(image)
return image
def encode_image(image):
image = Image.fromarray(image)
buffered = BytesIO(), format="PNG")
img_str = 'data:image/png;base64,' + base64.b64encode(buffered.getvalue()).decode('ascii')
return img_str
class ImJoyPlugin():
def setup(self):
async def run_inference(self):
# run inference
results = await execute_model([self.input_image[None, :, :].astype('float32'), {}],
mask = results['output_0'][1, :, :] * 255
await self.viewer.view_image(encode_image(mask.astype('uint8')), name="mask")
async def run(self, ctx):
viewer = await api.createWindow({"src": "", "fullscreen": True})
widget_config = {
"_rintf": True,
"name": "Control",
"type": "control",
"elements": [
"type": "button",
"label": "Run inference",
"callback": self.run_inference,
await viewer.add_widget(pyodide.to_js(widget_config, dict_converter=Object.fromEntries))
image = await fetch_image('', grayscale=True)
await viewer.view_image(encode_image(image), name="image")
self.input_image = image
self.viewer = viewer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment