FrancescoLR commited on
Commit
1d7bf30
·
1 Parent(s): 6670942

Updated app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -6
app.py CHANGED
@@ -29,16 +29,30 @@ def download_model():
29
  def extract_middle_slice(nifti_path, output_image_path):
30
  """
31
  Extracts a middle slice from a 3D NIfTI image and saves it as a PNG file.
 
32
  """
 
 
 
 
33
  img = nib.load(nifti_path)
34
  data = img.get_fdata()
35
- middle_slice_index = data.shape[2] // 2 # Middle slice along the z-axis
36
- plt.figure(figsize=(6, 6))
37
- plt.imshow(data[:, :, middle_slice_index], cmap="gray")
 
 
 
 
 
 
 
 
 
38
  plt.axis("off")
39
  plt.savefig(output_image_path, bbox_inches="tight", pad_inches=0)
40
  plt.close()
41
-
42
  # Function to run nnUNet inference
43
  @spaces.GPU # Decorate the function to allocate GPU for its execution
44
  def run_nnunet_predict(nifti_file):
@@ -100,9 +114,9 @@ interface = gr.Interface(
100
  fn=run_nnunet_predict,
101
  inputs=gr.File(label="Upload FLAIR Image (.nii.gz)"),
102
  outputs=[
 
103
  gr.Image(label="Input Middle Slice"),
104
- gr.Image(label="Output Middle Slice"),
105
- gr.File(label="Download Segmentation Mask")
106
  ],
107
  title="FLAMeS: Multiple Sclerosis Lesion Segmentation",
108
  description="Upload a skull-stripped FLAIR image (.nii.gz) to generate a binary segmentation of MS lesions."
 
29
  def extract_middle_slice(nifti_path, output_image_path):
30
  """
31
  Extracts a middle slice from a 3D NIfTI image and saves it as a PNG file.
32
+ The figure size is adjusted dynamically based on the slice's aspect ratio.
33
  """
34
+ import nibabel as nib
35
+ import matplotlib.pyplot as plt
36
+
37
+ # Load NIfTI image and get the data
38
  img = nib.load(nifti_path)
39
  data = img.get_fdata()
40
+
41
+ # Get the middle slice along the z-axis
42
+ middle_slice_index = data.shape[2] // 2
43
+ slice_data = data[:, :, middle_slice_index]
44
+
45
+ # Calculate aspect ratio
46
+ height, width = slice_data.shape
47
+ aspect_ratio = width / height
48
+
49
+ # Dynamically adjust figure size based on aspect ratio
50
+ plt.figure(figsize=(6 * aspect_ratio, 6)) # Height fixed to 6, width scaled by aspect ratio
51
+ plt.imshow(slice_data, cmap="gray")
52
  plt.axis("off")
53
  plt.savefig(output_image_path, bbox_inches="tight", pad_inches=0)
54
  plt.close()
55
+
56
  # Function to run nnUNet inference
57
  @spaces.GPU # Decorate the function to allocate GPU for its execution
58
  def run_nnunet_predict(nifti_file):
 
114
  fn=run_nnunet_predict,
115
  inputs=gr.File(label="Upload FLAIR Image (.nii.gz)"),
116
  outputs=[
117
+ gr.File(label="Download Segmentation Mask"),
118
  gr.Image(label="Input Middle Slice"),
119
+ gr.Image(label="Output Middle Slice")
 
120
  ],
121
  title="FLAMeS: Multiple Sclerosis Lesion Segmentation",
122
  description="Upload a skull-stripped FLAIR image (.nii.gz) to generate a binary segmentation of MS lesions."