LiangLabUMB commited on
Commit
a3c88f8
·
verified ·
1 Parent(s): b51bd25

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +432 -0
app.py ADDED
@@ -0,0 +1,432 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ 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
+ MODEL_OPTIONS = {
14
+ "Hemocytometer Model": "hemocytometermodel.npy",
15
+ "General Model": "generalmodel.npy"
16
+ }
17
+
18
+ loaded_models = {}
19
+
20
+ # Removed the debug_editor_data function completely as per request
21
+
22
+ def extract_region_from_editor(editor_data):
23
+ """Extract the selected region from ImageEditor data"""
24
+ if editor_data is None:
25
+ return None, None
26
+
27
+ # ImageEditor can return different formats depending on version
28
+ # Let's handle the most common cases
29
+
30
+ if isinstance(editor_data, dict):
31
+ # Case 1: Dictionary with 'background' and 'layers'
32
+ background = editor_data.get('background')
33
+ layers = editor_data.get('layers', [])
34
+
35
+ if background is None:
36
+ return None, None
37
+
38
+ # Convert background to numpy array
39
+ background_np = np.array(background)
40
+
41
+ # If there are drawn layers, try to extract selection
42
+ if layers and len(layers) > 0:
43
+ # Get the first layer
44
+ selection_layer = layers[0]
45
+
46
+ # Convert to numpy array
47
+ selection_np = np.array(selection_layer)
48
+
49
+ # Find non-transparent/non-black pixels as selection
50
+ if len(selection_np.shape) == 3:
51
+ # For RGB, look for non-black pixels
52
+ if selection_np.shape[2] == 4: # RGBA
53
+ # Use alpha channel
54
+ mask = selection_np[:, :, 3] > 0
55
+ else: # RGB
56
+ # Use non-black pixels
57
+ mask = np.any(selection_np > 0, axis=2)
58
+ else:
59
+ # Grayscale
60
+ mask = selection_np > 0
61
+
62
+ # Find bounding box of the selection
63
+ coords = np.where(mask)
64
+ if len(coords[0]) > 0:
65
+ y_min, y_max = coords[0].min(), coords[0].max()
66
+ x_min, x_max = coords[1].min(), coords[1].max()
67
+
68
+ # Add some padding to ensure we don't get tiny regions
69
+ pad = 5
70
+ h, w = background_np.shape[:2]
71
+ y_min = max(0, y_min - pad)
72
+ y_max = min(h, y_max + pad)
73
+ x_min = max(0, x_min - pad)
74
+ x_max = min(w, x_max + pad)
75
+
76
+ # Extract the region
77
+ region = background_np[y_min:y_max+1, x_min:x_max+1]
78
+ return region, (x_min, y_min, x_max, y_max)
79
+
80
+ # If no selection, return the full image
81
+ return background_np, None
82
+
83
+ else:
84
+ # Case 2: Direct PIL Image
85
+ if hasattr(editor_data, 'size'): # Check if it's a PIL Image
86
+ image_np = np.array(editor_data)
87
+ return image_np, None
88
+ else:
89
+ return None, None
90
+
91
+ def crop_image_with_coords(image_np, coords):
92
+ """Crop image using provided coordinates"""
93
+ if coords is None:
94
+ return image_np
95
+
96
+ x_min, y_min, x_max, y_max = coords
97
+ return image_np[y_min:y_max+1, x_min:x_max+1]
98
+
99
+ def classify_cells_by_blueness(image_np, masks, blue_threshold):
100
+ """
101
+ Classify cells as dead (blue) or alive based on single blueness metric
102
+
103
+ Args:
104
+ image_np: RGB image array
105
+ masks: Cellpose segmentation masks
106
+ blue_threshold: Single threshold value (0-100) for blueness detection
107
+
108
+ Returns:
109
+ dead_count, alive_count, colored_overlay
110
+ """
111
+ # Convert RGB to HSV
112
+ hsv = cv2.cvtColor(image_np, cv2.COLOR_RGB2HSV)
113
+
114
+ # Calculate blueness index for each pixel
115
+ hue = hsv[:, :, 0].astype(np.float32)
116
+ saturation = hsv[:, :, 1].astype(np.float32)
117
+
118
+ # Hue score: peaks around 115 (blue in HSV), drops off towards edges
119
+ # Handle hue wrap-around for blue detection (100-130 range)
120
+ hue_distance = np.minimum(np.abs(hue - 115), 180 - np.abs(hue - 115))
121
+ hue_score = np.maximum(0, 1 - hue_distance / 65) # 65 gives good blue range
122
+
123
+ # Combine hue proximity with saturation intensity
124
+ blueness = hue_score * (saturation / 255.0)
125
+
126
+ # Convert threshold from 0-100 to 0-1 scale
127
+ threshold = blue_threshold / 100.0
128
+
129
+ # Get unique cell IDs (excluding background)
130
+ cell_ids = np.unique(masks)
131
+ cell_ids = cell_ids[cell_ids > 0] # Remove background (0)
132
+
133
+ dead_cells = []
134
+ alive_cells = []
135
+
136
+ # Classify each cell
137
+ for cell_id in cell_ids:
138
+ # Get mask for this specific cell
139
+ cell_mask = (masks == cell_id)
140
+
141
+ # Calculate average blueness for this cell
142
+ cell_blueness = np.mean(blueness[cell_mask])
143
+
144
+ # Classify based on threshold
145
+ if cell_blueness > threshold:
146
+ dead_cells.append(cell_id)
147
+ else:
148
+ alive_cells.append(cell_id)
149
+
150
+ # Create colored overlay
151
+ overlay = image_np.copy()
152
+
153
+ # Color dead cells red, alive cells green
154
+ for cell_id in dead_cells:
155
+ cell_mask = (masks == cell_id)
156
+ overlay[cell_mask] = [255, 0, 0] # Red for dead
157
+
158
+ for cell_id in alive_cells:
159
+ cell_mask = (masks == cell_id)
160
+ overlay[cell_mask] = [0, 255, 0] # Green for alive
161
+
162
+ # Blend with original image
163
+ alpha = 0.4
164
+ overlay = (1 - alpha) * image_np + alpha * overlay
165
+ overlay = np.clip(overlay, 0, 255).astype(np.uint8)
166
+
167
+ return len(dead_cells), len(alive_cells), overlay
168
+
169
+ @spaces.GPU
170
+ def segment_and_count_viability(editor_data, model_choice, blue_threshold, crop_coords=None):
171
+ """
172
+ Segment cells and analyze viability using single blueness threshold
173
+ """
174
+ try:
175
+ # Load the model
176
+ model_filename = MODEL_OPTIONS[model_choice]
177
+ model_path = hf_hub_download(repo_id=HF_REPO_ID, filename=model_filename)
178
+
179
+ if model_filename in loaded_models:
180
+ model = loaded_models[model_filename]
181
+ else:
182
+ model = models.CellposeModel(gpu=True, pretrained_model=model_path)
183
+ loaded_models[model_filename] = model
184
+
185
+ # Extract region from editor
186
+ region_np, region_coords = extract_region_from_editor(editor_data)
187
+
188
+ if region_np is None:
189
+ return 0, 0, 0, 0.0, None, f"No image provided."
190
+
191
+ # If manual crop coordinates are provided, use them instead
192
+ if crop_coords and crop_coords.strip():
193
+ try:
194
+ coords = [int(x.strip()) for x in crop_coords.split(',')]
195
+ if len(coords) == 4:
196
+ x_min, y_min, x_max, y_max = coords
197
+ # Ensure coordinates are within image bounds
198
+ h, w = region_np.shape[:2]
199
+ x_min = max(0, min(x_min, w-1))
200
+ y_min = max(0, min(y_min, h-1))
201
+ x_max = max(x_min+1, min(x_max, w))
202
+ y_max = max(y_min+1, min(y_max, h))
203
+
204
+ region_np = region_np[y_min:y_max, x_min:x_max]
205
+ region_coords = (x_min, y_min, x_max, y_max)
206
+ except ValueError:
207
+ pass # Invalid coordinates, continue with current region
208
+
209
+ # If grayscale, convert to RGB
210
+ if len(region_np.shape) == 2:
211
+ region_np = cv2.cvtColor(region_np, cv2.COLOR_GRAY2RGB)
212
+ elif len(region_np.shape) == 3 and region_np.shape[2] == 4:
213
+ # Handle RGBA images
214
+ region_np = cv2.cvtColor(region_np, cv2.COLOR_RGBA2RGB)
215
+
216
+ # Run Cellpose on the selected region
217
+ masks, flows, styles = model.eval(region_np, diameter=None, channels=[0, 0])
218
+
219
+ # Classify cells by blueness
220
+ dead_count, alive_count, viability_overlay = classify_cells_by_blueness(
221
+ region_np, masks, blue_threshold
222
+ )
223
+
224
+ total_count = dead_count + alive_count
225
+ viability_percent = (alive_count / total_count * 100) if total_count > 0 else 0.0
226
+
227
+ # Convert result to PIL Image for output
228
+ overlay_image = Image.fromarray(viability_overlay)
229
+
230
+ # Create info message
231
+ info_msg = f"Total cells: {total_count}\nAlive cells: {alive_count}\nDead cells: {dead_count}\nViability: {viability_percent:.1f}%\nBlue threshold: {blue_threshold}%"
232
+ if region_coords:
233
+ info_msg += f"\nProcessed region: {region_coords[0]},{region_coords[1]} to {region_coords[2]},{region_coords[3]}"
234
+
235
+ return total_count, alive_count, dead_count, viability_percent, overlay_image, info_msg
236
+
237
+ except Exception as e:
238
+ return 0, 0, 0, 0.0, None, f"Error occurred: {str(e)}"
239
+
240
+ @spaces.GPU
241
+ def segment_with_coords_viability(image, model_choice, blue_threshold, crop_coords):
242
+ """Segment using regular image input with manual coordinate specification and viability analysis"""
243
+ if image is None:
244
+ return 0, 0, 0, 0.0, None, "No image provided"
245
+
246
+ try:
247
+ # Load the model
248
+ model_filename = MODEL_OPTIONS[model_choice]
249
+ model_path = hf_hub_download(repo_id=HF_REPO_ID, filename=model_filename)
250
+
251
+ if model_filename in loaded_models:
252
+ model = loaded_models[model_filename]
253
+ else:
254
+ model = models.CellposeModel(gpu=True, pretrained_model=model_path)
255
+ loaded_models[model_filename] = model
256
+
257
+ # Convert PIL Image to numpy array
258
+ image_np = np.array(image)
259
+
260
+ # Apply crop if coordinates are provided
261
+ if crop_coords and crop_coords.strip():
262
+ try:
263
+ coords = [int(x.strip()) for x in crop_coords.split(',')]
264
+ if len(coords) == 4:
265
+ x_min, y_min, x_max, y_max = coords
266
+ # Ensure coordinates are within image bounds
267
+ h, w = image_np.shape[:2]
268
+ x_min = max(0, min(x_min, w-1))
269
+ y_min = max(0, min(y_min, h-1))
270
+ x_max = max(x_min+1, min(x_max, w))
271
+ y_max = max(y_min+1, min(y_max, h))
272
+
273
+ image_np = image_np[y_min:y_max, x_min:x_max]
274
+ except ValueError:
275
+ pass # Invalid coordinates, use full image
276
+
277
+ # Process image format
278
+ if len(image_np.shape) == 2:
279
+ image_np = cv2.cvtColor(image_np, cv2.COLOR_GRAY2RGB)
280
+ elif len(image_np.shape) == 3 and image_np.shape[2] == 4:
281
+ image_np = cv2.cvtColor(image_np, cv2.COLOR_RGBA2RGB)
282
+
283
+ # Run Cellpose
284
+ masks, flows, styles = model.eval(image_np, diameter=None, channels=[0, 0])
285
+
286
+ # Classify cells by blueness
287
+ dead_count, alive_count, viability_overlay = classify_cells_by_blueness(
288
+ image_np, masks, blue_threshold
289
+ )
290
+
291
+ total_count = dead_count + alive_count
292
+ viability_percent = (alive_count / total_count * 100) if total_count > 0 else 0.0
293
+
294
+ overlay_image = Image.fromarray(viability_overlay)
295
+
296
+ info_msg = f"Total cells: {total_count}\nAlive cells: {alive_count}\nDead cells: {dead_count}\nViability: {viability_percent:.1f}%\nBlue threshold: {blue_threshold}%"
297
+ if crop_coords:
298
+ info_msg += f"\nProcessed with coordinates: {crop_coords}"
299
+
300
+ return total_count, alive_count, dead_count, viability_percent, overlay_image, info_msg
301
+
302
+ except Exception as e:
303
+ return 0, 0, 0, 0.0, None, f"Error occurred: {str(e)}"
304
+
305
+ # Create the Gradio interface with tabs for different input methods
306
+ with gr.Blocks(title="Cell Viability Counter with Trypan Blue Analysis") as demo:
307
+ gr.Markdown("# Cell Viability Counter with Trypan Blue Analysis")
308
+ gr.Markdown("Upload a trypan blue stained microscopy image and analyze cell viability. Dead cells appear blue, alive cells are unstained.")
309
+
310
+ with gr.Tab("Image Editor (Draw Selection)"):
311
+ gr.Markdown("Use the drawing tools to select a region of the image for segmentation and viability analysis.")
312
+
313
+ with gr.Row():
314
+ with gr.Column():
315
+ image_editor = gr.ImageEditor(
316
+ label="Draw selection on image",
317
+ type="pil",
318
+ brush=gr.Brush(colors=["#ff0000"], color_mode="fixed", default_size=20),
319
+ eraser=gr.Eraser(default_size=20)
320
+ )
321
+
322
+ model_dropdown1 = gr.Dropdown(
323
+ choices=list(MODEL_OPTIONS.keys()),
324
+ label="Select Model",
325
+ value="Hemocytometer Model"
326
+ )
327
+
328
+ # Single blue threshold control
329
+ gr.Markdown("### Trypan Blue Threshold")
330
+ blue_threshold1 = gr.Slider(
331
+ 0, 100, value=25, step=1,
332
+ label="Blue Threshold (%)",
333
+ info="Higher values = more selective for blue cells"
334
+ )
335
+
336
+ segment_btn1 = gr.Button("Analyze Viability", variant="primary")
337
+
338
+ with gr.Column():
339
+ with gr.Row():
340
+ total_count_output1 = gr.Number(label="Total Cells")
341
+ alive_count_output1 = gr.Number(label="Alive Cells")
342
+ dead_count_output1 = gr.Number(label="Dead Cells")
343
+
344
+ viability_output1 = gr.Number(label="Viability (%)", precision=1)
345
+ overlay_output1 = gr.Image(type="pil", label="Viability Analysis (Green=Alive, Red=Dead)")
346
+ info_output1 = gr.Textbox(label="Analysis Results")
347
+
348
+ segment_btn1.click(
349
+ fn=segment_and_count_viability,
350
+ inputs=[image_editor, model_dropdown1, blue_threshold1],
351
+ outputs=[total_count_output1, alive_count_output1, dead_count_output1, viability_output1, overlay_output1, info_output1]
352
+ )
353
+
354
+ with gr.Tab("Manual Coordinates"):
355
+ gr.Markdown("Upload an image and specify coordinates manually (format: x_min,y_min,x_max,y_max)")
356
+
357
+ with gr.Row():
358
+ with gr.Column():
359
+ image_input = gr.Image(type="pil", label="Trypan Blue Stained Image")
360
+
361
+ model_dropdown2 = gr.Dropdown(
362
+ choices=list(MODEL_OPTIONS.keys()),
363
+ label="Select Model",
364
+ value="Hemocytometer Model"
365
+ )
366
+
367
+ coord_input = gr.Textbox(
368
+ label="Crop Coordinates (optional)",
369
+ placeholder="e.g., 100,100,400,400",
370
+ info="Format: x_min,y_min,x_max,y_max"
371
+ )
372
+
373
+ # Single blue threshold control
374
+ gr.Markdown("### Trypan Blue Threshold")
375
+ blue_threshold2 = gr.Slider(
376
+ 0, 100, value=25, step=1,
377
+ label="Blue Threshold (%)",
378
+ info="Higher values = more selective for blue cells"
379
+ )
380
+
381
+ segment_btn2 = gr.Button("Analyze Viability", variant="primary")
382
+
383
+ with gr.Column():
384
+ with gr.Row():
385
+ total_count_output2 = gr.Number(label="Total Cells")
386
+ alive_count_output2 = gr.Number(label="Alive Cells")
387
+ dead_count_output2 = gr.Number(label="Dead Cells")
388
+
389
+ viability_output2 = gr.Number(label="Viability (%)", precision=1)
390
+ overlay_output2 = gr.Image(type="pil", label="Viability Analysis (Green=Alive, Red=Dead)")
391
+ info_output2 = gr.Textbox(label="Analysis Results")
392
+
393
+ segment_btn2.click(
394
+ fn=segment_with_coords_viability,
395
+ inputs=[image_input, model_dropdown2, blue_threshold2, coord_input],
396
+ outputs=[total_count_output2, alive_count_output2, dead_count_output2, viability_output2, overlay_output2, info_output2]
397
+ )
398
+
399
+ # Add usage instructions
400
+ with gr.Tab("Usage Instructions"):
401
+ gr.Markdown("""
402
+ ## How to Use This Tool
403
+
404
+ ### 1. Image Preparation
405
+ - Upload a trypan blue stained microscopy image
406
+ - Dead cells should appear blue, alive cells should be unstained (clear)
407
+
408
+ ### 2. Blue Threshold Adjustment
409
+ - **Single Slider (0-100%)**: Adjusts sensitivity to blue coloration
410
+ - **Lower values (10-20%)**: More sensitive, detects lightly stained cells
411
+ - **Higher values (30-50%)**: More selective, only detects strongly blue cells
412
+ - **Default (25%)**: Works well for most standard trypan blue staining
413
+
414
+ ### 3. How the Blue Detection Works
415
+ - Analyzes hue (color) and saturation (intensity) in each cell
416
+ - Focuses on blue range around 115° in HSV color space
417
+ - Combines color proximity with saturation strength
418
+ - Calculates average \"blueness\" for each detected cell
419
+
420
+ ### 4. Output
421
+ - **Green overlay**: Live cells
422
+ - **Red overlay**: Dead cells
423
+ - **Viability %**: (Alive cells / Total cells) × 100
424
+
425
+ ### 5. Tips
426
+ - Use consistent lighting and staining protocols for best results
427
+ - Adjust thresholds if cells are misclassified
428
+ - The tool works best with clear contrast between stained and unstained cells
429
+ """)
430
+
431
+ if __name__ == "__main__":
432
+ demo.launch()