File size: 1,990 Bytes
7a1d3a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0fbcbdf
7a1d3a9
 
 
 
 
 
 
 
822e118
 
708ffff
 
7a1d3a9
2492cff
7a1d3a9
 
 
 
 
 
 
0fbcbdf
 
 
 
 
 
7a1d3a9
708ffff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import os
from PIL import Image
import streamlit as st
import ast
import numpy as np
import pandas as pd
from cellpose import models, io, plot
from pathlib import Path


def rle_decode(mask_rle, shape=(520, 704)):
    """
    mask_rle: run-length as string formated (start length)
    shape: (height,width) of array to return
    Returns numpy array, 1 - mask, 0 - background

    """
    s = mask_rle.split()
    starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]
    starts -= 1
    ends = starts + lengths
    img = np.zeros(shape[0] * shape[1], dtype=np.uint8)
    for lo, hi in zip(starts, ends):
        img[lo:hi] = 1
    return img.reshape(shape)


def rle_encode(img):
    pixels = img.flatten()
    pixels = np.concatenate([[0], pixels, [0]])
    runs = np.where(pixels[1:] != pixels[:-1])[0] + 1
    runs[1::2] -= runs[::2]
    return " ".join(str(x) for x in runs)


def inference(image, model_path, **model_params):
    img = image

    model_inference = models.CellposeModel(gpu=False, pretrained_model=model_path)
    preds, flows, _ = model_inference.eval([img], **model_params)
    return preds, flows


if __name__ == "__main__":

    st.title("Sartorius Cell Segmentation")

    uploaded_img = st.file_uploader(label="Upload neuronal cell image")
    if uploaded_img is not None:
        st.image(uploaded_img)
    segment = st.button("Perform segmentation")
    if uploaded_img is not None and segment:
        img = Image.open(uploaded_img)
        img = np.array(img)

        model_params = {
            "diameter": 19.0,
            "channels": [0, 0],
            "augment": True,
            "resample": True,
        }
        with st.spinner("Performing segmentation. This might take a while..."):
            preds, flows = inference(
                image=img,
                model_path="./cellpose_residual_on_style_on_concatenation_off_fold1_ep_649_cv_0.2834",
                **model_params
            )

        print(preds)