Permalink
Browse files

added option to converter --output-face-scale-modifier

  • Loading branch information...
iperov committed Nov 28, 2018
1 parent 2576a41 commit 64c3e57f1cb09d87626a5166687c6b417c798c8e
Showing with 29 additions and 19 deletions.
  1. +8 −6 facelib/LandmarksProcessor.py
  2. +8 −0 main.py
  3. +13 −13 models/ConverterMasked.py
@@ -35,8 +35,8 @@
"left_eye": (42, 48),
"nose": (27, 36), # missed one point
"jaw": (0, 17) }
def get_transform_mat (image_landmarks, output_size, face_type):
def get_transform_mat (image_landmarks, output_size, face_type, scale=1.0):
if not isinstance(image_landmarks, np.ndarray):
image_landmarks = np.array (image_landmarks)
@@ -63,13 +63,15 @@ def get_transform_mat (image_landmarks, output_size, face_type):
padding = (output_size / 64) * 24
else:
raise ValueError ('wrong face_type')
mat = umeyama(image_landmarks[17:], landmarks_2D, True)[0:2]
mat = mat * (output_size - 2 * padding)
mat[:,2] += padding
mat[:,2] += padding
mat *= (1 / scale)
mat[:,2] += -output_size*( ( (1 / scale) - 1.0 ) / 2 )
return mat
def transform_points(points, mat, invert=False):
if invert:
mat = cv2.invertAffineTransform (mat)
@@ -137,6 +137,11 @@ def process_convert(arguments):
except:
arguments.blur_mask_modifier = 0
try:
arguments.output_face_scale_modifier = int ( input ("Choose output face scale modifier [-50..50] (default 0) : ") )
except:
arguments.output_face_scale_modifier = 0
try:
arguments.alpha = bool ( {"1":True,"0":False}[input("Export png with alpha channel? [0..1] (default 0) : ").lower()] )
except:
@@ -149,6 +154,7 @@ def process_convert(arguments):
arguments.erode_mask_modifier = np.clip ( int(arguments.erode_mask_modifier), -100, 100)
arguments.blur_mask_modifier = np.clip ( int(arguments.blur_mask_modifier), -100, 200)
arguments.output_face_scale_modifier = np.clip ( int(arguments.output_face_scale_modifier), -50, 50)
from mainscripts import Converter
Converter.main (
@@ -162,6 +168,7 @@ def process_convert(arguments):
masked_hist_match = arguments.masked_hist_match,
erode_mask_modifier = arguments.erode_mask_modifier,
blur_mask_modifier = arguments.blur_mask_modifier,
output_face_scale_modifier = arguments.output_face_scale_modifier,
force_best_gpu_idx = arguments.force_best_gpu_idx,
alpha = arguments.alpha,
transfercolor = arguments.transfercolor,
@@ -178,6 +185,7 @@ def process_convert(arguments):
convert_parser.add_argument('--masked-hist-match', type=str2bool, nargs='?', const=True, default=None, help="True or False. Excludes background for hist match. Default - model decide.")
convert_parser.add_argument('--erode-mask-modifier', type=int, dest="erode_mask_modifier", default=0, help="Automatic erode mask modifier. Valid range [-100..100].")
convert_parser.add_argument('--blur-mask-modifier', type=int, dest="blur_mask_modifier", default=0, help="Automatic blur mask modifier. Valid range [-100..200].")
convert_parser.add_argument('--output-face-scale-modifier', type=int, dest="output_face_scale_modifier", default=0, help="Output face scale modifier. Valid range [-50..50].")
convert_parser.add_argument('--debug', action="store_true", dest="debug", default=False, help="Debug converter.")
convert_parser.add_argument('--alpha', action="store_true", dest="alpha", default=False, help="alpha channel.")
convert_parser.add_argument('--transfercolor', action="store_true", dest="transfercolor", default=False, help="transfer color from dst to merged.")
@@ -5,12 +5,7 @@
import numpy as np
from utils import image_utils
'''
predictor:
input: [predictor_input_size, predictor_input_size, BGRA]
output: [predictor_input_size, predictor_input_size, BGRA]
'''
class ConverterMasked(ConverterBase):
#override
@@ -24,9 +19,10 @@ def __init__(self, predictor,
masked_hist_match = False,
mode='seamless',
erode_mask_modifier=0,
blur_mask_modifier=0,
blur_mask_modifier=0,
output_face_scale_modifier=0.0,
alpha=False,
transfercolor=False,
transfercolor=False,
**in_options):
super().__init__(predictor)
@@ -41,6 +37,7 @@ def __init__(self, predictor,
self.mode = mode
self.erode_mask_modifier = erode_mask_modifier
self.blur_mask_modifier = blur_mask_modifier
self.output_face_scale = np.clip(1.0 + output_face_scale_modifier*0.01, 0.5, 1.0)
self.alpha = alpha
self.transfercolor = transfercolor
@@ -68,6 +65,8 @@ def convert_face (self, img_bgr, img_face_landmarks, debug):
img_face_mask_a = LandmarksProcessor.get_image_hull_mask (img_bgr, img_face_landmarks)
face_mat = LandmarksProcessor.get_transform_mat (img_face_landmarks, self.output_size, face_type=self.face_type)
face_output_mat = LandmarksProcessor.get_transform_mat (img_face_landmarks, self.output_size, face_type=self.face_type, scale=self.output_face_scale)
dst_face_bgr = cv2.warpAffine( img_bgr , face_mat, (self.output_size, self.output_size), flags=cv2.INTER_LANCZOS4 )
dst_face_mask_a_0 = cv2.warpAffine( img_face_mask_a, face_mat, (self.output_size, self.output_size), flags=cv2.INTER_LANCZOS4 )
@@ -84,7 +83,7 @@ def convert_face (self, img_bgr, img_face_landmarks, debug):
prd_face_mask_a = np.expand_dims (prd_face_mask_a_0, axis=-1)
prd_face_mask_aaa = np.repeat (prd_face_mask_a, (3,), axis=-1)
img_prd_face_mask_aaa = cv2.warpAffine( prd_face_mask_aaa, face_mat, img_size, np.zeros(img_bgr.shape, dtype=float), flags=cv2.WARP_INVERSE_MAP | cv2.INTER_LANCZOS4 )
img_prd_face_mask_aaa = cv2.warpAffine( prd_face_mask_aaa, face_output_mat, img_size, np.zeros(img_bgr.shape, dtype=float), flags=cv2.WARP_INVERSE_MAP | cv2.INTER_LANCZOS4 )
img_prd_face_mask_aaa = np.clip (img_prd_face_mask_aaa, 0.0, 1.0)
img_face_mask_aaa = img_prd_face_mask_aaa
@@ -146,7 +145,7 @@ def convert_face (self, img_bgr, img_face_landmarks, debug):
if self.mode == 'hist-match' or self.mode == 'hist-match-bw':
if debug:
debugs += [ cv2.warpAffine( prd_face_bgr, face_mat, img_size, np.zeros(img_bgr.shape, dtype=np.float32), cv2.WARP_INVERSE_MAP | cv2.INTER_LANCZOS4, cv2.BORDER_TRANSPARENT ) ]
debugs += [ cv2.warpAffine( prd_face_bgr, face_output_mat, img_size, np.zeros(img_bgr.shape, dtype=np.float32), cv2.WARP_INVERSE_MAP | cv2.INTER_LANCZOS4, cv2.BORDER_TRANSPARENT ) ]
hist_mask_a = np.ones ( prd_face_bgr.shape[:2] + (1,) , dtype=prd_face_bgr.dtype)
@@ -159,8 +158,9 @@ def convert_face (self, img_bgr, img_face_landmarks, debug):
if self.mode == 'hist-match-bw':
prd_face_bgr = prd_face_bgr.astype(np.float32)
out_img = cv2.warpAffine( prd_face_bgr, face_mat, img_size, out_img, cv2.WARP_INVERSE_MAP | cv2.INTER_LANCZOS4, cv2.BORDER_TRANSPARENT )
out_img = cv2.warpAffine( prd_face_bgr, face_output_mat, img_size, out_img, cv2.WARP_INVERSE_MAP | cv2.INTER_LANCZOS4, cv2.BORDER_TRANSPARENT )
if debug:
debugs += [out_img.copy()]
@@ -177,7 +177,7 @@ def convert_face (self, img_bgr, img_face_landmarks, debug):
debugs += [out_img.copy()]
if self.clip_border_mask_per > 0:
img_prd_border_rect_mask_a = cv2.warpAffine( prd_border_rect_mask_a, face_mat, img_size, np.zeros(img_bgr.shape, dtype=np.float32), cv2.WARP_INVERSE_MAP | cv2.INTER_LANCZOS4, cv2.BORDER_TRANSPARENT )
img_prd_border_rect_mask_a = cv2.warpAffine( prd_border_rect_mask_a, face_output_mat, img_size, np.zeros(img_bgr.shape, dtype=np.float32), cv2.WARP_INVERSE_MAP | cv2.INTER_LANCZOS4, cv2.BORDER_TRANSPARENT )
img_prd_border_rect_mask_a = np.expand_dims (img_prd_border_rect_mask_a, -1)
out_img = out_img * img_prd_border_rect_mask_a + img_bgr * (1.0 - img_prd_border_rect_mask_a)
@@ -186,7 +186,7 @@ def convert_face (self, img_bgr, img_face_landmarks, debug):
out_img = np.clip( img_bgr*(1-img_mask_blurry_aaa) + (out_img*img_mask_blurry_aaa) , 0, 1.0 )
if self.mode == 'seamless-hist-match':
out_face_bgr = cv2.warpAffine( out_img, face_mat, (self.output_size, self.output_size) )
out_face_bgr = cv2.warpAffine( out_img, face_mat, (self.output_size, self.output_size) )
new_out_face_bgr = image_utils.color_hist_match(out_face_bgr, dst_face_bgr )
new_out = cv2.warpAffine( new_out_face_bgr, face_mat, img_size, img_bgr.copy(), cv2.WARP_INVERSE_MAP | cv2.INTER_LANCZOS4, cv2.BORDER_TRANSPARENT )
out_img = np.clip( img_bgr*(1-img_mask_blurry_aaa) + (new_out*img_mask_blurry_aaa) , 0, 1.0 )

0 comments on commit 64c3e57

Please sign in to comment.