Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,95 +6,67 @@ import cv2
|
|
| 6 |
from PIL import Image
|
| 7 |
from huggingface_hub import hf_hub_download
|
| 8 |
|
| 9 |
-
# Hugging Face model repo
|
| 10 |
HF_REPO_ID = "myang4218/cellposemodel"
|
| 11 |
|
| 12 |
-
# Model filename options
|
| 13 |
MODEL_OPTIONS = {
|
| 14 |
"Hemocytometer Model": "hemocytometermodel.npy",
|
| 15 |
"General Model": "generalmodel.npy"
|
| 16 |
}
|
| 17 |
|
| 18 |
-
# Cache loaded models
|
| 19 |
loaded_models = {}
|
| 20 |
-
print("Gradio version:", gr.__version__)
|
| 21 |
|
| 22 |
@spaces.GPU
|
| 23 |
-
def segment_and_count(
|
| 24 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
model_filename = MODEL_OPTIONS[model_choice]
|
| 26 |
model_path = hf_hub_download(repo_id=HF_REPO_ID, filename=model_filename)
|
| 27 |
-
|
| 28 |
if model_filename in loaded_models:
|
| 29 |
model = loaded_models[model_filename]
|
| 30 |
else:
|
| 31 |
model = models.CellposeModel(gpu=True, pretrained_model=model_path)
|
| 32 |
loaded_models[model_filename] = model
|
| 33 |
|
| 34 |
-
|
| 35 |
-
if box is None:
|
| 36 |
-
# No box selected – process entire image
|
| 37 |
-
roi_pil = image
|
| 38 |
-
x, y = 0, 0
|
| 39 |
-
else:
|
| 40 |
-
x, y = int(box["x"]), int(box["y"])
|
| 41 |
-
w, h = int(box["width"]), int(box["height"])
|
| 42 |
-
roi_pil = image.crop((x, y, x + w, y + h))
|
| 43 |
-
|
| 44 |
-
# Convert cropped region to NumPy
|
| 45 |
-
roi_np = np.array(roi_pil)
|
| 46 |
|
| 47 |
-
# Convert
|
| 48 |
-
if len(
|
| 49 |
-
|
| 50 |
-
elif
|
| 51 |
-
|
| 52 |
|
| 53 |
-
# Run Cellpose
|
| 54 |
-
masks, flows, styles = model.eval(
|
| 55 |
-
cell_count = len(np.unique(masks)) - 1 #
|
| 56 |
|
| 57 |
-
#
|
| 58 |
-
|
| 59 |
if masks.max() > 0:
|
| 60 |
np.random.seed(42)
|
| 61 |
colors = np.random.randint(0, 255, size=(masks.max() + 1, 3))
|
| 62 |
colors[0] = [0, 0, 0]
|
| 63 |
colored_mask = colors[masks]
|
| 64 |
-
|
| 65 |
-
overlay_roi = (1 - alpha) * overlay_roi + alpha * colored_mask
|
| 66 |
|
| 67 |
-
|
|
|
|
| 68 |
|
| 69 |
-
|
| 70 |
-
full_overlay = np.array(image).astype(np.float32)
|
| 71 |
-
if full_overlay.shape[2] == 4:
|
| 72 |
-
full_overlay = cv2.cvtColor(full_overlay, cv2.COLOR_RGBA2RGB)
|
| 73 |
-
|
| 74 |
-
# Paste the overlay ROI back onto the full image
|
| 75 |
-
h, w = overlay_roi.shape[:2]
|
| 76 |
-
full_overlay[y:y + h, x:x + w] = overlay_roi
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
|
|
|
| 80 |
|
| 81 |
-
|
|
|
|
|
|
|
| 82 |
|
|
|
|
| 83 |
|
| 84 |
-
# Gradio interface
|
| 85 |
-
demo = gr.Interface(
|
| 86 |
-
fn=segment_and_count,
|
| 87 |
-
inputs=[
|
| 88 |
-
gr.Image(type="pil", label="Microscopy Image", tool="select"),
|
| 89 |
-
gr.Dropdown(choices=list(MODEL_OPTIONS.keys()), label="Select Model", value="Hemocytometer Model")
|
| 90 |
-
],
|
| 91 |
-
outputs=[
|
| 92 |
-
gr.Number(label="Number of Cells"),
|
| 93 |
-
gr.Image(type="pil", label="Segmented Overlay")
|
| 94 |
-
],
|
| 95 |
-
title="Cell Counter with Cellpose",
|
| 96 |
-
description="Upload a microscopy image and optionally select a region to segment cells using Cellpose."
|
| 97 |
-
)
|
| 98 |
-
|
| 99 |
-
if __name__ == "__main__":
|
| 100 |
-
demo.launch()
|
|
|
|
| 6 |
from PIL import Image
|
| 7 |
from huggingface_hub import hf_hub_download
|
| 8 |
|
|
|
|
| 9 |
HF_REPO_ID = "myang4218/cellposemodel"
|
| 10 |
|
|
|
|
| 11 |
MODEL_OPTIONS = {
|
| 12 |
"Hemocytometer Model": "hemocytometermodel.npy",
|
| 13 |
"General Model": "generalmodel.npy"
|
| 14 |
}
|
| 15 |
|
|
|
|
| 16 |
loaded_models = {}
|
|
|
|
| 17 |
|
| 18 |
@spaces.GPU
|
| 19 |
+
def segment_and_count(image_with_crop, model_choice):
|
| 20 |
+
# Extract image and optional crop region
|
| 21 |
+
image = image_with_crop["image"]
|
| 22 |
+
crop_coords = image_with_crop.get("crop")
|
| 23 |
+
|
| 24 |
+
if crop_coords:
|
| 25 |
+
# Crop the image if a region was selected
|
| 26 |
+
x0, y0, x1, y1 = map(int, crop_coords)
|
| 27 |
+
image = image.crop((x0, y0, x1, y1))
|
| 28 |
+
|
| 29 |
model_filename = MODEL_OPTIONS[model_choice]
|
| 30 |
model_path = hf_hub_download(repo_id=HF_REPO_ID, filename=model_filename)
|
|
|
|
| 31 |
if model_filename in loaded_models:
|
| 32 |
model = loaded_models[model_filename]
|
| 33 |
else:
|
| 34 |
model = models.CellposeModel(gpu=True, pretrained_model=model_path)
|
| 35 |
loaded_models[model_filename] = model
|
| 36 |
|
| 37 |
+
image_np = np.array(image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
# Convert grayscale or RGBA to RGB
|
| 40 |
+
if len(image_np.shape) == 2:
|
| 41 |
+
image_np = cv2.cvtColor(image_np, cv2.COLOR_GRAY2RGB)
|
| 42 |
+
elif image_np.shape[2] == 4:
|
| 43 |
+
image_np = cv2.cvtColor(image_np, cv2.COLOR_RGBA2RGB)
|
| 44 |
|
| 45 |
+
# Run Cellpose
|
| 46 |
+
masks, flows, styles = model.eval(image_np, diameter=None, channels=[0, 0])
|
| 47 |
+
cell_count = len(np.unique(masks)) - 1 # exclude background
|
| 48 |
|
| 49 |
+
# Overlay visualization
|
| 50 |
+
overlay = image_np.copy().astype(np.float32)
|
| 51 |
if masks.max() > 0:
|
| 52 |
np.random.seed(42)
|
| 53 |
colors = np.random.randint(0, 255, size=(masks.max() + 1, 3))
|
| 54 |
colors[0] = [0, 0, 0]
|
| 55 |
colored_mask = colors[masks]
|
| 56 |
+
overlay = (1 - 0.4) * overlay + 0.4 * colored_mask
|
|
|
|
| 57 |
|
| 58 |
+
overlay = np.clip(overlay, 0, 255).astype(np.uint8)
|
| 59 |
+
overlay_image = Image.fromarray(overlay)
|
| 60 |
|
| 61 |
+
return cell_count, overlay_image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
# Gradio Blocks Interface
|
| 64 |
+
with gr.Blocks() as demo:
|
| 65 |
+
gr.Markdown("## 🧫 Cell Counter with Cellpose")
|
| 66 |
|
| 67 |
+
with gr.Row():
|
| 68 |
+
image_input = gr.Image(type="pil", label="Microscopy Image", tool="select")
|
| 69 |
+
model_dropdown = gr.Dropdown(choices=list(MODEL_OPTIONS.keys()), label="Select Model", value="Hemocytometer Model")
|
| 70 |
|
| 71 |
+
run_button = gr.Button("
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|