Update app.py
Browse files
app.py
CHANGED
|
@@ -129,9 +129,32 @@ def measure_confluency(masks, image_np):
|
|
| 129 |
confluency = cell_pixels / tot_pixels * 100
|
| 130 |
return confluency
|
| 131 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
@spaces.GPU
|
| 134 |
-
def run_segmentation(image, model_choice):
|
| 135 |
image_np = np.array(image)
|
| 136 |
image_np = safe_resize(image_np)
|
| 137 |
|
|
@@ -159,6 +182,13 @@ def run_segmentation(image, model_choice):
|
|
| 159 |
# Run Cellpose segmentation
|
| 160 |
masks, flows, styles = model.eval(processed_image_np, diameter=None, channels=[0, 0])
|
| 161 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
cell_count = len(np.unique(masks)) - 1
|
| 163 |
confluency = measure_confluency(masks, processed_image_np)
|
| 164 |
|
|
@@ -261,6 +291,14 @@ with gr.Blocks(
|
|
| 261 |
label="Select Model",
|
| 262 |
value="Hemocytometer Model"
|
| 263 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
segment_btn1 = gr.Button("🔬 Run Segmentation", variant="primary", size="lg")
|
| 265 |
|
| 266 |
with gr.Column():
|
|
@@ -296,7 +334,7 @@ with gr.Blocks(
|
|
| 296 |
# Event handlers
|
| 297 |
segment_btn1.click(
|
| 298 |
fn=run_segmentation,
|
| 299 |
-
inputs=[img_input, model_dropdown1],
|
| 300 |
outputs=[cell_count_output1, overlay_output1, info_output1, viability_section1, masks_state, image_state, confluency_output1]
|
| 301 |
).then( # Chain the initial viability assessment after segmentation
|
| 302 |
fn=update_viability_realtime,
|
|
|
|
| 129 |
confluency = cell_pixels / tot_pixels * 100
|
| 130 |
return confluency
|
| 131 |
|
| 132 |
+
def filter_mask_by_size(masks,minimum_pixels):
|
| 133 |
+
filtered_masks=masks.copy()
|
| 134 |
+
cell_ids = np.unique(masks)
|
| 135 |
+
cell_ids = cell_ids[cell_ids > 0]
|
| 136 |
+
|
| 137 |
+
removed_count = 0
|
| 138 |
+
|
| 139 |
+
for cell_id in cell_ids:
|
| 140 |
+
cell_mask = (masks == cell_id)
|
| 141 |
+
cell_pixels = np.count_nonzero(cell_mask)
|
| 142 |
+
|
| 143 |
+
if cell_pixels < minimum_pixels:
|
| 144 |
+
filtered_masks[cell_mask] = 0
|
| 145 |
+
removed_count +=1
|
| 146 |
+
|
| 147 |
+
unique_ids = np.unique(filtered_masks)
|
| 148 |
+
unique_ids = unique_ids[unique_ids > 0]
|
| 149 |
+
|
| 150 |
+
renumbered_masks = np.zeros_like(filtered_masks)
|
| 151 |
+
for new_id, old_id in enumerate(unique_ids, start=1):
|
| 152 |
+
renumbered_masks[filtered_masks == old_id] = new_id
|
| 153 |
+
|
| 154 |
+
return renumbered_masks, removed_count
|
| 155 |
|
| 156 |
@spaces.GPU
|
| 157 |
+
def run_segmentation(image, model_choice,min_cell_size):
|
| 158 |
image_np = np.array(image)
|
| 159 |
image_np = safe_resize(image_np)
|
| 160 |
|
|
|
|
| 182 |
# Run Cellpose segmentation
|
| 183 |
masks, flows, styles = model.eval(processed_image_np, diameter=None, channels=[0, 0])
|
| 184 |
|
| 185 |
+
if min_cell_size > 0:
|
| 186 |
+
masks, removed_count = filter_mask_by_size(masks, min_cell_size)
|
| 187 |
+
filter_msg = f"Removed {removed_count} small objects (< {min_cell_size} pixels).\n"
|
| 188 |
+
else:
|
| 189 |
+
removed_count = 0
|
| 190 |
+
filter_msg=""
|
| 191 |
+
|
| 192 |
cell_count = len(np.unique(masks)) - 1
|
| 193 |
confluency = measure_confluency(masks, processed_image_np)
|
| 194 |
|
|
|
|
| 291 |
label="Select Model",
|
| 292 |
value="Hemocytometer Model"
|
| 293 |
)
|
| 294 |
+
min_size_slider1 = gr.Slider(
|
| 295 |
+
minimum=0,
|
| 296 |
+
maximum=500,
|
| 297 |
+
value=50,
|
| 298 |
+
step=10,
|
| 299 |
+
label="Minimum Cell Size (pixels)",
|
| 300 |
+
|
| 301 |
+
)
|
| 302 |
segment_btn1 = gr.Button("🔬 Run Segmentation", variant="primary", size="lg")
|
| 303 |
|
| 304 |
with gr.Column():
|
|
|
|
| 334 |
# Event handlers
|
| 335 |
segment_btn1.click(
|
| 336 |
fn=run_segmentation,
|
| 337 |
+
inputs=[img_input, model_dropdown1,min_size_slider1],
|
| 338 |
outputs=[cell_count_output1, overlay_output1, info_output1, viability_section1, masks_state, image_state, confluency_output1]
|
| 339 |
).then( # Chain the initial viability assessment after segmentation
|
| 340 |
fn=update_viability_realtime,
|