Beispiel #1
0
    def train(cls, lmks, imgs, ref, frac, kmax):
        smodel = ShapeModel.load('data/shape.npz')
        tmodel = TextureModel.load('data/texture.npz')

        # build diagonal matrix of weights that measures
        # the unit difference between shape and texture parameters
        r = tmodel.variance.sum() / smodel.variance.sum()
        Ws = r * np.eye(smodel.num_modes())

        n_samples = lmks.shape[1]

        tm = TextureMapper(480, 640)

        # create empty matrix
        n_feats = smodel.num_modes() + tmodel.num_modes()
        A = np.empty((n_feats, n_samples))

        # concatenate shape and texture parameters for each training example
        for i in xrange(n_samples):
            img = next(imgs)
            lmk = lmks[:,i]
            sparams = smodel.calc_params(lmk)
            tparams = tmodel.calc_params(img, lmk, ref, tm.warp_triangles)
            # ignore first 4 shape parameters
            A[:,i] = np.concatenate((Ws.dot(sparams[4:]), tparams))

        D = pca(A, frac, kmax)

        # compute variance
        Q = pow(D.T.dot(A), 2)
        e = Q.sum(axis=1) / (n_samples-1)

        return cls(D, e, Ws)
Beispiel #2
0
def view_combined_model(shp_fn, txt_fn, cmb_fn, scale, tranx, trany, width, height):
    img = np.empty((height, width, 3), dtype='uint8')
    # cv2.namedWindow('combined model')
    tm = TextureMapper(img.shape[1], img.shape[0])
    smodel = ShapeModel.load(shp_fn)
    tmodel = TextureModel.load(txt_fn)
    cmodel = CombinedModel.load(cmb_fn)
    vals = genvals()
    params = smodel.get_params(scale, tranx, trany)
    ref = smodel.calc_shape(params)
    ref = ref.reshape((ref.size // 2, 2))
    verts = get_vertices(ref)
    while True:
        for k in range(cmodel.num_modes()):
            for v in vals:
                p = np.zeros(cmodel.num_modes())
                p[k] = v * 3 * np.sqrt(cmodel.variance[k])
                sparams, tparams = cmodel.calc_shp_tex_params(p, smodel.num_modes())
                params[4:] = sparams

                shp = smodel.calc_shape(params)
                shp = shp.reshape(ref.shape)
                t = tmodel.calc_texture(tparams)
                img[:] = 0
                draw_texture(img, t, ref)
                warped = tm.warp_triangles(img, ref[verts], shp[verts])

                s = 'mode: %d, val: %f sd' % (k, v * 3)
                draw_string(warped, s)
                print('#####')
                # cv2.imshow('combined model', warped)
                cv2.imwrite('output/%d.jpg'%(int(time.time()*1000000)), warped)
Beispiel #3
0
def view_combined_model(shp_fn, txt_fn, cmb_fn, scale, tranx, trany, width, height):
    img = np.empty((height, width, 3), dtype='uint8')
    cv2.namedWindow('combined model')
    tm = TextureMapper(img.shape[1], img.shape[0])
    smodel = ShapeModel.load(shp_fn)
    tmodel = TextureModel.load(txt_fn)
    cmodel = CombinedModel.load(cmb_fn)
    vals = genvals()
    params = smodel.get_params(scale, tranx, trany)
    ref = smodel.calc_shape(params)
    ref = ref.reshape((ref.size//2, 2))
    verts = get_vertices(ref)
    while True:
        for k in xrange(cmodel.num_modes()):
            for v in vals:
                p = np.zeros(cmodel.num_modes())
                p[k] = v * 3 * np.sqrt(cmodel.variance[k])
                sparams, tparams = cmodel.calc_shp_tex_params(p, smodel.num_modes())
                params[4:] = sparams

                shp = smodel.calc_shape(params)
                shp = shp.reshape(ref.shape)
                t = tmodel.calc_texture(tparams)
                img[:] = 0
                draw_texture(img, t, ref)
                warped = tm.warp_triangles(img, ref[verts], shp[verts])

                s = 'mode: %d, val: %f sd' % (k, v*3)
                draw_string(warped, s)
                cv2.imshow('combined model', warped)

                if cv2.waitKey(10) == 27:
                    sys.exit()
Beispiel #4
0
    def train(cls, lmks, imgs, ref, frac, kmax):
        smodel = ShapeModel.load('data/shape.npz')
        tmodel = TextureModel.load('data/texture.npz')

        # build diagonal matrix of weights that measures
        # the unit difference between shape and texture parameters
        r = tmodel.variance.sum() / smodel.variance.sum()
        Ws = r * np.eye(smodel.num_modes())

        n_samples = lmks.shape[1]

        tm = TextureMapper(480, 640)

        # create empty matrix
        n_feats = smodel.num_modes() + tmodel.num_modes()
        A = np.empty((n_feats, n_samples))

        # concatenate shape and texture parameters for each training example
        for i in xrange(n_samples):
            img = next(imgs)
            lmk = lmks[:, i]
            sparams = smodel.calc_params(lmk)
            tparams = tmodel.calc_params(img, lmk, ref, tm.warp_triangles)
            # ignore first 4 shape parameters
            A[:, i] = np.concatenate((Ws.dot(sparams[4:]), tparams))

        D = pca(A, frac, kmax)

        # compute variance
        Q = pow(D.T.dot(A), 2)
        e = Q.sum(axis=1) / (n_samples - 1)

        return cls(D, e, Ws)
Beispiel #5
0
def view_texture_model(shp_fn, txt_fn, scale, tranx, trany, width, height):
    img = np.empty((height, width, 3), dtype='uint8')
    smodel = ShapeModel.load(shp_fn)
    tmodel = TextureModel.load(txt_fn)
    vals = genvals()
    # get reference shape
    ref = smodel.get_shape(scale, tranx, trany)
    ref = ref.reshape((ref.size // 2, 2))
    while True:
        for k in range(tmodel.num_modes()):
            for v in vals:
                p = np.zeros(tmodel.num_modes())
                p[k] = v * 3 * np.sqrt(tmodel.variance[k])
                img[:] = 0
                s = 'mode: %d, val: %f sd' % (k, v * 3)
                draw_string(img, s)
                t = tmodel.calc_texture(p)
                draw_texture(img, t, ref)
                cv2.imshow('texture model', img)
                if cv2.waitKey(10) == 27:
                    sys.exit()
Beispiel #6
0
def view_texture_model(shp_fn, txt_fn, scale, tranx, trany, width, height):
    img = np.empty((height, width, 3), dtype='uint8')
    smodel = ShapeModel.load(shp_fn)
    tmodel = TextureModel.load(txt_fn)
    vals = genvals()
    # get reference shape
    ref = smodel.get_shape(scale, tranx, trany)
    ref = ref.reshape((ref.size//2, 2))
    while True:
        for k in xrange(tmodel.num_modes()):
            for v in vals:
                p = np.zeros(tmodel.num_modes())
                p[k] = v * 3 * np.sqrt(tmodel.variance[k])
                img[:] = 0
                s = 'mode: %d, val: %f sd' % (k, v*3)
                draw_string(img, s)
                t = tmodel.calc_texture(p)
                draw_texture(img, t, ref)
                cv2.imshow('texture model', img)
                if cv2.waitKey(10) == 27:
                    sys.exit()
Beispiel #7
0
                        help='output file')
    parser.add_argument('--no-flip', action='store_false', dest='flipped',
                        help='exclude flipped data')
    return parser.parse_args()

if __name__ == '__main__':
    args = parse_args()

    muct = MuctDataset()
    muct.load(clean=True)

    # If a face is too close to the image border and we perturbe
    # the scale then that face may grow beyond the image border.
    # Ignore problematic images.
    muct.ignore('i405wc-fn')

    data = muct.all_lmks()
    imgs = muct.iterimages(mirror=True)
    print 'training samples:', len(data)

    smodel = ShapeModel.load('data/shape.npz')
    tmodel = TextureModel.load('data/texture.npz')

    # get reference shape
    params = smodel.get_params(200, 150, 150)
    ref = smodel.calc_shape(params)
    ref = ref.reshape((ref.size//2, 2))

    experiments(imgs, data, smodel, tmodel, ref, args.fout)
    print 'wrote', args.fout