Exemple #1
0
def make_posmaps(num_to_read=10, folder='300W_LP'):
    uv_h = uv_w = 256
    image_h = image_w = 256
    save_folder = 'data_input'
    if folder == 'AFLW2000':
        save_folder = 'posmap_output'

    global uv_coords
    uv_coords = face3d.morphable_model.load.load_uv_coords(
        'BFM/Out/BFM_UV.mat')
    uv_coords = process_uv(uv_coords, uv_h, uv_w)

    bfm = MorphabelModel('BFM/Out/BFM.mat')

    img_paths = glob.glob(folder + '/*.jpg')
    mat_paths = glob.glob(folder + '/*.mat')

    img_mat_comb = []
    for i in range(len(img_paths)):
        img_paths[i] = img_paths[i].replace('\\', '/')
        mat_paths[i] = mat_paths[i].replace('\\', '/')
        img_mat_comb.append([img_paths[i], mat_paths[i]])

    random.shuffle(img_mat_comb)

    for i in range(num_to_read):
        run_posmap_300W_LP(bfm, img_mat_comb[i][0], img_mat_comb[i][1],
                           save_folder)
def generate_batch_sample(input_dir, save_folder='./300WLP'):
    if not os.path.exists(save_folder):
        os.mkdir(save_folder)
    # set para
    uv_h = uv_w = 256

    # load uv coords
    global uv_coords
    uv_coords = face3d.morphable_model.load.load_uv_coords('BFM/BFM_UV.mat')  #
    uv_coords = process_uv(uv_coords, uv_h, uv_w)

    # load bfm
    bfm = MorphabelModel('BFM/BFM.mat')

    # Batch generating uv_map Dataset
    """
    @date: 2019/07/19
    Train Dataset:
        AFW. 10413.
        HELEN. 75351.
        LFPW. 33111.
    Test Dataset:
        IBUG. 3571.

    """
    base = 0

    for idx, item in enumerate(os.listdir(input_dir)):
        if 'jpg' in item:
            ab_path = os.path.join(input_dir, item)
            img_path = ab_path
            mat_path = ab_path.replace('jpg', 'mat')

            run_posmap_300W_LP(bfm, img_path, mat_path, save_folder, idx + base)
            print("Number {} uv_pos_map was generated!".format(idx))
def generate_batch_sample(input_dir, save_folder):
    uv_h = uv_w = 256

    # load uv coords
    global uv_coords
    uv_coords = face3d.morphable_model.load.load_uv_coords(
        './examples/Data/BFM/Out/BFM_UV.mat')  #
    uv_coords = process_uv(uv_coords, uv_h, uv_w)

    # load bfm
    bfm = MorphabelModel('./examples/Data/BFM/Out/BFM.mat')

    base = 0
    path_w = open(
        '/media/weepies/Seagate Backup Plus Drive/3DMM/train_path_afw.txt',
        'w')
    print('input: ', input_dir)
    for idx, item in enumerate(os.listdir(input_dir)):
        print('dealing')
        if 'jpg' in item:
            ab_path = os.path.join(input_dir, item)
            img_path = ab_path
            mat_path = ab_path.replace('jpg', 'mat')

            run_posmap_300W_LP(bfm, img_path, mat_path, save_folder,
                               idx + base)
            name_p = image_name = img_path.strip().split('/')[-1]
            num_name = name_p.split('.jpg')
            path_w.write(
                '/media/weepies/Seagate Backup Plus Drive/3DMM/posnet/300W_HELEN/'
                + name_p + '*' +
                '/media/weepies/Seagate Backup Plus Drive/3DMM/posnet/300W_HELEN/'
                + num_name[0] + '.txt' + '\n')
            # print("Number {} uv_pos_map was generated!".format(idx))
    path_w.close()
Exemple #4
0
def generate_bfm_base_obj():
    bfm = MorphabelModel('../Data/BFM/Out/BFM.mat')
    sp = bfm.get_shape_para('zero')
    ep = bfm.get_exp_para('zero')
    tp = bfm.get_tex_para('random')
    vertices = bfm.generate_vertices(sp, ep)
    colors = bfm.generate_colors(tp)
    colors = np.minimum(np.maximum(colors, 0), 255)
    write_obj_with_colors('head_fit.obj', vertices, bfm.triangles, colors)
