Commit
·
d28a2fe
1
Parent(s):
699bef4
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
import torch
|
4 |
+
import numpy as np
|
5 |
+
import matplotlib.pyplot as plt
|
6 |
+
from celldetection import fetch_model, to_tensor
|
7 |
+
|
8 |
+
device = 'cpu'
|
9 |
+
model = fetch_model('ginoro_CpnResNeXt101UNet-fbe875f1a3e5ce2c').to(device).eval()
|
10 |
+
|
11 |
+
def segment(image):
|
12 |
+
img_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) / 255.0
|
13 |
+
x = to_tensor(img_rgb, transpose=True, device=device, dtype=torch.float32)[None]
|
14 |
+
|
15 |
+
with torch.no_grad():
|
16 |
+
output = model(x)
|
17 |
+
|
18 |
+
contours = output['contours'][0]
|
19 |
+
original = (img_rgb * 255).astype(np.uint8).copy()
|
20 |
+
segmented = original.copy()
|
21 |
+
|
22 |
+
for contour in contours:
|
23 |
+
contour = np.array(contour.cpu(), dtype=np.int32)
|
24 |
+
cv2.drawContours(segmented, [contour], -1, (0, 255, 0), 2)
|
25 |
+
|
26 |
+
h, w, c = original.shape
|
27 |
+
gap = 60
|
28 |
+
canvas = np.zeros((h, w * 2 + gap, c), dtype=np.uint8)
|
29 |
+
canvas[:, :w, :] = original
|
30 |
+
canvas[:, w + gap:, :] = segmented
|
31 |
+
return cv2.cvtColor(canvas, cv2.COLOR_RGB2BGR)
|
32 |
+
|
33 |
+
gr.Interface(fn=segment, inputs=gr.Image(type="numpy"), outputs="image",
|
34 |
+
title="Cell Segmentation Demo (FZJ-INM1)",
|
35 |
+
description="Upload a microscopy image to see side-by-side segmentation.").launch()
|