Spaces:
Sleeping
Sleeping
dbouget
commited on
Commit
•
b68937a
1
Parent(s):
6b70d16
Missing utils file[skip ci]
Browse files- .gitignore +5 -0
- AeroPath/utils.py +67 -0
.gitignore
CHANGED
@@ -7,3 +7,8 @@ venv/
|
|
7 |
*.ini
|
8 |
*__pycache__/
|
9 |
*.DS_Store
|
|
|
|
|
|
|
|
|
|
|
|
7 |
*.ini
|
8 |
*__pycache__/
|
9 |
*.DS_Store
|
10 |
+
*.json
|
11 |
+
*.onnx
|
12 |
+
*.xml
|
13 |
+
*.txt
|
14 |
+
*.obj
|
AeroPath/utils.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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):
|
8 |
+
if type(data_path) != str:
|
9 |
+
data_path = data_path.name
|
10 |
+
|
11 |
+
image = nib.load(data_path)
|
12 |
+
resampled = resample_to_output(image, None, order=0)
|
13 |
+
data = resampled.get_fdata()
|
14 |
+
|
15 |
+
data = np.rot90(data, k=1, axes=(0, 1))
|
16 |
+
|
17 |
+
data[data < -1024] = -1024
|
18 |
+
data[data > 1024] = 1024
|
19 |
+
|
20 |
+
data = data - np.amin(data)
|
21 |
+
data = data / np.amax(data) * 255
|
22 |
+
data = data.astype("uint8")
|
23 |
+
|
24 |
+
print(data.shape)
|
25 |
+
return [data[..., i] for i in range(data.shape[-1])]
|
26 |
+
|
27 |
+
|
28 |
+
def load_pred_volume_to_numpy(data_path):
|
29 |
+
if type(data_path) != str:
|
30 |
+
data_path = data_path.name
|
31 |
+
|
32 |
+
image = nib.load(data_path)
|
33 |
+
resampled = resample_to_output(image, None, order=0)
|
34 |
+
data = resampled.get_fdata()
|
35 |
+
|
36 |
+
data = np.rot90(data, k=1, axes=(0, 1))
|
37 |
+
|
38 |
+
data[data > 0] = 1
|
39 |
+
data = data.astype("uint8")
|
40 |
+
|
41 |
+
print(data.shape)
|
42 |
+
return [data[..., i] for i in range(data.shape[-1])]
|
43 |
+
|
44 |
+
|
45 |
+
def nifti_to_glb(path, output="prediction.obj"):
|
46 |
+
# load NIFTI into numpy array
|
47 |
+
image = nib.load(path)
|
48 |
+
resampled = resample_to_output(image, [1, 1, 1], order=1)
|
49 |
+
data = resampled.get_fdata().astype("uint8")
|
50 |
+
|
51 |
+
# extract surface
|
52 |
+
verts, faces, normals, values = marching_cubes(data, 0)
|
53 |
+
faces += 1
|
54 |
+
|
55 |
+
with open(output, "w") as thefile:
|
56 |
+
for item in verts:
|
57 |
+
thefile.write("v {0} {1} {2}\n".format(item[0], item[1], item[2]))
|
58 |
+
|
59 |
+
for item in normals:
|
60 |
+
thefile.write("vn {0} {1} {2}\n".format(item[0], item[1], item[2]))
|
61 |
+
|
62 |
+
for item in faces:
|
63 |
+
thefile.write(
|
64 |
+
"f {0}//{0} {1}//{1} {2}//{2}\n".format(
|
65 |
+
item[0], item[1], item[2]
|
66 |
+
)
|
67 |
+
)
|