Exemple #5
0
def get_depth_image(image_path):
    im = cv2.imread(image_path)
    h, w, c = im.shape
    landmarks = pd.read_pickle(image_path.replace('.jpg', '.pkl'))

    bfm = MorphabelModel('Data/BFM/Out/BFM.mat')
    x = mesh.transform.from_image(landmarks, h, w)
    X_ind = bfm.kpt_ind

    fitted_sp, fitted_ep, fitted_s, fitted_angles, fitted_t = bfm.fit(
        x, X_ind, max_iter=200, isShow=False)
    colors = bfm.generate_colors(np.random.rand(bfm.n_tex_para, 1))
    colors = np.minimum(np.maximum(colors, 0), 1)

    fitted_vertices = bfm.generate_vertices(fitted_sp, fitted_ep)
    transformed_vertices = bfm.transform(fitted_vertices, fitted_s,
                                         fitted_angles, fitted_t)
    image_vertices = mesh.transform.to_image(transformed_vertices, h, w)

    triangles = bfm.triangles
    z = image_vertices[:, 2:]
    z = z - np.min(z)
    z = z / np.max(z)
    attribute = z
    depth_image = mesh.render.render_colors(image_vertices,
                                            triangles,
                                            attribute,
                                            h,
                                            w,
                                            c=1)
    depth_image = (depth_image * 255).astype('uint8')

    savename = image_path.replace('.jpg', '-depth.jpg')
    io.imsave(savename, np.squeeze(depth_image))
def generate_prnet_trainset(root_300wlp, save_trainset):
    '''
        Input:
            root_300wlp.   The root path of your 300w_lp.
            save_trainset. The path you want to save.
                            (All train set totally about 80G)
    '''
    root_300wlp = os.path.abspath(root_300wlp).replace('\\',
                                                       '/')  # For windows
    save_trainset = os.path.abspath(save_trainset).replace('\\',
                                                           '/')  # For windows
    # set para
    uv_h = uv_w = 256

    # load uv coords
    global uv_coords
    uv_coords = face3d.morphable_model.load.load_uv_coords(
        'Data/BFM/Out/BFM_UV.mat')
    uv_coords = process_uv(uv_coords, uv_h, uv_w)

    # load bfm
    bfm = MorphabelModel('Data/BFM/Out/BFM.mat')

    # run
    fp_label = open('trainDataLabel.txt', "w")
    sub_dir_list = os.listdir(root_300wlp)
    for item in sub_dir_list:
        save_folder = os.path.join(save_trainset, item)
        if not os.path.exists(save_folder):
            os.makedirs(save_folder)
        img_list_rep = os.path.join(root_300wlp, item, "*.jpg")
        img_list = glob.glob(img_list_rep)
        for img_path in img_list:
            img_path = img_path.replace('\\', '/')  # For windows
            mat_path = img_path.replace('jpg', 'mat')
            if not os.path.exists(mat_path):
                continue

            save_img_path = img_path.replace(root_300wlp, save_trainset)
            save_npy_path = save_img_path.replace('jpg', 'npy')
            if os.path.exists(save_img_path) and os.path.exists(save_npy_path):
                fp_label.writelines(save_img_path + ' ' + save_npy_path + '\n')
                print("Passing ...")
                continue

            run_posmap_300W_LP(bfm, img_path, mat_path, save_folder)
            print(save_img_path)
            fp_label.writelines(save_img_path + ' ' + save_npy_path + '\n')
    fp_label.close()
Exemple #7
0
 def __init__(self, model_dir, render_only=False):
     self.bfm = MorphabelModel(osp.join(model_dir, 'BFM.mat'))
     self.index_ind = self.bfm.kpt_ind
     uv_coords = face3d.morphable_model.load.load_uv_coords(
         osp.join(model_dir, 'BFM_UV.mat'))
     self.uv_size = (224, 224)
     self.mask_stxr = 0.1
     self.mask_styr = 0.33
     self.mask_etxr = 0.9
     self.mask_etyr = 0.7
     self.tex_h, self.tex_w, self.tex_c = self.uv_size[1], self.uv_size[
         0], 3
     texcoord = np.zeros_like(uv_coords)
     texcoord[:, 0] = uv_coords[:, 0] * (self.tex_h - 1)
     texcoord[:, 1] = uv_coords[:, 1] * (self.tex_w - 1)
     texcoord[:, 1] = self.tex_w - texcoord[:, 1] - 1
     self.texcoord = np.hstack((texcoord, np.zeros((texcoord.shape[0], 1))))
     self.X_ind = self.bfm.kpt_ind
     if not render_only:
         from image_3d68 import Handler
         self.if3d68_handler = Handler(osp.join(model_dir, 'if1k3d68'),
                                       0,
                                       192,
                                       ctx_id=0)
