Update app.py
Browse files
app.py
CHANGED
@@ -1,31 +1,54 @@
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
-
|
4 |
-
import
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
else:
|
13 |
-
raise ValueError("Unsupported file
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
)
|
29 |
|
30 |
if __name__ == "__main__":
|
31 |
-
|
|
|
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()
|