Beispiel #1
0
def main(args):
    if args.isShow or args.isTexture:
        import cv2
        from utils.cv_plot import plot_kpt, plot_vertices, plot_pose_box

    # ---- init PRN
    os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu  # GPU number, -1 for CPU
    prn = PRN(is_dlib=args.isDlib)

    # ------------- load data
    image_folder = args.inputDir
    save_folder = args.outputDir
    if not os.path.exists(save_folder):
        os.mkdir(save_folder)

    types = ('*.jpg', '*.png')
    image_path_list = []
    if os.path.isfile(image_folder):
        image_path_list.append(image_folder)
    for files in types:
        image_path_list.extend(glob(os.path.join(image_folder, files)))
    total_num = len(image_path_list)

    for i, image_path in enumerate(image_path_list):

        name = image_path.strip().split('/')[-1][:-4]

        # read image
        image = imread(image_path)
        [h, w, _] = image.shape

        # the core: regress position map
        if args.isDlib:
            max_size = max(image.shape[0], image.shape[1])
            if max_size > 1000:
                image = rescale(image, 1000. / max_size)
                image = (image * 255).astype(np.uint8)
            pos, crop_image = prn.process(image)  # use dlib to detect face
        else:
            if image.shape[1] == image.shape[2]:
                image = resize(image, (256, 256))
                pos = prn.net_forward(
                    image / 255.)  # input image has been cropped to 256x256
                crop_image = None
            else:
                box = np.array([0, image.shape[1] - 1, 0, image.shape[0] - 1
                                ])  # cropped with bounding box
                pos, crop_image = prn.process(image, box)

        image = image / 255.
        if pos is None:
            continue

        if args.is3d or args.isMat or args.isPose or args.isShow:
            # 3D vertices
            vertices = prn.get_vertices(pos)
            if args.isFront:
                save_vertices = frontalize(vertices)
            else:
                save_vertices = vertices.copy()
            save_vertices[:, 1] = h - 1 - save_vertices[:, 1]

        if args.isImage and crop_image is not None:
            imsave(os.path.join(save_folder, name + '_crop.jpg'), crop_image)
            imsave(os.path.join(save_folder, name + '_orig.jpg'), image)

        if args.is3d:
            # corresponding colors
            colors = prn.get_colors(image, vertices)

            if args.isTexture:
                texture = cv2.remap(image,
                                    pos[:, :, :2].astype(np.float32),
                                    None,
                                    interpolation=cv2.INTER_NEAREST,
                                    borderMode=cv2.BORDER_CONSTANT,
                                    borderValue=(0))
                if args.isMask:
                    vertices_vis = get_visibility(vertices, prn.triangles, h,
                                                  w)
                    uv_mask = get_uv_mask(vertices_vis, prn.triangles,
                                          prn.uv_coords, h, w,
                                          prn.resolution_op)
                    texture = texture * uv_mask[:, :, np.newaxis]
                write_obj_with_texture(
                    os.path.join(save_folder,
                                 name + '.obj'), save_vertices, colors,
                    prn.triangles, texture, prn.uv_coords / prn.resolution_op
                )  #save 3d face with texture(can open with meshlab)
            else:
                write_obj(os.path.join(save_folder,
                                       name + '.obj'), save_vertices, colors,
                          prn.triangles)  #save 3d face(can open with meshlab)

        if args.isDepth:
            depth_image = get_depth_image(vertices, prn.triangles, h, w, True)
            depth = get_depth_image(vertices, prn.triangles, h, w)
            imsave(os.path.join(save_folder, name + '_depth.jpg'), depth_image)
            sio.savemat(os.path.join(save_folder, name + '_depth.mat'),
                        {'depth': depth})

        if args.isMat:
            sio.savemat(os.path.join(save_folder, name + '_mesh.mat'), {
                'vertices': vertices,
                'colors': colors,
                'triangles': prn.triangles
            })

        if args.isKpt or args.isShow:
            # get landmarks
            kpt = prn.get_landmarks(pos)
            np.savetxt(os.path.join(save_folder, name + '_kpt.txt'), kpt)

        if args.isPose or args.isShow:
            # estimate pose
            camera_matrix, pose = estimate_pose(vertices)
            np.savetxt(os.path.join(save_folder, name + '_pose.txt'), pose)
            np.savetxt(os.path.join(save_folder, name + '_camera_matrix.txt'),
                       camera_matrix)

            np.savetxt(os.path.join(save_folder, name + '_pose.txt'), pose)

        if args.isShow:
            # ---------- Plot
            image_pose = plot_pose_box(image, camera_matrix, kpt)
            cv2.imshow('sparse alignment', plot_kpt(image, kpt))
            cv2.imshow('dense alignment', plot_vertices(image, vertices))
            cv2.imshow('pose', plot_pose_box(image, camera_matrix, kpt))
            if crop_image is not None:
                cv2.imshow('crop', crop_image)
            cv2.waitKey(0)
