Example #1
0
def render_color(mesh, f, Rt, img_size=(224, 224), bg_img=None):

    # apply Rt
    verts = mesh.points().copy()
    for i in range(len(verts)):
        verts[i] = np.dot(Rt[:3, :3], verts[i]) + Rt[:, 3]

    rn = ColoredRenderer()
    if bg_img is not None:
        rn.background_image = bg_img.astype(
            np.float) / 300. if bg_img.max() > 1 else bg_img

    rn.camera = ProjectPoints(v=verts,
                              rt=np.zeros(3),
                              t=np.zeros(3),
                              f=np.array([f, f]),
                              c=np.array([img_size[1], img_size[0]]) / 2.,
                              k=np.zeros(5))
    rn.frustum = {
        'near': .5,
        'far': 10.,
        'width': img_size[1],
        'height': img_size[0]
    }
    rn.v = verts
    rn.f = mesh.face_vertex_indices()
    rn.bgcolor = np.zeros(3)

    rn.vc = vc = mesh.vertex_colors()[:, :3]
    return rn.r
TTT = RT[:3, 3]
RRR = RT[:3, :3]
RRR = RRR * opencv2opengl

fff = model.f.copy()
vvv = model.r.copy()

rn.camera = ProjectPoints(f=ch.array([KKK[0, 0], KKK[1, 1]]),
                          rt=cv2.Rodrigues(RRR)[0].flatten(),
                          t=ch.array(TTT),
                          k=ch.array([0, 0, 0, 0]),
                          c=ch.array([KKK[0, 2], KKK[1, 2]]))
rn.frustum = {'near': 0.1, 'far': 15., 'width': w, 'height': h}
rn.set(v=vvv, f=fff, bgcolor=ch.zeros(3))
rn.background_image = img / 255. if img.max() > 1 else img

# Construct point light source
rn.vc = LambertianPointLight(f=model.f,
                             v=rn.v,
                             num_verts=len(model),
                             light_pos=ch.array([0, 0, 0]),
                             vc=np.ones_like(model) * .5,
                             light_color=ch.array([-100., -100., -100.]))

if toOpenGL:
    cv2.imshow('render_SMPL', rn.r)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
else:
    smpl_vertices = model.r