def sample_texture(img, pts, ref, tm): """returns a texture vector""" img = normalize(img, get_aabb(pts)) mask = get_mask(ref, img.shape[:2]) verts = get_vertices(ref) warp = tm.warp_triangles(img, pts[verts], ref[verts]) return warp[mask].ravel()
def get_data_matrix(imgs, lmks, ref): ref = ref.reshape((ref.size//2, 2)).astype('int32') mask = get_mask(ref, (640, 480)) # FIXME hardcoded image size verts = get_vertices(ref) tm = TextureMapper(480, 640) # ditto n_samples = lmks.shape[1] n_pixels = mask.sum() * 3 G = np.empty((n_pixels, n_samples)) for i in range(n_samples): src = lmks[:,i].reshape(ref.shape) img = normalize(next(imgs), get_aabb(src)) warp = tm.warp_triangles(img, src[verts], ref[verts]) G[:,i] = warp[mask].ravel() return G
def get_data_matrix(imgs, lmks, ref): ref = ref.reshape((ref.size//2, 2)).astype('int32') mask = get_mask(ref, (640, 480)) # FIXME hardcoded image size verts = get_vertices(ref) tm = TextureMapper(480, 640) # ditto n_samples = lmks.shape[1] n_pixels = mask.sum() * 3 G = np.empty((n_pixels, n_samples)) for i in xrange(n_samples): src = lmks[:,i].reshape(ref.shape) img = normalize(next(imgs), get_aabb(src)) warp = tm.warp_triangles(img, src[verts], ref[verts]) G[:,i] = warp[mask].ravel() return G
def calc_params(self, img, lmk, ref, warp_triangles): ref = ref.reshape((ref.size//2, 2)).astype('int32') src = lmk.reshape(ref.shape) img = normalize(img, get_aabb(src)) mask = get_mask(ref, img.shape[:2]) verts = get_vertices(ref) warp = warp_triangles(img, src[verts], ref[verts]) t = warp[mask].ravel() - self.mean p = self.model.T.dot(t) # clamp c = 3 for i in range(len(self.variance)): v = c * np.sqrt(self.variance[i]) if abs(p[i]) > v: p[i] = v if p[i] > 0 else -v return p
def calc_params(self, img, lmk, ref, warp_triangles): ref = ref.reshape((ref.size//2, 2)).astype('int32') src = lmk.reshape(ref.shape) img = normalize(img, get_aabb(src)) mask = get_mask(ref, img.shape[:2]) verts = get_vertices(ref) warp = warp_triangles(img, src[verts], ref[verts]) t = warp[mask].ravel() - self.mean p = self.model.T.dot(t) # clamp c = 3 for i in xrange(len(self.variance)): v = c * np.sqrt(self.variance[i]) if abs(p[i]) > v: p[i] = v if p[i] > 0 else -v return p
ref = smodel.calc_shape(params) ref = ref.reshape((len(ref) // 2, 2)) verts = get_vertices(ref) muct = MuctDataset() muct.load(clean=True) if args.use_texturemapper: from pyaam.texturemapper import TextureMapper tm = TextureMapper(480, 640) warp_triangles = tm.warp_triangles for name, tag, lmks, flipped in muct.iterdata(): img = muct.image(name) pts = lmks.reshape((len(lmks) // 2, 2)) aabb = get_aabb(pts) if args.normalize: img = normalize(img, aabb) orig = img.copy() if args.show_triangles: draw_polygons(img, pts[verts], Color.blue) cv2.imshow('triangles', img) warped = warp_triangles(orig, pts[verts], ref[verts]) if args.show_triangles: draw_polygons(warped, ref[verts], Color.blue) cv2.imshow('warp', warped[:300, :300]) key = cv2.waitKey() if key == 27: sys.exit()