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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -21
app.py CHANGED
@@ -163,32 +163,37 @@ def run_model_gpu1000(img):
163
  masks, flows, _ = model.eval(img)#, channels = [0,0])
164
  return masks, flows
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]
@@ -215,7 +220,10 @@ def cellpose_segment(filepath, resize = 1000):
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)
 
 
 
219
  b2 = gr.DownloadButton(visible=True, value = fname_out) #"outlines.png")
220
 
221
  return outpix, flows, b1, b2
 
163
  masks, flows, _ = model.eval(img)#, channels = [0,0])
164
  return masks, flows
165
 
166
+ from zipfile import Zipfile
167
  #@spaces.GPU(duration=10)
168
  def cellpose_segment(filepath, resize = 1000):
169
 
170
+ zip_path = 'masks.zip'
171
+ with ZipFile(zip_path, 'w') as myzip:
172
+ for j in range((len(filepath))):
173
+ img_input = imread(filepath[j])
174
+ #img_input = np.array(img_pil)
175
+ img = image_resize(img_input, resize = resize)
176
+
177
+ resize = np.max(img.shape)
178
+ if resize<1000:
179
+ masks, flows = run_model_gpu(img)
180
+ elif resize < 5000:
181
+ masks, flows = run_model_gpu60(img)
182
+ elif resize < 20000:
183
+ masks, flows = run_model_gpu240(img)
184
+ else:
185
+ raise ValueError("Image size must be less than 20,000")
186
 
187
+ target_size = (img_input.shape[1], img_input.shape[0])
188
+ if (target_size[0]!=img.shape[1] or target_size[1]!=img.shape[0]):
189
+ # scale it back to keep the orignal size
190
+ masks = cv2.resize(masks.astype('uint16'), target_size, interpolation=cv2.INTER_NEAREST).astype('uint16')
 
 
 
 
 
191
 
192
+ fname_masks = os.path.splitext(filepath[j])[0]+"_masks.tif"
193
+ imsave(fname_masks, masks)
 
 
 
 
 
194
 
195
+ myzip.write(fname_masks)
196
+
197
 
198
  #masks, flows, _ = model.eval(img, channels=[0,0])
199
  flows = flows[0]
 
220
  fname_out = os.path.splitext(filepath[-1])[0]+"_outlines.png"
221
  outpix.save(fname_out) #"outlines.png")
222
 
223
+ if len(filepath)>1:
224
+ b1 = gr.DownloadButton(visible=True, value = zip_path)
225
+ else:
226
+ b1 = gr.DownloadButton(visible=True, value = fname_masks)
227
  b2 = gr.DownloadButton(visible=True, value = fname_out) #"outlines.png")
228
 
229
  return outpix, flows, b1, b2