LiangLabUMB commited on
Commit
4d45408
·
verified ·
1 Parent(s): 16f75c4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -0
app.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ def debug_editor_data(editor_data):
23
+ """Debug function to understand the structure of ImageEditor data"""
24
+ if editor_data is None:
25
+ return "No data provided"
26
+ info = f"Type: {type(editor_data)}\n"
27
+ if isinstance(editor_data, dict):
28
+ info += f"Keys: {list(editor_data.keys())}\n"
29
+ for key, value in editor_data.items():
30
+ info += f" {key}: {type(value)}\n"
31
+ if key == 'layers' and value:
32
+ info += f" Layers count: {len(value)}\n"
33
+ for i, layer in enumerate(value):
34
+ info += f" Layer {i}: {type(layer)}\n"
35
+ if hasattr(layer, 'size'):
36
+ info += f" Size: {layer.size}\n"
37
+ info += f" Mode: {layer.mode}\n"
38
+ else: # Direct PIL Image
39
+ if hasattr(editor_data, 'size'):
40
+ info += f"Size: {editor_data.size}\n"
41
+ info += f"Mode: {editor_data.mode}\n"
42
+ return info
43
+
44
+ def extract_region_from_editor(editor_data):
45
+ """Extract the selected region from ImageEditor data"""
46
+ if editor_data is None:
47
+ print("DEBUG: extract_region_from_editor - editor_data is None")
48
+ return None, None
49
+
50
+ print(f"DEBUG: extract_region_from_editor - editor_data type: {type(editor_data)}")
51
+
52
+ if isinstance(editor_data, dict):
53
+ background = editor_data.get('background')
54
+ layers = editor_data.get('layers', [])
55
+
56
+ if background is None:
57
+ print("DEBUG: extract_region_from_editor - background is None")
58
+ return None, None
59
+
60
+ background_np = np.array(background)
61
+ print(f"DEBUG: extract_region_from_editor - background_np shape: {background_np.shape}")
62
+
63
+ if layers and len(layers) > 0:
64
+ selection_layer = layers[0]
65
+ selection_np = np.array(selection_layer)
66
+ print(f"DEBUG: extract_region_from_editor - selection_np shape: {selection_np.shape}")
67
+
68
+ if len(selection_np.shape) == 3:
69
+ if selection_np.shape[2] == 4: # RGBA
70
+ mask = selection_np[:, :, 3] > 0
71
+ else: # RGB
72
+ mask = np.any(selection_np > 0, axis=2)
73
+ else:
74
+ mask = selection_np > 0
75
+
76
+ coords = np.where(mask)
77
+ if len(coords[0]) > 0:
78
+ y_min, y_max = coords[0].min(), coords[0].max()
79
+ x_min, x_max = coords[1].min(), coords[1].max()
80
+
81
+ pad = 5
82
+ h, w = background_np.shape[:2]
83
+ y_min = max(0, y_min - pad)