From fec580984dd9dc79ada1323a59ca122feec52c23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Pedersen?= Date: Tue, 31 Oct 2023 15:07:18 +0100 Subject: [PATCH 1/2] Renamed to nifti_to_obj and moved to convert --- demo/src/convert.py | 35 +++++++++++++++++++++++++++++++++++ demo/src/gui.py | 6 +++--- demo/src/utils.py | 34 ---------------------------------- 3 files changed, 38 insertions(+), 37 deletions(-) create mode 100644 demo/src/convert.py diff --git a/demo/src/convert.py b/demo/src/convert.py new file mode 100644 index 0000000..d4b97da --- /dev/null +++ b/demo/src/convert.py @@ -0,0 +1,35 @@ +import nibabel as nib +from nibabel.processing import resample_to_output +from skimage.measure import marching_cubes + + +def nifti_to_obj(path, output="prediction.obj"): + # load NIFTI into numpy array + image = nib.load(path) + resampled = resample_to_output(image, [1, 1, 1], order=1) + data = resampled.get_fdata().astype("uint8") + + # Create a material with a red diffuse color (RGB value) + red_material = "newmtl RedMaterial\nKd 1 0 0" # Red diffuse color (RGB) + + # extract surface + verts, faces, normals, values = marching_cubes(data, 0) + faces += 1 + + with open(output, "w") as thefile: + # Write the material definition to the OBJ file + thefile.write(red_material + "\n") + + for item in verts: + # thefile.write('usemtl RedMaterial\n') + thefile.write("v {0} {1} {2}\n".format(item[0], item[1], item[2])) + + for item in normals: + thefile.write("vn {0} {1} {2}\n".format(item[0], item[1], item[2])) + + for item in faces: + thefile.write( + "f {0}//{0} {1}//{1} {2}//{2}\n".format( + item[0], item[1], item[2] + ) + ) diff --git a/demo/src/gui.py b/demo/src/gui.py index 1d6b71a..7c6ec13 100644 --- a/demo/src/gui.py +++ b/demo/src/gui.py @@ -2,6 +2,7 @@ import gradio as gr +from .convert import nifti_to_obj from .css_style import css from .inference import run_model from .logger import flush_logs @@ -9,7 +10,7 @@ from .logger import setup_logger from .utils import load_ct_to_numpy from .utils import load_pred_volume_to_numpy -from .utils import nifti_to_glb + # setup logging LOGGER = setup_logger() @@ -82,7 +83,7 @@ def process(self, mesh_file_name): name=self.result_names[self.class_name], ) LOGGER.info("Converting prediction NIfTI to OBJ...") - nifti_to_glb("prediction.nii.gz") + nifti_to_obj("prediction.nii.gz") LOGGER.info("Loading CT to numpy...") self.images = load_ct_to_numpy(path) @@ -113,7 +114,6 @@ def run(self): with gr.Blocks(css=css) as demo: with gr.Row(): with gr.Column(visible=True, scale=0.2) as sidebar_left: - # gr.Markdown("SideBar Left") logs = gr.Textbox( placeholder="\n" * 16, label="Logs", diff --git a/demo/src/utils.py b/demo/src/utils.py index 4dce092..4a7f9f9 100644 --- a/demo/src/utils.py +++ b/demo/src/utils.py @@ -1,7 +1,5 @@ import nibabel as nib import numpy as np -from nibabel.processing import resample_to_output -from skimage.measure import marching_cubes def load_ct_to_numpy(data_path): @@ -38,35 +36,3 @@ def load_pred_volume_to_numpy(data_path): print(data.shape) return [data[..., i] for i in range(data.shape[-1])] - - -def nifti_to_glb(path, output="prediction.obj"): - # load NIFTI into numpy array - image = nib.load(path) - resampled = resample_to_output(image, [1, 1, 1], order=1) - data = resampled.get_fdata().astype("uint8") - - # Create a material with a red diffuse color (RGB value) - red_material = "newmtl RedMaterial\nKd 1 0 0" # Red diffuse color (RGB) - - # extract surface - verts, faces, normals, values = marching_cubes(data, 0) - faces += 1 - - with open(output, "w") as thefile: - # Write the material definition to the OBJ file - thefile.write(red_material + "\n") - - for item in verts: - # thefile.write('usemtl RedMaterial\n') - thefile.write("v {0} {1} {2}\n".format(item[0], item[1], item[2])) - - for item in normals: - thefile.write("vn {0} {1} {2}\n".format(item[0], item[1], item[2])) - - for item in faces: - thefile.write( - "f {0}//{0} {1}//{1} {2}//{2}\n".format( - item[0], item[1], item[2] - ) - ) From 1e90a57aeba564f1ff0ff18fe35521b33eea639c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Pedersen?= Date: Tue, 31 Oct 2023 15:08:27 +0100 Subject: [PATCH 2/2] Minor refactoring --- demo/src/gui.py | 1 - demo/src/inference.py | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/demo/src/gui.py b/demo/src/gui.py index 7c6ec13..74df10d 100644 --- a/demo/src/gui.py +++ b/demo/src/gui.py @@ -11,7 +11,6 @@ from .utils import load_ct_to_numpy from .utils import load_pred_volume_to_numpy - # setup logging LOGGER = setup_logger() diff --git a/demo/src/inference.py b/demo/src/inference.py index 15a6ab1..5d3774c 100644 --- a/demo/src/inference.py +++ b/demo/src/inference.py @@ -91,6 +91,7 @@ def run_model( shutil.rmtree(patient_directory) if os.path.exists(output_path): shutil.rmtree(output_path) + except Exception: print(traceback.format_exc()) # Clean-up