Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import streamlit as st
|
| 4 |
+
import ast
|
| 5 |
+
import numpy as np
|
| 6 |
+
import pandas as pd
|
| 7 |
+
from cellpose import models, io, plot
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def rle_decode(mask_rle, shape=(520, 704)):
|
| 12 |
+
"""
|
| 13 |
+
mask_rle: run-length as string formated (start length)
|
| 14 |
+
shape: (height,width) of array to return
|
| 15 |
+
Returns numpy array, 1 - mask, 0 - background
|
| 16 |
+
|
| 17 |
+
"""
|
| 18 |
+
s = mask_rle.split()
|
| 19 |
+
starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]
|
| 20 |
+
starts -= 1
|
| 21 |
+
ends = starts + lengths
|
| 22 |
+
img = np.zeros(shape[0] * shape[1], dtype=np.uint8)
|
| 23 |
+
for lo, hi in zip(starts, ends):
|
| 24 |
+
img[lo:hi] = 1
|
| 25 |
+
return img.reshape(shape)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def rle_encode(img):
|
| 29 |
+
pixels = img.flatten()
|
| 30 |
+
pixels = np.concatenate([[0], pixels, [0]])
|
| 31 |
+
runs = np.where(pixels[1:] != pixels[:-1])[0] + 1
|
| 32 |
+
runs[1::2] -= runs[::2]
|
| 33 |
+
return " ".join(str(x) for x in runs)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def inference(image, model_path, **model_params):
|
| 37 |
+
img = image
|
| 38 |
+
|
| 39 |
+
model_inference = models.CellposeModel(gpu=False, pretrained_model=model_path)
|
| 40 |
+
preds, flows, _ = model_inference.eval(img, **model_params)
|
| 41 |
+
|
| 42 |
+
print(preds.shape)
|
| 43 |
+
print(flows.shape)
|
| 44 |
+
return preds, flows
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
if __name__ == "__main__":
|
| 48 |
+
|
| 49 |
+
st.title("Sartorius Cell Segmentation")
|
| 50 |
+
|
| 51 |
+
uploaded_img = st.file_uploader(label="Upload neuronal cell image")
|
| 52 |
+
if uploaded_img is not None:
|
| 53 |
+
img = Image.open(uploaded_img)
|
| 54 |
+
st.image(img)
|
| 55 |
+
|
| 56 |
+
model_params = {
|
| 57 |
+
"diameter": 19.0,
|
| 58 |
+
"channels": [0, 0],
|
| 59 |
+
"augment": True,
|
| 60 |
+
"resample": True,
|
| 61 |
+
}
|
| 62 |
+
preds, flows = inference(
|
| 63 |
+
image=img,
|
| 64 |
+
model_path="cellpose_residual_on_style_on_concatenation_off_fold1_ep_649_cv_0.2834",
|
| 65 |
+
**model_params
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
print(preds)
|