Spaces:
Runtime error
Runtime error
Update app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455import gradio as grfrom cellpose import modelsimport numpy as npimport cv2from PIL import Image# Load Cellpose pretrained model for cytoplasmmodel = models.Cellpose(model_type='cyto') # or 'nuclei' for nucleusdef segment_and_count(image: Image.Image):# Convert image to numpy RGBimg_np = np.array(image.convert("RGB"))# Run Cellposemasks, flows, styles, diams = model.eval(img_np, diameter=None, channels=[0, 0]) # grayscale or RGB# Create overlay imageoutlines = masks_to_overlay(img_np, masks)# Count unique masks (excluding background=0)unique_ids = np.unique(masks)cell_count = len(unique_ids[unique_ids > 0])return Image.fromarray(outlines), f"Detected cells: {cell_count}"# Draw outlines on imagedef masks_to_overlay(image, masks):overlay = image.copy()contours, _ = cv2.findContours(masks.astype(np.uint8), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)cv2.drawContours(overlay, contours, -1, (0, 255, 0), 1)return overlay# Gradio interfacewith gr.Blocks() as demo:gr.Markdown("## 🧬 Cell Counting with Cellpose")with gr.Row():with gr.Column():image_input = gr.Image(type="pil", label="Upload Microscopy Image")run_btn = gr.Button("Count Cells")count_output = gr.Textbox(label="Detected Cell Count")examples = gr.Examples(examples=[["examples/cells1.png"],["examples/cells2.jpg"],["examples/cells3.png"]],inputs=[image_input],label="Example Images")with gr.Column():image_output = gr.Image(type="pil", label="Cell Mask Overlay")run_btn.click(fn=segment_and_count, inputs=image_input, outputs=[image_output, count_output])demo.launch()12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455import gradio as grfrom cellpose import modelsimport numpy as npimport cv2from PIL import Image# Load Cellpose pretrained model for cytoplasmmodel = models.Cellpose(model_type='cyto') # or 'nuclei' for nucleusdef segment_and_count(image: Image.Image):# Convert image to numpy RGBimg_np = np.array(image.convert("RGB"))# Run Cellposemasks, flows, styles, diams = model.eval(img_np, diameter=None, channels=[0, 0]) # grayscale or RGB# Create overlay imageoutlines = masks_to_overlay(img_np, masks)# Count unique masks (excluding background=0)unique_ids = np.unique(masks)cell_count = len(unique_ids[unique_ids > 0])return Image.fromarray(outlines), f"Detected cells: {cell_count}"# Draw outlines on imagedef masks_to_overlay(image, masks):overlay = image.copy()contours, _ = cv2.findContours(masks.astype(np.uint8), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)cv2.drawContours(overlay, contours, -1, (0, 255, 0), 1)return overlay# Gradio interfacewith gr.Blocks() as demo:gr.Markdown("## 🧬 Cell Counting with Cellpose")with gr.Row():with gr.Column():image_input = gr.Image(type="pil", label="Upload Microscopy Image")run_btn = gr.Button("Count Cells")count_output = gr.Textbox(label="Detected Cell Count")examples = gr.Examples(examples=[["images/1.png"],["images/2.jpg"],["images/3.png"]],inputs=[image_input],label="Example Images")with gr.Column():image_output = gr.Image(type="pil", label="Cell Mask Overlay")run_btn.click(fn=segment_and_count, inputs=image_input, outputs=[image_output, count_output])demo.launch()
app.py+55-55