Beispiel #2
0
    # the core: regress position map
    if 'AFLW2000' in image_path:
        mat_path = image_path.replace('jpg', 'mat')
        info = sio.loadmat(mat_path)
        kpt = info['pt3d_68']
        pos = prn.process(
            image, kpt
        )  # kpt information is only used for detecting face and cropping image
    else:
        pos = prn.process(image)  # use dlib to detect face

    # -- Basic Applications
    # get landmarks
    kpt = prn.get_landmarks(pos)
    # 3D vertices
    vertices = prn.get_vertices(pos)
    # corresponding colors
    colors = prn.get_colors(image, vertices)

    # -- save
    name = image_path.strip().split('/')[-1][:-4]
    np.savetxt(os.path.join(save_folder, name + '.txt'), kpt)
    write_obj(os.path.join(save_folder, name + '.obj'), vertices, colors,
              prn.triangles)  #save 3d face(can open with meshlab)

    sio.savemat(os.path.join(save_folder, name + '_mesh.mat'), {
        'vertices': vertices,
        'colors': colors,
        'triangles': prn.triangles
    })
Beispiel #3
0
def main(args):

    # ---- init PRN
    os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu  # GPU number, -1 for CPU
    prn = PRN(is_dlib=args.isDlib)

    # ------------- load data
    # image_folder = args.inputDir
    # save_folder = args.outputDir
    # vertices_dir = args.vertDir

    #i.e. d:\source
    base_dir = args.baseDir

    #i.e. d:\characters
    base_save_dir = args.baseSavedir

    #i.e. source\raupach
    scene = args.sceneDir

    #i.e source\raupach\richardson (the target character)
    character = args.characterDir

    #i.e. source\rauapch\richardson\richardson_001
    source_num = args.sourceNum

    #    targ_character = args.targChar
    #i.e. richardson_targ_10
    targ_num = args.targNum

    # something like D:\source\raupach\richardson\raupach_richardson_001
    image_folder = "%s\\%s\\%s\\%s_%s_%s" % (base_dir, scene, character, scene,
                                             character, source_num)
    print(image_folder)

    #something like d:\character\richardson\vertices\richards_t10
    vertices_dir = "%s\\%s\\vertices\\%s_t%s" % (base_save_dir, character,
                                                 character, targ_num)
    print(vertices_dir)

    #something like d:\character\raupach\src\align\raupach_richardson_t10_s001\\obj
    save_folder = "%s\\%s\\src\\align\\%s_%s_s%s_t%s\\obj" % (
        base_save_dir, character, scene, character, source_num, targ_num)
    print(save_folder)

    if not os.path.exists(save_folder):
        os.makedirs(save_folder)

    # image_path_list= []
    # for root, dirs, files in os.walk('%s' % image_folder):
    #     for file in files:
    #         if file.endswith('.jpg'):
    #             image_path_list.append(file)
    # print (image_path_list)

    types = ('*.jpg', '*.png')
    image_path_list = []
    for files in types:
        image_path_list.extend(glob(os.path.join(image_folder, files)))
    total_num = len(image_path_list)
    image_path_list = sorted(image_path_list)
    #print (image_path_list)

    # #repeating the above logic for a vertices directory.
    types = ('*.npy', '*.jpg')
    vert_path_list = []
    for files in types:
        vert_path_list.extend(glob(os.path.join(vertices_dir, files)))
    total_num_vert = len(vert_path_list)
    # vert_path_list.reverse()
    vert_path_list = sorted(vert_path_list)
    #print (vert_path_list)

    for i, image_path in enumerate(image_path_list):
        name = image_path.strip().split('\\')[-1][:-4]

        print("%s aligned with %s" % (image_path_list[i], vert_path_list[i]))

        # read image
        image = imread(image_path)
        [h, w, _] = image.shape

        # the core: regress position map
        if args.isDlib:
            max_size = max(image.shape[0], image.shape[1])
            if max_size > 1000:
                image = rescale(image, 1000. / max_size)
                image = (image * 255).astype(np.uint8)
            pos = prn.process(image)  # use dlib to detect face
        else:
            if image.shape[1] == image.shape[2]:
                image = resize(image, (256, 256))
                pos = prn.net_forward(
                    image / 255.)  # input image has been cropped to 256x256
            else:
                box = np.array([0, image.shape[1] - 1, 0, image.shape[0] - 1
                                ])  # cropped with bounding box
                pos = prn.process(image, box)

        image = image / 255.
        if pos is None:
            continue

        vertices = prn.get_vertices(pos)
        #takes the nth file in the directory of the vertices to "frontalize" the source image.
        can_vert = vert_path_list[i]
        print(can_vert)
        save_vertices = align(vertices, can_vert)
        save_vertices[:, 1] = h - 1 - save_vertices[:, 1]

        colors = prn.get_colors(image, vertices)

        if args.isTexture:
            texture = cv2.remap(image,
                                pos[:, :, :2].astype(np.float32),
                                None,
                                interpolation=cv2.INTER_NEAREST,
                                borderMode=cv2.BORDER_CONSTANT,
                                borderValue=(0))
            if args.isMask:
                vertices_vis = get_visibility(vertices, prn.triangles, h, w)
                uv_mask = get_uv_mask(vertices_vis, prn.triangles,
                                      prn.uv_coords, h, w, prn.resolution_op)
                texture = texture * uv_mask[:, :, np.newaxis]
            write_obj_with_texture(
                os.path.join(save_folder,
                             name + '.obj'), save_vertices, colors,
                prn.triangles, texture, prn.uv_coords / prn.resolution_op
            )  #save 3d face with texture(can open with meshlab)
        else:
            write_obj(os.path.join(save_folder,
                                   name + '.obj'), save_vertices, colors,
                      prn.triangles)  #save 3d face(can open with meshlab)
