コード例 #1
0
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()
コード例 #2
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))
コード例 #4
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))
コード例 #5
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)
コード例 #6
0
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()
コード例 #7
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)
コード例 #8
0
ファイル: mask_renderer.py プロジェクト: zlapp/insightface
 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)
コード例 #9
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):
コード例 #10
0
ファイル: 2_3dmm.py プロジェクト: nwdxbx/xbx_nd
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]
コード例 #11
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):
コード例 #12
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]
コード例 #13
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))