Exemple #8
0
def demo():
    save_folder = 'results/posmap_300WLP'
    if not os.path.exists(save_folder):
        os.mkdir(save_folder)

    # set para
    uv_h = uv_w = 256

    # load uv coords
    global uv_coords
    uv_coords = face3d.morphable_model.load.load_uv_coords('Data/BFM/Out/BFM_UV.mat')
    uv_coords = process_uv(uv_coords, uv_h, uv_w)

    # load bfm
    bfm = MorphabelModel('Data/BFM/Out/BFM.mat')

    # run
    image_path = 'Data/IBUG_image_008_1_0.jpg'
    mat_path = 'Data/IBUG_image_008_1_0.mat'
    run_posmap_300W_LP(bfm, image_path, mat_path, save_folder)
Exemple #9
0
import numpy as np
import scipy.io as sio
from skimage import io
from time import time
import matplotlib.pyplot as plt
import glob

sys.path.append('..')
import face3d
from face3d import mesh
from face3d import mesh_cython
from face3d.morphable_model import MorphabelModel

# --------------------- Forward: parameters(shape, expression, pose) --> 3D obj --> 2D image  ---------------
# --- 1. load model
bfm = MorphabelModel('Data/BFM/Out/BFM.mat')
print('init bfm model success')


save_folder = 'results/faces/output'
if not os.path.exists(save_folder):
    os.mkdir(save_folder)

h = w = 200; c = 3


for filename in glob.glob('{}/predicted_*.npy'.format(save_folder)):
	i = os.path.splitext(os.path.basename(filename))[0].split('_')[-1]

	params = np.load(filename)
	params = np.float32(params)
