@@ -127,6 +127,44 @@ def overlap(self, val: float):
127
127
raise ValueError ("Overlap must be between 0 and 1." )
128
128
self ._overlap = val
129
129
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
+
130
168
def compute (self , op_input : InputContext , op_output : OutputContext , context : ExecutionContext ):
131
169
"""Infers with the input image and save the predicted image to output
132
170
@@ -145,11 +183,10 @@ def compute(self, op_input: InputContext, op_output: OutputContext, context: Exe
145
183
if not input_image :
146
184
raise ValueError ("Input is None." )
147
185
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" ))
153
190
154
191
pre_transforms : Compose = self ._pre_transform
155
192
post_transforms : Compose = self ._post_transforms
@@ -196,7 +233,7 @@ def compute(self, op_input: InputContext, op_output: OutputContext, context: Exe
196
233
out_ndarray = np .swapaxes (out_ndarray , 2 , 0 ).astype (np .uint8 )
197
234
print (f"Output Seg image numpy array shaped: { out_ndarray .shape } " )
198
235
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 )
200
237
op_output .set (out_image , "seg_image" )
201
238
finally :
202
239
# Reset state on completing this method execution.
0 commit comments