Spaces:
Runtime error
Runtime error
Commit
·
9cbbb9b
1
Parent(s):
ad2f907
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import nibabel as nib
|
2 |
+
import matplotlib.pyplot as plt
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
def visualize_nii_gz(file_path):
|
6 |
+
# Load the NIfTI file
|
7 |
+
img = nib.load(file_path.name)
|
8 |
+
data = img.get_fdata()
|
9 |
+
|
10 |
+
# Get the middle slice of the data in all three dimensions
|
11 |
+
x_slice = data[data.shape[0] // 2, :, :]
|
12 |
+
y_slice = data[:, data.shape[1] // 2, :]
|
13 |
+
z_slice = data[:, :, data.shape[2] // 2]
|
14 |
+
|
15 |
+
# Plot the slices
|
16 |
+
fig = plt.figure(figsize=(12, 4))
|
17 |
+
|
18 |
+
ax1 = fig.add_subplot(131)
|
19 |
+
ax1.imshow(x_slice.T, cmap="gray", origin="lower")
|
20 |
+
ax1.set_title("X slice")
|
21 |
+
|
22 |
+
ax2 = fig.add_subplot(132)
|
23 |
+
ax2.imshow(y_slice.T, cmap="gray", origin="lower")
|
24 |
+
ax2.set_title("Y slice")
|
25 |
+
|
26 |
+
ax3 = fig.add_subplot(133)
|
27 |
+
ax3.imshow(z_slice.T, cmap="gray", origin="lower")
|
28 |
+
ax3.set_title("Z slice")
|
29 |
+
|
30 |
+
# Return the figure
|
31 |
+
plt.tight_layout()
|
32 |
+
return fig
|
33 |
+
|
34 |
+
# Create the Gradio interface
|
35 |
+
interface = gr.Interface(
|
36 |
+
fn=visualize_nii_gz,
|
37 |
+
inputs="file",
|
38 |
+
outputs="plot",
|
39 |
+
live=True,
|
40 |
+
title="3D NIfTI Visualizer",
|
41 |
+
description="Upload a .nii.gz file to visualize its slices in 3D."
|
42 |
+
)
|
43 |
+
|
44 |
+
interface.launch()
|