LiangLabUMB commited on
Commit
a82a8aa
·
verified ·
1 Parent(s): 998114e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -44
app.py CHANGED
@@ -16,40 +16,31 @@ MODEL_OPTIONS = {
16
  loaded_models = {}
17
 
18
  @spaces.GPU
19
- def segment_and_count(image_with_crop, model_choice):
20
- # Extract PIL image and crop box
21
- image = image_with_crop["image"]
22
- crop_box = image_with_crop["crop"] # Format: [x1, y1, x2, y2]
23
-
24
- # Crop the image if a box was drawn
25
- if crop_box is not None:
26
- x1, y1, x2, y2 = map(int, crop_box)
27
- image = image.crop((x1, y1, x2, y2))
28
-
29
- # Convert to NumPy array
30
- image_np = np.array(image)
31
-
32
- # Ensure RGB format
33
- if len(image_np.shape) == 2:
34
- image_np = cv2.cvtColor(image_np, cv2.COLOR_GRAY2RGB)
35
- elif image_np.shape[2] == 4:
36
- image_np = cv2.cvtColor(image_np, cv2.COLOR_RGBA2RGB)
37
-
38
- # Load model
39
  model_filename = MODEL_OPTIONS[model_choice]
40
  model_path = hf_hub_download(repo_id=HF_REPO_ID, filename=model_filename)
41
-
42
  if model_filename in loaded_models:
43
  model = loaded_models[model_filename]
44
  else:
45
  model = models.CellposeModel(gpu=True, pretrained_model=model_path)
46
  loaded_models[model_filename] = model
47
 
 
 
 
 
 
 
 
 
 
48
  # Run Cellpose
49
  masks, flows, styles = model.eval(image_np, diameter=None, channels=[0, 0])
50
 
51
- # Count cells
52
- cell_count = len(np.unique(masks)) - 1 # background = 0
53
 
54
  # Overlay visualization
55
  overlay = image_np.copy().astype(np.float32)
@@ -58,41 +49,31 @@ def segment_and_count(image_with_crop, model_choice):
58
  colors = np.random.randint(0, 255, size=(masks.max() + 1, 3))
59
  colors[0] = [0, 0, 0]
60
  colored_mask = colors[masks]
61
- overlay = (1 - 0.4) * overlay + 0.4 * colored_mask
 
62
 
63
  overlay = np.clip(overlay, 0, 255).astype(np.uint8)
64
  overlay_image = Image.fromarray(overlay)
65
 
66
  return cell_count, overlay_image
67
 
68
- # Build Gradio app using Blocks
69
  with gr.Blocks() as demo:
70
- gr.Markdown("## 🧪 Cell Counter with Cellpose")
 
71
 
72
  with gr.Row():
73
- image_input = gr.Image(
74
- type="pil",
75
- label="Microscopy Image",
76
- tool="select"
77
- )
78
- model_dropdown = gr.Dropdown(
79
- choices=list(MODEL_OPTIONS.keys()),
80
- label="Select Model",
81
- value="Hemocytometer Model"
82
- )
83
-
84
- run_button = gr.Button("Run Segmentation")
85
 
86
  with gr.Row():
87
  count_output = gr.Number(label="Number of Cells")
88
- overlay_output = gr.Image(type="pil", label="Segmented Overlay")
89
 
90
- run_button.click(
91
- fn=segment_and_count,
92
- inputs=[image_input, model_dropdown],
93
- outputs=[count_output, overlay_output]
94
- )
95
 
96
  if __name__ == "__main__":
97
  demo.launch()
98
 
 
 
16
  loaded_models = {}
17
 
18
  @spaces.GPU
19
+ def segment_and_count(edited_image, model_choice):
20
+ # Load selected model
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  model_filename = MODEL_OPTIONS[model_choice]
22
  model_path = hf_hub_download(repo_id=HF_REPO_ID, filename=model_filename)
23
+
24
  if model_filename in loaded_models:
25
  model = loaded_models[model_filename]
26
  else:
27
  model = models.CellposeModel(gpu=True, pretrained_model=model_path)
28
  loaded_models[model_filename] = model
29
 
30
+ # Convert edited PIL image to numpy
31
+ image_np = np.array(edited_image)
32
+
33
+ # If grayscale, convert to RGB
34
+ if len(image_np.shape) == 2:
35
+ image_np = cv2.cvtColor(image_np, cv2.COLOR_GRAY2RGB)
36
+ elif len(image_np.shape) == 3 and image_np.shape[2] == 4:
37
+ image_np = cv2.cvtColor(image_np, cv2.COLOR_RGBA2RGB)
38
+
39
  # Run Cellpose
40
  masks, flows, styles = model.eval(image_np, diameter=None, channels=[0, 0])
41
 
42
+ # Count unique cells
43
+ cell_count = len(np.unique(masks)) - 1
44
 
45
  # Overlay visualization
46
  overlay = image_np.copy().astype(np.float32)
 
49
  colors = np.random.randint(0, 255, size=(masks.max() + 1, 3))
50
  colors[0] = [0, 0, 0]
51
  colored_mask = colors[masks]
52
+ alpha = 0.4
53
+ overlay = (1 - alpha) * overlay + alpha * colored_mask
54
 
55
  overlay = np.clip(overlay, 0, 255).astype(np.uint8)
56
  overlay_image = Image.fromarray(overlay)
57
 
58
  return cell_count, overlay_image
59
 
60
+ # Gradio UI
61
  with gr.Blocks() as demo:
62
+ gr.Markdown("## 🧪 Cell Counter with Cellpose + ImageEditor")
63
+ gr.Markdown("Upload a microscopy image, draw/crop a region using the editor, then select a model to count cells in that region.")
64
 
65
  with gr.Row():
66
+ image_editor = gr.ImageEditor(label="Draw or Crop Region", type="pil")
67
+ model_selector = gr.Dropdown(choices=list(MODEL_OPTIONS.keys()), value="Hemocytometer Model", label="Select Model")
 
 
 
 
 
 
 
 
 
 
68
 
69
  with gr.Row():
70
  count_output = gr.Number(label="Number of Cells")
71
+ overlay_output = gr.Image(label="Segmented Overlay")
72
 
73
+ image_editor.change(fn=segment_and_count, inputs=[image_editor, model_selector], outputs=[count_output, overlay_output])
74
+ model_selector.change(fn=segment_and_count, inputs=[image_editor, model_selector], outputs=[count_output, overlay_output])
 
 
 
75
 
76
  if __name__ == "__main__":
77
  demo.launch()
78
 
79
+