Hemaxi commited on
Commit
f9fed5c
·
verified ·
1 Parent(s): a0eb692

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -25
app.py CHANGED
@@ -1,31 +1,54 @@
1
  import gradio as gr
2
  import numpy as np
3
- from skimage import io
4
- import czifile
5
-
6
- def load_3d_image(file):
7
- if file.name.endswith('.tif'):
8
- image = io.imread(file.name)
9
- elif file.name.endswith('.czi'):
10
- with czifile.CziFile(file.name) as czi:
11
- image = czi.asarray()
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  else:
13
- raise ValueError("Unsupported file type. Please upload a .tif or .czi file.")
14
- return image
15
-
16
- def process_3d_image(input_image):
17
- # Your model processing logic here
18
- # This is a placeholder function
19
- output_mask = np.random.randint(0, 2, input_image.shape).astype(bool)
20
- return output_mask
21
-
22
- demo = gr.Interface(
23
- fn=process_3d_image,
24
- inputs=gr.File(label="Input 3D Image (.tif or .czi)", file_types=[".tif", ".czi"]),
25
- outputs=gr.Image(label="Output 3D Binary Mask", type="numpy"),
26
- title="3D Image to Binary Mask",
27
- description="Upload a 3D image (.tif or .czi) and get a 3D binary mask as output."
 
 
 
 
 
 
 
 
 
 
28
  )
29
 
30
  if __name__ == "__main__":
31
- demo.launch()
 
1
  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):
8
+ # Dummy model implementation: Replace with your actual model logic
9
+ binary_mask = (image > image.mean()).astype(np.uint8)
10
+ return binary_mask
11
+
12
+ # Function to handle file input and processing
13
+ def process_file(file):
14
+ """
15
+ Process the uploaded file and return the binary mask.
16
+ """
17
+ if file.name.endswith(".tif"):
18
+ # Load .tif file as a 3D numpy array
19
+ image = tiff.imread(file.name)
20
+ elif file.name.endswith(".czi"):
21
+ # Load .czi file using AICSImage
22
+ img = AICSImage(file.name)
23
+ image = img.get_image_data("CZYX") # Extracting 3D data
24
+ image = image[0, 0] # Assuming single channel, single timepoint
25
  else:
26
+ raise ValueError("Unsupported file format. Please upload a .tif or .czi file.")
27
+
28
+ # Ensure image is 3D
29
+ if len(image.shape) != 3:
30
+ raise ValueError("Input image is not 3D.")
31
+
32
+ # Process image through the model
33
+ binary_mask = process_3d_image(image)
34
+
35
+ # Save binary mask to a .tif file to return
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__":
54
+ iface.launch()