diff --git a/monailabel/config.py b/monailabel/config.py index e1efb35b7..4de6c896f 100644 --- a/monailabel/config.py +++ b/monailabel/config.py @@ -9,6 +9,7 @@ # See the License for the specific language governing permissions and # limitations under the License. import os +from distutils.util import strtobool from importlib.metadata import distributions from typing import Any, Dict, List, Optional @@ -115,6 +116,10 @@ class Settings(BaseSettings): else "https://huggingface.co/facebook/sam2-hiera-large/resolve/main/sam2_hiera_l.yaml" ) + MONAI_LABEL_USE_ITK_FOR_DICOM_SEG: bool = bool( + strtobool(os.environ.get("MONAI_LABEL_USE_ITK_FOR_DICOM_SEG", "True")) + ) + model_config = SettingsConfigDict( env_file=".env", case_sensitive=True, diff --git a/monailabel/datastore/utils/convert.py b/monailabel/datastore/utils/convert.py index c39d0f0d7..f5429a1ef 100644 --- a/monailabel/datastore/utils/convert.py +++ b/monailabel/datastore/utils/convert.py @@ -23,6 +23,7 @@ from monai.transforms import LoadImage from pydicom.filereader import dcmread +from monailabel.config import settings from monailabel.datastore.utils.colors import GENERIC_ANATOMY_COLORS from monailabel.transform.writer import write_itk from monailabel.utils.others.generic import run_command @@ -80,7 +81,12 @@ def binary_to_image(reference_image, label, dtype=np.uint8, file_ext=".nii.gz"): return output_file -def nifti_to_dicom_seg(series_dir, label, label_info, file_ext="*", use_itk=True) -> str: +def nifti_to_dicom_seg(series_dir, label, label_info, file_ext="*", use_itk=None) -> str: + + # Only use config if no explicit override + if use_itk is None: + use_itk = settings.MONAI_LABEL_USE_ITK_FOR_DICOM_SEG + start = time.time() label_np, meta_dict = LoadImage(image_only=False)(label) diff --git a/monailabel/endpoints/infer.py b/monailabel/endpoints/infer.py index abf2cc4cc..aa5d664e8 100644 --- a/monailabel/endpoints/infer.py +++ b/monailabel/endpoints/infer.py @@ -183,7 +183,7 @@ def run_inference( suffixes = [".nii", ".nii.gz", ".nrrd"] image_path = [image_uri.replace(suffix, "") for suffix in suffixes if image_uri.endswith(suffix)][0] res_img = result.get("file") if result.get("file") else result.get("label") - dicom_seg_file = nifti_to_dicom_seg(image_path, res_img, p.get("label_info"), use_itk=True) + dicom_seg_file = nifti_to_dicom_seg(image_path, res_img, p.get("label_info")) result["dicom_seg"] = dicom_seg_file return send_response(instance.datastore(), result, output, background_tasks)