Last active
December 14, 2020 00:37
-
-
Save oeway/60d0e6d540064956bcdbd066a5ce7613 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<docs lang="markdown"> | |
# CellPose | |
A generalist algorithm for cell and nucleus segmentation. | |
https://github.com/MouseLand/cellpose | |
</docs> | |
<config lang="json"> | |
{ | |
"name": "CellPose-Segmentation", | |
"type": "native-python", | |
"version": "0.1.2", | |
"description": "A generalist algorithm for cell and nucleus segmentation.", | |
"tags": [], | |
"ui": "", | |
"cover": "", | |
"inputs": null, | |
"outputs": null, | |
"flags": [], | |
"icon": "extension", | |
"api_version": "0.1.8", | |
"env": [{"type": "binder", "spec": "oeway/cellpose_web/binder", "skip_requirements": true}], | |
"permissions": [], | |
"requirements": ["repo: https://github.com/MouseLand/cellpose_web", "cmd: pip install -r cellpose_web/requirements.txt"], | |
"dependencies": [] | |
} | |
</config> | |
<script lang="python"> | |
import os | |
# for Jupyter notebooks | |
if os.path.exists('cellpose_web'): | |
os.chdir('cellpose_web') | |
import re | |
import base64 | |
import io | |
import imageio | |
import cv2 | |
from imjoy import api | |
from main import * | |
def image_to_base64(image_array): | |
'''This function takes a numpy image array as input | |
and encode it into a base64 string | |
''' | |
buf = io.BytesIO() | |
imageio.imwrite(buf, image_array, "PNG") | |
buf.seek(0) | |
img_bytes = buf.getvalue() | |
base64_string = base64.b64encode(img_bytes).decode('ascii') | |
return 'data:image/png;base64,' + base64_string | |
def base64_to_image(base64_string, format=None): | |
'''This function takes a base64 string as input | |
and decode it into an numpy array image | |
''' | |
base64_string = re.sub("^data:image/.+;base64,", "", base64_string) | |
image_file = io.BytesIO(base64.b64decode(base64_string.encode('ascii'))) | |
return imageio.imread(image_file, format) | |
class ImJoyPlugin(): | |
def setup(self): | |
api.log('initialized') | |
def segment(self, config): | |
input64 = config["input"] | |
original_img = base64_to_image(input64, config.get('format')) | |
mask, flow, img = cellpose_segment(original_img, config) | |
results = {"success": True, "input_shape": original_img.shape} | |
outputs = config.get("outputs", "mask").split(",") | |
if "geojson" in outputs: | |
geojson_features = mask_to_geojson(mask) | |
results["geojson"] = geojson_features | |
if "img" in outputs: | |
_, buffer = cv2.imencode('.png', img) | |
img64 = base64.b64encode(buffer).decode() | |
results["img"] = img64 | |
if "flow" in outputs: | |
_, buffer = cv2.imencode('.png', flow) | |
flow64 = base64.b64encode(buffer).decode() | |
results["flow"] = flow64 | |
if "mask" in outputs: | |
_, buffer = cv2.imencode('.png', mask.astype('uint16')) | |
mask64 = base64.b64encode(buffer).decode() | |
results["mask"] = mask64 | |
if "outline_plot" in outputs: | |
outpix = plot_outlines(mask) | |
results["outline_plot"] = img_to_html(img, outpix=outpix) | |
if "overlay_plot" in outputs: | |
overlay = plot_overlay(img, mask) | |
results["overlay_plot"] = img_to_html(overlay) | |
if "flow_plot" in outputs: | |
results["flow_plot"] = img_to_html(flow) | |
if "img_plot" in outputs: | |
results["img_plot"] = img_to_html(img) | |
return results | |
async def run(self, ctx): | |
if ctx.data and 'input' in ctx.data: | |
return self.segment(ctx.data) | |
else: | |
try: | |
path = await api.prompt("Please input an image file path or URL", "http://www.cellpose.org/static/images/img00.png") | |
api.showStatus('Loading example image...') | |
image = imageio.imread(path) | |
api.showStatus('Running segmentation with cellpose...') | |
outputs = self.segment({'input': image_to_base64(image), "diam": 30, "net": "cyto", "chan1": 0, "chan2": 0, "outputs": "flow,mask,outline_plot,overlay_plot"}) | |
api.showStatus('Displaying result...') | |
outline_plot = base64_to_image(outputs['outline_plot']) | |
await api.createWindow(src="https://kitware.github.io/itk-vtk-viewer/app/", name="CellPose Segmentation Result", data={"image": outline_plot}) | |
api.showStatus('Done.') | |
except Exception as e: | |
api.showMessage("Failed to run cellpose: " + str(e)) | |
raise e | |
api.export(ImJoyPlugin()) | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment