mouseland commited on
Commit
5ba71fa
·
verified ·
1 Parent(s): fcdd787

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -36
app.py CHANGED
@@ -165,37 +165,40 @@ def run_model_gpu1000(img):
165
 
166
  #@spaces.GPU(duration=10)
167
  def cellpose_segment(filepath, resize = 1000):
168
- img_input = imread(filepath[0])
169
- print(img_input.shape)
170
- #img_input = np.array(img_pil)
171
- img = image_resize(img_input, resize = resize)
172
- print(img.shape)
173
-
174
- resize = np.max(img.shape)
175
- if resize<1000:
176
- masks, flows = run_model_gpu(img)
177
- elif resize < 5000:
178
- masks, flows = run_model_gpu60(img)
179
- elif resize < 20000:
180
- masks, flows = run_model_gpu240(img)
181
- else:
182
- raise ValueError("Image size must be less than 20,000")
183
-
184
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
185
  #masks, flows, _ = model.eval(img, channels=[0,0])
186
  flows = flows[0]
187
  # masks = np.zeros(img.shape[:2])
188
  # flows = np.zeros_like(img)
189
 
190
  outpix = plot_outlines(img, masks)
191
- overlay = plot_overlay(img, masks)
192
-
193
- target_size = (img_input.shape[1], img_input.shape[0])
194
- if (target_size[0]!=img.shape[1] or target_size[1]!=img.shape[0]):
195
- # scale it back to keep the orignal size
196
- masks = cv2.resize(masks.astype('uint16'), target_size, interpolation=cv2.INTER_NEAREST).astype('uint16')
197
- #flows = cv2.resize(flows.astype('float32'), target_size).astype('uint8')
198
 
 
199
 
200
  #crand = .2 + .8 * np.random.rand(np.max(masks.flatten()).astype('int')+1,).astype('float32')
201
  #crand[0] = 0
@@ -206,19 +209,10 @@ def cellpose_segment(filepath, resize = 1000):
206
  Ly, Lx = img.shape[:2]
207
  c = Lx
208
  outpix = outpix.resize((Lx, Ly), resample = Image.BICUBIC)
209
- overlay = overlay.resize((Lx, Ly), resample = Image.BICUBIC)
210
  flows = flows.resize((Lx, Ly), resample = Image.BICUBIC)
211
 
212
- #masks = Image.fromarray(255. * crand[masks])
213
- #pil_masks = Image.fromarray(masks.astype('int32'))
214
- #pil_masks.save(fname_mask)
215
-
216
- fname_out = os.path.splitext(filepath[0])[0]+"_outlines.png"
217
- fname_masks = os.path.splitext(filepath[0])[0]+"_masks.tif"
218
-
219
- imsave(fname_masks, masks)
220
-
221
-
222
  outpix.save(fname_out) #"outlines.png")
223
 
224
  b1 = gr.DownloadButton(visible=True, value = fname_masks)
@@ -250,7 +244,7 @@ def upload_file(filepath):
250
 
251
  #b1 = gr.DownloadButton("Download masks as TIFF", visible=False)
252
  #b2 = gr.DownloadButton("Download outline image as PNG", visible=False)
253
- return filepath[0] #, b1, b2
254
 
255
 
256
  def update_image(filepath):
@@ -298,7 +292,6 @@ with gr.Blocks(title = "Hello",
298
  #img_overlay = gr.Image(label = "Overlay", type = "pil", format = 'png') #, width = "50vw", height = "20vw")
299
  flows = gr.Image(label = "Cellpose flows", type = "pil", format = 'png') #, width = "50vw", height = "20vw")
300
 
301
-
302
 
303
  sample_list = []
304
  for j in range(23):
 
165
 
166
  #@spaces.GPU(duration=10)
167
  def cellpose_segment(filepath, resize = 1000):
168
+
169
+ for j in range((len(filepath))):
170
+ img_input = imread(filepath[j])
171
+ #img_input = np.array(img_pil)
172
+ img = image_resize(img_input, resize = resize)
 
 
 
 
 
 
 
 
 
 
 
173
 
174
+ resize = np.max(img.shape)
175
+ if resize<1000:
176
+ masks, flows = run_model_gpu(img)
177
+ elif resize < 5000:
178
+ masks, flows = run_model_gpu60(img)
179
+ elif resize < 20000:
180
+ masks, flows = run_model_gpu240(img)
181
+ else:
182
+ raise ValueError("Image size must be less than 20,000")
183
+
184
+ target_size = (img_input.shape[1], img_input.shape[0])
185
+ if (target_size[0]!=img.shape[1] or target_size[1]!=img.shape[0]):
186
+ # scale it back to keep the orignal size
187
+ masks = cv2.resize(masks.astype('uint16'), target_size, interpolation=cv2.INTER_NEAREST).astype('uint16')
188
+
189
+ fname_masks = os.path.splitext(filepath[j])[0]+"_masks.tif"
190
+ imsave(fname_masks, masks)
191
+
192
+
193
  #masks, flows, _ = model.eval(img, channels=[0,0])
194
  flows = flows[0]
195
  # masks = np.zeros(img.shape[:2])
196
  # flows = np.zeros_like(img)
197
 
198
  outpix = plot_outlines(img, masks)
199
+ #overlay = plot_overlay(img, masks)
 
 
 
 
 
 
200
 
201
+
202
 
203
  #crand = .2 + .8 * np.random.rand(np.max(masks.flatten()).astype('int')+1,).astype('float32')
204
  #crand[0] = 0
 
209
  Ly, Lx = img.shape[:2]
210
  c = Lx
211
  outpix = outpix.resize((Lx, Ly), resample = Image.BICUBIC)
212
+ #overlay = overlay.resize((Lx, Ly), resample = Image.BICUBIC)
213
  flows = flows.resize((Lx, Ly), resample = Image.BICUBIC)
214
 
215
+ fname_out = os.path.splitext(filepath[-1])[0]+"_outlines.png"
 
 
 
 
 
 
 
 
 
216
  outpix.save(fname_out) #"outlines.png")
217
 
218
  b1 = gr.DownloadButton(visible=True, value = fname_masks)
 
244
 
245
  #b1 = gr.DownloadButton("Download masks as TIFF", visible=False)
246
  #b2 = gr.DownloadButton("Download outline image as PNG", visible=False)
247
+ return filepath[-1] #, b1, b2
248
 
249
 
250
  def update_image(filepath):
 
292
  #img_overlay = gr.Image(label = "Overlay", type = "pil", format = 'png') #, width = "50vw", height = "20vw")
293
  flows = gr.Image(label = "Cellpose flows", type = "pil", format = 'png') #, width = "50vw", height = "20vw")
294
 
 
295
 
296
  sample_list = []
297
  for j in range(23):