mouseland commited on
Commit
9a7b38a
·
verified ·
1 Parent(s): 50f6c2b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -15
app.py CHANGED
@@ -149,45 +149,41 @@ def image_resize(img, resize=400):
149
 
150
 
151
  @spaces.GPU(duration=10)
152
- def run_model_gpu(img):
153
- masks, flows, _ = model.eval(img, niter = 250)#, channels = [0,0])
154
  return masks, flows
155
 
156
  @spaces.GPU(duration=60)
157
  def run_model_gpu60(img):
158
- masks, flows, _ = model.eval(img, niter = 500)#, channels = [0,0])
159
  return masks, flows
160
 
161
  @spaces.GPU(duration=240)
162
  def run_model_gpu240(img):
163
- masks, flows, _ = model.eval(img, niter = 500)#, channels = [0,0])
164
  return masks, flows
165
 
166
- @spaces.GPU(duration=1000)
167
- def run_model_gpu1000(img):
168
- masks, flows, _ = model.eval(img, niter = 500)#, channels = [0,0])
169
- return masks, flows
170
 
171
 
172
  from zipfile import ZipFile
173
- def cellpose_segment(filepath, resize = 1000):
174
 
175
  zip_path = os.path.splitext(filepath[-1])[0]+"_masks.zip"
176
  #zip_path = 'masks.zip'
177
  with ZipFile(zip_path, 'w') as myzip:
178
  for j in range((len(filepath))):
179
- print(j)
180
  img_input = imread(filepath[j])
181
  #img_input = np.array(img_pil)
182
  img = image_resize(img_input, resize = resize)
183
 
184
  maxsize = np.max(img.shape)
185
  if maxsize<1000:
186
- masks, flows = run_model_gpu(img)
187
  elif maxsize < 5000:
188
- masks, flows = run_model_gpu60(img)
189
  elif maxsize < 20000:
190
- masks, flows = run_model_gpu240(img)
191
  else:
192
  raise ValueError("Image size must be less than 20,000")
193
 
@@ -315,7 +311,9 @@ with gr.Blocks(title = "Hello",
315
  with gr.Column(scale=1):
316
  with gr.Row():
317
  resize = gr.Number(label = 'max resize', value = 1000)
318
- up_btn = gr.UploadButton("Multi-file upload (png, jpg, tif etc)", visible=True, file_count = "multiple")
 
 
319
 
320
  #gr.HTML("""<h4 style="color:white;"> Note2: Only the first image of a tif will display the segmentations, but you can download segmentations for all planes. </h4>""")
321
 
@@ -340,7 +338,7 @@ with gr.Blocks(title = "Hello",
340
  input_image.upload(update_button, input_image, [input_image, up_btn, outlines, flows])
341
  up_btn.upload(update_image, up_btn, [input_image, up_btn, outlines, flows])
342
 
343
- send_btn.click(cellpose_segment, [up_btn, resize], [outlines, flows, down_btn, down_btn2])
344
 
345
  #down_btn.click(download_function, None, [down_btn, down_btn2])
346
 
 
149
 
150
 
151
  @spaces.GPU(duration=10)
152
+ def run_model_gpu(img, max_iter):
153
+ masks, flows, _ = model.eval(img, niter = max_iter)#, channels = [0,0])
154
  return masks, flows
155
 
156
  @spaces.GPU(duration=60)
157
  def run_model_gpu60(img):
158
+ masks, flows, _ = model.eval(img, niter = max_iter)#, channels = [0,0])
159
  return masks, flows
160
 
161
  @spaces.GPU(duration=240)
162
  def run_model_gpu240(img):
163
+ masks, flows, _ = model.eval(img, niter = max_iter)#, channels = [0,0])
164
  return masks, flows
165
 
 
 
 
 
166
 
167
 
168
  from zipfile import ZipFile
169
+ def cellpose_segment(filepath, resize = 1000,max_iter = 250):
170
 
171
  zip_path = os.path.splitext(filepath[-1])[0]+"_masks.zip"
172
  #zip_path = 'masks.zip'
173
  with ZipFile(zip_path, 'w') as myzip:
174
  for j in range((len(filepath))):
175
+ print(filepath[j])
176
  img_input = imread(filepath[j])
177
  #img_input = np.array(img_pil)
178
  img = image_resize(img_input, resize = resize)
179
 
180
  maxsize = np.max(img.shape)
181
  if maxsize<1000:
182
+ masks, flows = run_model_gpu(img, max_iter=max_iter)
183
  elif maxsize < 5000:
184
+ masks, flows = run_model_gpu60(img, max_iter=max_iter)
185
  elif maxsize < 20000:
186
+ masks, flows = run_model_gpu240(img, max_iter=max_iter)
187
  else:
188
  raise ValueError("Image size must be less than 20,000")
189
 
 
311
  with gr.Column(scale=1):
312
  with gr.Row():
313
  resize = gr.Number(label = 'max resize', value = 1000)
314
+ max_iter = gr.Number(label = 'max iterations', value = 250)
315
+
316
+ up_btn = gr.UploadButton("Multi-file upload (png, jpg, tif etc)", visible=True, file_count = "multiple")
317
 
318
  #gr.HTML("""<h4 style="color:white;"> Note2: Only the first image of a tif will display the segmentations, but you can download segmentations for all planes. </h4>""")
319
 
 
338
  input_image.upload(update_button, input_image, [input_image, up_btn, outlines, flows])
339
  up_btn.upload(update_image, up_btn, [input_image, up_btn, outlines, flows])
340
 
341
+ send_btn.click(cellpose_segment, [up_btn, resize, max_iter], [outlines, flows, down_btn, down_btn2])
342
 
343
  #down_btn.click(download_function, None, [down_btn, down_btn2])
344