DiGuaQiu commited on
Commit
58ca1a1
1 Parent(s): 52811bb

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +27 -26
utils.py CHANGED
@@ -4,16 +4,13 @@ from pathlib import Path
4
 
5
  import matplotlib as mpl
6
  import matplotlib.pyplot as plt
7
- import nibabel as nib
8
  import numpy as np
 
9
  import torch
10
- from monai.transforms import LoadImage
11
  from mrsegmentator import inference
12
  from mrsegmentator.utils import add_postfix
13
- from PIL import Image, ImageColor, ImageDraw, ImageEnhance
14
  from scipy import ndimage
15
- from monai.transforms import LoadImage, Orientation, Spacing
16
- import SimpleITK as sitk
17
 
18
  import streamlit as st
19
 
@@ -63,7 +60,7 @@ def run(tmpdirname):
63
  if st.session_state.option is not None:
64
  image = Path(__file__).parent / str(st.session_state.option)
65
 
66
- inference.infer([image], tmpdirname, [0], split_level=2, cpu_only=True)
67
  seg_name = add_postfix(image.name, "seg")
68
  preds_path = tmpdirname + "/" + seg_name
69
  st.session_state.preds_3D = read_image(preds_path)
@@ -78,9 +75,7 @@ def reflect_box_into_model(box_3d):
78
  x2_prompt = int(x2 * 256.0 / 325.0)
79
  y2_prompt = int(y2 * 256.0 / 325.0)
80
  z2_prompt = int(z2 * 32.0 / 325.0)
81
- return torch.tensor(
82
- np.array([z1_prompt, y1_prompt, x1_prompt, z2_prompt, y2_prompt, x2_prompt])
83
- )
84
 
85
 
86
  def reflect_json_data_to_3D_box(json_data, view):
@@ -88,19 +83,17 @@ def reflect_json_data_to_3D_box(json_data, view):
88
  st.session_state.rectangle_3Dbox[1] = json_data["objects"][0]["top"]
89
  st.session_state.rectangle_3Dbox[2] = json_data["objects"][0]["left"]
90
  st.session_state.rectangle_3Dbox[4] = (
91
- json_data["objects"][0]["top"]
92
- + json_data["objects"][0]["height"] * json_data["objects"][0]["scaleY"]
93
  )
94
  st.session_state.rectangle_3Dbox[5] = (
95
- json_data["objects"][0]["left"]
96
- + json_data["objects"][0]["width"] * json_data["objects"][0]["scaleX"]
97
  )
98
  print(st.session_state.rectangle_3Dbox)
99
 
100
 
101
- def make_fig(image, preds, px_range = (10, 400), transparency=0.5):
102
 
103
- fig, ax = plt.subplots(1, 1, figsize=(4,4))
104
  image_slice = image.clip(*px_range)
105
 