Beispiel #4
0
def main(args):
    if args.isShow:
        args.isOpencv = True
        from utils.cv_plot import plot_kpt, plot_vertices, plot_pose_box
    if args.isObj:
        from utils.write import write_obj
    if args.isPose:
        from utils.estimate_pose import estimate_pose

    # ---- init PRN
    os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu  # GPU number, -1 for CPU

    prn = PRN(is_dlib=args.isDlib, is_opencv=args.isOpencv)

    # ------------- load data
    image_folder = args.inputFolder
    save_folder = args.outputFolder
    if not os.path.exists(save_folder):
        os.mkdir(save_folder)

    types = ('*.jpg', '*.png')
    image_path_list = []
    for files in types:
        image_path_list.extend(glob(os.path.join(image_folder, files)))
    total_num = len(image_path_list)

    for i, image_path in enumerate(image_path_list):

        name = image_path.strip().split('/')[-1][:-4]

        # read image
        image = imread(image_path)

        # the core: regress position map
        pos = prn.process(image)  # use dlib to detect face

        if args.isObj or args.isShow:
            # 3D vertices
            vertices = prn.get_vertices(pos)
            # corresponding colors
            colors = prn.get_colors(image, vertices)
            write_obj(os.path.join(save_folder,
                                   name + '.obj'), vertices, colors,
                      prn.triangles)  #save 3d face(can open with meshlab)

        if args.isKpt or args.isShow:
            # get landmarks
            kpt = prn.get_landmarks(pos)
            np.savetxt(os.path.join(save_folder, name + '_kpt.txt'), kpt)

        if args.isPose or args.isShow:
            # estimate pose
            camera_matrix, pose = estimate_pose(vertices)
            np.savetxt(os.path.join(save_folder, name + '_pose.txt'), pose)

        if args.isShow:
            # ---------- Plot
            image_pose = plot_pose_box(image, camera_matrix, kpt)
            cv2.imshow('sparse alignment', plot_kpt(image, kpt))
            cv2.imshow('dense alignment', plot_vertices(image, vertices))
            cv2.imshow('pose', plot_pose_box(image, camera_matrix, kpt))
            cv2.waitKey(0)
