staswrs commited on
Commit
a9a84f7
·
1 Parent(s): e0b11b4

fix add normals 1

Browse files
Files changed (1) hide show
  1. app.py +58 -16
app.py CHANGED
@@ -63,6 +63,51 @@ rmbg_net = BriaRMBG.from_pretrained(rmbg_path).to(device)
63
  rmbg_net.eval()
64
 
65
  # Генерация .glb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  def generate(image_path, face_number=50000, guidance_scale=5.0, num_steps=25):
67
  print("[API CALL] image_path received:", image_path)
68
  print("[API CALL] File exists:", os.path.exists(image_path))
@@ -85,26 +130,22 @@ def generate(image_path, face_number=50000, guidance_scale=5.0, num_steps=25):
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
- mesh = trimesh.Trimesh(vertices=mesh.vertices, faces=mesh.faces)
89
- mesh.rezero()
90
- mesh.fix_normals()
91
- mesh.apply_translation(-mesh.center_mass)
92
 
93
- # Безопасная очистка визуала
94
- # if hasattr(mesh, "visual") and mesh.visual is not None:
95
- # mesh.visual = trimesh.visual.ColorVisuals(mesh=mesh, face_colors=None) ## вот тут последнее изменение
96
 
97
- ## можно вернуть это, если нужно
98
- # try:
99
- # mesh.visual = None
100
- # except Exception:
101
- # print("[WARN] Failed to clear visual, skipping")
102
 
103
- # mesh.metadata.clear()
104
- # mesh.name = "endless_tools_mesh"
105
 
106
- # Экспорт .glb
107
- # glb_data = export_glb(mesh)
 
108
  glb_data = mesh.export(file_type='glb')
109
  with open(output_path, "wb") as f:
110
  f.write(glb_data)
@@ -117,6 +158,7 @@ def generate(image_path, face_number=50000, guidance_scale=5.0, num_steps=25):
117
  traceback.print_exc()
118
  return f"Error: {e}"
119
 
 
120
  # Интерфейс Gradio
121
  demo = gr.Interface(
122
  fn=generate,
 
63
  rmbg_net.eval()
64
 
65
  # Генерация .glb
66
+ # def generate(image_path, face_number=50000, guidance_scale=5.0, num_steps=25):
67
+ # print("[API CALL] image_path received:", image_path)
68
+ # print("[API CALL] File exists:", os.path.exists(image_path))
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(
76
+ # pipe=pipe,
77
+ # image_input=image_path,
78
+ # rmbg_net=rmbg_net,
79
+ # seed=42,
80
+ # num_inference_steps=int(num_steps),
81
+ # guidance_scale=float(guidance_scale),
82
+ # faces=int(face_number),
83
+ # )
84
+
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
+ # mesh = trimesh.Trimesh(vertices=mesh.vertices, faces=mesh.faces)
89
+ # mesh.rezero()
90
+ # mesh.fix_normals()
91
+ # mesh.apply_translation(-mesh.center_mass)
92
+
93
+ # # Масштабируем, чтобы модель вписывалась в размер 1x1x1
94
+ # # Если нужно будет подгонять под размер в Endless Tools, то можно использовать:
95
+ # # scale_factor = 1.0 / np.max(np.linalg.norm(mesh.vertices, axis=1))
96
+ # # mesh.apply_scale(scale_factor)
97
+
98
+
99
+ # glb_data = mesh.export(file_type='glb')
100
+ # with open(output_path, "wb") as f:
101
+ # f.write(glb_data)
102
+
103
+ # print(f"[DEBUG] Mesh saved to {output_path}")
104
+ # return output_path if os.path.exists(output_path) else None
105
+
106
+ # except Exception as e:
107
+ # print("[ERROR]", e)
108
+ # traceback.print_exc()
109
+ # return f"Error: {e}"
110
+
111
  def generate(image_path, face_number=50000, guidance_scale=5.0, num_steps=25):
112
  print("[API CALL] image_path received:", image_path)
113
  print("[API CALL] File exists:", os.path.exists(image_path))
 
130
  if mesh is None or mesh.vertices.shape[0] == 0 or mesh.faces.shape[0] == 0:
131
  raise ValueError("Mesh generation returned an empty mesh")
132
 
133
+ # 🔧 Пересоздаём Trimesh и гарантируем чистоту геометрии
134
+ mesh = trimesh.Trimesh(vertices=mesh.vertices, faces=mesh.faces, process=True)
 
 
135
 
136
+ # Центрируем модель
137
+ mesh.apply_translation(-mesh.center_mass)
 
138
 
139
+ # Масштабируем к единичному размеру (все модели ~одинаковые)
140
+ scale_factor = 1.0 / np.max(np.linalg.norm(mesh.vertices, axis=1))
141
+ mesh.apply_scale(scale_factor)
 
 
142
 
143
+ # ✅ Гарантированно пересчитываем нормали
144
+ mesh.fix_normals()
145
 
146
+ print("[DEBUG] Normals present:", mesh.has_vertex_normals)
147
+
148
+ # 💾 Сохраняем GLB
149
  glb_data = mesh.export(file_type='glb')
150
  with open(output_path, "wb") as f:
151
  f.write(glb_data)
 
158
  traceback.print_exc()
159
  return f"Error: {e}"
160
 
161
+
162
  # Интерфейс Gradio
163
  demo = gr.Interface(
164
  fn=generate,