andreped commited on
Commit
0e46e8b
2 Parent(s): f558298 1e90a57

Merge pull request #39 from andreped/gui

Browse files

nifti_to_obj rename and moved to convert; minor refactoring

demo/src/convert.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import nibabel as nib
2
+ from nibabel.processing import resample_to_output
3
+ from skimage.measure import marching_cubes
4
+
5
+
6
+ def nifti_to_obj(path, output="prediction.obj"):
7
+ # load NIFTI into numpy array
8
+ image = nib.load(path)
9
+ resampled = resample_to_output(image, [1, 1, 1], order=1)
10
+ data = resampled.get_fdata().astype("uint8")
11
+
12
+ # Create a material with a red diffuse color (RGB value)
13
+ red_material = "newmtl RedMaterial\nKd 1 0 0" # Red diffuse color (RGB)
14
+
15
+ # extract surface
16
+ verts, faces, normals, values = marching_cubes(data, 0)
17
+ faces += 1
18
+
19
+ with open(output, "w") as thefile:
20
+ # Write the material definition to the OBJ file
21
+ thefile.write(red_material + "\n")
22
+
23
+ for item in verts:
24
+ # thefile.write('usemtl RedMaterial\n')
25
+ thefile.write("v {0} {1} {2}\n".format(item[0], item[1], item[2]))
26
+
27
+ for item in normals:
28
+ thefile.write("vn {0} {1} {2}\n".format(item[0], item[1], item[2]))
29
+
30
+ for item in faces:
31
+ thefile.write(
32
+ "f {0}//{0} {1}//{1} {2}//{2}\n".format(
33
+ item[0], item[1], item[2]
34
+ )
35
+ )
demo/src/gui.py CHANGED
@@ -2,6 +2,7 @@ import os
2
 
3
  import gradio as gr
4
 
 
5
  from .css_style import css
6
  from .inference import run_model
7
  from .logger import flush_logs
@@ -9,7 +10,6 @@ from .logger import read_logs
9
  from .logger import setup_logger
10
  from .utils import load_ct_to_numpy
11
  from .utils import load_pred_volume_to_numpy
12
- from .utils import nifti_to_glb
13
 
14
  # setup logging
15
  LOGGER = setup_logger()
@@ -82,7 +82,7 @@ class WebUI:
82
  name=self.result_names[self.class_name],
83
  )
84
  LOGGER.info("Converting prediction NIfTI to OBJ...")
85
- nifti_to_glb("prediction.nii.gz")
86
 
87
  LOGGER.info("Loading CT to numpy...")
88
  self.images = load_ct_to_numpy(path)
@@ -113,7 +113,6 @@ class WebUI:
113
  with gr.Blocks(css=css) as demo:
114
  with gr.Row():
115
  with gr.Column(visible=True, scale=0.2) as sidebar_left:
116
- # gr.Markdown("SideBar Left")
117
  logs = gr.Textbox(
118
  placeholder="\n" * 16,
119
  label="Logs",
 
2
 
3
  import gradio as gr
4
 
5
+ from .convert import nifti_to_obj
6
  from .css_style import css
7
  from .inference import run_model
8
  from .logger import flush_logs
 
10
  from .logger import setup_logger
11
  from .utils import load_ct_to_numpy
12
  from .utils import load_pred_volume_to_numpy
 
13
 
14
  # setup logging
15
  LOGGER = setup_logger()
 
82
  name=self.result_names[self.class_name],
83
  )
84
  LOGGER.info("Converting prediction NIfTI to OBJ...")
85
+ nifti_to_obj("prediction.nii.gz")
86
 
87
  LOGGER.info("Loading CT to numpy...")
88
  self.images = load_ct_to_numpy(path)
 
113
  with gr.Blocks(css=css) as demo:
114
  with gr.Row():
115
  with gr.Column(visible=True, scale=0.2) as sidebar_left:
 
116
  logs = gr.Textbox(
117
  placeholder="\n" * 16,
118
  label="Logs",
demo/src/inference.py CHANGED
@@ -91,6 +91,7 @@ def run_model(
91
  shutil.rmtree(patient_directory)
92
  if os.path.exists(output_path):
93
  shutil.rmtree(output_path)
 
94
  except Exception:
95
  print(traceback.format_exc())
96
  # Clean-up
 
91
  shutil.rmtree(patient_directory)
92
  if os.path.exists(output_path):
93
  shutil.rmtree(output_path)
94
+
95
  except Exception:
96
  print(traceback.format_exc())
97
  # Clean-up
demo/src/utils.py CHANGED
@@ -1,7 +1,5 @@
1
  import nibabel as nib
2
  import numpy as np
3
- from nibabel.processing import resample_to_output
4
- from skimage.measure import marching_cubes
5
 
6
 
7
  def load_ct_to_numpy(data_path):
@@ -38,35 +36,3 @@ def load_pred_volume_to_numpy(data_path):
38
 
39
  print(data.shape)
40
  return [data[..., i] for i in range(data.shape[-1])]
41
-
42
-
43
- def nifti_to_glb(path, output="prediction.obj"):
44
- # load NIFTI into numpy array
45
- image = nib.load(path)
46
- resampled = resample_to_output(image, [1, 1, 1], order=1)
47
- data = resampled.get_fdata().astype("uint8")
48
-
49
- # Create a material with a red diffuse color (RGB value)
50
- red_material = "newmtl RedMaterial\nKd 1 0 0" # Red diffuse color (RGB)
51
-
52
- # extract surface
53
- verts, faces, normals, values = marching_cubes(data, 0)
54
- faces += 1
55
-
56
- with open(output, "w") as thefile:
57
- # Write the material definition to the OBJ file
58
- thefile.write(red_material + "\n")
59
-
60
- for item in verts:
61
- # thefile.write('usemtl RedMaterial\n')
62
- thefile.write("v {0} {1} {2}\n".format(item[0], item[1], item[2]))
63
-
64
- for item in normals:
65
- thefile.write("vn {0} {1} {2}\n".format(item[0], item[1], item[2]))
66
-
67
- for item in faces:
68
- thefile.write(
69
- "f {0}//{0} {1}//{1} {2}//{2}\n".format(
70
- item[0], item[1], item[2]
71
- )
72
- )
 
1
  import nibabel as nib
2
  import numpy as np
 
 
3
 
4
 
5
  def load_ct_to_numpy(data_path):
 
36
 
37
  print(data.shape)
38
  return [data[..., i] for i in range(data.shape[-1])]