Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -17,34 +17,39 @@ loaded_models = {}
|
|
| 17 |
|
| 18 |
@spaces.GPU
|
| 19 |
def segment_and_count(image_with_crop, model_choice):
|
| 20 |
-
# Extract image and
|
| 21 |
image = image_with_crop["image"]
|
| 22 |
-
|
| 23 |
|
| 24 |
-
if
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
image = image.crop((
|
| 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 |
-
|
|
|
|
|
|
|
| 48 |
|
| 49 |
# Overlay visualization
|
| 50 |
overlay = image_np.copy().astype(np.float32)
|
|
@@ -60,13 +65,34 @@ def segment_and_count(image_with_crop, model_choice):
|
|
| 60 |
|
| 61 |
return cell_count, overlay_image
|
| 62 |
|
| 63 |
-
# Gradio
|
| 64 |
with gr.Blocks() as demo:
|
| 65 |
-
gr.Markdown("##
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
with gr.Row():
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
-
|
|
|
|
| 72 |
|
|
|
|
| 17 |
|
| 18 |
@spaces.GPU
|
| 19 |
def segment_and_count(image_with_crop, model_choice):
|
| 20 |
+
# Extract PIL image and crop box
|
| 21 |
image = image_with_crop["image"]
|
| 22 |
+
crop_box = image_with_crop["crop"] # Format: [x1, y1, x2, y2]
|
| 23 |
|
| 24 |
+
# Crop the image if a box was drawn
|
| 25 |
+
if crop_box is not None:
|
| 26 |
+
x1, y1, x2, y2 = map(int, crop_box)
|
| 27 |
+
image = image.crop((x1, y1, x2, y2))
|
| 28 |
|
| 29 |
+
# Convert to NumPy array
|
| 30 |
+
image_np = np.array(image)
|
| 31 |
+
|
| 32 |
+
# Ensure RGB format
|
| 33 |
+
if len(image_np.shape) == 2:
|
| 34 |
+
image_np = cv2.cvtColor(image_np, cv2.COLOR_GRAY2RGB)
|
| 35 |
+
elif image_np.shape[2] == 4:
|
| 36 |
+
image_np = cv2.cvtColor(image_np, cv2.COLOR_RGBA2RGB)
|
| 37 |
+
|
| 38 |
+
# Load model
|
| 39 |
model_filename = MODEL_OPTIONS[model_choice]
|
| 40 |
model_path = hf_hub_download(repo_id=HF_REPO_ID, filename=model_filename)
|
| 41 |
+
|
| 42 |
if model_filename in loaded_models:
|
| 43 |
model = loaded_models[model_filename]
|
| 44 |
else:
|
| 45 |
model = models.CellposeModel(gpu=True, pretrained_model=model_path)
|
| 46 |
loaded_models[model_filename] = model
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
# Run Cellpose
|
| 49 |
masks, flows, styles = model.eval(image_np, diameter=None, channels=[0, 0])
|
| 50 |
+
|
| 51 |
+
# Count cells
|
| 52 |
+
cell_count = len(np.unique(masks)) - 1 # background = 0
|
| 53 |
|
| 54 |
# Overlay visualization
|
| 55 |
overlay = image_np.copy().astype(np.float32)
|
|
|
|
| 65 |
|
| 66 |
return cell_count, overlay_image
|
| 67 |
|
| 68 |
+
# Build Gradio app using Blocks
|
| 69 |
with gr.Blocks() as demo:
|
| 70 |
+
gr.Markdown("## 🧪 Cell Counter with Cellpose")
|
| 71 |
+
|
| 72 |
+
with gr.Row():
|
| 73 |
+
image_input = gr.Image(
|
| 74 |
+
type="pil",
|
| 75 |
+
label="Microscopy Image",
|
| 76 |
+
tool="select"
|
| 77 |
+
)
|
| 78 |
+
model_dropdown = gr.Dropdown(
|
| 79 |
+
choices=list(MODEL_OPTIONS.keys()),
|
| 80 |
+
label="Select Model",
|
| 81 |
+
value="Hemocytometer Model"
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
run_button = gr.Button("Run Segmentation")
|
| 85 |
|
| 86 |
with gr.Row():
|
| 87 |
+
count_output = gr.Number(label="Number of Cells")
|
| 88 |
+
overlay_output = gr.Image(type="pil", label="Segmented Overlay")
|
| 89 |
+
|
| 90 |
+
run_button.click(
|
| 91 |
+
fn=segment_and_count,
|
| 92 |
+
inputs=[image_input, model_dropdown],
|
| 93 |
+
outputs=[count_output, overlay_output]
|
| 94 |
+
)
|
| 95 |
|
| 96 |
+
if __name__ == "__main__":
|
| 97 |
+
demo.launch()
|
| 98 |
|