Gladiator commited on
Commit
271c748
·
1 Parent(s): 631764b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -23
app.py CHANGED
@@ -2,24 +2,33 @@ import streamlit as st
2
  import numpy as np
3
  import matplotlib.pyplot as plt
4
  from PIL import Image
5
- from cellpose import models, io, plot
6
 
7
- def inference(image, model_path, **model_params):
8
- img = image
9
 
10
- model_inference = models.CellposeModel(gpu=False, pretrained_model=model_path)
11
- preds, flows, _ = model_inference.eval([img], **model_params)
12
- return preds, flows
 
13
 
14
 
15
  if __name__ == "__main__":
16
 
17
- st.title("Sartorius Cell Segmentation")
 
 
 
 
18
 
19
  uploaded_img = st.file_uploader(label="Upload neuronal cell image")
20
- if uploaded_img is not None:
21
- st.image(uploaded_img)
 
 
 
 
 
22
  segment = st.button("Perform segmentation")
 
23
  if uploaded_img is not None and segment:
24
  img = Image.open(uploaded_img)
25
  img = np.array(img)
@@ -31,17 +40,16 @@ if __name__ == "__main__":
31
  "resample": True,
32
  }
33
  with st.spinner("Performing segmentation. This might take a while..."):
34
- preds, flows = inference(
35
- image=img,
36
- model_path="./cellpose_residual_on_style_on_concatenation_off_fold1_ep_649_cv_0.2834",
37
- **model_params
38
- )
39
-
40
- fig, (ax1, ax2) = plt.subplots(1, 2)
41
- ax1.axis('off')
42
- ax2.axis('off')
43
- ax1.set_title("Segmented image")
44
- ax1.imshow(preds[0])
45
- ax2.set_title("Image flows")
46
- ax2.imshow(flows[0][0])
47
- st.pyplot(fig)
 
2
  import numpy as np
3
  import matplotlib.pyplot as plt
4
  from PIL import Image
5
+ from cellpose import models
6
 
 
 
7
 
8
+ @st.cache()
9
+ def load_model(model_path):
10
+ inf_model = models.CellposeModel(gpu=False, pretrained_model=model_path)
11
+ return inf_model
12
 
13
 
14
  if __name__ == "__main__":
15
 
16
+ st.title("Sartorius Neuronal Cell Segmentation")
17
+
18
+ inf_model = load_model(
19
+ model_path="./cellpose_residual_on_style_on_concatenation_off_fold1_ep_649_cv_0.2834"
20
+ )
21
 
22
  uploaded_img = st.file_uploader(label="Upload neuronal cell image")
23
+
24
+ with st.expander("View input image"):
25
+ if uploaded_img is not None:
26
+ st.image(uploaded_img)
27
+ else:
28
+ st.warning("Please upload an image")
29
+
30
  segment = st.button("Perform segmentation")
31
+
32
  if uploaded_img is not None and segment:
33
  img = Image.open(uploaded_img)
34
  img = np.array(img)
 
40
  "resample": True,
41
  }
42
  with st.spinner("Performing segmentation. This might take a while..."):
43
+ preds, flows, _ = inf_model.eval([img], **model_params)
44
+
45
+ fig, (ax1, ax2, ax3) = plt.subplots(1, 3)
46
+ ax1.axis("off")
47
+ ax2.axis("off")
48
+ ax3.axis("off")
49
+ ax1.set_title("Original Image")
50
+ ax1.imshow(img)
51
+ ax2.set_title("Segmented image")
52
+ ax2.imshow(preds[0])
53
+ ax3.set_title("Image flows")
54
+ ax3.imshow(flows[0][0])
55
+ st.pyplot(fig)