Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -27,48 +27,47 @@ def download_model():
|
|
27 |
subprocess.run(["unzip", "-o", zip_path, "-d", MODEL_DIR])
|
28 |
print("Dataset004_WML downloaded and extracted.")
|
29 |
|
30 |
-
def extract_middle_slices(nifti_path, output_image_path):
|
31 |
"""
|
32 |
-
Extracts
|
33 |
-
and
|
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)
|
41 |
data = img.get_fdata()
|
42 |
|
43 |
-
#
|
44 |
-
|
45 |
-
|
46 |
-
middle_sagittal = data[data.shape[0] // 2, :, :] # Sagittal (x-axis)
|
47 |
|
48 |
-
#
|
49 |
-
|
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 |
-
#
|
54 |
-
|
55 |
-
|
|
|
|
|
56 |
|
57 |
-
|
58 |
-
|
|
|
59 |
|
60 |
# Create subplots
|
61 |
-
fig, axes = plt.subplots(1, 3, figsize=(12, 4))
|
62 |
|
63 |
# Plot each slice
|
64 |
-
axes[0].imshow(
|
65 |
axes[0].axis("off")
|
|
|
66 |
|
67 |
-
axes[1].imshow(
|
68 |
axes[1].axis("off")
|
|
|
69 |
|
70 |
-
axes[2].imshow(
|
71 |
axes[2].axis("off")
|
|
|
72 |
|
73 |
# Save the figure
|
74 |
plt.tight_layout()
|
@@ -76,6 +75,7 @@ def extract_middle_slices(nifti_path, output_image_path):
|
|
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):
|
|
|
27 |
subprocess.run(["unzip", "-o", zip_path, "-d", MODEL_DIR])
|
28 |
print("Dataset004_WML downloaded and extracted.")
|
29 |
|
30 |
+
def extract_middle_slices(nifti_path, output_image_path, slice_size=180):
|
31 |
"""
|
32 |
+
Extracts slices centered around the center of mass of non-zero voxels in a 3D NIfTI image.
|
33 |
+
The slices are taken along axial, coronal, and sagittal planes and saved as a single PNG.
|
34 |
"""
|
|
|
|
|
|
|
|
|
35 |
# Load NIfTI image and get the data
|
36 |
img = nib.load(nifti_path)
|
37 |
data = img.get_fdata()
|
38 |
|
39 |
+
# Compute the center of mass of non-zero voxels
|
40 |
+
com = center_of_mass(data > 0)
|
41 |
+
center = np.round(com).astype(int)
|
|
|
42 |
|
43 |
+
# Define half the slice size to extract regions around the center of mass
|
44 |
+
half_size = slice_size // 2
|
|
|
|
|
45 |
|
46 |
+
# Extract slices around the center of mass
|
47 |
+
def extract_slice(data, center, axis):
|
48 |
+
slices = [slice(None)] * 3
|
49 |
+
slices[axis] = slice(center[axis] - half_size, center[axis] + half_size)
|
50 |
+
return np.take(data, range(center[axis] - half_size, center[axis] + half_size), axis=axis, mode='constant', cval=0)
|
51 |
|
52 |
+
axial_slice = extract_slice(data, center, axis=2) # Axial (z-axis)
|
53 |
+
coronal_slice = extract_slice(data, center, axis=1) # Coronal (y-axis)
|
54 |
+
sagittal_slice = extract_slice(data, center, axis=0) # Sagittal (x-axis)
|
55 |
|
56 |
# Create subplots
|
57 |
+
fig, axes = plt.subplots(1, 3, figsize=(12, 4))
|
58 |
|
59 |
# Plot each slice
|
60 |
+
axes[0].imshow(axial_slice, cmap="gray", origin="lower")
|
61 |
axes[0].axis("off")
|
62 |
+
axes[0].set_title("Axial")
|
63 |
|
64 |
+
axes[1].imshow(coronal_slice, cmap="gray", origin="lower")
|
65 |
axes[1].axis("off")
|
66 |
+
axes[1].set_title("Coronal")
|
67 |
|
68 |
+
axes[2].imshow(sagittal_slice, cmap="gray", origin="lower")
|
69 |
axes[2].axis("off")
|
70 |
+
axes[2].set_title("Sagittal")
|
71 |
|
72 |
# Save the figure
|
73 |
plt.tight_layout()
|
|
|
75 |
plt.close()
|
76 |
|
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):
|