|
from io import BytesIO |
|
|
|
import PIL.Image as Image |
|
import numpy as np |
|
import openvino.runtime as ov |
|
import streamlit as st |
|
from cellpose import models |
|
|
|
model = models.Cellpose( |
|
gpu=False, |
|
model_type="cyto2", |
|
net_avg=False, |
|
) |
|
|
|
uploaded_file = st.file_uploader("Choose a file") |
|
if uploaded_file is not None: |
|
bytes_data = uploaded_file.getvalue() |
|
image = Image.open(BytesIO(bytes_data)) |
|
img_np = np.asarray(image) |
|
img_input = img_np.copy() |
|
|
|
if len(img_np.shape) <= 2: |
|
img_np = np.expand_dims(img_np, axis=-1) |
|
img_np = np.expand_dims(img_np, axis=0) |
|
|
|
img_np = img_np / (2**16 - 1) |
|
|
|
mask, *_ = model.eval( |
|
img_np, |
|
batch_size=64, |
|
normalize=False, |
|
diameter=None, |
|
flow_threshold=0.4, |
|
channels=(0, 0), |
|
) |
|
|
|
st.image(img_np) |
|
st.image((mask > 0) * 255) |
|
|
|
image = np.expand_dims(img_input[:224, :224], axis=0) |
|
image = np.expand_dims( |
|
np.concatenate( |
|
[ |
|
image, |
|
np.zeros_like(image), |
|
], |
|
axis=0, |
|
), |
|
axis=0, |
|
).astype(float) |
|
image /= 2**16 - 1 |
|
|
|
core = ov.Core() |
|
model = core.read_model("./converted_unet_cellpose/cyto2.xml") |
|
compiled_model = core.compile_model(model, "CPU") |
|
|
|
input_layer = compiled_model.input(0) |
|
output_layer = compiled_model.output(0) |
|
|
|
result = compiled_model(image) |
|
result = {k.get_any_name(): v for k, v in result.items()} |
|
outputs = result["gradients"], result["styles"] |
|
|
|
st.write("OPENVINO OK") |
|
|