Update app.py
Browse files
app.py
CHANGED
|
@@ -3,78 +3,88 @@ import spaces
|
|
| 3 |
from cellpose import models
|
| 4 |
import numpy as np
|
| 5 |
import cv2
|
| 6 |
-
import matplotlib.pyplot as plt
|
| 7 |
-
import tempfile
|
| 8 |
from PIL import Image
|
| 9 |
-
import io
|
| 10 |
from huggingface_hub import hf_hub_download
|
| 11 |
|
|
|
|
| 12 |
HF_REPO_ID = "myang4218/cellposemodel"
|
| 13 |
|
|
|
|
| 14 |
MODEL_OPTIONS = {
|
| 15 |
"Hemocytometer Model": "hemocytometermodel.npy",
|
| 16 |
"General Model": "generalmodel.npy"
|
| 17 |
}
|
| 18 |
|
|
|
|
| 19 |
loaded_models = {}
|
| 20 |
|
| 21 |
@spaces.GPU
|
| 22 |
-
def segment_and_count(image, model_choice):
|
| 23 |
-
|
| 24 |
model_filename = MODEL_OPTIONS[model_choice]
|
| 25 |
model_path = hf_hub_download(repo_id=HF_REPO_ID, filename=model_filename)
|
|
|
|
| 26 |
if model_filename in loaded_models:
|
| 27 |
model = loaded_models[model_filename]
|
| 28 |
else:
|
| 29 |
model = models.CellposeModel(gpu=True, pretrained_model=model_path)
|
| 30 |
loaded_models[model_filename] = model
|
| 31 |
-
|
| 32 |
-
#
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
#
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
#
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
if masks.max() > 0:
|
| 53 |
-
|
| 54 |
-
np.random.seed(42) # For reproducible colors
|
| 55 |
colors = np.random.randint(0, 255, size=(masks.max() + 1, 3))
|
| 56 |
-
colors[0] = [0, 0, 0]
|
| 57 |
-
|
| 58 |
-
# Create colored overlay
|
| 59 |
colored_mask = colors[masks]
|
| 60 |
-
|
| 61 |
-
# Blend with original image
|
| 62 |
alpha = 0.4
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
return cell_count, overlay_image
|
| 72 |
|
|
|
|
| 73 |
# Gradio interface
|
| 74 |
demo = gr.Interface(
|
| 75 |
fn=segment_and_count,
|
| 76 |
inputs=[
|
| 77 |
-
gr.Image(type="pil", label="Microscopy Image"),
|
| 78 |
gr.Dropdown(choices=list(MODEL_OPTIONS.keys()), label="Select Model", value="Hemocytometer Model")
|
| 79 |
],
|
| 80 |
outputs=[
|
|
@@ -82,8 +92,8 @@ demo = gr.Interface(
|
|
| 82 |
gr.Image(type="pil", label="Segmented Overlay")
|
| 83 |
],
|
| 84 |
title="Cell Counter with Cellpose",
|
| 85 |
-
description="Upload a microscopy image and select a
|
| 86 |
)
|
| 87 |
|
| 88 |
if __name__ == "__main__":
|
| 89 |
-
demo.launch()
|
|
|
|
| 3 |
from cellpose import models
|
| 4 |
import numpy as np
|
| 5 |
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 |
|
| 21 |
@spaces.GPU
|
| 22 |
+
def segment_and_count(image, box, model_choice):
|
| 23 |
+
# Download and load selected model
|
| 24 |
model_filename = MODEL_OPTIONS[model_choice]
|
| 25 |
model_path = hf_hub_download(repo_id=HF_REPO_ID, filename=model_filename)
|
| 26 |
+
|
| 27 |
if model_filename in loaded_models:
|
| 28 |
model = loaded_models[model_filename]
|
| 29 |
else:
|
| 30 |
model = models.CellposeModel(gpu=True, pretrained_model=model_path)
|
| 31 |
loaded_models[model_filename] = model
|
| 32 |
+
|
| 33 |
+
# Get ROI coordinates from selection box
|
| 34 |
+
if box is None:
|
| 35 |
+
# No box selected – process entire image
|
| 36 |
+
roi_pil = image
|
| 37 |
+
x, y = 0, 0
|
| 38 |
+
else:
|
| 39 |
+
x, y = int(box["x"]), int(box["y"])
|
| 40 |
+
w, h = int(box["width"]), int(box["height"])
|
| 41 |
+
roi_pil = image.crop((x, y, x + w, y + h))
|
| 42 |
+
|
| 43 |
+
# Convert cropped region to NumPy
|
| 44 |
+
roi_np = np.array(roi_pil)
|
| 45 |
+
|
| 46 |
+
# Convert to RGB if needed
|
| 47 |
+
if len(roi_np.shape) == 2:
|
| 48 |
+
roi_np = cv2.cvtColor(roi_np, cv2.COLOR_GRAY2RGB)
|
| 49 |
+
elif roi_np.shape[2] == 4:
|
| 50 |
+
roi_np = cv2.cvtColor(roi_np, cv2.COLOR_RGBA2RGB)
|
| 51 |
+
|
| 52 |
+
# Run Cellpose on cropped ROI
|
| 53 |
+
masks, flows, styles = model.eval(roi_np, diameter=None, channels=[0, 0])
|
| 54 |
+
cell_count = len(np.unique(masks)) - 1 # Exclude background
|
| 55 |
+
|
| 56 |
+
# Create colored overlay for ROI
|
| 57 |
+
overlay_roi = roi_np.copy().astype(np.float32)
|
| 58 |
if masks.max() > 0:
|
| 59 |
+
np.random.seed(42)
|
|
|
|
| 60 |
colors = np.random.randint(0, 255, size=(masks.max() + 1, 3))
|
| 61 |
+
colors[0] = [0, 0, 0]
|
|
|
|
|
|
|
| 62 |
colored_mask = colors[masks]
|
|
|
|
|
|
|
| 63 |
alpha = 0.4
|
| 64 |
+
overlay_roi = (1 - alpha) * overlay_roi + alpha * colored_mask
|
| 65 |
+
|
| 66 |
+
overlay_roi = np.clip(overlay_roi, 0, 255).astype(np.uint8)
|
| 67 |
+
|
| 68 |
+
# Prepare final overlay image
|
| 69 |
+
full_overlay = np.array(image).astype(np.float32)
|
| 70 |
+
if full_overlay.shape[2] == 4:
|
| 71 |
+
full_overlay = cv2.cvtColor(full_overlay, cv2.COLOR_RGBA2RGB)
|
| 72 |
+
|
| 73 |
+
# Paste the overlay ROI back onto the full image
|
| 74 |
+
h, w = overlay_roi.shape[:2]
|
| 75 |
+
full_overlay[y:y + h, x:x + w] = overlay_roi
|
| 76 |
+
|
| 77 |
+
# Convert back to PIL for output
|
| 78 |
+
overlay_image = Image.fromarray(full_overlay.astype(np.uint8))
|
| 79 |
+
|
| 80 |
return cell_count, overlay_image
|
| 81 |
|
| 82 |
+
|
| 83 |
# Gradio interface
|
| 84 |
demo = gr.Interface(
|
| 85 |
fn=segment_and_count,
|
| 86 |
inputs=[
|
| 87 |
+
gr.Image(type="pil", label="Microscopy Image", tool="select"),
|
| 88 |
gr.Dropdown(choices=list(MODEL_OPTIONS.keys()), label="Select Model", value="Hemocytometer Model")
|
| 89 |
],
|
| 90 |
outputs=[
|
|
|
|
| 92 |
gr.Image(type="pil", label="Segmented Overlay")
|
| 93 |
],
|
| 94 |
title="Cell Counter with Cellpose",
|
| 95 |
+
description="Upload a microscopy image and optionally select a region to segment cells using Cellpose."
|
| 96 |
)
|
| 97 |
|
| 98 |
if __name__ == "__main__":
|
| 99 |
+
demo.launch()
|