Commit
·
9cea6a9
1
Parent(s):
7cec736
Add example images and enable Gradio examples
Browse files- app.py +22 -6
- requirements.txt +3 -2
app.py
CHANGED
@@ -5,20 +5,22 @@ 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, (255, 0, 0), 2)
|
@@ -28,8 +30,22 @@ def segment(image):
|
|
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 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
import matplotlib.pyplot as plt
|
6 |
from celldetection import fetch_model, to_tensor
|
7 |
|
8 |
+
# ✅ Load the model
|
9 |
device = 'cpu'
|
10 |
model = fetch_model('ginoro_CpnResNeXt101UNet-fbe875f1a3e5ce2c').to(device).eval()
|
11 |
|
12 |
+
# ✅ Inference function
|
13 |
def segment(image):
|
14 |
img_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) / 255.0
|
15 |
x = to_tensor(img_rgb, transpose=True, device=device, dtype=torch.float32)[None]
|
16 |
+
|
17 |
with torch.no_grad():
|
18 |
output = model(x)
|
19 |
+
|
20 |
contours = output['contours'][0]
|
21 |
original = (img_rgb * 255).astype(np.uint8).copy()
|
22 |
segmented = original.copy()
|
23 |
+
|
24 |
for contour in contours:
|
25 |
contour = np.array(contour.cpu(), dtype=np.int32)
|
26 |
cv2.drawContours(segmented, [contour], -1, (255, 0, 0), 2)
|
|
|
30 |
canvas = np.zeros((h, w * 2 + gap, c), dtype=np.uint8)
|
31 |
canvas[:, :w, :] = original
|
32 |
canvas[:, w + gap:, :] = segmented
|
33 |
+
|
34 |
return cv2.cvtColor(canvas, cv2.COLOR_RGB2BGR)
|
35 |
|
36 |
+
# ✅ Example images list
|
37 |
+
examples = [
|
38 |
+
["examples/1.png"],
|
39 |
+
["examples/2.png"],
|
40 |
+
["examples/3.png"]
|
41 |
+
]
|
42 |
+
|
43 |
+
# ✅ Launch the Gradio interface
|
44 |
+
gr.Interface(
|
45 |
+
fn=segment,
|
46 |
+
inputs=gr.Image(type="numpy"),
|
47 |
+
outputs="image",
|
48 |
+
title="Cell Segmentation Demo (FZJ-INM1)",
|
49 |
+
description="Upload a microscopy image to see side-by-side segmentation.",
|
50 |
+
examples=examples
|
51 |
+
).launch()
|
requirements.txt
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
gradio
|
2 |
-
|
3 |
celldetection
|
4 |
-
|
|
|
5 |
matplotlib
|
|
|
1 |
gradio
|
2 |
+
opencv-python
|
3 |
celldetection
|
4 |
+
torch
|
5 |
+
numpy
|
6 |
matplotlib
|