Commit
·
f0821bf
1
Parent(s):
6e2a82e
adding outlines
Browse files
app.py
CHANGED
|
@@ -1,10 +1,10 @@
|
|
| 1 |
-
import os
|
| 2 |
import numpy as np
|
| 3 |
import gradio as gr
|
| 4 |
import cv2
|
| 5 |
from cellpose import models
|
| 6 |
from matplotlib.colors import hsv_to_rgb
|
| 7 |
-
import
|
|
|
|
| 8 |
|
| 9 |
try:
|
| 10 |
model = models.CellposeModel(gpu=False, pretrained_model="cyto3")
|
|
@@ -22,7 +22,7 @@ def plot_flows(y):
|
|
| 22 |
flow = (hsv_to_rgb(HSV) * 255).astype(np.uint8)
|
| 23 |
return flow
|
| 24 |
|
| 25 |
-
def plot_outlines(masks):
|
| 26 |
outpix = []
|
| 27 |
contours, hierarchy = cv2.findContours(masks.astype(np.int32), mode=cv2.RETR_FLOODFILL, method=cv2.CHAIN_APPROX_SIMPLE)
|
| 28 |
for c in range(len(contours)):
|
|
@@ -31,7 +31,29 @@ def plot_outlines(masks):
|
|
| 31 |
peri = cv2.arcLength(contours[c], True)
|
| 32 |
approx = cv2.approxPolyDP(contours[c], 0.001, True)[:,0,:]
|
| 33 |
outpix.append(approx)
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
def plot_overlay(img, masks):
|
| 37 |
img = normalize99(img.astype(np.float32).mean(axis=-1))
|
|
@@ -66,7 +88,7 @@ def image_resize(img, resize=224):
|
|
| 66 |
return img
|
| 67 |
|
| 68 |
def cellpose_segment(img):
|
| 69 |
-
img_input = image_resize(img)
|
| 70 |
masks, flows, _ = model.eval(img_input, channels=[0,0])
|
| 71 |
flows = flows[0]
|
| 72 |
# masks = np.zeros(img.shape[:2])
|
|
@@ -77,16 +99,16 @@ def cellpose_segment(img):
|
|
| 77 |
masks = cv2.resize(masks.astype('uint16'), target_size, interpolation=cv2.INTER_NEAREST).astype('uint16')
|
| 78 |
flows = cv2.resize(flows.astype('float32'), target_size).astype('uint8')
|
| 79 |
|
| 80 |
-
outpix = plot_outlines(masks)
|
| 81 |
overlay = plot_overlay(img, masks)
|
| 82 |
|
| 83 |
-
return overlay, flows, masks
|
| 84 |
|
| 85 |
# Gradio Interface
|
| 86 |
iface = gr.Interface(
|
| 87 |
fn=cellpose_segment,
|
| 88 |
inputs="image",
|
| 89 |
-
outputs=["image", "image", "image"],
|
| 90 |
title="cellpose segmentation",
|
| 91 |
description="upload an image, then cellpose will segment it at a max size of 224x224"
|
| 92 |
)
|
|
|
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
import gradio as gr
|
| 3 |
import cv2
|
| 4 |
from cellpose import models
|
| 5 |
from matplotlib.colors import hsv_to_rgb
|
| 6 |
+
import matplotlib.pyplot as plt
|
| 7 |
+
import os, io, base64
|
| 8 |
|
| 9 |
try:
|
| 10 |
model = models.CellposeModel(gpu=False, pretrained_model="cyto3")
|
|
|
|
| 22 |
flow = (hsv_to_rgb(HSV) * 255).astype(np.uint8)
|
| 23 |
return flow
|
| 24 |
|
| 25 |
+
def plot_outlines(img, masks):
|
| 26 |
outpix = []
|
| 27 |
contours, hierarchy = cv2.findContours(masks.astype(np.int32), mode=cv2.RETR_FLOODFILL, method=cv2.CHAIN_APPROX_SIMPLE)
|
| 28 |
for c in range(len(contours)):
|
|
|
|
| 31 |
peri = cv2.arcLength(contours[c], True)
|
| 32 |
approx = cv2.approxPolyDP(contours[c], 0.001, True)[:,0,:]
|
| 33 |
outpix.append(approx)
|
| 34 |
+
|
| 35 |
+
figsize = (6,6)
|
| 36 |
+
if img.shape[0]>img.shape[1]:
|
| 37 |
+
figsize = (6*img.shape[1]/img.shape[0], 6)
|
| 38 |
+
else:
|
| 39 |
+
figsize = (6, 6*img.shape[0]/img.shape[1])
|
| 40 |
+
fig = plt.figure(figsize=figsize, facecolor='k')
|
| 41 |
+
ax = fig.add_axes([0.0,0.0,1,1])
|
| 42 |
+
ax.set_xlim([0,img.shape[1]])
|
| 43 |
+
ax.set_ylim([0,img.shape[0]])
|
| 44 |
+
ax.imshow(img[::-1], origin='upper')
|
| 45 |
+
if outpix is not None:
|
| 46 |
+
for o in outpix:
|
| 47 |
+
ax.plot(o[:,0], img.shape[0]-o[:,1], color=[1,0,0], lw=1)
|
| 48 |
+
ax.axis('off')
|
| 49 |
+
bytes_image = io.BytesIO()
|
| 50 |
+
plt.savefig(bytes_image, format='png', facecolor=fig.get_facecolor(), edgecolor='none')
|
| 51 |
+
bytes_image.seek(0)
|
| 52 |
+
img_html = base64.b64encode(bytes_image.getvalue()).decode()
|
| 53 |
+
del bytes_image
|
| 54 |
+
fig.clf()
|
| 55 |
+
plt.close(fig)
|
| 56 |
+
return img_html
|
| 57 |
|
| 58 |
def plot_overlay(img, masks):
|
| 59 |
img = normalize99(img.astype(np.float32).mean(axis=-1))
|
|
|
|
| 88 |
return img
|
| 89 |
|
| 90 |
def cellpose_segment(img):
|
| 91 |
+
img_input = image_resize(img.copy())
|
| 92 |
masks, flows, _ = model.eval(img_input, channels=[0,0])
|
| 93 |
flows = flows[0]
|
| 94 |
# masks = np.zeros(img.shape[:2])
|
|
|
|
| 99 |
masks = cv2.resize(masks.astype('uint16'), target_size, interpolation=cv2.INTER_NEAREST).astype('uint16')
|
| 100 |
flows = cv2.resize(flows.astype('float32'), target_size).astype('uint8')
|
| 101 |
|
| 102 |
+
outpix = plot_outlines(img, masks)
|
| 103 |
overlay = plot_overlay(img, masks)
|
| 104 |
|
| 105 |
+
return outpix, overlay, flows, masks
|
| 106 |
|
| 107 |
# Gradio Interface
|
| 108 |
iface = gr.Interface(
|
| 109 |
fn=cellpose_segment,
|
| 110 |
inputs="image",
|
| 111 |
+
outputs=["image", "image", "image", "image"],
|
| 112 |
title="cellpose segmentation",
|
| 113 |
description="upload an image, then cellpose will segment it at a max size of 224x224"
|
| 114 |
)
|