facebookresearch / pytorch3d

PyTorch3D is FAIR's library of reusable components for deep learning with 3D data
https://pytorch3d.org/
Other
8.81k stars 1.32k forks source link

Help rendering meshlab object #1561

Open Adblu opened 1 year ago

Adblu commented 1 year ago

Hi. I am trying to load into pytorch3d following file: eva.obj -> link

Screenshot from 2023-06-15 09-55-30

Here is the pytorch3D code:

import os
import sys
import torch
import pytorch3d
import os
import torch
import matplotlib.pyplot as plt

from pytorch3d.io import load_objs_as_meshes, load_obj

from pytorch3d.structures import Meshes
from pytorch3d.vis.plotly_vis import AxisArgs, plot_batch_individually, plot_scene
from pytorch3d.vis.texture_vis import texturesuv_image_matplotlib
from pytorch3d.renderer import (
    look_at_view_transform,
    FoVPerspectiveCameras, 
    PointLights, 
    DirectionalLights, 
    Materials, 
    RasterizationSettings, 
    MeshRenderer, 
    MeshRasterizer,  
    SoftPhongShader,
    TexturesUV,
    TexturesVertex
)

from pytorch3d.renderer import (PerspectiveCameras,HardGouraudShader,CamerasBase,OpenGLOrthographicCameras,
             OpenGLPerspectiveCameras,OrthographicCameras,   PerspectiveCameras,    camera_position_from_spherical_angles,              
    FoVPerspectiveCameras, look_at_view_transform, look_at_rotation, 
    RasterizationSettings, MeshRenderer, MeshRasterizer, BlendParams,
    SoftSilhouetteShader, HardPhongShader, PointLights, TexturesVertex,
)

import sys
import os
from plot_image_grid import image_grid
import numpy as np

from pytorch3d.transforms import RotateAxisAngle, Scale
from pytorch3d.renderer import OpenGLPerspectiveCameras

from pytorch3d.renderer import (
  RasterizationSettings,
  MeshRasterizer,
  SfMPerspectiveCameras,
)

if torch.cuda.is_available():
    device = torch.device("cuda:0")
    torch.cuda.set_device(device)
else:
    device = torch.device("cpu")

obj_filename_a = "eva.obj"

verts, faces, aux = load_obj(obj_filename_a, device=device, load_textures=True)

tex = TexturesVertex(
    torch.ones((1, verts.shape[0], 3), device=device, dtype=torch.float32)
)

mesh = Meshes(verts=[verts], faces=[faces.verts_idx], textures=tex)

R, T = look_at_view_transform(dist=2.7, elev=0, azim=180)

cameras = FoVPerspectiveCameras(device=device, R=R, T=T)

raster_settings = RasterizationSettings(
    blur_radius=0, faces_per_pixel=1, max_faces_per_bin=20000
)
renderer = MeshRenderer(
    rasterizer=MeshRasterizer(cameras=cameras, raster_settings=raster_settings),
    shader=HardGouraudShader(device=device, cameras=cameras),
)
image = renderer(mesh)

rgb = image[0, ..., :3].detach().squeeze().cpu()

a=(rgb.detach().cpu().numpy() * 255).astype(np.uint8)

At the end, Im getting nothing. How to modify following code so the image can be rendered properly in pytorch3d ?

Also, once I will load the file, how do I change "Shading" options as in meshlab ? Screenshot from 2023-06-15 09-55-30 Screenshot from 2023-06-15 09-55-39 Screenshot from 2023-06-15 09-55-45

bottler commented 1 year ago

When you say "getting nothing", do you mean you have a blank image? If so, can you use plot_batch_individually to plot the mesh and the camera in the same plot, to check they are lined up the way you want.

What does the shading option do? From the image, it looks like it does something related to the lighting model, but I don't know how. "None" possibly corresponds to using AmbientLights in PyTorch3D. Perhaps "Face" makes the whole of each face be modelled as having a constant normal? To achieve that in PyTorch3D, perhaps do this: ensure that vertices are separately specified for each face, calculate face normals, and then make a new Meshes object with every vertex normal set to the corresponding face normal.

Adblu commented 1 year ago

Yes, white/blank page.