Skip to content

Commit

Permalink
Merge pull request #39 from andreped/gui
Browse files Browse the repository at this point in the history
nifti_to_obj rename and moved to convert; minor refactoring
  • Loading branch information
andreped authored Oct 31, 2023
2 parents f558298 + 1e90a57 commit 0e46e8b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 37 deletions.
35 changes: 35 additions & 0 deletions demo/src/convert.py
Original file line number Diff line number Diff line change
@@ -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]
)
)
5 changes: 2 additions & 3 deletions demo/src/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

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
from .logger import read_logs
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()
Expand Down Expand Up @@ -82,7 +82,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)
Expand Down Expand Up @@ -113,7 +113,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",
Expand Down
1 change: 1 addition & 0 deletions demo/src/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 0 additions & 34 deletions demo/src/utils.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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]
)
)

0 comments on commit 0e46e8b

Please sign in to comment.