LiangLabUMB commited on
Commit
aa6df7a
·
verified ·
1 Parent(s): 04626a1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -22
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import gradio as gr
 
2
  from cellpose import models
3
  import numpy as np
4
  import cv2
@@ -8,52 +9,68 @@ from PIL import Image
8
  import io
9
  from huggingface_hub import hf_hub_download
10
 
 
11
  HF_REPO_ID = "myang4218/cellposemodel"
12
  HF_MODEL_FILENAME = "cellposemodel.npy"
13
-
14
-
15
-
16
  model_path = hf_hub_download(repo_id=HF_REPO_ID, filename=HF_MODEL_FILENAME)
17
  print(f"Model downloaded to: {model_path}")
18
 
19
- # Load pretrained Cellpose model (nuclei or cyto depending on your data)
20
- model = models.CellposeModel(gpu=True, pretrained_model=model_path)
21
-
22
-
23
  def segment_and_count(image):
 
 
 
24
  # Convert PIL Image to numpy array
25
  image_np = np.array(image)
26
-
27
  # If grayscale, convert to RGB
28
  if len(image_np.shape) == 2:
29
  image_np = cv2.cvtColor(image_np, cv2.COLOR_GRAY2RGB)
30
-
 
 
 
31
  # Run Cellpose
32
  masks, flows, styles = model.eval(image_np, diameter=None, channels=[0, 0])
33
-
34
  # Count unique cells
35
  cell_count = len(np.unique(masks)) - 1 # subtract 1 for background
36
-
37
- # Overlay mask for display
38
-
39
- overlay = image_np.copy()
40
- alpha = 0.3 # Transparency factor (0.0 - 1.0, where 1.0 is fully opaque)
41
-
42
- # Blend the red color with the original image where mask > 0
43
- overlay[masks > 0] = (1 - alpha) * overlay[masks > 0] + alpha * np.array([255, 0, 0])
44
-
 
 
 
 
 
 
 
 
 
 
 
 
45
  # Convert result to PIL Image for output
46
  overlay_image = Image.fromarray(overlay)
47
-
48
  return cell_count, overlay_image
49
 
50
  # Gradio interface
51
  demo = gr.Interface(
52
  fn=segment_and_count,
53
  inputs=gr.Image(type="pil"),
54
- outputs=[gr.Number(label="Number of Cells"), gr.Image(type="pil", label="Segmented Overlay")],
 
 
 
55
  title="Cell Counter with Cellpose",
56
- description="Upload a microscopy image and count the number of cells using Cellpose segmentation."
57
  )
58
 
59
  if __name__ == "__main__":
 
1
  import gradio as gr
2
+ import spaces
3
  from cellpose import models
4
  import numpy as np
5
  import cv2
 
9
  import io
10
  from huggingface_hub import hf_hub_download
11
 
12
+ # Download model at startup
13
  HF_REPO_ID = "myang4218/cellposemodel"
14
  HF_MODEL_FILENAME = "cellposemodel.npy"
 
 
 
15
  model_path = hf_hub_download(repo_id=HF_REPO_ID, filename=HF_MODEL_FILENAME)
16
  print(f"Model downloaded to: {model_path}")
17
 
18
+ @spaces.GPU
 
 
 
19
  def segment_and_count(image):
20
+ # Initialize model inside the GPU-decorated function
21
+ model = models.CellposeModel(gpu=True, pretrained_model=model_path)
22
+
23
  # Convert PIL Image to numpy array
24
  image_np = np.array(image)
25
+
26
  # If grayscale, convert to RGB
27
  if len(image_np.shape) == 2:
28
  image_np = cv2.cvtColor(image_np, cv2.COLOR_GRAY2RGB)
29
+ elif len(image_np.shape) == 3 and image_np.shape[2] == 4:
30
+ # Handle RGBA images
31
+ image_np = cv2.cvtColor(image_np, cv2.COLOR_RGBA2RGB)
32
+
33
  # Run Cellpose
34
  masks, flows, styles = model.eval(image_np, diameter=None, channels=[0, 0])
35
+
36
  # Count unique cells
37
  cell_count = len(np.unique(masks)) - 1 # subtract 1 for background
38
+
39
+ # Create better overlay visualization
40
+ overlay = image_np.copy().astype(np.float32)
41
+
42
+ # Create colored mask overlay
43
+ if masks.max() > 0:
44
+ # Generate random colors for each cell
45
+ np.random.seed(42) # For reproducible colors
46
+ colors = np.random.randint(0, 255, size=(masks.max() + 1, 3))
47
+ colors[0] = [0, 0, 0] # Background stays black
48
+
49
+ # Create colored overlay
50
+ colored_mask = colors[masks]
51
+
52
+ # Blend with original image
53
+ alpha = 0.4
54
+ overlay = (1 - alpha) * overlay + alpha * colored_mask
55
+
56
+ # Ensure values are in valid range and convert to uint8
57
+ overlay = np.clip(overlay, 0, 255).astype(np.uint8)
58
+
59
  # Convert result to PIL Image for output
60
  overlay_image = Image.fromarray(overlay)
61
+
62
  return cell_count, overlay_image
63
 
64
  # Gradio interface
65
  demo = gr.Interface(
66
  fn=segment_and_count,
67
  inputs=gr.Image(type="pil"),
68
+ outputs=[
69
+ gr.Number(label="Number of Cells"),
70
+ gr.Image(type="pil", label="Segmented Overlay")
71
+ ],
72
  title="Cell Counter with Cellpose",
73
+ description="Upload a microscopy image and count the number of cells using Cellpose segmentation.",
74
  )
75
 
76
  if __name__ == "__main__":