File size: 6,111 Bytes
d28a2fe b8015e9 9cea6a9 d28a2fe 9cea6a9 d28a2fe 9cea6a9 d28a2fe 9cea6a9 d28a2fe 9cea6a9 d28a2fe 7cec736 d28a2fe 9cea6a9 d28a2fe 9cea6a9 b8015e9 9cea6a9 b8015e9 | 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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | import gradio as gr
import cv2
import torch
import numpy as np
import matplotlib.pyplot as plt
from celldetection import fetch_model, to_tensor
# --- DOCUMENTATION STRINGS (Client Friendly) ---
USAGE_GUIDELINES = """
## 1. Clear Setup and Run Instructions (Quick Start)
This application uses the advanced GINORO segmentation model, pre-trained for identifying cell nuclei in microscopy images.
1. **Preparation:** Ensure your image is a clear microscopy slide image, preferably showing distinct cell nuclei.
2. **Upload:** Click the 'Input Microscopy Image' box and upload your image (drag and drop, or click to select).
3. **Run:** Click the **"Run Segmentation"** button. If using an example, clicking the thumbnail will load and run the segmentation automatically.
4. **Review:** The result panel will display two images side-by-side: the Original (Left) and the Segmented result (Right).
"""
INPUT_EXPLANATION = """
## 2. Expected Inputs
| Input Field | Purpose | Requirement |
| :--- | :--- | :--- |
| **Input Microscopy Image** | The high-resolution image containing the cells you wish to analyze. | Must be an image file (PNG, JPG, TIF). Optimal results are achieved with clear, well-focused images typical of fluorescence microscopy (e.g., DAPI staining for nuclei). |
"""
OUTPUT_EXPLANATION = """
## 3. Expected Outputs (Side-by-Side Segmentation)
The output is a single image combining the original input and the segmented result for easy comparison.
* **Left Side (Original):** The unmodified input image.
* **Right Side (Segmented):** The same image with outlines (contours) drawn over the detected cellular structures.
* **Contour Color:** The detected cell nuclei are outlined in **Blue**.
"""
# ✅ Load the model
device = 'cpu'
model = fetch_model('ginoro_CpnResNeXt101UNet-fbe875f1a3e5ce2c').to(device).eval()
# ✅ Inference function
def segment(image):
img_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) / 255.0
x = to_tensor(img_rgb, transpose=True, device=device, dtype=torch.float32)[None]
with torch.no_grad():
output = model(x)
contours = output['contours'][0]
original = (img_rgb * 255).astype(np.uint8).copy()
segmented = original.copy()
for contour in contours:
contour = np.array(contour.cpu(), dtype=np.int32)
cv2.drawContours(segmented, [contour], -1, (255, 0, 0), 2)
h, w, c = original.shape
gap = 60
canvas = np.zeros((h, w * 2 + gap, c), dtype=np.uint8)
canvas[:, :w, :] = original
canvas[:, w + gap:, :] = segmented
return cv2.cvtColor(canvas, cv2.COLOR_RGB2BGR)
# ✅ Example images list
examples = [
["./sample_data/1.png"],
["./sample_data/2.png"],
["./sample_data/3.png"]
]
# ✅ Launch the Gradio interface
with gr.Blocks(title="Cell Segmentation Demo (FZJ-INM1)") as demo:
gr.Markdown(
"""
# Cell Segmentation Demo (FZJ-INM1)
**Purpose:** Automatically identify and outline cell nuclei in microscopy images using a specialized neural network.
"""
)
# 1. Guidelines Accordion (Documentation Section)
with gr.Accordion(" Tips & Guidelines ", open=False):
gr.Markdown(USAGE_GUIDELINES)
gr.Markdown("---")
gr.Markdown(INPUT_EXPLANATION)
gr.Markdown("---")
gr.Markdown(OUTPUT_EXPLANATION)
gr.Markdown("---")
# Define Components
gr.Markdown("## Step 1: Upload an Image")
input_image = gr.Image(type="numpy", label="Input Microscopy Image")
gr.Markdown("## Step 2: Click button")
run_button = gr.Button("Run Segmentation", variant="primary")
gr.Markdown("## Output")
output_image = gr.Image(label="Output: Original (Left) vs. Segmented (Right)")
# Layout the Application Interface
# with gr.Row():
# with gr.Column(scale=1):
# input_image
# gr.Markdown("## Step 2: Click button")
# run_button
# with gr.Column(scale=2):
# output_image
# Event Handler
run_button.click(
fn=segment,
inputs=input_image,
outputs=output_image
)
gr.Markdown("---")
gr.Markdown("## Examples ")
# 2. Examples Section (Error Fixed)
# By providing explicit inputs, outputs, and fn, we resolve the ValueError.
gr.Examples(
examples=examples,
inputs=[input_image],
outputs=output_image,
fn=segment,
label="Click on an image thumbnail below to load and run a sample segmentation.",
)
demo.launch(
server_name = "0.0.0.0",
server_port = 7860
)
# import gradio as gr
# import cv2
# import torch
# import numpy as np
# import matplotlib.pyplot as plt
# from celldetection import fetch_model, to_tensor
# # ✅ Load the model
# device = 'cpu'
# model = fetch_model('ginoro_CpnResNeXt101UNet-fbe875f1a3e5ce2c').to(device).eval()
# # ✅ Inference function
# def segment(image):
# img_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) / 255.0
# x = to_tensor(img_rgb, transpose=True, device=device, dtype=torch.float32)[None]
# with torch.no_grad():
# output = model(x)
# contours = output['contours'][0]
# original = (img_rgb * 255).astype(np.uint8).copy()
# segmented = original.copy()
# for contour in contours:
# contour = np.array(contour.cpu(), dtype=np.int32)
# cv2.drawContours(segmented, [contour], -1, (255, 0, 0), 2)
# h, w, c = original.shape
# gap = 60
# canvas = np.zeros((h, w * 2 + gap, c), dtype=np.uint8)
# canvas[:, :w, :] = original
# canvas[:, w + gap:, :] = segmented
# return cv2.cvtColor(canvas, cv2.COLOR_RGB2BGR)
# # ✅ Example images list
# examples = [
# ["1.png"],
# ["2.png"],
# ["3.png"]
# ]
# # ✅ Launch the Gradio interface
# gr.Interface(
# fn=segment,
# inputs=gr.Image(type="numpy"),
# outputs="image",
# title="Cell Segmentation Demo (FZJ-INM1)",
# description="Upload a microscopy image to see side-by-side segmentation.",
# examples=examples
# ).launch() |