Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -216,9 +216,34 @@ def measure_confluency(masks, image_np):
|
|
| 216 |
confluency = cell_pixels / tot_pixels * 100
|
| 217 |
return confluency
|
| 218 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
|
| 220 |
@spaces.GPU
|
| 221 |
-
def run_segmentation_editor(editor_data, model_choice):
|
| 222 |
"""
|
| 223 |
Runs cell segmentation using ImageEditor data.
|
| 224 |
Returns initial segmentation overlay, counts, confluency, and also masks/image for state.
|
|
@@ -252,6 +277,13 @@ def run_segmentation_editor(editor_data, model_choice):
|
|
| 252 |
# Run Cellpose segmentation
|
| 253 |
masks, flows, styles = model.eval(processed_image_np, diameter=None, channels=[0, 0])
|
| 254 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
cell_count = len(np.unique(masks)) - 1
|
| 256 |
confluency = measure_confluency(masks, processed_image_np)
|
| 257 |
|
|
@@ -357,6 +389,15 @@ with gr.Blocks(
|
|
| 357 |
label="Select Model",
|
| 358 |
value="Hemocytometer Model"
|
| 359 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 360 |
segment_btn1 = gr.Button("🔬 Run Segmentation", variant="primary", size="lg")
|
| 361 |
|
| 362 |
with gr.Column():
|
|
@@ -392,7 +433,7 @@ with gr.Blocks(
|
|
| 392 |
# Event handlers
|
| 393 |
segment_btn1.click(
|
| 394 |
fn=run_segmentation_editor,
|
| 395 |
-
inputs=[image_editor, model_dropdown1],
|
| 396 |
outputs=[cell_count_output1, overlay_output1, info_output1, viability_section1, masks_state, image_state, confluency_output1]
|
| 397 |
).then( # Chain the initial viability assessment after segmentation
|
| 398 |
fn=update_viability_realtime,
|
|
|
|
| 216 |
confluency = cell_pixels / tot_pixels * 100
|
| 217 |
return confluency
|
| 218 |
|
| 219 |
+
def filter_mask_by_size(masks,minimum_pixels):
|
| 220 |
+
filtered_masks=masks.copy()
|
| 221 |
+
cell_ids = np.unique(masks)
|
| 222 |
+
cell_ids = cell_ids[cell_ids > 0]
|
| 223 |
+
|
| 224 |
+
removed_count = 0
|
| 225 |
+
|
| 226 |
+
for cell_id in cell_ids:
|
| 227 |
+
cell_mask = (masks == cell_id)
|
| 228 |
+
cell_pixels = np.count_nonzero(cell_mask)
|
| 229 |
+
|
| 230 |
+
if cell_pixels < minimum_pixels:
|
| 231 |
+
filtered_masks[cell_mask] = 0
|
| 232 |
+
removed count +=1
|
| 233 |
+
|
| 234 |
+
unique_ids = np.unique(filtered_masks)
|
| 235 |
+
unique_ids = unique_ids[unique_ids > 0]
|
| 236 |
+
|
| 237 |
+
renumbered_masks = np.zeros_like(filtered_masks)
|
| 238 |
+
for new_id, old_id in enumerate(unique_ids, start=1):
|
| 239 |
+
renumbered_masks[filtered_masks == old_id] = new_id
|
| 240 |
+
|
| 241 |
+
return renumbered_masks, removed_count
|
| 242 |
+
|
| 243 |
+
|
| 244 |
|
| 245 |
@spaces.GPU
|
| 246 |
+
def run_segmentation_editor(editor_data, model_choice, min_cell_size):
|
| 247 |
"""
|
| 248 |
Runs cell segmentation using ImageEditor data.
|
| 249 |
Returns initial segmentation overlay, counts, confluency, and also masks/image for state.
|
|
|
|
| 277 |
# Run Cellpose segmentation
|
| 278 |
masks, flows, styles = model.eval(processed_image_np, diameter=None, channels=[0, 0])
|
| 279 |
|
| 280 |
+
if min_cell_size > 0:
|
| 281 |
+
masks, removed_count = filter_mask_by_size(masks, min_cell_size)
|
| 282 |
+
filter_msg = f"Removed {removed_count} small objects (< {min_cell_size} pixels).\n"
|
| 283 |
+
else:
|
| 284 |
+
removed_count = 0
|
| 285 |
+
filter_msg=""
|
| 286 |
+
|
| 287 |
cell_count = len(np.unique(masks)) - 1
|
| 288 |
confluency = measure_confluency(masks, processed_image_np)
|
| 289 |
|
|
|
|
| 389 |
label="Select Model",
|
| 390 |
value="Hemocytometer Model"
|
| 391 |
)
|
| 392 |
+
min_size_slider1 = gr.slider(
|
| 393 |
+
minimum=0,
|
| 394 |
+
maximum=500,
|
| 395 |
+
value=50,
|
| 396 |
+
step=10,
|
| 397 |
+
label="Minimum Cell Size (pixels)",
|
| 398 |
+
|
| 399 |
+
)
|
| 400 |
+
|
| 401 |
segment_btn1 = gr.Button("🔬 Run Segmentation", variant="primary", size="lg")
|
| 402 |
|
| 403 |
with gr.Column():
|
|
|
|
| 433 |
# Event handlers
|
| 434 |
segment_btn1.click(
|
| 435 |
fn=run_segmentation_editor,
|
| 436 |
+
inputs=[image_editor, model_dropdown1, min_size_slider1],
|
| 437 |
outputs=[cell_count_output1, overlay_output1, info_output1, viability_section1, masks_state, image_state, confluency_output1]
|
| 438 |
).then( # Chain the initial viability assessment after segmentation
|
| 439 |
fn=update_viability_realtime,
|