赞
踩
$ pip install gradio==4.29 -i "https://pypi.doubanio.com/simple/"
$ pip install SimpleITK==2.3.1 -i "https://pypi.doubanio.com/simple/"
dicom2nii-webui # 目录
--/lib_SimpleITK.py # py文件
--/main.py # py文件,入口
import SimpleITK as sitk import os def dicom_to_nii(dicom_dir_path: str, nii_file_path: str): """ dicom文件转nii文件 """ # 读取 dicom reader = sitk.ImageSeriesReader() img_name = reader.GetGDCMSeriesFileNames(dicom_dir_path) reader.SetFileNames(img_name) image = reader.Execute() # 转为 nii.gz image_array = sitk.GetArrayFromImage(image) image_out = sitk.GetImageFromArray(image_array) image_out.SetOrigin(image.GetOrigin()) image_out.SetSpacing(image.GetSpacing()) image_out.SetDirection(image.GetDirection()) # 保存nii.gz文件到配置的目录下 example_nii_dir_path = os.path.dirname(nii_file_path) if not os.path.exists(example_nii_dir_path) or not os.path.isdir(example_nii_dir_path): os.mkdir(example_nii_dir_path) sitk.WriteImage(image, nii_file_path)
import gradio as gr import os import zipfile import lib_SimpleITK example_dicom_dir_path = 'D:/project/xxx/Task/ABC_Test_02/xxx' example_nii_file_path = 'D:/project/res/result_image.nii.gz' def service_local_dicom_to_nii(dicom_dir_path: str, nii_file_path: str): # 校验 if not dicom_dir_path or dicom_dir_path.strip() == "": gr.Warning(f"请填入[dicom文件目录路径],例如'{example_dicom_dir_path}'") return if not nii_file_path or nii_file_path.strip() == "": nii_file_path = os.path.join(dicom_dir_path, "result_image.nii.gz") try: lib_SimpleITK.dicom_to_nii(os.path.abspath(dicom_dir_path), os.path.abspath(nii_file_path)) except Exception as e: raise gr.Error("dicom 转 nii 时,发生异常:" + str(e)) return nii_file_path, nii_file_path local_iface = gr.Interface( fn=service_local_dicom_to_nii, inputs=[ gr.Textbox(label="dicom文件目录路径", info=f"例如'{example_dicom_dir_path}'"), gr.Textbox(label="生成的nii文件路径", info=f"例如'{example_nii_file_path}'。如果不填,会输出到dicom目录下。") ], outputs=[gr.Textbox(label="生成的nii文件路径"), gr.File(label="生成的nii文件")], description="在本地电脑端启动时使用。利用 SimpleITK 库,将 dicom 文件转为 nii文件。", ) def unzip_file(zip_file_path: str, save_dir_path: str): with zipfile.ZipFile(zip_file_path, "r") as zfile: # 中文有乱码问题 zfile.extractall(save_dir_path) def service_server_dicom_to_nii(file): # 解压 save_dir_path = os.path.dirname(file.name) print(save_dir_path) unzip_file(file.name, save_dir_path) # dicom 转 nii dicom_dir_path = save_dir_path nii_file_path = os.path.join(save_dir_path, "result_image.nii.gz") try: lib_SimpleITK.dicom_to_nii(dicom_dir_path, nii_file_path) except Exception as e: raise gr.Error("dicom 转 nii 时,发生异常:" + str(e)) # return nii_file_path, nii_file_path return file.name, file.name server_iface = gr.Interface( fn=service_server_dicom_to_nii, inputs=[gr.File(label="dicom文件目录的压缩包(建议不要带中文)")], outputs=[gr.Textbox(label="生成的nii文件路径"), gr.File(label="生成的nii文件")], description="在服务器端启动时使用。利用 SimpleITK 库,将 dicom 文件转为 nii文件。", ) tabbed_interface = gr.TabbedInterface( interface_list = [local_iface, server_iface], tab_names = ["本地端", "通用端"], title="dicom 转 nii" ) tabbed_interface.launch()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。