Esempio n. 1
0
def _create_renderer(  # pylint: disable=too-many-arguments
        w=640,
        h=480,
        rt=np.zeros(3),
        t=np.zeros(3),
        f=None,
        c=None,
        k=None,
        near=1.,
        far=10.,
        texture=None):
    """Create a renderer for the specified parameters."""
    f = np.array([w, w]) / 2. if f is None else f
    c = np.array([w, h]) / 2. if c is None else c
    k = np.zeros(5) if k is None else k

    if texture is not None:
        rn = TexturedRenderer()
    else:
        rn = ColoredRenderer()

    rn.camera = ProjectPoints(rt=rt, t=t, f=f, c=c, k=k)
    rn.frustum = {'near': near, 'far': far, 'height': h, 'width': w}
    if texture is not None:
        rn.texture_image = np.asarray(cv2.imread(texture), np.float64) / 255.
    return rn
Esempio n. 2
0
def create_renderer(w=640,
                    h=480,
                    rt=np.zeros(3),
                    t=np.zeros(3),
                    f=None,
                    c=None,
                    k=None,
                    near=.5,
                    far=10.,
                    mesh=None):
    f = np.array([w, w]) / 2. if f is None else f
    c = np.array([w, h]) / 2. if c is None else c
    k = np.zeros(5) if k is None else k

    if mesh is not None and hasattr(mesh, 'texture_image'):
        from opendr.renderer import TexturedRenderer
        rn = TexturedRenderer()
        rn.texture_image = mesh.texture_image
        if rn.texture_image.max() > 1:
            rn.texture_image[:] = rn.texture_image[:].r / 255.
        rn.ft = mesh.ft
        rn.vt = mesh.vt
    else:
        from opendr.renderer import ColoredRenderer
        rn = ColoredRenderer()

    rn.camera = ProjectPoints(rt=rt, t=t, f=f, c=c, k=k)
    rn.frustum = {'near': near, 'far': far, 'height': h, 'width': w}
    return rn
Esempio n. 3
0
    def generate(self, img_bgr, texture_bgr):
        img = img_bgr
        self.set_texture(texture_bgr)
        vert_shifted, theta, cam_for_render = self.hmr.predict(img)
        pose = theta[self.num_cam:(self.num_cam + self.num_theta)]
        beta = theta[(self.num_cam + self.num_theta):]

        self.body.pose[:] = pose
        self.body.betas[:] = beta

        rn_vis = TexturedRenderer()
        rn_vis.camera = ProjectPoints(t=np.zeros(3),
                                      rt=np.zeros(3),
                                      c=cam_for_render[1:],
                                      f=np.ones(2) * cam_for_render[0],
                                      k=np.zeros(5),
                                      v=vert_shifted)
        rn_vis.frustum = {
            'near': 0.1,
            'far': 1000.,
            'width': self.width,
            'height': self.height
        }
        rn_vis.set(v=vert_shifted,
                   f=self.m.f,
                   vc=self.m.vc,
                   texture_image=self.m.texture_image,
                   ft=self.m.ft,
                   vt=self.m.vt,
                   bgcolor=np.zeros(3))
        # rn_vis.background_image = img_bgr / 255. if img_bgr.max() > 1 else img_bgr

        out_img = rn_vis.r
        out_img = (out_img * 255).astype(np.uint8)
        out_img = cv2.cvtColor(out_img, cv2.COLOR_RGB2BGR)

        silhouette_rn = ColoredRenderer()
        silhouette_rn.camera = ProjectPoints(v=self.body,
                                             rt=np.zeros(3),
                                             t=np.zeros(3),
                                             f=np.ones(2) * cam_for_render[0],
                                             c=cam_for_render[1:],
                                             k=np.zeros(5))
        silhouette_rn.frustum = {
            'near': 0.1,
            'far': 1000.,
            'width': self.width,
            'height': self.height
        }
        silhouette_rn.set(v=vert_shifted,
                          f=self.m.f,
                          vc=self.m.vc,
                          bgcolor=np.zeros(3))

        return out_img, texture_dr_wrt(rn_vis,
                                       silhouette_rn.r), silhouette_rn.r
Esempio n. 4
0
    def render(self, thetas, texture_bgr, rotate=np.array([0, 0, 0]), background_img=None):
        """
        get the rendered image and rendered silhouette
        :param thetas: model parameters, 3 * camera parameter + 72 * body pose + 10 * body shape
        :param texture_bgr: texture image in bgr format
        :return: the rendered image and deviation of rendered image to texture image
        (rendered image, deviation of rendered image, silhouette)
        """
        self.set_texture(texture_bgr)
        thetas = thetas.reshape(-1)
        cams = thetas[:self.num_cam]
        theta = thetas[self.num_cam: (self.num_cam + self.num_theta)]
        beta = thetas[(self.num_cam + self.num_theta):]

        self.body.pose[:] = theta
        self.body.betas[:] = beta

        #
        # size = cams[0] * min(self.w, self.h)
        # position = cams[1:3] * min(self.w, self.h) / 2 + min(self.w, self.h) / 2
        """
        ####################################################################
        ATTENTION!
        I do not know why the flength is 500.
        But it worked
        ####################################################################
        """

        texture_rn = TexturedRenderer()
        texture_rn.camera = ProjectPoints(v=self.body, rt=rotate, t=ch.array([0, 0, 2]),
                                          f=np.ones(2) * self.img_size * 0.62,
                                          c=np.array([self.w / 2, self.h / 2]),
                                          k=ch.zeros(5))
        texture_rn.frustum = {'near': 1., 'far': 10., 'width': self.w, 'height': self.h}
        texture_rn.set(v=self.body, f=self.m.f, vc=self.m.vc, texture_image=self.m.texture_image, ft=self.m.ft,
                       vt=self.m.vt)
        if background_img is not None:
            texture_rn.background_image = background_img / 255. if background_img.max() > 1 else background_img

        silhouette_rn = ColoredRenderer()
        silhouette_rn.camera = ProjectPoints(v=self.body, rt=rotate, t=ch.array([0, 0, 2]),
                                             f=np.ones(2) * self.img_size * 0.62,
                                             c=np.array([self.w / 2, self.h / 2]),
                                             k=ch.zeros(5))
        silhouette_rn.frustum = {'near': 1., 'far': 10., 'width': self.w, 'height': self.h}
        silhouette_rn.set(v=self.body, f=self.m.f, vc=np.ones_like(self.body), bgcolor=np.zeros(3))

        return texture_rn.r, texture_dr_wrt(texture_rn, silhouette_rn.r), silhouette_rn.r