Spaces:
Running
on
L40S
Running
on
L40S
update
Browse files- refine/func.py +35 -3
- refine/render.py +1 -1
refine/func.py
CHANGED
@@ -14,6 +14,37 @@ import pymeshlab as ml
|
|
14 |
from pymeshlab import Percentage
|
15 |
import nvdiffrast.torch as dr
|
16 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
|
19 |
def _translation(x, y, z, device):
|
@@ -162,11 +193,12 @@ class Pix2FacesRenderer:
|
|
162 |
pix_to_face = rast_out[..., -1].to(torch.int32) - 1
|
163 |
return pix_to_face
|
164 |
|
165 |
-
pix2faces_renderer = Pix2FacesRenderer()
|
|
|
166 |
|
167 |
def get_visible_faces(meshes: Meshes, cameras: CamerasBase, resolution=1024):
|
168 |
-
|
169 |
-
pix_to_face = pix2faces_renderer.render_pix2faces_nvdiff(meshes, cameras, H=resolution, W=resolution)
|
170 |
|
171 |
unique_faces = torch.unique(pix_to_face.flatten())
|
172 |
unique_faces = unique_faces[unique_faces != -1]
|
|
|
14 |
from pymeshlab import Percentage
|
15 |
import nvdiffrast.torch as dr
|
16 |
import numpy as np
|
17 |
+
from pytorch3d.renderer import MeshRasterizer
|
18 |
+
from pytorch3d.renderer.mesh.rasterizer import Fragments
|
19 |
+
|
20 |
+
def render_pix2faces_py3d(meshes, cameras, H=512, W=512, blur_radius=0.0, faces_per_pixel=1):
|
21 |
+
"""
|
22 |
+
Renders pix2face of visible faces.
|
23 |
+
:param mesh: Pytorch3d.structures.Meshes
|
24 |
+
:param cameras: pytorch3d.renderer.Cameras
|
25 |
+
:param H: target image height
|
26 |
+
:param W: target image width
|
27 |
+
:param blur_radius: Float distance in the range [0, 2] used to expand the face
|
28 |
+
bounding boxes for rasterization. Setting blur radius
|
29 |
+
results in blurred edges around the shape instead of a
|
30 |
+
hard boundary. Set to 0 for no blur.
|
31 |
+
:param faces_per_pixel: (int) Number of faces to keep track of per pixel.
|
32 |
+
We return the nearest faces_per_pixel faces along the z-axis.
|
33 |
+
"""
|
34 |
+
# Define the settings for rasterization and shading
|
35 |
+
raster_settings = RasterizationSettings(
|
36 |
+
image_size=(H, W),
|
37 |
+
blur_radius=blur_radius,
|
38 |
+
faces_per_pixel=faces_per_pixel
|
39 |
+
)
|
40 |
+
rasterizer=MeshRasterizer(
|
41 |
+
cameras=cameras,
|
42 |
+
raster_settings=raster_settings
|
43 |
+
)
|
44 |
+
fragments: Fragments = rasterizer(meshes, cameras=cameras)
|
45 |
+
return {
|
46 |
+
"pix_to_face": fragments.pix_to_face[..., 0],
|
47 |
+
}
|
48 |
|
49 |
|
50 |
def _translation(x, y, z, device):
|
|
|
193 |
pix_to_face = rast_out[..., -1].to(torch.int32) - 1
|
194 |
return pix_to_face
|
195 |
|
196 |
+
# pix2faces_renderer = Pix2FacesRenderer()
|
197 |
+
pix2faces_renderer = None
|
198 |
|
199 |
def get_visible_faces(meshes: Meshes, cameras: CamerasBase, resolution=1024):
|
200 |
+
pix_to_face = render_pix2faces_py3d(meshes, cameras, H=resolution, W=resolution)['pix_to_face']
|
201 |
+
# pix_to_face = pix2faces_renderer.render_pix2faces_nvdiff(meshes, cameras, H=resolution, W=resolution)
|
202 |
|
203 |
unique_faces = torch.unique(pix_to_face.flatten())
|
204 |
unique_faces = unique_faces[unique_faces != -1]
|
refine/render.py
CHANGED
@@ -15,7 +15,7 @@ def _warmup(glctx, device=None):
|
|
15 |
tri = tensor([[0, 1, 2]], dtype=torch.int32)
|
16 |
dr.rasterize(glctx, pos, tri, resolution=[256, 256])
|
17 |
|
18 |
-
glctx = dr.RasterizeCudaContext(
|
19 |
|
20 |
class NormalsRenderer:
|
21 |
|
|
|
15 |
tri = tensor([[0, 1, 2]], dtype=torch.int32)
|
16 |
dr.rasterize(glctx, pos, tri, resolution=[256, 256])
|
17 |
|
18 |
+
glctx = dr.RasterizeCudaContext()
|
19 |
|
20 |
class NormalsRenderer:
|
21 |
|