Spaces:
Sleeping
Sleeping
staswrs
commited on
Commit
·
8aa156a
1
Parent(s):
e98d874
clean scene 13
Browse files- app.py +17 -59
- requirements.txt +1 -1
app.py
CHANGED
@@ -1,3 +1,5 @@
|
|
|
|
|
|
1 |
import os
|
2 |
import subprocess
|
3 |
|
@@ -25,10 +27,6 @@ from inference_triposg import run_triposg
|
|
25 |
from triposg.pipelines.pipeline_triposg import TripoSGPipeline
|
26 |
from briarmbg import BriaRMBG
|
27 |
|
28 |
-
from pygltflib import GLTF2, Scene, Node, Mesh, Buffer, BufferView, Accessor, BufferTarget, ComponentType, AccessorType
|
29 |
-
import numpy as np
|
30 |
-
import base64
|
31 |
-
|
32 |
|
33 |
print("Trimesh version:", trimesh.__version__)
|
34 |
|
@@ -71,6 +69,7 @@ def generate(image_path, face_number=50000, guidance_scale=5.0, num_steps=25):
|
|
71 |
|
72 |
temp_id = str(uuid.uuid4())
|
73 |
output_path = f"/tmp/{temp_id}.glb"
|
|
|
74 |
|
75 |
try:
|
76 |
mesh = run_triposg(
|
@@ -86,62 +85,21 @@ def generate(image_path, face_number=50000, guidance_scale=5.0, num_steps=25):
|
|
86 |
if mesh is None or mesh.vertices.shape[0] == 0 or mesh.faces.shape[0] == 0:
|
87 |
raise ValueError("Mesh generation returned an empty mesh")
|
88 |
|
89 |
-
|
90 |
-
|
|
|
|
|
|
|
|
|
91 |
|
92 |
-
|
93 |
-
|
94 |
-
index_bytes = indices.tobytes()
|
95 |
-
total_bytes = vertex_bytes + index_bytes
|
96 |
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
target=BufferTarget.ARRAY_BUFFER.value
|
103 |
-
)
|
104 |
-
buffer_view_index = BufferView(
|
105 |
-
buffer=0,
|
106 |
-
byteOffset=len(vertex_bytes),
|
107 |
-
byteLength=len(index_bytes),
|
108 |
-
target=BufferTarget.ELEMENT_ARRAY_BUFFER.value
|
109 |
-
)
|
110 |
-
|
111 |
-
accessor_vert = Accessor(
|
112 |
-
bufferView=0,
|
113 |
-
byteOffset=0,
|
114 |
-
componentType=ComponentType.FLOAT.value,
|
115 |
-
count=len(vertices),
|
116 |
-
type=AccessorType.VEC3.value,
|
117 |
-
min=vertices.min(axis=0).tolist(),
|
118 |
-
max=vertices.max(axis=0).tolist()
|
119 |
-
)
|
120 |
-
|
121 |
-
accessor_index = Accessor(
|
122 |
-
bufferView=1,
|
123 |
-
byteOffset=0,
|
124 |
-
componentType=ComponentType.UNSIGNED_INT.value,
|
125 |
-
count=len(indices),
|
126 |
-
type=AccessorType.SCALAR.value
|
127 |
-
)
|
128 |
-
|
129 |
-
gltf = GLTF2(
|
130 |
-
buffers=[buffer],
|
131 |
-
bufferViews=[buffer_view_vert, buffer_view_index],
|
132 |
-
accessors=[accessor_vert, accessor_index],
|
133 |
-
meshes=[Mesh(primitives=[{
|
134 |
-
"attributes": {"POSITION": 0},
|
135 |
-
"indices": 1
|
136 |
-
}])],
|
137 |
-
scenes=[Scene(nodes=[0])],
|
138 |
-
nodes=[Node(mesh=0)],
|
139 |
-
scene=0
|
140 |
-
)
|
141 |
-
|
142 |
-
# Inject binary blob
|
143 |
-
gltf.set_binary_blob(total_bytes)
|
144 |
-
gltf.save_binary(output_path)
|
145 |
|
146 |
print(f"[DEBUG] Mesh saved to {output_path}")
|
147 |
return output_path if os.path.exists(output_path) else None
|
@@ -150,7 +108,7 @@ def generate(image_path, face_number=50000, guidance_scale=5.0, num_steps=25):
|
|
150 |
print("[ERROR]", e)
|
151 |
traceback.print_exc()
|
152 |
return f"Error: {e}"
|
153 |
-
|
154 |
# Интерфейс Gradio
|
155 |
demo = gr.Interface(
|
156 |
fn=generate,
|
|
|
1 |
+
|
2 |
+
|
3 |
import os
|
4 |
import subprocess
|
5 |
|
|
|
27 |
from triposg.pipelines.pipeline_triposg import TripoSGPipeline
|
28 |
from briarmbg import BriaRMBG
|
29 |
|
|
|
|
|
|
|
|
|
30 |
|
31 |
print("Trimesh version:", trimesh.__version__)
|
32 |
|
|
|
69 |
|
70 |
temp_id = str(uuid.uuid4())
|
71 |
output_path = f"/tmp/{temp_id}.glb"
|
72 |
+
print("[DEBUG] Generating mesh from:", image_path)
|
73 |
|
74 |
try:
|
75 |
mesh = run_triposg(
|
|
|
85 |
if mesh is None or mesh.vertices.shape[0] == 0 or mesh.faces.shape[0] == 0:
|
86 |
raise ValueError("Mesh generation returned an empty mesh")
|
87 |
|
88 |
+
# Безопасная очистка визуала
|
89 |
+
if hasattr(mesh, "visual") and mesh.visual is not None:
|
90 |
+
try:
|
91 |
+
mesh.visual = None
|
92 |
+
except Exception:
|
93 |
+
print("[WARN] Failed to clear visual, skipping")
|
94 |
|
95 |
+
mesh.metadata.clear()
|
96 |
+
mesh.name = "endless_tools_mesh"
|
|
|
|
|
97 |
|
98 |
+
# Экспорт .glb
|
99 |
+
# glb_data = export_glb(mesh)
|
100 |
+
glb_data = mesh.export(file_type='glb')
|
101 |
+
with open(output_path, "wb") as f:
|
102 |
+
f.write(glb_data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
|
104 |
print(f"[DEBUG] Mesh saved to {output_path}")
|
105 |
return output_path if os.path.exists(output_path) else None
|
|
|
108 |
print("[ERROR]", e)
|
109 |
traceback.print_exc()
|
110 |
return f"Error: {e}"
|
111 |
+
|
112 |
# Интерфейс Gradio
|
113 |
demo = gr.Interface(
|
114 |
fn=generate,
|
requirements.txt
CHANGED
@@ -15,4 +15,4 @@ jaxtyping
|
|
15 |
typeguard
|
16 |
pymeshlab
|
17 |
einops
|
18 |
-
|
|
|
15 |
typeguard
|
16 |
pymeshlab
|
17 |
einops
|
18 |
+
|