Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -32,6 +32,9 @@ def extract_middle_slices(nifti_path, output_image_path):
|
|
| 32 |
Extracts middle slices from a 3D NIfTI image in axial, coronal, and sagittal planes
|
| 33 |
and saves them as a single PNG file with subplots.
|
| 34 |
"""
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
# Load NIfTI image and get the data
|
| 37 |
img = nib.load(nifti_path)
|
|
@@ -43,9 +46,16 @@ def extract_middle_slices(nifti_path, output_image_path):
|
|
| 43 |
middle_sagittal = data[data.shape[0] // 2, :, :] # Sagittal (x-axis)
|
| 44 |
|
| 45 |
# Rotate slices for proper orientation
|
| 46 |
-
middle_axial = np.rot90(middle_axial, k=-1)
|
| 47 |
-
middle_coronal = np.rot90(middle_coronal, k
|
| 48 |
-
middle_sagittal = np.rot90(middle_sagittal, k=-1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
# Create subplots
|
| 51 |
fig, axes = plt.subplots(1, 3, figsize=(12, 4)) # 3 subplots in a row
|
|
@@ -53,15 +63,12 @@ def extract_middle_slices(nifti_path, output_image_path):
|
|
| 53 |
# Plot each slice
|
| 54 |
axes[0].imshow(middle_axial, cmap="gray")
|
| 55 |
axes[0].axis("off")
|
| 56 |
-
axes[0].set_title("Axial")
|
| 57 |
|
| 58 |
axes[1].imshow(middle_coronal, cmap="gray")
|
| 59 |
axes[1].axis("off")
|
| 60 |
-
axes[1].set_title("Coronal")
|
| 61 |
|
| 62 |
-
axes[2].imshow(
|
| 63 |
axes[2].axis("off")
|
| 64 |
-
axes[2].set_title("Sagittal")
|
| 65 |
|
| 66 |
# Save the figure
|
| 67 |
plt.tight_layout()
|
|
@@ -69,7 +76,6 @@ def extract_middle_slices(nifti_path, output_image_path):
|
|
| 69 |
plt.close()
|
| 70 |
|
| 71 |
|
| 72 |
-
|
| 73 |
# Function to run nnUNet inference
|
| 74 |
@spaces.GPU # Decorate the function to allocate GPU for its execution
|
| 75 |
def run_nnunet_predict(nifti_file):
|
|
|
|
| 32 |
Extracts middle slices from a 3D NIfTI image in axial, coronal, and sagittal planes
|
| 33 |
and saves them as a single PNG file with subplots.
|
| 34 |
"""
|
| 35 |
+
import nibabel as nib
|
| 36 |
+
import matplotlib.pyplot as plt
|
| 37 |
+
import numpy as np
|
| 38 |
|
| 39 |
# Load NIfTI image and get the data
|
| 40 |
img = nib.load(nifti_path)
|
|
|
|
| 46 |
middle_sagittal = data[data.shape[0] // 2, :, :] # Sagittal (x-axis)
|
| 47 |
|
| 48 |
# Rotate slices for proper orientation
|
| 49 |
+
middle_axial = np.rot90(middle_axial, k=-1) # Rotate 90 degrees clockwise
|
| 50 |
+
middle_coronal = np.rot90(middle_coronal, k=2) # Rotate 180 degrees
|
| 51 |
+
middle_sagittal = np.flipud(np.rot90(middle_sagittal, k=-1)) # Rotate 90 degrees clockwise and flip up-down
|
| 52 |
+
|
| 53 |
+
# Pad sagittal slice to match dimensions of other slices
|
| 54 |
+
max_height = max(middle_axial.shape[0], middle_sagittal.shape[0])
|
| 55 |
+
max_width = max(middle_axial.shape[1], middle_sagittal.shape[1])
|
| 56 |
+
|
| 57 |
+
padded_sagittal = np.zeros((max_height, max_width))
|
| 58 |
+
padded_sagittal[:middle_sagittal.shape[0], :middle_sagittal.shape[1]] = middle_sagittal
|
| 59 |
|
| 60 |
# Create subplots
|
| 61 |
fig, axes = plt.subplots(1, 3, figsize=(12, 4)) # 3 subplots in a row
|
|
|
|
| 63 |
# Plot each slice
|
| 64 |
axes[0].imshow(middle_axial, cmap="gray")
|
| 65 |
axes[0].axis("off")
|
|
|
|
| 66 |
|
| 67 |
axes[1].imshow(middle_coronal, cmap="gray")
|
| 68 |
axes[1].axis("off")
|
|
|
|
| 69 |
|
| 70 |
+
axes[2].imshow(padded_sagittal, cmap="gray")
|
| 71 |
axes[2].axis("off")
|
|
|
|
| 72 |
|
| 73 |
# Save the figure
|
| 74 |
plt.tight_layout()
|
|
|
|
| 76 |
plt.close()
|
| 77 |
|
| 78 |
|
|
|
|
| 79 |
# Function to run nnUNet inference
|
| 80 |
@spaces.GPU # Decorate the function to allocate GPU for its execution
|
| 81 |
def run_nnunet_predict(nifti_file):
|