106
  ax.imshow(
@@ -150,7 +143,7 @@ def make_fig(image, preds, px_range = (10, 400), transparency=0.5):
150
  plt.axis("off")
151
  ax.set_xticks([])
152
  ax.set_yticks([])
153
-
154
  fig.canvas.draw()
155
 
156
  # transform to image
@@ -160,8 +153,8 @@ def make_fig(image, preds, px_range = (10, 400), transparency=0.5):
160
  #######################################
161
 
162
 
163
- def make_isotropic(image, interpolator = sitk.sitkLinear, spacing = None):
164
- '''
165
  Many file formats (e.g. jpg, png,...) expect the pixels to be isotropic, same
166
  spacing for all axes. Saving non-isotropic data in these formats will result in
167
  distorted images. This function makes an image isotropic via resampling, if needed.
@@ -175,7 +168,7 @@ def make_isotropic(image, interpolator = sitk.sitkLinear, spacing = None):
175
  Returns:
176
  SimpleITK.Image with isotropic spacing which occupies the same region in space as
177
  the input image.
178
- '''
179
  original_spacing = image.GetSpacing()
180
  # Image is already isotropic, just return a copy.
181
  if all(spc == original_spacing[0] for spc in original_spacing):
@@ -184,18 +177,26 @@ def make_isotropic(image, interpolator = sitk.sitkLinear, spacing = None):
184
  original_size = image.GetSize()
185
  if spacing is None:
186
  spacing = min(original_spacing)
187
- new_spacing = [spacing]*image.GetDimension()
188
- new_size = [int(round(osz*ospc/spacing)) for osz, ospc in zip(original_size, original_spacing)]
189
- return sitk.Resample(image, new_size, sitk.Transform(), interpolator,
190
- image.GetOrigin(), new_spacing, image.GetDirection(), 0, # default pixel value
191
- image.GetPixelID())
 
 
 
 
 
 
 
 
192
 
193
 
194
  def read_image(path):
195
-
196
  img = sitk.ReadImage(path)
197
  img = sitk.DICOMOrient(img, "LPS")
198
  img = make_isotropic(img)
199
  img = sitk.GetArrayFromImage(img)
200
-
201
  return img
 
4
 
5
  import matplotlib as mpl
6
  import matplotlib.pyplot as plt
 
7
  import numpy as np
8
+ import SimpleITK as sitk
9
  import torch
 
10
  from mrsegmentator import inference
11
  from mrsegmentator.utils import add_postfix
12
+ from PIL import Image
13
  from scipy import ndimage
 
 
14
 
15
  import streamlit as st
16
 
 
60
  if st.session_state.option is not None:
61
  image = Path(__file__).parent / str(st.session_state.option)
62
 
63
+ inference.infer([image], tmpdirname, st.session_state.folds, split_level=1)
64
  seg_name = add_postfix(image.name, "seg")
65
  preds_path = tmpdirname + "/" + seg_name
66
  st.session_state.preds_3D = read_image(preds_path)
 
75
  x2_prompt = int(x2 * 256.0 / 325.0)
76
  y2_prompt = int(y2 * 256.0 / 325.0)
77
  z2_prompt = int(z2 * 32.0 / 325.0)
78
+ return torch.tensor(np.array([z1_prompt, y1_prompt, x1_prompt, z2_prompt, y2_prompt, x2_prompt]))
 
 
79
 
80
 
81
  def reflect_json_data_to_3D_box(json_data, view):
 
83
  st.session_state.rectangle_3Dbox[1] = json_data["objects"][0]["top"]
84
  st.session_state.rectangle_3Dbox[2] = json_data["objects"][0]["left"]
85
  st.session_state.rectangle_3Dbox[4] = (
86
+ json_data["objects"][0]["top"] + json_data["objects"][0]["height"] * json_data["objects"][0]["scaleY"]
 
87
  )
88
  st.session_state.rectangle_3Dbox[5] = (
89
+ json_data["objects"][0]["left"] + json_data["objects"][0]["width"] * json_data["objects"][0]["scaleX"]
 
90
  )
91
  print(st.session_state.rectangle_3Dbox)
92
 
93
 
94
+ def make_fig(image, preds, px_range=(10, 400), transparency=0.5):
95
 
96
+ fig, ax = plt.subplots(1, 1, figsize=(4, 4))
97
  image_slice = image.clip(*px_range)
98
 
99
  ax.imshow(
 
143
  plt.axis("off")
144
  ax.set_xticks([])
145
  ax.set_yticks([])
146
+
147
  fig.canvas.draw()
148
 
149
  # transform to image
 
153
  #######################################
154
 
155
 
156
+ def make_isotropic(image, interpolator=sitk.sitkLinear, spacing=None):
157
+ """
158
  Many file formats (e.g. jpg, png,...) expect the pixels to be isotropic, same
159
  spacing for all axes. Saving non-isotropic data in these formats will result in
160
  distorted images. This function makes an image isotropic via resampling, if needed.
 
168
  Returns:
169
  SimpleITK.Image with isotropic spacing which occupies the same region in space as
170
  the input image.
171
+ """
172
  original_spacing = image.GetSpacing()
173
  # Image is already isotropic, just return a copy.
174
  if all(spc == original_spacing[0] for spc in original_spacing):
 
177
  original_size = image.GetSize()
178
  if spacing is None:
179
  spacing = min(original_spacing)
180
+ new_spacing = [spacing] * image.GetDimension()
181
+ new_size = [int(round(osz * ospc / spacing)) for osz, ospc in zip(original_size, original_spacing)]
182
+ return sitk.Resample(
183
+ image,
184
+ new_size,
185
+ sitk.Transform(),
186
+ interpolator,
187
+ image.GetOrigin(),
188
+ new_spacing,
189
+ image.GetDirection(),
190
+ 0, # default pixel value
191
+ image.GetPixelID(),
192
+ )
193
 
194
 
195
  def read_image(path):
196
+
197
  img = sitk.ReadImage(path)
198
  img = sitk.DICOMOrient(img, "LPS")
199
  img = make_isotropic(img)
200
  img = sitk.GetArrayFromImage(img)
201
+
202
  return img