Spaces:
Running
on
Zero
Running
on
Zero
Update scripts/utils.py
Browse files- scripts/utils.py +65 -5
scripts/utils.py
CHANGED
@@ -1,11 +1,22 @@
|
|
|
|
1 |
import torch
|
2 |
import numpy as np
|
3 |
from PIL import Image
|
4 |
import pymeshlab
|
5 |
import pymeshlab as ml
|
6 |
from pymeshlab import PercentageValue
|
7 |
-
from pytorch3d.renderer import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
from pytorch3d.structures import Meshes
|
|
|
9 |
from rembg import new_session, remove
|
10 |
import torch
|
11 |
import torch.nn.functional as F
|
@@ -239,17 +250,66 @@ def save_py3dmesh_with_trimesh_fast(meshes: Meshes, save_glb_path, apply_sRGB_to
|
|
239 |
fix_vert_color_glb(save_glb_path)
|
240 |
print(f"saving to {save_glb_path}")
|
241 |
|
242 |
-
|
243 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
244 |
import time
|
|
|
|
|
245 |
if '.' in save_mesh_prefix:
|
246 |
save_mesh_prefix = ".".join(save_mesh_prefix.split('.')[:-1])
|
247 |
if with_timestamp:
|
248 |
save_mesh_prefix = save_mesh_prefix + f"_{int(time.time())}"
|
249 |
ret_mesh = save_mesh_prefix + ".glb"
|
250 |
-
|
|
|
251 |
save_py3dmesh_with_trimesh_fast(meshes, ret_mesh)
|
252 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
253 |
|
254 |
|
255 |
def simple_clean_mesh(pyml_mesh: ml.Mesh, apply_smooth=True, stepsmoothnum=1, apply_sub_divide=False, sub_divide_threshold=0.25):
|
|
|
1 |
+
import os
|
2 |
import torch
|
3 |
import numpy as np
|
4 |
from PIL import Image
|
5 |
import pymeshlab
|
6 |
import pymeshlab as ml
|
7 |
from pymeshlab import PercentageValue
|
8 |
+
from pytorch3d.renderer import (
|
9 |
+
look_at_view_transform,
|
10 |
+
FoVPerspectiveCameras,
|
11 |
+
PointLights,
|
12 |
+
RasterizationSettings,
|
13 |
+
MeshRenderer,
|
14 |
+
MeshRasterizer,
|
15 |
+
SoftPhongShader,
|
16 |
+
TexturesVertex,
|
17 |
+
)
|
18 |
from pytorch3d.structures import Meshes
|
19 |
+
import imageio
|
20 |
from rembg import new_session, remove
|
21 |
import torch
|
22 |
import torch.nn.functional as F
|
|
|
250 |
fix_vert_color_glb(save_glb_path)
|
251 |
print(f"saving to {save_glb_path}")
|
252 |
|
253 |
+
def save_glb_and_video(
|
254 |
+
save_mesh_prefix: str,
|
255 |
+
meshes: Meshes,
|
256 |
+
with_timestamp=True,
|
257 |
+
dist=3.5,
|
258 |
+
resolution=512,
|
259 |
+
fov_in_degrees=45,
|
260 |
+
cam_type="persp",
|
261 |
+
export_video=True
|
262 |
+
) -> Tuple[str, str]:
|
263 |
import time
|
264 |
+
|
265 |
+
# Generar el nombre del archivo GLB
|
266 |
if '.' in save_mesh_prefix:
|
267 |
save_mesh_prefix = ".".join(save_mesh_prefix.split('.')[:-1])
|
268 |
if with_timestamp:
|
269 |
save_mesh_prefix = save_mesh_prefix + f"_{int(time.time())}"
|
270 |
ret_mesh = save_mesh_prefix + ".glb"
|
271 |
+
|
272 |
+
# Guardar el modelo GLB
|
273 |
save_py3dmesh_with_trimesh_fast(meshes, ret_mesh)
|
274 |
+
|
275 |
+
# Si no se solicita el video, retornar solo el GLB
|
276 |
+
if not export_video:
|
277 |
+
return ret_mesh, None
|
278 |
+
|
279 |
+
# Configurar el directorio para guardar el video
|
280 |
+
output_dir = os.path.dirname(save_mesh_prefix)
|
281 |
+
os.makedirs(output_dir, exist_ok=True)
|
282 |
+
video_path = save_mesh_prefix + "_video.mp4"
|
283 |
+
|
284 |
+
# Configurar la cámara
|
285 |
+
device = meshes.device
|
286 |
+
R, T = look_at_view_transform(dist=dist, elev=0, azim=np.linspace(0, 360, 72, endpoint=False))
|
287 |
+
cameras = FoVPerspectiveCameras(device=device, R=R, T=T, fov=fov_in_degrees)
|
288 |
+
|
289 |
+
# Configurar el renderizador
|
290 |
+
raster_settings = RasterizationSettings(
|
291 |
+
image_size=resolution,
|
292 |
+
blur_radius=0.0,
|
293 |
+
faces_per_pixel=1,
|
294 |
+
)
|
295 |
+
lights = PointLights(device=device, location=[[0.0, 0.0, -3.0]])
|
296 |
+
renderer = MeshRenderer(
|
297 |
+
rasterizer=MeshRasterizer(cameras=cameras, raster_settings=raster_settings),
|
298 |
+
shader=SoftPhongShader(device=device, cameras=cameras, lights=lights),
|
299 |
+
)
|
300 |
+
|
301 |
+
# Renderizar fotogramas
|
302 |
+
frames = []
|
303 |
+
for i in range(len(cameras)):
|
304 |
+
camera = FoVPerspectiveCameras(device=device, R=R[i].unsqueeze(0), T=T[i].unsqueeze(0), fov=fov_in_degrees)
|
305 |
+
image = renderer(meshes, cameras=camera)
|
306 |
+
image = (image[0, ..., :3].cpu().numpy() * 255).astype(np.uint8) # Convertir a imagen RGB
|
307 |
+
frames.append(image)
|
308 |
+
|
309 |
+
# Guardar los fotogramas como un video
|
310 |
+
imageio.mimsave(video_path, frames, fps=30)
|
311 |
+
|
312 |
+
return ret_mesh, video_path
|
313 |
|
314 |
|
315 |
def simple_clean_mesh(pyml_mesh: ml.Mesh, apply_smooth=True, stepsmoothnum=1, apply_sub_divide=False, sub_divide_threshold=0.25):
|