Beispiel #5
0
def main(args):
    if args.isShow or args.isTexture:
        import cv2
        from utils.cv_plot import plot_kpt, plot_vertices, plot_pose_box

    # ---- init PRN
    os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu # GPU number, -1 for CPU
    prn = PRN(is_dlib = args.isDlib) 

    # ------------- load data
    image_folder = args.inputDir
    save_folder = args.outputDir
    if not os.path.exists(save_folder):
        os.mkdir(save_folder)

    types = ('*.jpg', '*.png')
    image_path_list= []
    for files in types:
        image_path_list.extend(glob(os.path.join(image_folder, files)))
    total_num = len(image_path_list)

    for i, image_path in enumerate(image_path_list):
        
        name = image_path.strip().split('/')[-1][:-4]
        
        # read image
        image = imread(image_path)
        [h, w, _] = image.shape

        # the core: regress position map    
        if args.isDlib:
            max_size = max(image.shape[0], image.shape[1]) 
            if max_size> 1000:
                image = rescale(image, 1000./max_size)
            pos = prn.process(image) # use dlib to detect face
        else:
            if image.shape[1] == image.shape[2]:
                image = resize(image, (256,256))
                pos = prn.net_forward(image/255.) # input image has been cropped to 256x256
            else:
                box = np.array([0, image.shape[1]-1, 0, image.shape[0]-1]) # cropped with bounding box
                pos = prn.process(image, box)

        image = image/255.
        if pos is None:
            continue

        if args.is3d or args.isMat or args.isPose or args.isShow:        
            # 3D vertices
            vertices = prn.get_vertices(pos)
            if args.isFront:
                save_vertices = frontalize(vertices)
            else:
                save_vertices = vertices

        if args.isImage:
            imsave(os.path.join(save_folder, name + '.jpg'), image) 

        if args.is3d:
            # corresponding colors
            colors = prn.get_colors(image, vertices)

            if args.isTexture:
                texture = cv2.remap(image, pos[:,:,:2].astype(np.float32), None, interpolation=cv2.INTER_NEAREST, borderMode=cv2.BORDER_CONSTANT,borderValue=(0))
                if args.isMask:
                    vertices_vis = get_visibility(vertices, prn.triangles, h, w)
                    uv_mask = get_uv_mask(vertices_vis, prn.triangles, prn.uv_coords, h, w, prn.resolution_op)
                    texture = texture*uv_mask[:,:,np.newaxis]
                write_obj_with_texture(os.path.join(save_folder, name + '.obj'), save_vertices, colors, prn.triangles, texture, prn.uv_coords/prn.resolution_op)#save 3d face with texture(can open with meshlab)
            else:
                write_obj(os.path.join(save_folder, name + '.obj'), save_vertices, colors, prn.triangles) #save 3d face(can open with meshlab)

        if args.isDepth:
            depth_image = get_depth_image(vertices, prn.triangles, h, w) 
            imsave(os.path.join(save_folder, name + '_depth.jpg'), depth_image) 

        if args.isMat:
            sio.savemat(os.path.join(save_folder, name + '_mesh.mat'), {'vertices': save_vertices, 'colors': colors, 'triangles': prn.triangles})

        if args.isKpt or args.isShow:
            # get landmarks
            kpt = prn.get_landmarks(pos)
            np.savetxt(os.path.join(save_folder, name + '_kpt.txt'), kpt) 
        
        if args.isPose or args.isShow:
            # estimate pose
            camera_matrix, pose = estimate_pose(vertices)
            np.savetxt(os.path.join(save_folder, name + '_pose.txt'), pose) 

        if args.isShow:
            # ---------- Plot
            image_pose = plot_pose_box(image, camera_matrix, kpt)
            cv2.imshow('sparse alignment', plot_kpt(image, kpt))
            cv2.imshow('dense alignment', plot_vertices(image, vertices))
            cv2.imshow('pose', plot_pose_box(image, camera_matrix, kpt))
            cv2.waitKey(0)
Beispiel #6
0
    image_path_list.extend(glob(os.path.join(image_folder, files)))
total_num = len(image_path_list)

for i, image_path in enumerate(image_path_list):
    # read image
    image = imread(image_path)

    # the core: regress position map    
    if 'AFLW2000' in image_path:
        mat_path = image_path.replace('jpg', 'mat')
        info = sio.loadmat(mat_path)
        kpt = info['pt3d_68']
        pos = prn.process(image, kpt) # kpt information is only used for detecting face and cropping image
    else:
        pos = prn.process(image) # use dlib to detect face

    # -- Basic Applications
    # get landmarks
    kpt = prn.get_landmarks(pos)
    # 3D vertices
    vertices = prn.get_vertices(pos)
    # corresponding colors
    colors = prn.get_colors(image, vertices)

    # -- save
    name = image_path.strip().split('/')[-1][:-4]
    np.savetxt(os.path.join(save_folder, name + '.txt'), kpt) 
    write_obj(os.path.join(save_folder, name + '.obj'), vertices, colors, prn.triangles) #save 3d face(can open with meshlab)

    sio.savemat(os.path.join(save_folder, name + '_mesh.mat'), {'vertices': vertices, 'colors': colors, 'triangles': prn.triangles})