LiangLabUMB commited on
Commit
e43377b
·
verified ·
1 Parent(s): bd317c5

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -149
app.py DELETED
@@ -1,149 +0,0 @@
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
- loaded_models = {}
18
-
19
- # Global variable for region coordinates (can remain global if only for info display)
20
- current_region_coords = None
21
-
22
- """
23
- # Commented out debugging function
24
- def debug_editor_data(editor_data):
25
- \"\"\"Debug function to understand the structure of ImageEditor data\"\"\"
26
- if editor_data is None:
27
- return "No data provided"
28
- info = f"Type: {type(editor_data)}\n"
29
- if isinstance(editor_data, dict):
30
- info += f"Keys: {list(editor_data.keys())}\n"
31
- for key, value in editor_data.items():
32
- info += f" {key}: {type(value)}\n"
33
- if key == 'layers' and value:
34
- info += f" Layers count: {len(value)}\n"
35
- for i, layer in enumerate(value):
36
- info += f" Layer {i}: {type(layer)}\n"
37
- if hasattr(layer, 'size'):
38
- info += f" Size: {layer.size}\n"
39
- info += f" Mode: {layer.mode}\n"
40
- else: # Direct PIL Image
41
- if hasattr(editor_data, 'size'):
42
- info += f"Size: {editor_data.size}\n"
43
- info += f"Mode: {editor_data.mode}\n"
44
- return info
45
- """
46
-
47
- def extract_region_from_editor(editor_data):
48
- """Extract the selected region from ImageEditor data"""
49
- if editor_data is None:
50
- # print("DEBUG: extract_region_from_editor - editor_data is None")
51
- return None, None
52
-
53
- # print(f"DEBUG: extract_region_from_editor - editor_data type: {type(editor_data)}")
54
-
55
- if isinstance(editor_data, dict):
56
- background = editor_data.get('background')
57
- layers = editor_data.get('layers', [])
58
-
59
- if background is None:
60
- # print("DEBUG: extract_region_from_editor - background is None")
61
- return None, None
62
-
63
- background_np = np.array(background)
64
- # print(f"DEBUG: extract_region_from_editor - background_np shape: {background_np.shape}")
65
-
66
- if layers and len(layers) > 0:
67
- selection_layer = layers[0]
68
- selection_np = np.array(selection_layer)
69
- # print(f"DEBUG: extract_region_from_editor - selection_np shape: {selection_np.shape}")
70
-
71
- if len(selection_np.shape) == 3:
72
- if selection_np.shape[2] == 4: # RGBA
73
- mask = selection_np[:, :, 3] > 0
74
- else: # RGB
75
- mask = np.any(selection_np > 0, axis=2)
76
- else:
77
- mask = selection_np > 0
78
-
79
- coords = np.where(mask)
80
- if len(coords[0]) > 0:
81
- y_min, y_max = coords[0].min(), coords[0].max()
82
- x_min, x_max = coords[1].min(), coords[1].max()
83
-
84
- pad = 5
85
- h, w = background_np.shape[:2]
86
- y_min = max(0, y_min - pad)
87
- y_max = min(h, y_max + pad)
88
- x_min = max(0, x_min - pad)
89
- x_max = min(w, x_max + pad)
90
-
91
- region = background_np[y_min:y_max+1, x_min:x_max+1]
92
- # print(f"DEBUG: extract_region_from_editor - Extracted region shape: {region.shape}")
93
- return region, (x_min, y_min, x_max, y_max)
94
- # print("DEBUG: extract_region_from_editor - No selection or empty selection, returning full background.")
95
- return background_np, None
96
- else: # Assuming it's a direct PIL Image if not dict
97
- if hasattr(editor_data, 'size'):
98
- image_np = np.array(editor_data)
99
- # print(f"DEBUG: extract_region_from_editor - Direct PIL image, shape: {image_np.shape}")
100
- return image_np, None
101
- else:
102
- # print("DEBUG: extract_region_from_editor - editor_data is not dict and no size attribute, returning None.")
103
- return None, None
104
-
105
- def calculate_blue_intensity(image_np, masks):
106
- """Calculate blue intensity for each cell"""
107
- # Ensure image is in correct format
108
- if len(image_np.shape) == 2:
109
- # Convert grayscale to RGB
110
- image_np = cv2.cvtColor(image_np, cv2.COLOR_GRAY2RGB)
111
- elif len(image_np.shape) == 3 and image_np.shape[2] == 4:
112
- # Handle RGBA
113
- image_np = cv2.cvtColor(image_np, cv2.COLOR_RGBA2RGB)
114
-
115
- # Calculate blue intensity for each cell
116
- cell_blue_intensities = {}
117
- unique_cells = np.unique(masks)
118
-
119
- # print(f"DEBUG: Found {len(unique_cells)} unique mask values: {unique_cells}")
120
- # print(f"DEBUG: Image shape: {image_np.shape}, dtype: {image_np.dtype}")
121
- # print(f"DEBUG: Masks shape: {masks.shape}, dtype: {masks.dtype}")
122
-
123
- for cell_id in unique_cells:
124
- if cell_id == 0: # Skip background
125
- continue
126
-
127
- cell_mask = masks == cell_id
128
- cell_pixels = image_np[cell_mask]
129
-
130
- if len(cell_pixels) > 0:
131
- # Calculate mean blue intensity (blue channel is index 2)
132
- blue_intensity = np.mean(cell_pixels[:, 2]) # Blue channel
133
- cell_blue_intensities[cell_id] = blue_intensity
134
- # print(f"DEBUG: Cell {cell_id}: {len(cell_pixels)} pixels, blue intensity: {blue_intensity:.2f}")
135
- # print(f"DEBUG: Total cells with blue intensity: {len(cell_blue_intensities)}")
136
- return cell_blue_intensities
137
-
138
- def create_viability_overlay(image_np, masks, blue_threshold):
139
- """Create overlay showing live/dead cells based on blue threshold"""
140
-
141
- if masks is None or image_np is None:
142
- # print("DEBUG: create_viability_overlay - masks or image_np is None, returning None.")
143
- return None, 0, 0
144
-
145
- # print(f"DEBUG: create_viability_overlay called with threshold: {blue_threshold}")
146
- # print(f"DEBUG: Image shape: {image_np.shape}, Masks shape: {masks.shape}, Masks max: {masks.max()}")
147
-
148
- # Ensure image is RGB
149
- if len(