Spaces:
Runtime error
Runtime error
eliphatfs
commited on
Commit
·
02f3e52
1
Parent(s):
b23a3f1
Fix loading and render.
Browse files- app.py +7 -5
- misc_utils.py +30 -6
app.py
CHANGED
@@ -58,7 +58,8 @@ def load_data():
|
|
58 |
|
59 |
|
60 |
def render_pc(pc):
|
61 |
-
|
|
|
62 |
rgb = (pc[:, 3:] * 255).astype(numpy.uint8)
|
63 |
g = go.Scatter3d(
|
64 |
x=pc[:, 0], y=pc[:, 1], z=pc[:, 2],
|
@@ -66,6 +67,7 @@ def render_pc(pc):
|
|
66 |
marker=dict(size=2, color=[f'rgb({rgb[i, 0]}, {rgb[i, 1]}, {rgb[i, 2]})' for i in range(len(pc))]),
|
67 |
)
|
68 |
fig = go.Figure(data=[g])
|
|
|
69 |
st.plotly_chart(fig)
|
70 |
# st.caption("Point Cloud Preview")
|
71 |
|
@@ -87,10 +89,10 @@ try:
|
|
87 |
with tab_pc2img:
|
88 |
prompt = st.text_input("Prompt")
|
89 |
noise_scale = st.slider('Variation Level', 0, 5)
|
90 |
-
cfg_scale = st.slider('Guidance Scale', 0.0, 30.0,
|
91 |
-
steps = st.slider('Diffusion Steps',
|
92 |
-
width = st.slider('Width',
|
93 |
-
height = st.slider('Height',
|
94 |
if st.button("Generate"):
|
95 |
pc = load_data()
|
96 |
render_pc(pc)
|
|
|
58 |
|
59 |
|
60 |
def render_pc(pc):
|
61 |
+
rand = numpy.random.permutation(len(pc))[:2048]
|
62 |
+
pc = pc[rand]
|
63 |
rgb = (pc[:, 3:] * 255).astype(numpy.uint8)
|
64 |
g = go.Scatter3d(
|
65 |
x=pc[:, 0], y=pc[:, 1], z=pc[:, 2],
|
|
|
67 |
marker=dict(size=2, color=[f'rgb({rgb[i, 0]}, {rgb[i, 1]}, {rgb[i, 2]})' for i in range(len(pc))]),
|
68 |
)
|
69 |
fig = go.Figure(data=[g])
|
70 |
+
fig.update_layout(scene_camera=dict(up=dict(x=0, y=1, z=0)))
|
71 |
st.plotly_chart(fig)
|
72 |
# st.caption("Point Cloud Preview")
|
73 |
|
|
|
89 |
with tab_pc2img:
|
90 |
prompt = st.text_input("Prompt")
|
91 |
noise_scale = st.slider('Variation Level', 0, 5)
|
92 |
+
cfg_scale = st.slider('Guidance Scale', 0.0, 30.0, 3.0)
|
93 |
+
steps = st.slider('Diffusion Steps', 8, 80, 10)
|
94 |
+
width = st.slider('Width', 480, 640, step=32)
|
95 |
+
height = st.slider('Height', 480, 640, step=32)
|
96 |
if st.button("Generate"):
|
97 |
pc = load_data()
|
98 |
render_pc(pc)
|
misc_utils.py
CHANGED
@@ -34,7 +34,24 @@ def model_to_pc(mesh: trimesh.Trimesh, n_sample_points=10000):
|
|
34 |
elif isinstance(mesh.visual, trimesh.visual.TextureVisuals):
|
35 |
bc = trimesh.proximity.points_to_barycentric(mesh.triangles[face_idx], pcd)
|
36 |
uv = numpy.einsum('ntc,nt->nc', mesh.visual.uv[mesh.faces[face_idx]], bc)
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
if rgba.max() > 1:
|
39 |
if rgba.max() > 255:
|
40 |
rgba = rgba.astype(f32) / rgba.max()
|
@@ -45,11 +62,18 @@ def model_to_pc(mesh: trimesh.Trimesh, n_sample_points=10000):
|
|
45 |
|
46 |
def trimesh_to_pc(scene_or_mesh):
|
47 |
if isinstance(scene_or_mesh, trimesh.Scene):
|
48 |
-
meshes = [
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
if not len(meshes):
|
54 |
return None
|
55 |
return numpy.concatenate(meshes)
|
|
|
34 |
elif isinstance(mesh.visual, trimesh.visual.TextureVisuals):
|
35 |
bc = trimesh.proximity.points_to_barycentric(mesh.triangles[face_idx], pcd)
|
36 |
uv = numpy.einsum('ntc,nt->nc', mesh.visual.uv[mesh.faces[face_idx]], bc)
|
37 |
+
material = mesh.visual.material
|
38 |
+
if hasattr(material, 'materials'):
|
39 |
+
if len(material.materials) == 0:
|
40 |
+
rgba = numpy.ones_like(pcd) * 0.8
|
41 |
+
texture = None
|
42 |
+
st.warning("Empty MultiMaterial found, falling back to light grey")
|
43 |
+
else:
|
44 |
+
material = material.materials[0]
|
45 |
+
if hasattr(material, 'image'):
|
46 |
+
texture = material.image
|
47 |
+
elif hasattr(material, 'baseColorTexture'):
|
48 |
+
texture = material.baseColorTexture
|
49 |
+
else:
|
50 |
+
texture = None
|
51 |
+
rgba = numpy.ones_like(pcd) * 0.8
|
52 |
+
st.warning("Unknown material, falling back to light grey")
|
53 |
+
if texture is not None:
|
54 |
+
rgba = trimesh.visual.uv_to_interpolated_color(uv, texture)
|
55 |
if rgba.max() > 1:
|
56 |
if rgba.max() > 255:
|
57 |
rgba = rgba.astype(f32) / rgba.max()
|
|
|
62 |
|
63 |
def trimesh_to_pc(scene_or_mesh):
|
64 |
if isinstance(scene_or_mesh, trimesh.Scene):
|
65 |
+
meshes = []
|
66 |
+
for node_name in scene_or_mesh.graph.nodes_geometry:
|
67 |
+
# which geometry does this node refer to
|
68 |
+
transform, geometry_name = scene_or_mesh.graph[node_name]
|
69 |
+
|
70 |
+
# get the actual potential mesh instance
|
71 |
+
geometry = scene_or_mesh.geometry[geometry_name].copy()
|
72 |
+
if not hasattr(geometry, 'triangles'):
|
73 |
+
continue
|
74 |
+
geometry: trimesh.Trimesh
|
75 |
+
geometry = geometry.apply_transform(transform)
|
76 |
+
meshes.append(model_to_pc(geometry, 10000 // len(scene_or_mesh.geometry)))
|
77 |
if not len(meshes):
|
78 |
return None
|
79 |
return numpy.concatenate(meshes)
|