staswrs commited on
Commit
4763a4b
·
1 Parent(s): 3b1ae59

clean scene 11

Browse files
Files changed (3) hide show
  1. app.py +59 -15
  2. app_backlog.py +193 -64
  3. requirements.txt +1 -1
app.py CHANGED
@@ -25,6 +25,10 @@ from inference_triposg import run_triposg
25
  from triposg.pipelines.pipeline_triposg import TripoSGPipeline
26
  from briarmbg import BriaRMBG
27
 
 
 
 
 
28
 
29
  print("Trimesh version:", trimesh.__version__)
30
 
@@ -67,7 +71,6 @@ def generate(image_path, face_number=50000, guidance_scale=5.0, num_steps=25):
67
 
68
  temp_id = str(uuid.uuid4())
69
  output_path = f"/tmp/{temp_id}.glb"
70
- print("[DEBUG] Generating mesh from:", image_path)
71
 
72
  try:
73
  mesh = run_triposg(
@@ -83,21 +86,62 @@ def generate(image_path, face_number=50000, guidance_scale=5.0, num_steps=25):
83
  if mesh is None or mesh.vertices.shape[0] == 0 or mesh.faces.shape[0] == 0:
84
  raise ValueError("Mesh generation returned an empty mesh")
85
 
86
- # Безопасная очистка визуала
87
- if hasattr(mesh, "visual") and mesh.visual is not None:
88
- try:
89
- mesh.visual = None
90
- except Exception:
91
- print("[WARN] Failed to clear visual, skipping")
 
92
 
93
- mesh.metadata.clear()
94
- mesh.name = "endless_tools_mesh"
 
 
 
 
 
 
 
 
 
 
 
95
 
96
- # Экспорт .glb
97
- # glb_data = export_glb(mesh)
98
- glb_data = mesh.export(file_type='glb')
99
- with open(output_path, "wb") as f:
100
- f.write(glb_data)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
 
102
  print(f"[DEBUG] Mesh saved to {output_path}")
103
  return output_path if os.path.exists(output_path) else None
@@ -106,7 +150,7 @@ def generate(image_path, face_number=50000, guidance_scale=5.0, num_steps=25):
106
  print("[ERROR]", e)
107
  traceback.print_exc()
108
  return f"Error: {e}"
109
-
110
  # Интерфейс Gradio
111
  demo = gr.Interface(
112
  fn=generate,
 
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
 
72
  temp_id = str(uuid.uuid4())
73
  output_path = f"/tmp/{temp_id}.glb"
 
74
 
75
  try:
76
  mesh = run_triposg(
 
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
+ vertices = mesh.vertices.astype(np.float32)
90
+ indices = mesh.faces.astype(np.uint32).flatten()
91
+
92
+ # Pack binary data
93
+ vertex_bytes = vertices.tobytes()
94
+ index_bytes = indices.tobytes()
95
+ total_bytes = vertex_bytes + index_bytes
96
 
97
+ buffer = Buffer(byteLength=len(total_bytes))
98
+ buffer_view_vert = BufferView(
99
+ buffer=0,
100
+ byteOffset=0,
101
+ byteLength=len(vertex_bytes),
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
  print("[ERROR]", e)
151
  traceback.print_exc()
152
  return f"Error: {e}"
153
+
154
  # Интерфейс Gradio
155
  demo = gr.Interface(
156
  fn=generate,
app_backlog.py CHANGED
@@ -1,9 +1,182 @@
1
 
2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  import os
4
  import subprocess
5
 
6
- # Убираем pyenv, если вдруг остался .python-version
7
  os.environ.pop("PYENV_VERSION", None)
8
 
9
  # Установка зависимостей
@@ -13,7 +186,7 @@ subprocess.run([
13
  "diso@git+https://github.com/SarahWeiii/diso.git"
14
  ], check=True)
15
 
16
- # Импорты
17
  import gradio as gr
18
  import uuid
19
  import torch
@@ -21,15 +194,15 @@ import zipfile
21
  import requests
22
  import traceback
23
  import trimesh
24
- from trimesh.exchange.gltf import export_glb
25
-
26
- print("Trimesh version:", trimesh.__version__)
27
-
28
 
29
  from inference_triposg import run_triposg
30
  from triposg.pipelines.pipeline_triposg import TripoSGPipeline
31
  from briarmbg import BriaRMBG
32
 
 
 
 
33
  # Настройки устройства
34
  device = "cuda" if torch.cuda.is_available() else "cpu"
35
  dtype = torch.float16 if device == "cuda" else torch.float32
@@ -63,26 +236,15 @@ rmbg_net = BriaRMBG.from_pretrained(rmbg_path).to(device)
63
  rmbg_net.eval()
64
 
65
  # Генерация .glb
66
- # def generate(image_path):
67
  def generate(image_path, face_number=50000, guidance_scale=5.0, num_steps=25):
68
  print("[API CALL] image_path received:", image_path)
69
  print("[API CALL] File exists:", os.path.exists(image_path))
70
 
71
  temp_id = str(uuid.uuid4())
72
  output_path = f"/tmp/{temp_id}.glb"
73
-
74
  print("[DEBUG] Generating mesh from:", image_path)
75
 
76
  try:
77
- # mesh = run_triposg(
78
- # pipe=pipe,
79
- # image_input=image_path,
80
- # rmbg_net=rmbg_net,
81
- # seed=42,
82
- # num_inference_steps=25,
83
- # guidance_scale=5.0,
84
- # faces=-1,
85
- # )
86
  mesh = run_triposg(
87
  pipe=pipe,
88
  image_input=image_path,
@@ -93,60 +255,31 @@ def generate(image_path, face_number=50000, guidance_scale=5.0, num_steps=25):
93
  faces=int(face_number),
94
  )
95
 
96
- # if mesh is None:
97
- # raise ValueError("Mesh generation failed")
98
-
99
- # mesh.export(output_path)
100
- # print(f"[DEBUG] Mesh saved to {output_path}")
101
-
102
- # return output_path if os.path.exists(output_path) else "Error: output file not found"
103
-
104
-
105
- # if mesh is None:
106
- # raise ValueError("Mesh generation failed")
107
-
108
- # # Убираем визуал, метаданные, обертки
109
- # mesh.visual = None
110
- # mesh.metadata.clear()
111
- # mesh.name = "endless_tools"
112
-
113
- # # Экспорт только геометрии
114
- # glb_data = mesh.export(file_type="glb")
115
- # with open(output_path, "wb") as f:
116
- # f.write(glb_data)
117
 
118
- # print(f"[DEBUG] Mesh saved to {output_path}")
 
 
 
 
 
119
 
120
- # return output_path if os.path.exists(output_path) else "Error: output file not found"
121
-
122
- if mesh is None:
123
- raise ValueError("Mesh generation returned None")
124
-
125
- # Очистка визуала, метаданных и имени
126
- mesh.visual = None
127
  mesh.metadata.clear()
128
- mesh.name = "geometry_0"
129
 
130
- # glb_data = mesh.export(file_type="glb")
131
- glb_data = export_glb(mesh)
 
132
  with open(output_path, "wb") as f:
133
  f.write(glb_data)
134
 
135
- # Экспорт .glb вручную (иначе Trimesh добавляет сцену)
136
- # glb_data = mesh.export(file_type="glb")
137
- # with open(output_path, "wb") as f:
138
- # f.write(glb_data)
139
-
140
-
141
  print(f"[DEBUG] Mesh saved to {output_path}")
142
  return output_path if os.path.exists(output_path) else None
143
- # except Exception as e:
144
- # print("[ERROR]", e)
145
- # return f"Error: {e}"
146
  except Exception as e:
147
- # import traceback
148
  print("[ERROR]", e)
149
- traceback.print_exc() # ← выведет полную трассировку в логи
150
  return f"Error: {e}"
151
 
152
  # Интерфейс Gradio
@@ -160,7 +293,3 @@ demo = gr.Interface(
160
 
161
  # Запуск
162
  demo.launch()
163
-
164
-
165
-
166
-
 
1
 
2
 
3
+ # import os
4
+ # import subprocess
5
+
6
+ # # Убираем pyenv, если вдруг остался .python-version
7
+ # os.environ.pop("PYENV_VERSION", None)
8
+
9
+ # # Установка зависимостей
10
+ # subprocess.run(["pip", "install", "torch", "wheel"], check=True)
11
+ # subprocess.run([
12
+ # "pip", "install", "--no-build-isolation",
13
+ # "diso@git+https://github.com/SarahWeiii/diso.git"
14
+ # ], check=True)
15
+
16
+ # # Импорты
17
+ # import gradio as gr
18
+ # import uuid
19
+ # import torch
20
+ # import zipfile
21
+ # import requests
22
+ # import traceback
23
+ # import trimesh
24
+ # from trimesh.exchange.gltf import export_glb
25
+
26
+ # print("Trimesh version:", trimesh.__version__)
27
+
28
+
29
+ # from inference_triposg import run_triposg
30
+ # from triposg.pipelines.pipeline_triposg import TripoSGPipeline
31
+ # from briarmbg import BriaRMBG
32
+
33
+ # # Настройки устройства
34
+ # device = "cuda" if torch.cuda.is_available() else "cpu"
35
+ # dtype = torch.float16 if device == "cuda" else torch.float32
36
+
37
+ # # Загрузка весов
38
+ # weights_dir = "pretrained_weights"
39
+ # triposg_path = os.path.join(weights_dir, "TripoSG")
40
+ # rmbg_path = os.path.join(weights_dir, "RMBG-1.4")
41
+
42
+ # if not (os.path.exists(triposg_path) and os.path.exists(rmbg_path)):
43
+ # print("📦 Downloading pretrained weights...")
44
+ # url = "https://huggingface.co/datasets/endlesstools/pretrained-assets/resolve/main/pretrained_models.zip"
45
+ # zip_path = "pretrained_models.zip"
46
+
47
+ # with requests.get(url, stream=True) as r:
48
+ # r.raise_for_status()
49
+ # with open(zip_path, "wb") as f:
50
+ # for chunk in r.iter_content(chunk_size=8192):
51
+ # f.write(chunk)
52
+
53
+ # print("📦 Extracting weights...")
54
+ # with zipfile.ZipFile(zip_path, "r") as zip_ref:
55
+ # zip_ref.extractall(weights_dir)
56
+
57
+ # os.remove(zip_path)
58
+ # print("✅ Weights ready.")
59
+
60
+ # # Загрузка моделей
61
+ # pipe = TripoSGPipeline.from_pretrained(triposg_path).to(device, dtype)
62
+ # rmbg_net = BriaRMBG.from_pretrained(rmbg_path).to(device)
63
+ # rmbg_net.eval()
64
+
65
+ # # Генерация .glb
66
+ # # def generate(image_path):
67
+ # def generate(image_path, face_number=50000, guidance_scale=5.0, num_steps=25):
68
+ # print("[API CALL] image_path received:", image_path)
69
+ # print("[API CALL] File exists:", os.path.exists(image_path))
70
+
71
+ # temp_id = str(uuid.uuid4())
72
+ # output_path = f"/tmp/{temp_id}.glb"
73
+
74
+ # print("[DEBUG] Generating mesh from:", image_path)
75
+
76
+ # try:
77
+ # # mesh = run_triposg(
78
+ # # pipe=pipe,
79
+ # # image_input=image_path,
80
+ # # rmbg_net=rmbg_net,
81
+ # # seed=42,
82
+ # # num_inference_steps=25,
83
+ # # guidance_scale=5.0,
84
+ # # faces=-1,
85
+ # # )
86
+ # mesh = run_triposg(
87
+ # pipe=pipe,
88
+ # image_input=image_path,
89
+ # rmbg_net=rmbg_net,
90
+ # seed=42,
91
+ # num_inference_steps=int(num_steps),
92
+ # guidance_scale=float(guidance_scale),
93
+ # faces=int(face_number),
94
+ # )
95
+
96
+ # # if mesh is None:
97
+ # # raise ValueError("Mesh generation failed")
98
+
99
+ # # mesh.export(output_path)
100
+ # # print(f"[DEBUG] Mesh saved to {output_path}")
101
+
102
+ # # return output_path if os.path.exists(output_path) else "Error: output file not found"
103
+
104
+
105
+ # # if mesh is None:
106
+ # # raise ValueError("Mesh generation failed")
107
+
108
+ # # # Убираем визуал, метаданные, обертки
109
+ # # mesh.visual = None
110
+ # # mesh.metadata.clear()
111
+ # # mesh.name = "endless_tools"
112
+
113
+ # # # Экспорт только геометрии
114
+ # # glb_data = mesh.export(file_type="glb")
115
+ # # with open(output_path, "wb") as f:
116
+ # # f.write(glb_data)
117
+
118
+ # # print(f"[DEBUG] Mesh saved to {output_path}")
119
+
120
+ # # return output_path if os.path.exists(output_path) else "Error: output file not found"
121
+
122
+ # if mesh is None:
123
+ # raise ValueError("Mesh generation returned None")
124
+
125
+ # # Очистка визуала, метаданных и имени
126
+ # mesh.visual = None
127
+ # mesh.metadata.clear()
128
+ # mesh.name = "geometry_0"
129
+
130
+ # # glb_data = mesh.export(file_type="glb")
131
+ # glb_data = export_glb(mesh)
132
+ # with open(output_path, "wb") as f:
133
+ # f.write(glb_data)
134
+
135
+ # # Экспорт .glb вручную (иначе Trimesh добавляет сцену)
136
+ # # glb_data = mesh.export(file_type="glb")
137
+ # # with open(output_path, "wb") as f:
138
+ # # f.write(glb_data)
139
+
140
+
141
+ # print(f"[DEBUG] Mesh saved to {output_path}")
142
+ # return output_path if os.path.exists(output_path) else None
143
+ # # except Exception as e:
144
+ # # print("[ERROR]", e)
145
+ # # return f"Error: {e}"
146
+ # except Exception as e:
147
+ # # import traceback
148
+ # print("[ERROR]", e)
149
+ # traceback.print_exc() # ← ��ыведет полную трассировку в логи
150
+ # return f"Error: {e}"
151
+
152
+ # # Интерфейс Gradio
153
+ # demo = gr.Interface(
154
+ # fn=generate,
155
+ # inputs=gr.Image(type="filepath", label="Upload image"),
156
+ # outputs=gr.File(label="Download .glb"),
157
+ # title="TripoSG Image to 3D",
158
+ # description="Upload an image to generate a 3D model (.glb)",
159
+ # )
160
+
161
+ # # Запуск
162
+ # demo.launch()
163
+
164
+
165
+
166
+
167
+
168
+
169
+
170
+
171
+
172
+
173
+
174
+
175
+
176
  import os
177
  import subprocess
178
 
179
+ # Убираем pyenv
180
  os.environ.pop("PYENV_VERSION", None)
181
 
182
  # Установка зависимостей
 
186
  "diso@git+https://github.com/SarahWeiii/diso.git"
187
  ], check=True)
188
 
189
+ # Импорты (перенесены после установки зависимостей)
190
  import gradio as gr
191
  import uuid
192
  import torch
 
194
  import requests
195
  import traceback
196
  import trimesh
197
+ from trimesh.exchange.gltf import export_glb
 
 
 
198
 
199
  from inference_triposg import run_triposg
200
  from triposg.pipelines.pipeline_triposg import TripoSGPipeline
201
  from briarmbg import BriaRMBG
202
 
203
+
204
+ print("Trimesh version:", trimesh.__version__)
205
+
206
  # Настройки устройства
207
  device = "cuda" if torch.cuda.is_available() else "cpu"
208
  dtype = torch.float16 if device == "cuda" else torch.float32
 
236
  rmbg_net.eval()
237
 
238
  # Генерация .glb
 
239
  def generate(image_path, face_number=50000, guidance_scale=5.0, num_steps=25):
240
  print("[API CALL] image_path received:", image_path)
241
  print("[API CALL] File exists:", os.path.exists(image_path))
242
 
243
  temp_id = str(uuid.uuid4())
244
  output_path = f"/tmp/{temp_id}.glb"
 
245
  print("[DEBUG] Generating mesh from:", image_path)
246
 
247
  try:
 
 
 
 
 
 
 
 
 
248
  mesh = run_triposg(
249
  pipe=pipe,
250
  image_input=image_path,
 
255
  faces=int(face_number),
256
  )
257
 
258
+ if mesh is None or mesh.vertices.shape[0] == 0 or mesh.faces.shape[0] == 0:
259
+ raise ValueError("Mesh generation returned an empty mesh")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
260
 
261
+ # Безопасная очистка визуала
262
+ if hasattr(mesh, "visual") and mesh.visual is not None:
263
+ try:
264
+ mesh.visual = None
265
+ except Exception:
266
+ print("[WARN] Failed to clear visual, skipping")
267
 
 
 
 
 
 
 
 
268
  mesh.metadata.clear()
269
+ mesh.name = "endless_tools_mesh"
270
 
271
+ # Экспорт .glb
272
+ # glb_data = export_glb(mesh)
273
+ glb_data = mesh.export(file_type='glb')
274
  with open(output_path, "wb") as f:
275
  f.write(glb_data)
276
 
 
 
 
 
 
 
277
  print(f"[DEBUG] Mesh saved to {output_path}")
278
  return output_path if os.path.exists(output_path) else None
279
+
 
 
280
  except Exception as e:
 
281
  print("[ERROR]", e)
282
+ traceback.print_exc()
283
  return f"Error: {e}"
284
 
285
  # Интерфейс Gradio
 
293
 
294
  # Запуск
295
  demo.launch()
 
 
 
 
requirements.txt CHANGED
@@ -15,4 +15,4 @@ jaxtyping
15
  typeguard
16
  pymeshlab
17
  einops
18
-
 
15
  typeguard
16
  pymeshlab
17
  einops
18
+ pygltflib