{ "cells": [ { "cell_type": "markdown", "id": "1a138390-e848-401e-b286-ffeef5d8df83", "metadata": {}, "source": [ "# stackview in combination with Voila\n", "If you combine stackview with [voila](https://voila.readthedocs.io/en/stable/), you can build powerful, interactive, browser-based image processing apps and demonstrators. \n", "Just build a graphical user interface in a Jupyter notebook and then start it using\n", "```\n", "voila apoc_gui.ipynb\n", "```" ] }, { "cell_type": "code", "execution_count": 1, "id": "f3c290cd-a8db-4f56-938f-28ec81c44c32", "metadata": {}, "outputs": [], "source": [ "from skimage.io import imread\n", "import numpy as np\n", "import stackview\n", "import ipywidgets as widgets\n", "from IPython.display import display, clear_output\n", "import apoc\n", "import pyclesperanto_prototype as cle" ] }, { "cell_type": "code", "execution_count": 2, "id": "ff5f404a-ef5d-44e3-b9e6-eab55b0db872", "metadata": {}, "outputs": [], "source": [ "image = imread('data/blobs.tif')" ] }, { "cell_type": "code", "execution_count": 3, "id": "e13631b7-5608-4c77-b3ef-ef3e36164cfb", "metadata": {}, "outputs": [], "source": [ "annotation = cle.push(np.zeros_like(image, dtype=np.uint32)).astype(np.uint32)\n", "labels = cle.push(np.zeros_like(image, dtype=np.uint16)).astype(np.uint32)" ] }, { "cell_type": "code", "execution_count": 4, "id": "a46b0d9f-1de1-4df1-aeaf-be1f7f33d97c", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "411bcf6bd41c425a87fa1c94ae05b68b", "version_major": 2, "version_minor": 0 }, "text/plain": [ "HBox(children=(VBox(children=(VBox(children=(HBox(children=(HBox(children=(VBox(children=(ImageWidget(height=2…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Buttons\n", "train_button = widgets.Button(\n", " description='Train', \n", " button_style='success', \n", " tooltip='Start machine learning training')\n", "train_button.layout.height = '500'\n", "\n", "def on_train_click(change):\n", " model_filename = \"object_segmenter.cl\"\n", " apoc.erase_classifier(model_filename)\n", " model = apoc.ObjectSegmenter(model_filename)\n", " model.train(apoc.PredefinedFeatureSet.small_quick.value, image=image, ground_truth=annotation)\n", " new_labels = model.predict(image=image)\n", " cle.copy(new_labels, labels)\n", " slice_placeholder.update()\n", "\n", "train_button.on_click(on_train_click)\n", "\n", "reset_button = widgets.Button(\n", " description='Reset', \n", " button_style='danger', \n", " tooltip='Reset annotation')\n", "\n", "def on_reset_click(change):\n", " cle.set(annotation, 0)\n", " cle.set(labels, 0) \n", " annotate_placeholder.update()\n", " slice_placeholder.update()\n", " \n", "reset_button.on_click(on_reset_click)\n", "\n", "# Image interaction\n", "annotate_placeholder = stackview.annotate(image, annotation)\n", "slice_placeholder = stackview.curtain(image, labels, alpha=0.7)\n", "\n", "# Initially display the annotate field\n", "display(widgets.HBox([\n", " widgets.VBox([\n", " annotate_placeholder, \n", " widgets.HBox([\n", " reset_button,\n", " train_button])\n", " ]),\n", " slice_placeholder\n", "]))\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "a890731e-638c-4c6b-b4f3-df42f055f48d", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.16" } }, "nbformat": 4, "nbformat_minor": 5 }