Skip to content

Commit

Permalink
allow run predict with imjoy api
Browse files Browse the repository at this point in the history
  • Loading branch information
Nanguage committed Feb 18, 2024
1 parent 4bc1fd2 commit 1227388
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 18 deletions.
15 changes: 5 additions & 10 deletions public/plugins/ufish.imjoy.html
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@
)
self.image = None

def load_image_from_bytes(self, file_name, img_bytes):
@staticmethod
def load_image_from_bytes(file_name, img_bytes):
_file = io.BytesIO(img_bytes)
_file.name = file_name
if file_name.endswith(".tif") or file_name.endswith(".tiff"):
Expand All @@ -134,24 +135,18 @@
image = image.squeeze()
else:
image = imageio.imread(_file)
self.image = image
return image

def save_image_to_bytes(self, image):
@staticmethod
def save_image_to_bytes(image):
_file = io.BytesIO()
with tifffile.TiffWriter(_file) as tif:
tif.save(image)
return _file.getvalue()

async def predict_2d(self, image):
image = scale_image(image)
enh_img = await self.onnx_runner.infer_2d(image)
df = call_spots_local_maxima(enh_img)
coords = df.values
return enh_img, coords

async def view_img_from_bytes(self, file_name, bytes_):
image = self.load_image_from_bytes(file_name, bytes_)
self.image = image
await self.viewer.view_image(image, name=file_name)
return image.shape

Expand Down
12 changes: 9 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import * as directives from 'vuetify/directives'
import App from './App.vue'
import router from './router'
import { isPluginMode } from './utils'
import { useRunStore } from './stores/run'

window.app = {}
window.app.router = router
Expand All @@ -34,11 +35,15 @@ async function createApi() {
console.log("U-FISH is ready.")
}

async function run() {
const runStore = useRunStore()
await runStore.run()
}

return {
"run": async () => {},
"setup": setup,
"run": () => {
console.log("run")
},
"runPredict": run,
}
}

Expand All @@ -50,6 +55,7 @@ async function initImJoy() {
window.app.imjoy_api = imjoy_api;
const ufish = await createApi();
window.app.ufish = ufish;
router.push("/predict")
await imjoy_api.export(ufish)
} else {
// start as an standalone app
Expand Down
3 changes: 3 additions & 0 deletions src/stores/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ export const useRunStore = defineStore("run", {
await waitRunable();
this.queryCount += 1;
await waitRunable();
},
setRunable(runable: boolean) {
this.runable = runable;
}
}
})
25 changes: 20 additions & 5 deletions src/views/PredictView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
<script lang="ts">
import * as ort from 'onnxruntime-web';
import { getImjoyApi, isPluginMode, downloadBlob } from '@/utils';
import { ref, onMounted } from 'vue';
import { ref, onMounted, watch } from 'vue';
import { useRunStore } from '@/stores/run';
export default {
setup() {
Expand All @@ -45,6 +46,8 @@ export default {
const runInfoText = ref("loading...")
const hasError = ref(false)
const runStore = useRunStore()
async function loadPlugin() {
const imjoy_api = await getImjoyApi()
imjoy_api.log("Hello from Imjoy!")
Expand Down Expand Up @@ -83,6 +86,7 @@ export default {
const plugP = loadPlugin()
plugP.then(() => {
runInfoText.value = "Plugin loaded"
runStore.setRunable(true)
})
plugP.catch((e) => {
runInfoText.value = "Failed to load plugin, see console for more detail."
Expand All @@ -99,12 +103,12 @@ export default {
async function run() {
running.value = true
const sImg = await plugin.value.scale_image()
const f32data = new Float32Array(sImg._rvalue)
const input = new ort.Tensor(
sImg._rdtype, f32data, sImg._rshape);
let output;
try {
const sImg = await plugin.value.scale_image()
const f32data = new Float32Array(sImg._rvalue)
const input = new ort.Tensor(
sImg._rdtype, f32data, sImg._rshape);
output = await infer_2d(input)
const outImg = {
_rtype: "ndarray",
Expand All @@ -128,6 +132,17 @@ export default {
}
}
watch(() => runStore.queryCount, async () => {
if (runStore.queryCount > 0) {
console.log('running from store')
await run()
}
})
watch(() => running.value, () => {
runStore.setRunable(!running.value)
})
return {
plugin,
run,
Expand Down

0 comments on commit 1227388

Please sign in to comment.