Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -12,64 +12,59 @@ def load_nrrd(file_obj):
|
|
12 |
data, _ = nrrd.read(file_obj)
|
13 |
return data
|
14 |
|
15 |
-
def
|
16 |
data = load_nrrd(file_obj)
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
-
# Extract slices
|
28 |
-
slice_axial = data[:, :, slice_index_axial]
|
29 |
-
slice_coronal = data[:, slice_index_coronal, :]
|
30 |
-
slice_sagittal = data[slice_index_sagittal, :, :]
|
31 |
-
|
32 |
-
# Plot the slices with the correct aspect ratios
|
33 |
-
fig, axes = plt.subplots(1, 3, figsize=(15, 5))
|
34 |
-
|
35 |
-
axes[0].imshow(slice_axial, cmap='gray', aspect='auto')
|
36 |
-
axes[0].set_title("Axial View")
|
37 |
-
axes[0].axis('off')
|
38 |
-
|
39 |
-
axes[1].imshow(slice_coronal.T, cmap='gray', aspect='auto', extent=[0, data.shape[0], 0, data.shape[2]])
|
40 |
-
axes[1].set_title("Coronal View")
|
41 |
-
axes[1].axis('off')
|
42 |
-
|
43 |
-
axes[2].imshow(slice_sagittal.T, cmap='gray', aspect='auto', extent=[0, data.shape[1], 0, data.shape[2]])
|
44 |
-
axes[2].set_title("Sagittal View")
|
45 |
-
axes[2].axis('off')
|
46 |
-
|
47 |
# Convert matplotlib figure to PIL Image
|
48 |
buf = io.BytesIO()
|
49 |
-
|
50 |
-
fig.savefig(buf, format='png')
|
51 |
plt.close(fig)
|
52 |
buf.seek(0)
|
53 |
pil_img = Image.open(buf)
|
54 |
|
55 |
return pil_img
|
56 |
|
57 |
-
def update_slider(file_obj):
|
58 |
data = load_nrrd(file_obj)
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
|
|
61 |
return gr.update(maximum=max_slices-1, value=0)
|
62 |
|
63 |
with gr.Blocks() as app:
|
64 |
-
gr.Markdown("## NRRD
|
65 |
-
gr.Markdown("Upload an NRRD file and use the slider to select and visualize
|
66 |
|
67 |
file_input = gr.File(label="Upload NRRD File")
|
|
|
68 |
slider = gr.Slider(minimum=0, maximum=1, step=1, value=0, label="Slice Selector")
|
69 |
-
image_output = gr.Image(type="pil", label="Selected
|
70 |
|
71 |
-
file_input.change(fn=update_slider, inputs=file_input, outputs=slider)
|
72 |
-
|
73 |
-
|
|
|
74 |
|
75 |
app.launch()
|
|
|
12 |
data, _ = nrrd.read(file_obj)
|
13 |
return data
|
14 |
|
15 |
+
def visualize_slice(file_obj, slice_index, view):
|
16 |
data = load_nrrd(file_obj)
|
17 |
|
18 |
+
# Choose the appropriate slice based on the selected view
|
19 |
+
if view == "Axial":
|
20 |
+
num_slices = data.shape[2]
|
21 |
+
slice_index = min(max(0, slice_index), num_slices - 1)
|
22 |
+
slice_image = data[:, :, slice_index]
|
23 |
+
elif view == "Coronal":
|
24 |
+
num_slices = data.shape[1]
|
25 |
+
slice_index = min(max(0, slice_index), num_slices - 1)
|
26 |
+
slice_image = data[:, slice_index, :]
|
27 |
+
elif view == "Sagittal":
|
28 |
+
num_slices = data.shape[0]
|
29 |
+
slice_index = min(max(0, slice_index), num_slices - 1)
|
30 |
+
slice_image = data[slice_index, :, :]
|
31 |
+
|
32 |
+
# Plot the slice
|
33 |
+
fig, ax = plt.subplots(figsize=(5, 5 * slice_image.shape[0] / slice_image.shape[1])) # Keep aspect ratio
|
34 |
+
ax.imshow(slice_image, cmap='gray')
|
35 |
+
plt.axis('off')
|
36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
# Convert matplotlib figure to PIL Image
|
38 |
buf = io.BytesIO()
|
39 |
+
fig.savefig(buf, format='png', bbox_inches='tight', pad_inches=0)
|
|
|
40 |
plt.close(fig)
|
41 |
buf.seek(0)
|
42 |
pil_img = Image.open(buf)
|
43 |
|
44 |
return pil_img
|
45 |
|
46 |
+
def update_slider(file_obj, view):
|
47 |
data = load_nrrd(file_obj)
|
48 |
+
if view == "Axial":
|
49 |
+
max_slices = data.shape[2]
|
50 |
+
elif view == "Coronal":
|
51 |
+
max_slices = data.shape[1]
|
52 |
+
elif view == "Sagittal":
|
53 |
+
max_slices = data.shape[0]
|
54 |
return gr.update(maximum=max_slices-1, value=0)
|
55 |
|
56 |
with gr.Blocks() as app:
|
57 |
+
gr.Markdown("## NRRD Slice Visualizer with View Selector")
|
58 |
+
gr.Markdown("Upload an NRRD file, select a view (Axial, Coronal, Sagittal), and use the slider to select and visualize slices.")
|
59 |
|
60 |
file_input = gr.File(label="Upload NRRD File")
|
61 |
+
view_selector = gr.Radio(choices=["Axial", "Coronal", "Sagittal"], value="Axial", label="View Selector")
|
62 |
slider = gr.Slider(minimum=0, maximum=1, step=1, value=0, label="Slice Selector")
|
63 |
+
image_output = gr.Image(type="pil", label="Selected Slice")
|
64 |
|
65 |
+
file_input.change(fn=update_slider, inputs=[file_input, view_selector], outputs=slider)
|
66 |
+
view_selector.change(fn=update_slider, inputs=[file_input, view_selector], outputs=slider)
|
67 |
+
view_selector.change(fn=visualize_slice, inputs=[file_input, slider, view_selector], outputs=image_output)
|
68 |
+
slider.change(fn=visualize_slice, inputs=[file_input, slider, view_selector], outputs=image_output)
|
69 |
|
70 |
app.launch()
|