Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,21 +16,44 @@ MODEL_OPTIONS = {
|
|
| 16 |
loaded_models = {}
|
| 17 |
|
| 18 |
@spaces.GPU
|
| 19 |
-
def segment_and_count(
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
model_filename = MODEL_OPTIONS[model_choice]
|
| 22 |
model_path = hf_hub_download(repo_id=HF_REPO_ID, filename=model_filename)
|
| 23 |
-
|
| 24 |
if model_filename in loaded_models:
|
| 25 |
model = loaded_models[model_filename]
|
| 26 |
else:
|
| 27 |
model = models.CellposeModel(gpu=True, pretrained_model=model_path)
|
| 28 |
loaded_models[model_filename] = model
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
image_np = np.array(edited_image)
|
| 32 |
-
|
| 33 |
-
# If grayscale, convert to RGB
|
| 34 |
if len(image_np.shape) == 2:
|
| 35 |
image_np = cv2.cvtColor(image_np, cv2.COLOR_GRAY2RGB)
|
| 36 |
elif len(image_np.shape) == 3 and image_np.shape[2] == 4:
|
|
@@ -39,7 +62,7 @@ def segment_and_count(edited_image, model_choice):
|
|
| 39 |
# Run Cellpose
|
| 40 |
masks, flows, styles = model.eval(image_np, diameter=None, channels=[0, 0])
|
| 41 |
|
| 42 |
-
# Count
|
| 43 |
cell_count = len(np.unique(masks)) - 1
|
| 44 |
|
| 45 |
# Overlay visualization
|
|
@@ -57,6 +80,7 @@ def segment_and_count(edited_image, model_choice):
|
|
| 57 |
|
| 58 |
return cell_count, overlay_image
|
| 59 |
|
|
|
|
| 60 |
# Gradio UI
|
| 61 |
with gr.Blocks() as demo:
|
| 62 |
gr.Markdown("## 🧪 Cell Counter with Cellpose + ImageEditor")
|
|
|
|
| 16 |
loaded_models = {}
|
| 17 |
|
| 18 |
@spaces.GPU
|
| 19 |
+
def segment_and_count(edited_input, model_choice):
|
| 20 |
+
if edited_input is None:
|
| 21 |
+
return 0, None
|
| 22 |
+
|
| 23 |
+
# Handle Gradio ImageEditor output
|
| 24 |
+
if isinstance(edited_input, dict) and "image" in edited_input:
|
| 25 |
+
image = edited_input["image"]
|
| 26 |
+
|
| 27 |
+
# Optional: apply cropping manually if edit info exists
|
| 28 |
+
edit_info = edited_input.get("edit", {})
|
| 29 |
+
crop_box = edit_info.get("crop")
|
| 30 |
+
if crop_box:
|
| 31 |
+
x = int(crop_box["x"])
|
| 32 |
+
y = int(crop_box["y"])
|
| 33 |
+
w = int(crop_box["width"])
|
| 34 |
+
h = int(crop_box["height"])
|
| 35 |
+
image = image.crop((x, y, x + w, y + h))
|
| 36 |
+
else:
|
| 37 |
+
image = edited_input # fallback, already a PIL Image
|
| 38 |
+
|
| 39 |
+
# Convert to NumPy
|
| 40 |
+
image_np = np.array(image)
|
| 41 |
+
|
| 42 |
+
# Validate shape
|
| 43 |
+
if image_np.size == 0 or len(image_np.shape) < 2:
|
| 44 |
+
return 0, None
|
| 45 |
+
|
| 46 |
+
# Load model
|
| 47 |
model_filename = MODEL_OPTIONS[model_choice]
|
| 48 |
model_path = hf_hub_download(repo_id=HF_REPO_ID, filename=model_filename)
|
| 49 |
+
|
| 50 |
if model_filename in loaded_models:
|
| 51 |
model = loaded_models[model_filename]
|
| 52 |
else:
|
| 53 |
model = models.CellposeModel(gpu=True, pretrained_model=model_path)
|
| 54 |
loaded_models[model_filename] = model
|
| 55 |
|
| 56 |
+
# Ensure image is RGB
|
|
|
|
|
|
|
|
|
|
| 57 |
if len(image_np.shape) == 2:
|
| 58 |
image_np = cv2.cvtColor(image_np, cv2.COLOR_GRAY2RGB)
|
| 59 |
elif len(image_np.shape) == 3 and image_np.shape[2] == 4:
|
|
|
|
| 62 |
# Run Cellpose
|
| 63 |
masks, flows, styles = model.eval(image_np, diameter=None, channels=[0, 0])
|
| 64 |
|
| 65 |
+
# Count cells
|
| 66 |
cell_count = len(np.unique(masks)) - 1
|
| 67 |
|
| 68 |
# Overlay visualization
|
|
|
|
| 80 |
|
| 81 |
return cell_count, overlay_image
|
| 82 |
|
| 83 |
+
|
| 84 |
# Gradio UI
|
| 85 |
with gr.Blocks() as demo:
|
| 86 |
gr.Markdown("## 🧪 Cell Counter with Cellpose + ImageEditor")
|