FrancescoLR commited on
Commit
79fd983
·
verified ·
1 Parent(s): 66267c0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -20
app.py CHANGED
@@ -27,38 +27,49 @@ 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_slice(nifti_path, output_image_path):
31
  """
32
- Extracts a middle slice from a 3D NIfTI image and saves it as a PNG file.
33
- The figure size is adjusted dynamically based on the slice's aspect ratio
34
- and scaled to be 50% smaller.
35
  """
36
- import nibabel as nib
37
- import matplotlib.pyplot as plt
38
 
39
  # Load NIfTI image and get the data
40
  img = nib.load(nifti_path)
41
  data = img.get_fdata()
42
-
43
- # Get the middle slice along the z-axis
44
- middle_slice_index = data.shape[2] // 2
45
- slice_data = data[:, :, middle_slice_index]
46
-
47
- # Rotate the slice 90 degrees clockwise
48
- slice_data = np.rot90(slice_data, k=-1)
49
 
50
- # Calculate aspect ratio
51
- height, width = slice_data.shape
52
- aspect_ratio = width / height
 
 
 
 
 
 
 
 
 
53
 
54
- # Dynamically adjust figure size based on aspect ratio and scale down by 0.5
55
- plt.figure(figsize=(4 * aspect_ratio, 4)) # Height scaled to 3, width scaled proportionally
56
- plt.imshow(slice_data, cmap="gray")
57
- plt.axis("off")
 
 
 
 
 
 
 
 
 
 
 
58
  plt.savefig(output_image_path, bbox_inches="tight", pad_inches=0)
59
  plt.close()
60
 
61
 
 
62
  # Function to run nnUNet inference
63
  @spaces.GPU # Decorate the function to allocate GPU for its execution
64
  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):
31
  """
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)
38
  data = img.get_fdata()
 
 
 
 
 
 
 
39
 
40
+ # Get middle slices along the three planes
41
+ middle_axial = data[:, :, data.shape[2] // 2] # Axial (z-axis)
42
+ middle_coronal = data[:, data.shape[1] // 2, :] # Coronal (y-axis)
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=-1)
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
52
 
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(middle_sagittal, cmap="gray")
63
+ axes[2].axis("off")
64
+ axes[2].set_title("Sagittal")
65
+
66
+ # Save the figure
67
+ plt.tight_layout()
68
  plt.savefig(output_image_path, bbox_inches="tight", pad_inches=0)
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):