LiangLabUMB commited on
Commit
f45623e
·
verified ·
1 Parent(s): d10d4ca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -1
app.py CHANGED
@@ -172,6 +172,16 @@ def run_segmentation_editor(editor_data, model_choice):
172
  if region_np is None:
173
  return 0, None, f"No image provided.", gr.update(visible=False), None, None, 0.0
174
 
 
 
 
 
 
 
 
 
 
 
175
  # Process image format to RGB
176
  if len(region_np.shape) == 2:
177
  processed_image_np = cv2.cvtColor(region_np, cv2.COLOR_GRAY2RGB)
@@ -229,6 +239,16 @@ def run_segmentation_manual(image, model_choice, crop_coords):
229
  loaded_models[model_filename] = model
230
 
231
  image_np = np.array(image)
 
 
 
 
 
 
 
 
 
 
232
 
233
  # Apply crop if coordinates are provided
234
  if crop_coords and crop_coords.strip():
@@ -310,9 +330,35 @@ def update_viability_realtime(blue_threshold, stored_masks, stored_image_np):
310
  except Exception as e:
311
  return None, 0, 0, 0.0, f"Error updating viability: {str(e)}"
312
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
313
 
314
  # Create the Gradio interface
315
- with gr.Blocks(title="Cell Viability Counter with Trypan Blue Analysis", theme=gr.themes.Soft()) as demo:
 
 
 
 
316
  gr.Markdown("# Cell Viability Counter with Trypan Blue Analysis")
317
  gr.Markdown("Upload a trypan blue stained microscopy image and analyze cell viability. Dead cells appear blue, alive cells are unstained. For accurate cell confluency, crop the image to display only desired area.")
318
 
 
172
  if region_np is None:
173
  return 0, None, f"No image provided.", gr.update(visible=False), None, None, 0.0
174
 
175
+ # Resize large images to prevent crashes
176
+ max_size = 1024
177
+ if region_np.shape[0] > max_size or region_np.shape[1] > max_size:
178
+ h, w = region_np.shape[:2]
179
+ if h > w:
180
+ new_h, new_w = max_size, int(w * max_size / h)
181
+ else:
182
+ new_h, new_w = int(h * max_size / w), max_size
183
+ region_np = cv2.resize(region_np, (new_w, new_h), interpolation=cv2.INTER_AREA)
184
+
185
  # Process image format to RGB
186
  if len(region_np.shape) == 2:
187
  processed_image_np = cv2.cvtColor(region_np, cv2.COLOR_GRAY2RGB)
 
239
  loaded_models[model_filename] = model
240
 
241
  image_np = np.array(image)
242
+
243
+ # Resize large images to prevent crashes
244
+ max_size = 1024
245
+ if image_np.shape[0] > max_size or image_np.shape[1] > max_size:
246
+ h, w = image_np.shape[:2]
247
+ if h > w:
248
+ new_h, new_w = max_size, int(w * max_size / h)
249
+ else:
250
+ new_h, new_w = int(h * max_size / w), max_size
251
+ image_np = cv2.resize(image_np, (new_w, new_h), interpolation=cv2.INTER_AREA)
252
 
253
  # Apply crop if coordinates are provided
254
  if crop_coords and crop_coords.strip():
 
330
  except Exception as e:
331
  return None, 0, 0, 0.0, f"Error updating viability: {str(e)}"
332
 
333
+ # PWA Head HTML - Updated for nested zip structure
334
+ pwa_head = """
335
+ <link rel="manifest" href="/file=static.zip/static/manifest.json">
336
+ <meta name="apple-mobile-web-app-capable" content="yes">
337
+ <meta name="apple-mobile-web-app-status-bar-style" content="default">
338
+ <meta name="apple-mobile-web-app-title" content="Cellpose Cell Counter">
339
+ <meta name="viewport" content="width=device-width, initial-scale=1">
340
+ <link rel="apple-touch-icon" href="/file=favicon.png">
341
+ <link rel="icon" type="image/png" sizes="192x192" href="/file=favicon.png">
342
+ <script>
343
+ if ('serviceWorker' in navigator) {
344
+ window.addEventListener('load', function() {
345
+ navigator.serviceWorker.register('/file=static.zip/static/service-worker.js')
346
+ .then(function(registration) {
347
+ console.log('ServiceWorker registration successful');
348
+ }, function(err) {
349
+ console.log('ServiceWorker registration failed: ', err);
350
+ });
351
+ });
352
+ }
353
+ </script>
354
+ """
355
 
356
  # Create the Gradio interface
357
+ with gr.Blocks(
358
+ title="Cell Viability Counter with Trypan Blue Analysis",
359
+ theme=gr.themes.Soft(),
360
+ head=pwa_head # Add PWA head elements
361
+ ) as demo:
362
  gr.Markdown("# Cell Viability Counter with Trypan Blue Analysis")
363
  gr.Markdown("Upload a trypan blue stained microscopy image and analyze cell viability. Dead cells appear blue, alive cells are unstained. For accurate cell confluency, crop the image to display only desired area.")
364