Hemaxi commited on
Commit
30b140a
·
verified ·
1 Parent(s): 931016d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -7
app.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
2
  import numpy as np
3
  import tifffile as tiff
4
  from aicsimageio import AICSImage # To handle .czi files
 
5
 
6
  # Placeholder for your 3D model
7
  def process_3d_image(image):
@@ -36,18 +37,53 @@ def process_file(file):
36
  output_path = "output_mask.tif"
37
  tiff.imwrite(output_path, binary_mask)
38
 
39
- return output_path
40
 
41
- # Gradio Interface
42
- def interface(file):
43
- return process_file(file)
 
 
 
 
 
 
 
 
 
 
 
 
44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  iface = gr.Interface(
46
  fn=interface,
47
- inputs=gr.File(label="Upload 3D Image (.tif or .czi)"),
48
- outputs=gr.File(label="Download Binary Mask (.tif)"),
 
 
 
 
 
 
49
  title="3D Image Processing with Binary Mask Output",
50
- description="Upload a 3D image in .tif or .czi format. The model will process the image and output a 3D binary mask."
51
  )
52
 
53
  if __name__ == "__main__":
 
2
  import numpy as np
3
  import tifffile as tiff
4
  from aicsimageio import AICSImage # To handle .czi files
5
+ import matplotlib.pyplot as plt
6
 
7
  # Placeholder for your 3D model
8
  def process_3d_image(image):
 
37
  output_path = "output_mask.tif"
38
  tiff.imwrite(output_path, binary_mask)
39
 
40
+ return image, binary_mask, output_path
41
 
42
+ # Function to generate the slice visualization
43
+ def visualize_slice(image, mask, slice_index):
44
+ """
45
+ Visualizes a 2D slice of the image and the corresponding mask at the given index.
46
+ """
47
+ fig, axes = plt.subplots(1, 2, figsize=(12, 6))
48
+
49
+ # Extract the 2D slices
50
+ image_slice = image[slice_index, :, :]
51
+ mask_slice = mask[slice_index, :, :]
52
+
53
+ # Plot image slice
54
+ axes[0].imshow(image_slice, cmap="gray")
55
+ axes[0].set_title("Image Slice")
56
+ axes[0].axis("off")
57
 
58
+ # Plot mask slice
59
+ axes[1].imshow(mask_slice, cmap="gray")
60
+ axes[1].set_title("Mask Slice")
61
+ axes[1].axis("off")
62
+
63
+ # Return the plot as a Gradio-compatible output
64
+ plt.tight_layout()
65
+ plt.close(fig)
66
+ return fig
67
+
68
+ # Gradio Interface function
69
+ def interface(file, slice_index):
70
+ image, mask, output_path = process_file(file)
71
+ fig = visualize_slice(image, mask, slice_index)
72
+ return fig, output_path
73
+
74
+ # Gradio Interface
75
  iface = gr.Interface(
76
  fn=interface,
77
+ inputs=[
78
+ gr.File(label="Upload 3D Image (.tif or .czi)"),
79
+ gr.Slider(minimum=0, maximum=100, step=1, label="Slice Index") # Adjust max value based on image size
80
+ ],
81
+ outputs=[
82
+ gr.Plot(label="2D Slice Visualization"),
83
+ gr.File(label="Download Binary Mask (.tif)")
84
+ ],
85
  title="3D Image Processing with Binary Mask Output",
86
+ description="Upload a 3D image in .tif or .czi format. The model will process the image and output a 3D binary mask. Use the slider to navigate through the 2D slices."
87
  )
88
 
89
  if __name__ == "__main__":