Example #1
0
def _fit_rot_trans(
        model,  # pylint: disable=too-many-arguments, too-many-locals
        j2d,
        center,
        init_t,
        init_pose,
        conf,
        flength):
    """Find a rotation and translation to minimize the projection error of the pose."""
    opt_pose = _ch.array(init_pose)  # pylint: disable=no-member
    opt_trans = _ch.array(init_t)  # pylint: disable=no-member
    (_, A_global) = _global_rigid_transformation(opt_pose,
                                                 model.J,
                                                 model.kintree_table,
                                                 xp=_ch)
    Jtr = _ch.vstack([g[:3, 3] for g in A_global]) + opt_trans  # pylint: disable=no-member

    cam = _ProjectPoints(f=_np.array([flength, flength]),
                         rt=_np.zeros(3),
                         t=_np.zeros(3),
                         k=_np.zeros(5),
                         c=center)
    cam_3d = _ProjectPoints3D(f=_np.array([flength, flength]),
                              rt=_np.zeros(3),
                              t=_np.zeros(3),
                              k=_np.zeros(5),
                              c=center)
    cam.v = Jtr
    cam_3d.v = Jtr
    free_variables = [opt_pose[:3],
                      opt_trans]  # Optimize global rotation and translation.
    j2d_ids = range(12)
    smpl_ids = [8, 5, 2, 1, 4, 7, 21, 19, 17, 16, 18, 20]
    #_CID_TORSO = [2, 3, 8, 9]
    #torso_smpl_ids = [2, 1, 17, 16]
    _ch.minimize(
        {
            'j2d': (j2d.T[j2d_ids] - cam[smpl_ids]).T * conf[j2d_ids],
            # 'dev^2': 1e2 * (opt_trans[2] - init_t[2])
        },
        x0=free_variables,
        method='dogleg',
        callback=None,  #on_step_vis,
        options={
            'maxiter': 100,
            'e_3': .0001,
            'disp': 0
        })
    _LOGGER.debug("Global rotation: %s, global translation: %s.",
                  str(opt_pose[:3].r), str(opt_trans.r))
    _LOGGER.debug("Points 3D: %s.", str(cam_3d.r[:10, :]))
    _LOGGER.debug("Points 2D: %s.", str(cam.r[:10, :]))
    return opt_pose[:3].r, opt_trans.r, cam_3d.r[:,
                                                 2].min(), cam_3d.r[:,
                                                                    2].max()
Example #2
0
def create_renderer(w=640,  # pylint: disable=too-many-arguments
                    h=480,
                    rt=_np.zeros(3),
                    t=_np.zeros(3),
                    f=None,  # pylint: disable=redefined-outer-name
                    c=None,
                    k=None,
                    near=.5,
                    far=10.):
    """Create a colored renderer."""
    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
    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
Example #3
0
def initialize_camera(
        model,  # pylint: disable=too-many-arguments, too-many-locals
        j2d,
        center,
        img,
        init_t,
        init_pose,
        conf,
        is_gt,
        flength=1000.,
        pix_thsh=25.,
        viz=False):
    """Initialize the camera."""
    # try to optimize camera translation and rotation based on torso joints
    # right shoulder, left shoulder, right hip, left hip
    torso_cids = _CID_TORSO
    torso_smpl_ids = [2, 1, 17, 16]

    # initialize camera rotation and translation
    rt = _ch.zeros(3)  # pylint: disable=no-member
    _LOGGER.info('Initializing camera: guessing translation via similarity')
    init_t = guess_init(model, flength, j2d, conf, init_pose, is_gt)
    t = _ch.array(init_t)  # pylint: disable=no-member

    # check how close the shoulders are
    try_both_orient = _np.linalg.norm(j2d[8] - j2d[9]) < pix_thsh

    opt_pose = _ch.array(init_pose)  # pylint: disable=no-member
    (_, A_global) = _global_rigid_transformation(opt_pose,
                                                 model.J,
                                                 model.kintree_table,
                                                 xp=_ch)
    Jtr = _ch.vstack([g[:3, 3] for g in A_global])  # pylint: disable=no-member

    # initialize the camera
    cam = _ProjectPoints(f=_np.array([flength, flength]),
                         rt=rt,
                         t=t,
                         k=_np.zeros(5),
                         c=center)

    # we are going to project the SMPL joints
    cam.v = Jtr

    if viz:
        viz_img = img.copy()

        # draw the target joints
        for coord in _np.around(j2d).astype(int):
            if (coord[0] < img.shape[1] and coord[0] >= 0
                    and coord[1] < img.shape[0] and coord[1] >= 0):
                _cv2.circle(viz_img, tuple(coord), 3, [0, 255, 0])

        import matplotlib.pyplot as plt
        plt.ion()

        # draw optimized joints at each iteration
        def on_step(_):
            """Draw a visualization."""
            plt.figure(1, figsize=(5, 5))
            plt.subplot(1, 1, 1)
            viz_img = img.copy()
            for coord in _np.around(cam.r[torso_smpl_ids]).astype(int):
                if (coord[0] < viz_img.shape[1] and coord[0] >= 0
                        and coord[1] < viz_img.shape[0] and coord[1] >= 0):
                    _cv2.circle(viz_img, tuple(coord), 3, [0, 0, 255])
            plt.imshow(viz_img[:, :, ::-1])
            plt.draw()
            plt.show()
    else:
        on_step = None
    free_variables = [cam.t, opt_pose[:3]]  # pylint: disable=no-member
    _ch.minimize(
        [(j2d[torso_cids] - cam[torso_smpl_ids]).T * conf[torso_cids], 1e2 *
         (cam.t[2] - init_t[2])],  # pylint: disable=no-member
        # The same for verbose output.
        #{'cam': (j2d[torso_cids] - cam[torso_smpl_ids]).T * conf[torso_cids],
        # # Reduce the weight here to avoid the 'small people' problem.
        # 'cam_t': 1e2*(cam.t[2]-init_t[2])},  # pylint: disable=no-member
        x0=free_variables,
        method='dogleg',
        callback=on_step,
        options={
            'maxiter': 100,
            'e_3': .0001,
            'disp': 0
        })
    if viz:
        plt.ioff()
    return (cam, try_both_orient, opt_pose[:3].r)