Skip to content

Commit c6b9ab1

Browse files
authored
Conver metadata types to native and assign metadata to in-mem output image obj (#186)
Signed-off-by: mmelqin <mingmelvinq@nvidia.com>
1 parent 9006298 commit c6b9ab1

File tree

1 file changed

+43
-6
lines changed

1 file changed

+43
-6
lines changed

monai/deploy/operators/monai_seg_inference_operator.py

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,44 @@ def overlap(self, val: float):
127127
raise ValueError("Overlap must be between 0 and 1.")
128128
self._overlap = val
129129

130+
def _convert_dicom_metadata_datatype(self, metadata: Dict):
131+
"""Converts metadata in pydicom types to the corresponding native types.
132+
133+
It is knwon that some values of the metadata are of the pydicom types, for images converted
134+
from DICOM series. Need to use this function to convert the types with best effort and for
135+
the few knowns metadata attributes, until the following issue is addressed:
136+
https://github.com/Project-MONAI/monai-deploy-app-sdk/issues/185
137+
138+
Args:
139+
metadata (Dict): The metadata for an Image object
140+
"""
141+
142+
if not metadata:
143+
return metadata
144+
145+
# Try to convert data type for the well knowned attributes. Add more as needed.
146+
if metadata.get("series_instance_uid", None):
147+
try:
148+
metadata["series_instance_uid"] = str(metadata["series_instance_uid"])
149+
except Exception:
150+
pass
151+
if metadata.get("row_pixel_spacing", None):
152+
try:
153+
metadata["row_pixel_spacing"] = float(metadata["row_pixel_spacing"])
154+
except Exception:
155+
pass
156+
if metadata.get("col_pixel_spacing", None):
157+
try:
158+
metadata["col_pixel_spacing"] = float(metadata["col_pixel_spacing"])
159+
except Exception:
160+
pass
161+
162+
print("Converted Image object metadata:")
163+
for k, v in metadata.items():
164+
print(f"{k}: {v}, type {type(v)}")
165+
166+
return metadata
167+
130168
def compute(self, op_input: InputContext, op_output: OutputContext, context: ExecutionContext):
131169
"""Infers with the input image and save the predicted image to output
132170
@@ -145,11 +183,10 @@ def compute(self, op_input: InputContext, op_output: OutputContext, context: Exe
145183
if not input_image:
146184
raise ValueError("Input is None.")
147185

148-
img_name = "Img_in_context"
149-
try:
150-
img_name = input_image.metadata().get["series_instance_uid", img_name]
151-
except Exception: # Best effort
152-
pass
186+
# Need to try to convert the data type of a few metadata attributes.
187+
input_img_metadata = self._convert_dicom_metadata_datatype(input_image.metadata())
188+
# Need to give a name to the image as in-mem Image obj has no name.
189+
img_name = str(input_img_metadata.get("series_instance_uid", "Img_in_context"))
153190

154191
pre_transforms: Compose = self._pre_transform
155192
post_transforms: Compose = self._post_transforms
@@ -196,7 +233,7 @@ def compute(self, op_input: InputContext, op_output: OutputContext, context: Exe
196233
out_ndarray = np.swapaxes(out_ndarray, 2, 0).astype(np.uint8)
197234
print(f"Output Seg image numpy array shaped: {out_ndarray.shape}")
198235
print(f"Output Seg image pixel max value: {np.amax(out_ndarray)}")
199-
out_image = Image(out_ndarray)
236+
out_image = Image(out_ndarray, input_img_metadata)
200237
op_output.set(out_image, "seg_image")
201238
finally:
202239
# Reset state on completing this method execution.

0 commit comments

Comments
 (0)