文章目录 Gradio 案例——将 dicom 文件转为 nii文件
Gradio 案例——将 dicom 文件转为 nii文件
利用 SimpleITK 库,将 dicom 文件转为 nii文件 更完整、丰富的示例项目见 GitHub - AlionSSS/dcm2niix-webui: The web UI for dcm2niix (DICOM to NIfTI converter)
界面截图
依赖安装
新建一个虚拟环境 Python 3.9.16 依赖 $ 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 osdef dicom_to_nii ( dicom_dir_path: str , nii_file_path: str ) : """dicom文件转nii文件""" reader = sitk. ImageSeriesReader( ) img_name = reader. GetGDCMSeriesFileNames( dicom_dir_path) reader. SetFileNames( img_name) image = reader. Execute( ) 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( ) ) 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_SimpleITKexample_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_dir_path = save_dir_pathnii_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 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( )