dennistrujillo commited on
Commit
a2df67a
1 Parent(s): bd76685

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -0
app.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import nrrd
4
+ import matplotlib.pyplot as plt
5
+ import io
6
+ from PIL import Image
7
+
8
+ # Set Matplotlib to use the 'Agg' backend
9
+ plt.switch_backend('Agg')
10
+
11
+ def load_nrrd(file_path):
12
+ data, _ = nrrd.read(file_path.name)
13
+ num_slices = data.shape[2]
14
+ return data, num_slices
15
+
16
+ def visualize_slice(file_path, slice_index):
17
+ data, num_slices = load_nrrd(file_path)
18
+
19
+ # Ensure the slice index is within the range of available slices
20
+ slice_index = min(max(0, slice_index), num_slices - 1)
21
+
22
+ slice_image = data[:, :, slice_index]
23
+
24
+ # Plot the slice
25
+ fig, ax = plt.subplots()
26
+ ax.imshow(slice_image, cmap='gray')
27
+ plt.axis('off')
28
+
29
+ # Convert matplotlib figure to PIL Image
30
+ buf = io.BytesIO()
31
+ fig.savefig(buf, format='png')
32
+ plt.close(fig)
33
+ buf.seek(0)
34
+ pil_img = Image.open(buf)
35
+
36
+ return pil_img
37
+
38
+ def update_slider(file_path):
39
+ _, num_slices = load_nrrd(file_path)
40
+ return gr.update(maximum=num_slices-1, value=0)
41
+
42
+ with gr.Blocks() as app:
43
+ gr.Markdown("## NRRD Slice Visualizer")
44
+ gr.Markdown("Upload an NRRD file and use the slider to select and visualize slices.")
45
+
46
+ file_input = gr.File(label="Upload NRRD File")
47
+ slider = gr.Slider(minimum=0, maximum=1, step=1, value=0, label="Slice Selector")
48
+ image_output = gr.Image(type="pil", label="Selected Slice")
49
+
50
+ file_input.change(fn=update_slider, inputs=file_input, outputs=slider)
51
+ file_input.change(fn=visualize_slice, inputs=[file_input, slider], outputs=image_output)
52
+ slider.change(fn=visualize_slice, inputs=[file_input, slider], outputs=image_output)
53
+
54
+ app.launch()