Spaces:
Running
on
Zero
Running
on
Zero
FrancescoLR
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -76,6 +76,28 @@ def extract_middle_slices(nifti_path, output_image_path, slice_size=180):
|
|
76 |
# Define half the slice size
|
77 |
half_size = slice_size // 2
|
78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
# Safely extract and pad 2D slices
|
80 |
def extract_2d_slice(data, center, axis):
|
81 |
slices = [slice(None)] * 3
|
@@ -171,11 +193,19 @@ def run_nnunet_predict(nifti_file):
|
|
171 |
if os.path.exists(output_file):
|
172 |
os.rename(output_file, new_output_file)
|
173 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
174 |
# Extract and save 2D slices
|
175 |
input_slice_path = os.path.join(OUTPUT_DIR, f"{base_filename}_input_slice.png")
|
176 |
output_slice_path = os.path.join(OUTPUT_DIR, f"{base_filename}_output_slice.png")
|
177 |
-
extract_middle_slices(input_path, input_slice_path)
|
178 |
-
extract_middle_slices(new_output_file, output_slice_path)
|
179 |
|
180 |
# Return paths for the Gradio interface
|
181 |
return new_output_file, input_slice_path, output_slice_path
|
|
|
76 |
# Define half the slice size
|
77 |
half_size = slice_size // 2
|
78 |
|
79 |
+
def extract_middle_slices(nifti_path, output_image_path, slice_size=180, center=None):
|
80 |
+
"""
|
81 |
+
Extracts slices from a 3D NIfTI image. If a center is provided, it uses it;
|
82 |
+
otherwise, computes the center of mass of non-zero voxels. Slices are taken
|
83 |
+
along axial, coronal, and sagittal planes and saved as a single PNG.
|
84 |
+
"""
|
85 |
+
# Load NIfTI image
|
86 |
+
img = nib.load(nifti_path)
|
87 |
+
data = img.get_fdata()
|
88 |
+
affine = img.affine
|
89 |
+
|
90 |
+
# Resample the image to 1 mm isotropic
|
91 |
+
resampled_data, _ = resample_to_isotropic(data, affine, target_spacing=1.0)
|
92 |
+
|
93 |
+
# Compute or reuse the center of mass
|
94 |
+
if center is None:
|
95 |
+
com = center_of_mass(resampled_data > 0)
|
96 |
+
center = np.round(com).astype(int)
|
97 |
+
|
98 |
+
# Define half the slice size
|
99 |
+
half_size = slice_size // 2
|
100 |
+
|
101 |
# Safely extract and pad 2D slices
|
102 |
def extract_2d_slice(data, center, axis):
|
103 |
slices = [slice(None)] * 3
|
|
|
193 |
if os.path.exists(output_file):
|
194 |
os.rename(output_file, new_output_file)
|
195 |
|
196 |
+
# Compute center of mass for the input image
|
197 |
+
img = nib.load(input_path)
|
198 |
+
data = img.get_fdata()
|
199 |
+
affine = img.affine
|
200 |
+
resampled_data, _ = resample_to_isotropic(data, affine, target_spacing=1.0)
|
201 |
+
com = center_of_mass(resampled_data > 0) # Center of mass
|
202 |
+
center = np.round(com).astype(int) # Round to integer
|
203 |
+
|
204 |
# Extract and save 2D slices
|
205 |
input_slice_path = os.path.join(OUTPUT_DIR, f"{base_filename}_input_slice.png")
|
206 |
output_slice_path = os.path.join(OUTPUT_DIR, f"{base_filename}_output_slice.png")
|
207 |
+
extract_middle_slices(input_path, input_slice_path, center=center)
|
208 |
+
extract_middle_slices(new_output_file, output_slice_path, center=center)
|
209 |
|
210 |
# Return paths for the Gradio interface
|
211 |
return new_output_file, input_slice_path, output_slice_path
|