-
Notifications
You must be signed in to change notification settings - Fork 203
Python Guide
Yuval Nirkin edited this page Sep 14, 2018
·
3 revisions
Make sure you built face_swap with the python binding enabled.
Add "path/to/face_swap/interfaces/python" to Python path.
First initialize the FaceSwap class:
import face_swap_py as fspy
import numpy as np
import cv2
# Initialize face swap
landmarks_path = 'data/shape_predictor_68_face_landmarks.dat'
model_3dmm_h5_path = 'data/BaselFaceModel_mod_wForehead_noEars.h5'
model_3dmm_dat_path = 'data/BaselFace.dat'
reg_model_path = 'data/3dmm_cnn_resnet_101.caffemodel'
reg_deploy_path = 'data/3dmm_cnn_resnet_101_deploy.prototxt'
reg_mean_path = 'data/3dmm_cnn_resnet_101_mean.binaryproto'
seg_model_path = 'data/face_seg_fcn8s.caffemodel' # or 'data/face_seg_fcn8s_300.caffemodel' for lower resolution
seg_deploy_path = 'data/face_seg_fcn8s_deploy.prototxt' # or 'data/face_seg_fcn8s_300_deploy.prototxt' for lower resolution
generic = False
with_expr = True
with_gpu = False
gpu_device_id = 0
fs = fspy.FaceSwap(landmarks_path, model_3dmm_h5_path,
model_3dmm_dat_path, reg_model_path,
reg_deploy_path, reg_mean_path,
seg_model_path, seg_deploy_path,
generic, with_expr, with_gpu, gpu_device_id)
Face swapping 2 images:
img1 = cv2.imread('data/images/brad_pitt_01.jpg')
img2 = cv2.imread('data/images/bruce_willis_01.jpg')
fd1 = fspy.FaceData(img1)
fd2 = fspy.FaceData(img2)
result_img = fs.swap(fd1, fd2)
For more control each image can be processed independently. Intermediate data for each image will be stored in it's corresponding FaceData object.
fs.process(fd1)
fs.process(fd2)
result_img = fs.swap(fd1, fd2)
Processing parameters:
fd1.enable_seg = True # Enable \ Disable segmentation
# Limit processing resolution. This is particularly important for high resolution images (500 is recommended).
fd1.max_bbox_res = 500
fs.process(fd1)
Using an external segmentation:
fd1.seg = external_seg # Single channel numpy image (0 for background pixels and 255 for face pixels)
fs.process(fd1)
Caching processed images: This is will save processing time if the same images are used repeatedly.
fd = fspy.FaceData()
fspy.read_face_data('data/images/brad_pitt_01.jpg', fd) # Will read cache file 'data/images/brad_pitt_01.fs' if exists
overwrite = False
fspy.write_face_data('data/images/brad_pitt_01.fs', fd, overwrite)
Accessing intermediate processing data:
fd.img # Input image
fd.seg # Input segmentation or calculated segmentation
fd.scaled_img # Scaled image according to the max_bbox_res parameter
fd.scaled_seg # Scaled segmentation according to the max_bbox_res parameter
fd.cropped_img # Image cropped around the face
fd.cropped_seg # Segmentation cropped around the face
fd.scaled_landmarks # Scaled landmarks corresponding to the scaled_img
fd.cropped_landmarks # Cropped landmarks corresponding to the cropped_img
fd.bbox # Face bounding box on the original image (img)
fd.scaled_bbox # Face bounding box on the scaled image (scaled_img)
fd.shape_coefficients # 3DMM shape coefficients [99X1]
fd.tex_coefficients # 3DMM texture coefficients [99X1]
fd.expr_coefficients # 3DMM expression coefficients [29X1]
fd.vecR # Face euler angles
fd.vecT # Face translation
fd.K # Face camera matrix
fd.shape_coefficients_flipped # Flipped 3DMM shape coefficients [99X1]
fd.tex_coefficients_flipped # Flipped 3DMM texture coefficients [99X1]
fd.expr_coefficients_flipped # Flipped 3DMM expression coefficients [29X1]
fd.vecR_flipped # Flipped face euler angles
fd.vecT_flipped # Flipped face translation
# Processing parameters
fd.enable_seg # Toggle segmentation
fd.max_bbox_res # Limit processing resolution