edyoshikun commited on
Commit
380e6c3
·
1 Parent(s): 8cfcc1c

option to merge or split nuc mem

Browse files
Files changed (1) hide show
  1. app.py +41 -8
app.py CHANGED
@@ -6,7 +6,6 @@ from numpy.typing import ArrayLike
6
  import numpy as np
7
  from skimage import exposure
8
  from skimage.transform import resize
9
- from skimage import img_as_float
10
  from skimage.util import invert
11
  import cmap
12
 
@@ -86,7 +85,7 @@ class VSGradio:
86
  nuc_rgb = apply_colormap(nuc_pred, green_colormap)
87
  mem_rgb = apply_colormap(mem_pred, magenta_colormap)
88
 
89
- return nuc_rgb, mem_rgb
90
 
91
 
92
  def apply_colormap(prediction, colormap: cmap.Colormap):
@@ -103,6 +102,11 @@ def apply_colormap(prediction, colormap: cmap.Colormap):
103
  return rgb_image_uint8
104
 
105
 
 
 
 
 
 
106
  def apply_image_adjustments(image, invert_image: bool, gamma_factor: float):
107
  """Applies all the image adjustments (invert, contrast, gamma) in sequence"""
108
  # Apply invert
@@ -188,9 +192,12 @@ if __name__ == "__main__":
188
  output_membrane = gr.Image(
189
  type="numpy", image_mode="RGB", label="VS Membrane"
190
  )
 
 
 
191
 
192
  # Checkbox for applying invert
193
- preprocess_invert = gr.Checkbox(label="Apply Invert", value=False)
194
 
195
  # Slider for gamma adjustment
196
  gamma_factor = gr.Slider(
@@ -204,6 +211,9 @@ if __name__ == "__main__":
204
  placeholder="Enter cell diameter in microns",
205
  )
206
 
 
 
 
207
  # Update the adjusted image based on all the transformations
208
  input_image.change(
209
  fn=apply_image_adjustments,
@@ -223,14 +233,37 @@ if __name__ == "__main__":
223
  outputs=adjusted_image,
224
  )
225
 
226
- # Button to trigger prediction
227
  submit_button = gr.Button("Submit")
228
 
229
- # Define what happens when the button is clicked (send adjusted image to predict)
 
 
 
 
 
 
 
 
230
  submit_button.click(
231
- vsgradio.predict,
232
- inputs=[adjusted_image, cell_diameter],
233
- outputs=[output_nucleus, output_membrane],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
234
  )
235
 
236
  # Example images and article
 
6
  import numpy as np
7
  from skimage import exposure
8
  from skimage.transform import resize
 
9
  from skimage.util import invert
10
  import cmap
11
 
 
85
  nuc_rgb = apply_colormap(nuc_pred, green_colormap)
86
  mem_rgb = apply_colormap(mem_pred, magenta_colormap)
87
 
88
+ return nuc_rgb, mem_rgb # Return both nucleus and membrane images
89
 
90
 
91
  def apply_colormap(prediction, colormap: cmap.Colormap):
 
102
  return rgb_image_uint8
103
 
104
 
105
+ def merge_images(nuc_rgb: ArrayLike, mem_rgb: ArrayLike) -> ArrayLike:
106
+ """Merge nucleus and membrane images into a single RGB image."""
107
+ return np.maximum(nuc_rgb, mem_rgb)
108
+
109
+
110
  def apply_image_adjustments(image, invert_image: bool, gamma_factor: float):
111
  """Applies all the image adjustments (invert, contrast, gamma) in sequence"""
112
  # Apply invert
 
192
  output_membrane = gr.Image(
193
  type="numpy", image_mode="RGB", label="VS Membrane"
194
  )
195
+ merged_image = gr.Image(
196
+ type="numpy", image_mode="RGB", label="Merged Image", visible=False
197
+ )
198
 
199
  # Checkbox for applying invert
200
+ preprocess_invert = gr.Checkbox(label="Invert Image", value=False)
201
 
202
  # Slider for gamma adjustment
203
  gamma_factor = gr.Slider(
 
211
  placeholder="Enter cell diameter in microns",
212
  )
213
 
214
+ # Checkbox for merging predictions
215
+ merge_checkbox = gr.Checkbox(label="Merge Predictions into one image", value=False)
216
+
217
  # Update the adjusted image based on all the transformations
218
  input_image.change(
219
  fn=apply_image_adjustments,
 
233
  outputs=adjusted_image,
234
  )
235
 
236
+ # Button to trigger prediction and update the output images
237
  submit_button = gr.Button("Submit")
238
 
239
+ # Function to handle prediction and merging if needed
240
+ def submit_and_merge(inp, cell_diameter, merge):
241
+ nucleus, membrane = vsgradio.predict(inp, cell_diameter)
242
+ if merge:
243
+ merged = merge_images(nucleus, membrane)
244
+ return merged, gr.update(visible=True), nucleus, gr.update(visible=False), membrane,gr.update(visible=False)
245
+ else:
246
+ return None, gr.update(visible=False), nucleus, gr.update(visible=True), membrane, gr.update(visible=True)
247
+
248
  submit_button.click(
249
+ fn=submit_and_merge,
250
+ inputs=[adjusted_image, cell_diameter, merge_checkbox],
251
+ outputs=[merged_image, merged_image, output_nucleus, output_nucleus,output_membrane, output_membrane],
252
+ )
253
+
254
+ # Function to handle merging the two predictions after they are shown
255
+ def merge_predictions_fn(nucleus_image, membrane_image, merge):
256
+ if merge:
257
+ merged = merge_images(nucleus_image, membrane_image)
258
+ return merged, gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)
259
+ else:
260
+ return None, gr.update(visible=False), gr.update(visible=True), gr.update(visible=True)
261
+
262
+ # Toggle between merged and separate views when the checkbox is checked
263
+ merge_checkbox.change(
264
+ fn=merge_predictions_fn,
265
+ inputs=[output_nucleus, output_membrane, merge_checkbox],
266
+ outputs=[merged_image, merged_image, output_nucleus, output_membrane],
267
  )
268
 
269
  # Example images and article