shubham-goel / ucmr

Code for the ECCV2020 paper "Shape and Viewpoint without Keypoints".
https://shubham-goel.github.io/ucmr/
148 stars 11 forks source link

Add texture to saved obj file #17

Closed felixboevr closed 3 years ago

felixboevr commented 3 years ago

Hi @shubham-goel and congrats for this amazing project.

I figured out how to save the mesh as obj file according to #15

It would be great to add the texture to the saved obj file so that it can be used in blender etc. Is this possible and could you please help with the code? Thank you.

shubham-goel commented 3 years ago

Hi @felixboevr, it would indeed be great if we can save the texture with the mesh!!

You can use the save_obj function defined in facebookresearch/pytorch3d#332. You don't need to install the entire pytorch3d package, but this will work:

    verts_uvs = predictor.faces_uv.view(-1,2)
    if opts.texture_uvshift:
            verts_uvs[:,0] = verts_uvs[:,0] + 0.5       # u -> u+0.5
            verts_uvs = torch.where(verts_uvs>=1, verts_uvs-2, verts_uvs)
    verts_uvs[:,1] *= -1
    verts_uvs = (verts_uvs + 1)/2
    faces_uvs = torch.arange(verts_uvs.shape[0]).long().view(-1,3)
    save_obj('demo.obj', outputs['verts'][0], predictor.faces, 
            verts_uvs = verts_uvs, faces_uvs = faces_uvs, 
            texture_map = outputs['uv_image'].permute(0,2,3,1) # expected shape (1, H, W, 3)
    )

Note that we assume a spherical UV-texture map for the bird. So this will still not save perfect texture for the few faces that cross the bird's plane-of-symmetry. This will be visible as a weird artifact on the bird's back.