LiangLabUMB commited on
Commit
2fef61c
·
verified ·
1 Parent(s): 8b3506c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -21
app.py CHANGED
@@ -8,7 +8,7 @@ import tempfile
8
  from PIL import Image
9
  import io
10
  from huggingface_hub import hf_hub_download
11
- import base64 # needed for mobile ImageEditor base64 handling
12
 
13
  HF_REPO_ID = "myang4218/cellposemodel"
14
  MODEL_OPTIONS = {
@@ -18,7 +18,7 @@ MODEL_OPTIONS = {
18
 
19
  loaded_models = {}
20
 
21
- # ---- mobile-safe size limits (aggressive for Safari) ----
22
  MAX_SIDE = 1024
23
  MAX_PIXELS = 1024 * 1024
24
 
@@ -31,11 +31,11 @@ def safe_resize(image_np):
31
  h, w = image_np.shape[:2]
32
  total = h * w
33
 
34
- # already small enough
35
  if max(h, w) <= MAX_SIDE and total <= MAX_PIXELS:
36
  return image_np
37
 
38
- # compute scale so BOTH constraints are satisfied
39
  scale_side = MAX_SIDE / max(h, w)
40
  scale_pixels = (MAX_PIXELS / total) ** 0.5
41
  scale = min(scale_side, scale_pixels)
@@ -132,7 +132,7 @@ def measure_confluency(masks, image_np):
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
 
@@ -187,7 +187,7 @@ def rec_min_size(masks, q=25):
187
 
188
 
189
  @spaces.GPU
190
- def run_segmentation(image, model_choice,min_cell_size):
191
  image_np = np.array(image)
192
  image_np = safe_resize(image_np)
193
 
@@ -213,18 +213,14 @@ def run_segmentation(image, model_choice,min_cell_size):
213
  processed_image_np = image_np
214
 
215
  # Run Cellpose segmentation
216
- masks, flows, styles = model.eval(processed_image_np, diameter=None, channels=[0, 0])
217
 
218
  ids = np.unique(masks_raw)
219
  ids = ids[ids > 0]
220
 
221
  sizes = np.array([np.count_nonzero(masks_raw == cid) for cid in ids])
222
 
223
- print("num_cells:", len(ids))
224
- print("mean:", sizes.mean())
225
- print("median:", np.median(sizes))
226
- print("p90:", np.percentile(sizes, 90))
227
- print("max:", sizes.max())
228
  # Compute recommendation from RAW masks
229
  recommend_min = rec_min_size(masks_raw)
230
 
@@ -233,7 +229,7 @@ def run_segmentation(image, model_choice,min_cell_size):
233
  min_used = recommend_min if (min_cell_size == 0) else int(min_cell_size)
234
 
235
  # Apply filters
236
- masks = masks_raw
237
  removed_small = 0
238
  removed_large = 0
239
 
@@ -256,7 +252,7 @@ def run_segmentation(image, model_choice,min_cell_size):
256
  # Create a basic segmentation overlay (without viability)
257
  segmentation_overlay = processed_image_np.copy().astype(np.float32)
258
  if masks.max() > 0:
259
- np.random.seed(42) # For consistent random colors
260
  colors = np.random.randint(0, 255, size=(masks.max() + 1, 3))
261
  colors[0] = [0, 0, 0]
262
  colored_mask = colors[masks]
@@ -269,11 +265,6 @@ def run_segmentation(image, model_choice,min_cell_size):
269
  info_msg += filter_msg
270
  info_msg += f"Segmentation complete! Found {cell_count} cells.\n"
271
  info_msg += f"Confluency: {confluency:.1f}%\n"
272
- if region_coords:
273
- info_msg += (
274
- f"Processed region: {region_coords[0]},{region_coords[1]} "
275
- f"to {region_coords[2]},{region_coords[3]}\n"
276
- )
277
  info_msg += "Now adjust the Blue Threshold for viability assessment."
278
 
279
  return (
@@ -423,9 +414,9 @@ with gr.Blocks(
423
  # Event handlers
424
  segment_btn1.click(
425
  fn=run_segmentation,
426
- inputs=[img_input, model_dropdown1,min_size_slider1, max_size_slider1],
427
  outputs=[cell_count_output1, overlay_output1, info_output1, viability_section1, masks_state, image_state, confluency_output1, min_size_slider1]
428
- ).then( # Chain the initial viability assessment after segmentation
429
  fn=update_viability_realtime,
430
  inputs=[blue_threshold1, masks_state, image_state], # Pass stored state as inputs
431
  outputs=[viability_overlay1, live_count_output1, dead_count_output1, viability_percent_output1, viability_info1]
 
8
  from PIL import Image
9
  import io
10
  from huggingface_hub import hf_hub_download
11
+ import base64
12
 
13
  HF_REPO_ID = "myang4218/cellposemodel"
14
  MODEL_OPTIONS = {
 
18
 
19
  loaded_models = {}
20
 
21
+ # mobile-safe size limits
22
  MAX_SIDE = 1024
23
  MAX_PIXELS = 1024 * 1024
24
 
 
31
  h, w = image_np.shape[:2]
32
  total = h * w
33
 
34
+
35
  if max(h, w) <= MAX_SIDE and total <= MAX_PIXELS:
36
  return image_np
37
 
38
+
39
  scale_side = MAX_SIDE / max(h, w)
40
  scale_pixels = (MAX_PIXELS / total) ** 0.5
41
  scale = min(scale_side, scale_pixels)
 
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] #subtract background
136
 
137
  removed_count = 0
138
 
 
187
 
188
 
189
  @spaces.GPU
190
+ def run_segmentation(image, model_choice, min_cell_size, max_cell_size):
191
  image_np = np.array(image)
192
  image_np = safe_resize(image_np)
193
 
 
213
  processed_image_np = image_np
214
 
215
  # Run Cellpose segmentation
216
+ masks_raw, flows, styles = model.eval(processed_image_np, diameter=None, channels=[0, 0])
217
 
218
  ids = np.unique(masks_raw)
219
  ids = ids[ids > 0]
220
 
221
  sizes = np.array([np.count_nonzero(masks_raw == cid) for cid in ids])
222
 
223
+
 
 
 
 
224
  # Compute recommendation from RAW masks
225
  recommend_min = rec_min_size(masks_raw)
226
 
 
229
  min_used = recommend_min if (min_cell_size == 0) else int(min_cell_size)
230
 
231
  # Apply filters
232
+ masks = masks_raw.copy()
233
  removed_small = 0
234
  removed_large = 0
235
 
 
252
  # Create a basic segmentation overlay (without viability)
253
  segmentation_overlay = processed_image_np.copy().astype(np.float32)
254
  if masks.max() > 0:
255
+ np.random.seed(42)
256
  colors = np.random.randint(0, 255, size=(masks.max() + 1, 3))
257
  colors[0] = [0, 0, 0]
258
  colored_mask = colors[masks]
 
265
  info_msg += filter_msg
266
  info_msg += f"Segmentation complete! Found {cell_count} cells.\n"
267
  info_msg += f"Confluency: {confluency:.1f}%\n"
 
 
 
 
 
268
  info_msg += "Now adjust the Blue Threshold for viability assessment."
269
 
270
  return (
 
414
  # Event handlers
415
  segment_btn1.click(
416
  fn=run_segmentation,
417
+ inputs=[img_input, model_dropdown1, min_size_slider1, max_size_slider1],
418
  outputs=[cell_count_output1, overlay_output1, info_output1, viability_section1, masks_state, image_state, confluency_output1, min_size_slider1]
419
+ ).then(
420
  fn=update_viability_realtime,
421
  inputs=[blue_threshold1, masks_state, image_state], # Pass stored state as inputs
422
  outputs=[viability_overlay1, live_count_output1, dead_count_output1, viability_percent_output1, viability_info1]