Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,52 +4,52 @@ import numpy as np
|
|
| 4 |
import cv2
|
| 5 |
from PIL import Image
|
| 6 |
|
| 7 |
-
#
|
| 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 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
with gr.Blocks() as demo:
|
| 35 |
-
gr.Markdown("
|
|
|
|
| 36 |
with gr.Row():
|
| 37 |
with gr.Column():
|
| 38 |
image_input = gr.Image(type="pil", label="Upload Microscopy Image")
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
| 41 |
examples = gr.Examples(
|
| 42 |
examples=[
|
| 43 |
["images/1.png"],
|
| 44 |
-
["images/
|
| 45 |
-
["images/
|
| 46 |
],
|
| 47 |
inputs=[image_input],
|
| 48 |
label="Example Images"
|
| 49 |
)
|
| 50 |
with gr.Column():
|
| 51 |
-
image_output = gr.Image(type="pil", label="
|
| 52 |
|
| 53 |
-
run_btn.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|