tbuyuktanir commited on
Commit
2477408
·
verified ·
1 Parent(s): 31e57da

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -28
app.py CHANGED
@@ -4,52 +4,52 @@ import numpy as np
4
  import cv2
5
  from PIL import Image
6
 
7
- # Load Cellpose pretrained model for cytoplasm
8
- model = models.CellposeModel(model_type='cyto') # or 'nuclei' for nucleus
9
-
10
- def segment_and_count(image: Image.Image):
11
- # Convert image to numpy RGB
12
- img_np = np.array(image.convert("RGB"))
13
-
14
- # Run Cellpose
15
- masks, flows, styles, diams = model.eval(img_np, diameter=None, channels=[0, 0]) # grayscale or RGB
16
-
17
- # Create overlay image
18
- outlines = masks_to_overlay(img_np, masks)
19
-
20
- # Count unique masks (excluding background=0)
21
- unique_ids = np.unique(masks)
22
- cell_count = len(unique_ids[unique_ids > 0])
23
-
24
- return Image.fromarray(outlines), f"Detected cells: {cell_count}"
25
-
26
- # Draw outlines on image
27
  def masks_to_overlay(image, masks):
28
  overlay = image.copy()
29
  contours, _ = cv2.findContours(masks.astype(np.uint8), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
30
  cv2.drawContours(overlay, contours, -1, (0, 255, 0), 1)
31
  return overlay
32
 
33
- # Gradio interface
 
 
 
 
 
 
 
 
 
 
 
34
  with gr.Blocks() as demo:
35
- gr.Markdown("## 🧬 Cell Counting with Cellpose")
 
36
  with gr.Row():
37
  with gr.Column():
38
  image_input = gr.Image(type="pil", label="Upload Microscopy Image")
39
- run_btn = gr.Button("Count Cells")
40
- count_output = gr.Textbox(label="Detected Cell Count")
 
 
 
41
  examples = gr.Examples(
42
  examples=[
43
  ["images/1.png"],
44
- ["images/2.jpg"],
45
- ["images/3.png"]
46
  ],
47
  inputs=[image_input],
48
  label="Example Images"
49
  )
50
  with gr.Column():
51
- image_output = gr.Image(type="pil", label="Cell Mask Overlay")
52
 
53
- run_btn.click(fn=segment_and_count, inputs=image_input, outputs=[image_output, count_output])
 
 
 
 
54
 
55
  demo.launch()
 
4
  import cv2
5
  from PIL import Image
6
 
7
+ # Draw contours on original image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  def masks_to_overlay(image, masks):
9
  overlay = image.copy()
10
  contours, _ = cv2.findContours(masks.astype(np.uint8), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
11
  cv2.drawContours(overlay, contours, -1, (0, 255, 0), 1)
12
  return overlay
13
 
14
+ # Main processing function
15
+ def segment_and_count(image: Image.Image, model_type: str, diameter: float):
16
+ img_np = np.array(image.convert("RGB"))
17
+ model = models.CellposeModel(model_type=model_type)
18
+ masks, _, _, _ = model.eval(img_np, diameter=None if diameter == 0 else diameter, channels=[0, 0])
19
+
20
+ overlay = masks_to_overlay(img_np, masks)
21
+ cell_count = len(np.unique(masks)) - 1 # exclude background 0
22
+
23
+ return Image.fromarray(overlay), f"Detected cells: {cell_count}"
24
+
25
+ # Gradio UI
26
  with gr.Blocks() as demo:
27
+ gr.Markdown("Cell Counting with Cellpose (Interactive)")
28
+
29
  with gr.Row():
30
  with gr.Column():
31
  image_input = gr.Image(type="pil", label="Upload Microscopy Image")
32
+ model_selector = gr.Radio(["cyto", "nuclei"], label="Model Type", value="cyto")
33
+ diameter_slider = gr.Slider(0, 100, step=1, value=0, label="Cell Diameter (0 = Auto)")
34
+ run_btn = gr.Button("Run Cell Counting")
35
+ count_output = gr.Textbox(label="Cell Count")
36
+
37
  examples = gr.Examples(
38
  examples=[
39
  ["images/1.png"],
40
+ ["images/1.png"],
41
+ ["images/1.png"]
42
  ],
43
  inputs=[image_input],
44
  label="Example Images"
45
  )
46
  with gr.Column():
47
+ image_output = gr.Image(type="pil", label="Mask Overlay")
48
 
49
+ run_btn.click(
50
+ fn=segment_and_count,
51
+ inputs=[image_input, model_selector, diameter_slider],
52
+ outputs=[image_output, count_output]
53
+ )
54
 
55
  demo.launch()