Exemple #10
0
class MaskRenderer:
    def __init__(self, model_dir, render_only=False):
        self.bfm = MorphabelModel(osp.join(model_dir, 'BFM.mat'))
        self.index_ind = self.bfm.kpt_ind
        uv_coords = face3d.morphable_model.load.load_uv_coords(
            osp.join(model_dir, 'BFM_UV.mat'))
        self.uv_size = (224, 224)
        self.mask_stxr = 0.1
        self.mask_styr = 0.33
        self.mask_etxr = 0.9
        self.mask_etyr = 0.7
        self.tex_h, self.tex_w, self.tex_c = self.uv_size[1], self.uv_size[
            0], 3
        texcoord = np.zeros_like(uv_coords)
        texcoord[:, 0] = uv_coords[:, 0] * (self.tex_h - 1)
        texcoord[:, 1] = uv_coords[:, 1] * (self.tex_w - 1)
        texcoord[:, 1] = self.tex_w - texcoord[:, 1] - 1
        self.texcoord = np.hstack((texcoord, np.zeros((texcoord.shape[0], 1))))
        self.X_ind = self.bfm.kpt_ind
        if not render_only:
            from image_3d68 import Handler
            self.if3d68_handler = Handler(osp.join(model_dir, 'if1k3d68'),
                                          0,
                                          192,
                                          ctx_id=0)

    def transform(self, shape3D, R):
        s = 1.0
        shape3D[:2, :] = shape3D[:2, :]
        shape3D = s * np.dot(R, shape3D)
        return shape3D

    def preprocess(self, vertices, w, h):
        R1 = mesh.transform.angle2matrix([0, 180, 180])
        t = np.array([-w // 2, -h // 2, 0])
        vertices = vertices.T
        vertices += t
        vertices = self.transform(vertices.T, R1).T
        return vertices

    def project_to_2d(self, vertices, s, angles, t):
        transformed_vertices = self.bfm.transform(vertices, s, angles, t)
        projected_vertices = transformed_vertices.copy(
        )  # using stantard camera & orth projection
        return projected_vertices[self.bfm.kpt_ind, :2]

    def params_to_vertices(self, params, H, W):
        fitted_sp, fitted_ep, fitted_s, fitted_angles, fitted_t = params
        fitted_vertices = self.bfm.generate_vertices(fitted_sp, fitted_ep)
        transformed_vertices = self.bfm.transform(fitted_vertices, fitted_s,
                                                  fitted_angles, fitted_t)
        transformed_vertices = self.preprocess(transformed_vertices.T, W, H)
        image_vertices = mesh.transform.to_image(transformed_vertices, H, W)
        return image_vertices

    def build_params(self, face_image):

        landmark = self.if3d68_handler.get(face_image)[:, :2]
        #print(landmark.shape, landmark.dtype)
        if landmark is None:
            return None  #face not found
        fitted_sp, fitted_ep, fitted_s, fitted_angles, fitted_t = self.bfm.fit(
            landmark, self.X_ind, max_iter=3)
        return [fitted_sp, fitted_ep, fitted_s, fitted_angles, fitted_t]

    def generate_mask_uv(self, mask, positions):
        uv_size = (self.uv_size[1], self.uv_size[0], 3)
        h, w, c = uv_size
        uv = np.zeros(shape=(self.uv_size[1], self.uv_size[0], 3),
                      dtype=np.uint8)
        stxr, styr = positions[0], positions[1]
        etxr, etyr = positions[2], positions[3]
        stx, sty = int(w * stxr), int(h * styr)
        etx, ety = int(w * etxr), int(h * etyr)
        height = ety - sty
        width = etx - stx
        mask = cv2.resize(mask, (width, height))
        uv[sty:ety, stx:etx] = mask
        return uv

    def render_mask(self,
                    face_image,
                    mask_image,
                    params,
                    auto_blend=True,
                    positions=[0.1, 0.33, 0.9, 0.7]):
        uv_mask_image = self.generate_mask_uv(mask_image, positions)
        h, w, c = face_image.shape
        image_vertices = self.params_to_vertices(params, h, w)
        output = (1 - mesh.render.render_texture(
            image_vertices, self.bfm.full_triangles, uv_mask_image,
            self.texcoord, self.bfm.full_triangles, h, w)) * 255
        output = output.astype(np.uint8)
        if auto_blend:
            mask_bd = (output == 255).astype(np.uint8)
            final = face_image * mask_bd + (1 - mask_bd) * output
            return final
        return output
Exemple #11
0
print(sys.version_info)

import scipy.io as sio
#from skimage import io
from time import time
#import matplotlib.pyplot as plt

sys.path.append('..')
import face3d
from face3d import mesh
from face3d.morphable_model import MorphabelModel

# --------------------- Forward: parameters(shape, expression, pose) --> 3D obj  ---------------
# --- 1. load model
bfm = MorphabelModel('Data/BFM/Out/BFM.mat')
print('init bfm model success')

# --- 2. generate face mesh: vertices(represent shape) & colors(represent texture)
sp = bfm.get_shape_para('random')
ep = bfm.get_exp_para('random')
vertices = bfm.generate_vertices(sp, ep)

tp = bfm.get_tex_para('random')
colors = bfm.generate_colors(tp)
colors = np.minimum(np.maximum(colors, 0), 1)

# --- 3. create a mesh in blender and load 3dmm vertices and faces
mesh = bpy.data.meshes.new("3dmm_face")  # add the new mesh
obj = bpy.data.objects.new(mesh.name, mesh)
col = bpy.data.collections.get("Collection")
Exemple #12
0
import os
import sys
import cv2
import numpy as np
import scipy.io as sci
import matplotlib.pyplot as plt

sys.path.append("..")
import face3d
from face3d import mesh
from face3d.morphable_model import MorphabelModel

import dlib
from skimage import io

bfm = MorphabelModel('Data/BFM/Out/BFM.mat')

predictor_path = "./shape_predictor_68_face_landmarks.dat"
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(predictor_path)


def detect_landmark(imgpath):
    img = cv2.imread(imgpath)
    dlib_img = io.imread(imgpath)
    dets = detector(img, 1)
    if len(dets) == 0:
        return 1, None
    x1 = dets[0].left()
    y1 = dets[0].top()
    x2 = dets[0].right()
Exemple #13
0
    save_folder = '300W_LP_out'
    if not os.path.exists(save_folder):
        os.mkdir(save_folder)

    # set para
    uv_h = uv_w = 450
    image_h = image_w = 450

    # load uv coords
    global uv_coords
    uv_coords = face3d.morphable_model.load.load_uv_coords(
        './BFM/BFM_UV.mat')  #
    uv_coords = process_uv(uv_coords, uv_h, uv_w)

    # load bfm
    bfm = MorphabelModel('./BFM/BFM.mat')

    # run
    folders = [
        "AFW", "AFW_Flip", "HELEN", "HELEN_Flip", "IBUG", "IBUG_Flip", "LFPW",
        "LFPW_Flip"
    ]
    folders = ["AFW", "IBUG"]
    for i in folders:
        folder_path = os.path.join(".", "300W_LP", i)
        files = [j[:-4] for j in os.listdir(folder_path) if j[-4:] == ".jpg"]
        for k in tqdm(files):
            image_path = os.path.join(folder_path, k + ".jpg")
            mat_path = os.path.join(folder_path, k + ".mat")
            save_path = os.path.join(save_folder, i + "_" + k)
            if not os.path.exists(save_path):
Exemple #14
0
def main(args):
    with open(args.img_list) as f:
        img_list = [x.strip() for x in f.readlines()]
    landmark_list = []
    if not os.path.exists(args.save_dir):
        os.mkdir(args.save_dir)
    if not os.path.exists(args.save_lmk_dir):
        os.mkdir(args.save_lmk_dir)

    for img_idx, img_fp in enumerate(tqdm(img_list)):
        im = cv2.imread(os.path.join(args.img_prefix, img_fp), 1)
        gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
        h, w, c = im.shape

        detector = dlib.get_frontal_face_detector()
        predictor = dlib.shape_predictor(
            "face3d/models/shape_predictor_68_face_landmarks.dat")

        rects = detector(gray, 1)
        shape = predictor(gray, rects[0])
        tl_corner_x = rects[0].center().x - rects[0].width() / 2
        tl_corner_y = rects[0].center().y - rects[0].height() / 2
        br_corner_x = rects[0].center().x + rects[0].width() / 2
        br_corner_y = rects[0].center().y + rects[0].height() / 2
        rects = [(tl_corner_x, tl_corner_y), (br_corner_x, br_corner_y)]
        landmarks = np.zeros((68, 2))

        for i, p in enumerate(shape.parts()):
            landmarks[i] = [p.x, p.y]
            im = cv2.circle(im, (p.x, p.y),
                            radius=3,
                            color=(0, 0, 255),
                            thickness=5)

        bfm = MorphabelModel('face3d/Data/BFM/Out/BFM.mat')
        x = mesh_numpy.transform.from_image(landmarks, h, w)
        X_ind = bfm.kpt_ind

        fitted_sp, fitted_ep, fitted_s, fitted_angles, fitted_t = bfm.fit(
            x, X_ind, max_iter=200, isShow=False)
        colors = bfm.generate_colors(np.random.rand(bfm.n_tex_para, 1))
        colors = np.minimum(np.maximum(colors, 0), 1)

        fitted_vertices = bfm.generate_vertices(fitted_sp, fitted_ep)
        transformed_vertices = bfm.transform(fitted_vertices, fitted_s,
                                             fitted_angles, fitted_t)
        image_vertices = mesh_numpy.transform.to_image(transformed_vertices, h,
                                                       w)

        triangles = bfm.triangles
        colors = colors / np.max(colors)

        attribute = colors
        color_image = mesh_numpy.render.render_colors(image_vertices,
                                                      triangles,
                                                      attribute,
                                                      h,
                                                      w,
                                                      c=3)
        io.imsave(os.path.join(args.save_lmk_dir,
                               str(img_idx) + ".png"),
                  img_as_ubyte(color_image))
Exemple #15
0
import scipy.io as sio
from skimage import io
from time import time
import matplotlib.pyplot as plt

sys.path.append('..')
import face3d
from face3d import mesh
from face3d.morphable_model import MorphabelModel

import warnings
warnings.filterwarnings("ignore")

# --------------------- Forward: parameters(shape, expression, pose) --> 3D obj --> 2D image  ---------------
# --- 1. load model
bfm = MorphabelModel('../examples/Data/BFM/Out/BFM.mat')

# save folder
save_folder = '3dmm_transform_angle'
if not os.path.exists(save_folder):
    os.mkdir(save_folder)

# --- 2. generate face mesh: vertices(represent shape) & colors(represent texture)
sp = bfm.get_shape_para('random')
ep = bfm.get_exp_para('random')
vertices = bfm.generate_vertices(sp, ep)

tp = bfm.get_tex_para('random')
colors = bfm.generate_colors(tp)
colors = np.minimum(np.maximum(colors, 0), 1)
Exemple #16
0
from face3d import mesh
from face3d import mesh_cython
from face3d.morphable_model import MorphabelModel

import cv2
from skimage import img_as_float
import glob as glob

# --------------------- Forward: parameters(shape, expression, pose) --> 3D obj --> 2D image  ---------------
samples = 768
std = 1.2
h = w = 200
c = 3

# --- 1. load model
bfm = MorphabelModel('Data/BFM/Out/BFM.mat')
pncc = face3d.morphable_model.load.load_pncc_code('Data/BFM/Out/pncc_code.mat')
print('init bfm model success')

# load BG images
imgfiles = glob.glob(os.path.join('/home/karim/Documents/Data/Random',
                                  '*.jpg'))

save_folder = 'results/faces/validation'
if not os.path.exists(save_folder):
    os.mkdir(save_folder)

for i in range(samples):
    b, g, r = cv2.split(cv2.imread(imgfiles[np.random.randint(len(imgfiles))]))
    BG = cv2.merge([r, g, b])
    BG = cv2.resize(BG, (w, h))
Exemple #17
0
import os, sys
import subprocess
import numpy as np
import scipy.io as sio
from skimage import io
from time import time
import matplotlib.pyplot as plt

sys.path.append('..')
import face3d
from face3d import mesh
from face3d.morphable_model import MorphabelModel

# --------------------- Forward: parameters(shape, expression, pose) --> 3D obj --> 2D image  ---------------
# --- 1. load model
bfm = MorphabelModel('Data/BFM/Out/BFM.mat')
print('init bfm model success')

# --- 2. generate face mesh: vertices(represent shape) & colors(represent texture)
sp = bfm.get_shape_para('random')
ep = bfm.get_exp_para('random')
vertices = bfm.generate_vertices(sp, ep)

tp = bfm.get_tex_para('random')
colors = bfm.generate_colors(tp)
colors = np.minimum(np.maximum(colors, 0), 1)

# --- 3. transform vertices to proper position
s = 8e-04
angles = [10, 30, 20]
t = [0, 0, 0]
import dlib
import glob
import subprocess
import numpy as np
import scipy.io as sio
from skimage import io
from time import time
import matplotlib.pyplot as plt

sys.path.append('..')
import face3d
from face3d import mesh
from face3d.morphable_model import MorphabelModel

# load BFM model
bfm = MorphabelModel('examples/Data/BFM/Out/BFM.mat')
print('init bfm model success')

uv_coords = face3d.morphable_model.load.load_uv_coords(
    'examples/Data/BFM/Out/BFM_UV.mat')
t = [0, 0, 0]
c = 3

#random texture and colors
tp = bfm.get_tex_para('random')
colors = bfm.generate_colors(tp)
colors = np.minimum(np.maximum(colors, 0), 1)

predictor_path = "shape_predictor_68_face_landmarks.dat"
faces_folder_path = sys.argv[1]
result_folder_path = sys.argv[2]
Exemple #19
0
if __name__ == '__main__':
    if not os.path.exists(FLAGS["save_uv_path"]):
        os.mkdir(FLAGS["save_uv_path"])

    if not os.path.exists(FLAGS["save_im_path"]):
        os.mkdir(FLAGS["save_im_path"])

    # load uv coords
    global uv_coords
    uv_coords = face3d.morphable_model.load.load_uv_coords(
        FLAGS["bfm_uv_path"])
    uv_coords = process_uv(uv_coords, FLAGS["uv_h"], FLAGS["uv_w"])

    # load bfm
    bfm = MorphabelModel(FLAGS["bfm_path"])
    img_names_list = Path(
        FLAGS["list_path"]).read_text().strip().split('\n')[:128]
    if FLAGS["is62Param"]:
        param_62d = pickle.load(open(FLAGS["param_path"], 'rb'))
        index = 0
        for img_name in tqdm(img_names_list):
            file_name = os.path.splitext(img_name)[0]
            image_path = os.path.join(FLAGS["data_path"], file_name + ".jpg")
            param = param_62d[index]
            generate_posmap_lb_62params(bfm, image_path, param,
                                        FLAGS["save_uv_path"],
                                        FLAGS["save_im_path"])
            index = index + 1
    else:
        for img_name in tqdm(img_names_list):
Exemple #20
0
import subprocess
import numpy as np
import scipy.io as sio
from skimage import io
from time import time
import matplotlib.pyplot as plt

sys.path.append('..')
import face3d
from face3d import mesh
from face3d import mesh_cython
from face3d.morphable_model import MorphabelModel

# --------------------- Forward: parameters(shape, expression, pose) --> 3D obj --> 2D image  ---------------
# --- 1. load model
bfm = MorphabelModel('Data/BFM/Out/BFM.mat')
print('init bfm model success')

# --- 2. generate face mesh: vertices(represent shape) & colors(represent texture)
sp = bfm.get_shape_para('random')
ep = bfm.get_exp_para('random')
vertices = bfm.generate_vertices(sp, ep)

tp = bfm.get_tex_para('random')
colors = bfm.generate_colors(tp)
colors = np.minimum(np.maximum(colors, 0), 1)

# --- 3. transform vertices to proper position
s = 8e-04
angles = [10, 30, 20]
t = [0, 0, 0]
Exemple #21
0
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")

rects = detector(gray, 1)
shape = predictor(gray, rects[0])
tl_corner_x = rects[0].center().x - rects[0].width()/2 # added
tl_corner_y = rects[0].center().y - rects[0].height()/2 # added
br_corner_x = rects[0].center().x + rects[0].width()/2 # added
br_corner_y = rects[0].center().y + rects[0].height()/2 # added
rects = [(tl_corner_x, tl_corner_y), (br_corner_x, br_corner_y)]
landmarks = np.zeros((68, 2))

for i, p in enumerate(shape.parts()):
    landmarks[i] = [p.x, p.y]
    im = cv2.circle(im, (p.x, p.y), radius=3, color=(0, 0, 255), thickness=5)

bfm = MorphabelModel('Data/BFM/Out/BFM.mat')
x = mesh.transform.from_image(landmarks, h, w)
X_ind = bfm.kpt_ind

global colors
global triangles
fitted_sp, fitted_ep, fitted_s, fitted_angles, fitted_t = bfm.fit(x, X_ind, max_iter=200, isShow=False)
colors = bfm.generate_colors(np.random.rand(bfm.n_tex_para, 1))
colors = np.minimum(np.maximum(colors, 0), 1)

fitted_vertices = bfm.generate_vertices(fitted_sp, fitted_ep)
transformed_vertices = bfm.transform(fitted_vertices, fitted_s, fitted_angles, fitted_t)
image_vertices = mesh.transform.to_image(transformed_vertices, h, w)

vertices = image_vertices
triangles = bfm.triangles
Exemple #22
0
3dmm parameters --> mesh 
fitting: 2d image + 3dmm -> 3d face
'''
import os
import sys

import numpy as np
from skimage import io

sys.path.append('..')
from face3d import mesh
from face3d.morphable_model import MorphabelModel

# --------------------- Forward: parameters(shape, expression, pose) --> 3D obj --> 2D image  ---------------
# --- 1. load model
bfm = MorphabelModel('Data/BFM/Out/BFM.mat')
print('init bfm model success')
# print(bfm.n_tex_para)

# --- 2. generate face mesh: vertices(represent shape) & colors(represent texture)
# sp = bfm.get_shape_para('random')
sp = bfm.get_shape_para('zero')
# print(sp_sigma)
# print(sp_sigma.shape)
ep = bfm.get_exp_para('zero')
print(ep.shape)

ep[:10] = np.array([[-2.4658144 ],
 [ 1.5232908 ],
 [-0.58274204],
 [ 1.1200945 ],