andreped commited on
Commit
660bf8a
1 Parent(s): 3adcffd

livermask gradio demo works locally

Browse files
Files changed (2) hide show
  1. .gitignore +2 -1
  2. demo/app.py +39 -14
.gitignore CHANGED
@@ -13,4 +13,5 @@ gradio_cached_examples/
13
  flagged/
14
  files/
15
  *.csv
16
- *.txt
 
 
13
  flagged/
14
  files/
15
  *.csv
16
+ *.txt
17
+ *.obj
demo/app.py CHANGED
@@ -1,22 +1,47 @@
1
  import gradio as gr
2
  import subprocess as sp
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
- def download_testdata():
5
- sp.check_call(["wget", "https://github.com/gradio-app/gradio/raw/main/demo/model3D/files/Duck.glb"])
6
 
7
  def load_mesh(mesh_file_name):
8
- return mesh_file_name
9
-
10
- demo = gr.Interface(
11
- fn=load_mesh,
12
- inputs=gr.Model3D(),
13
- outputs=gr.Model3D(clear_color=[0.0, 0.0, 0.0, 0.0], label="3D Model"),
14
- examples=[
15
- ["Duck.glb"],
16
- ],
17
- cache_examples=True,
18
- )
19
 
20
  if __name__ == "__main__":
21
- download_testdata()
 
 
 
 
 
22
  demo.launch()
 
1
  import gradio as gr
2
  import subprocess as sp
3
+ from skimage.measure import marching_cubes
4
+ import nibabel as nib
5
+ from nibabel.processing import resample_to_output
6
+
7
+
8
+ def nifti_to_glb(path):
9
+ # load NIFTI into numpy array
10
+ image = nib.load(path)
11
+ resampled = resample_to_output(image, [1, 1, 1], order=1)
12
+ data = resampled.get_fdata().astype("uint8")
13
+
14
+ # extract surface
15
+ verts, faces, normals, values = marching_cubes(data, 0)
16
+ faces += 1
17
+
18
+ with open('prediction.obj', 'w') as thefile:
19
+ for item in verts:
20
+ thefile.write("v {0} {1} {2}\n".format(item[0],item[1],item[2]))
21
+
22
+ for item in normals:
23
+ thefile.write("vn {0} {1} {2}\n".format(item[0],item[1],item[2]))
24
+
25
+ for item in faces:
26
+ thefile.write("f {0}//{0} {1}//{1} {2}//{2}\n".format(item[0],item[1],item[2]))
27
+
28
+
29
+ def run_model(input_path):
30
+ sp.check_call(["livermask", "--input", input_path, "--output", "prediction", "--verbose"])
31
 
 
 
32
 
33
  def load_mesh(mesh_file_name):
34
+ path = mesh_file_name.name
35
+ run_model(path)
36
+ nifti_to_glb("prediction-livermask.nii")
37
+ return "./prediction.obj"
38
+
 
 
 
 
 
 
39
 
40
  if __name__ == "__main__":
41
+ demo = gr.Interface(
42
+ fn=load_mesh,
43
+ inputs=gr.UploadButton(label="Click to Upload a File", file_type=[".nii", ".nii.nz"], file_count="single"),
44
+ outputs=gr.Model3D(clear_color=[0.0, 0.0, 0.0, 0.0], label="3D Model"),
45
+ )
46
+
47
  demo.launch()