Thomas Male
commited on
Update handler.py
Browse files- handler.py +29 -2
handler.py
CHANGED
@@ -8,10 +8,13 @@ from point_e.diffusion.sampler import PointCloudSampler
|
|
8 |
from point_e.models.download import load_checkpoint
|
9 |
from point_e.models.configs import MODEL_CONFIGS, model_from_config
|
10 |
from point_e.util.plotting import plot_point_cloud
|
|
|
|
|
11 |
import json
|
12 |
import base64
|
13 |
import numpy as np
|
14 |
from io import BytesIO
|
|
|
15 |
|
16 |
|
17 |
# set device
|
@@ -24,6 +27,7 @@ class EndpointHandler():
|
|
24 |
def __init__(self, path=""):
|
25 |
# load the optimized model
|
26 |
print('creating base model...')
|
|
|
27 |
|
28 |
print('creating base model...')
|
29 |
self.base_name = 'base40M-textvec'
|
@@ -49,6 +53,14 @@ class EndpointHandler():
|
|
49 |
|
50 |
print('downloading upsampler checkpoint...')
|
51 |
self.upsampler_model.load_state_dict(load_checkpoint('upsample', device))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
def __call__(self, data: Any) -> List[List[Dict[str, float]]]:
|
54 |
"""
|
@@ -113,6 +125,20 @@ class EndpointHandler():
|
|
113 |
pc = sampler.output_to_point_clouds(samples)[0]
|
114 |
print('type of pc: ', type(pc))
|
115 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
pc_dict = {}
|
117 |
|
118 |
data_list = pc.coords.tolist()
|
@@ -125,5 +151,6 @@ class EndpointHandler():
|
|
125 |
# Serialize the dictionary to a JSON-formatted string
|
126 |
channel_data = json.dumps(serializable_channels)
|
127 |
pc_dict['channels'] = channel_data
|
128 |
-
|
129 |
-
return pc_dict
|
|
|
|
8 |
from point_e.models.download import load_checkpoint
|
9 |
from point_e.models.configs import MODEL_CONFIGS, model_from_config
|
10 |
from point_e.util.plotting import plot_point_cloud
|
11 |
+
from point_e.util.pc_to_mesh import marching_cubes_mesh
|
12 |
+
from point_e.util.point_cloud import PointCloud
|
13 |
import json
|
14 |
import base64
|
15 |
import numpy as np
|
16 |
from io import BytesIO
|
17 |
+
import os
|
18 |
|
19 |
|
20 |
# set device
|
|
|
27 |
def __init__(self, path=""):
|
28 |
# load the optimized model
|
29 |
print('creating base model...')
|
30 |
+
os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"
|
31 |
|
32 |
print('creating base model...')
|
33 |
self.base_name = 'base40M-textvec'
|
|
|
53 |
|
54 |
print('downloading upsampler checkpoint...')
|
55 |
self.upsampler_model.load_state_dict(load_checkpoint('upsample', device))
|
56 |
+
|
57 |
+
print('creating SDF model...')
|
58 |
+
self.sdf_name = 'sdf'
|
59 |
+
self.sdf_model = model_from_config(MODEL_CONFIGS[self.sdf_name], device)
|
60 |
+
self.sdf_model.eval()
|
61 |
+
|
62 |
+
print('loading SDF model...')
|
63 |
+
self.sdf_model.load_state_dict(load_checkpoint(self.sdf_name, device))
|
64 |
|
65 |
def __call__(self, data: Any) -> List[List[Dict[str, float]]]:
|
66 |
"""
|
|
|
125 |
pc = sampler.output_to_point_clouds(samples)[0]
|
126 |
print('type of pc: ', type(pc))
|
127 |
|
128 |
+
# Produce a mesh (with vertex colors)
|
129 |
+
mesh = marching_cubes_mesh(
|
130 |
+
pc=pc,
|
131 |
+
model=self.sdf_model,
|
132 |
+
batch_size=4096,
|
133 |
+
grid_size=32, # increase to 128 for resolution used in evals
|
134 |
+
progress=True,
|
135 |
+
)
|
136 |
+
|
137 |
+
# Write the mesh to a PLY file to import into some other program.
|
138 |
+
with open('mesh.ply', 'wb') as f:
|
139 |
+
mesh.write_ply(f)
|
140 |
+
print(mesh)
|
141 |
+
|
142 |
pc_dict = {}
|
143 |
|
144 |
data_list = pc.coords.tolist()
|
|
|
151 |
# Serialize the dictionary to a JSON-formatted string
|
152 |
channel_data = json.dumps(serializable_channels)
|
153 |
pc_dict['channels'] = channel_data
|
154 |
+
|
155 |
+
#return pc_dict
|
156 |
+
return mesh
|