From 3dcedf6bf3fccb8f9d5d08b09b4e1fdfdebcba13 Mon Sep 17 00:00:00 2001 From: MaksV79 <38731398+MaksV79@users.noreply.github.com> Date: Sat, 17 Nov 2018 18:12:38 +0300 Subject: [PATCH 1/4] Added options --alpha and --transfer color --alpha will make export png with alpha channel (export only face) --transfercolor will transfer color from original DST image to fake --- main.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/main.py b/main.py index c2b42d1..1aae01e 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,4 @@ -import os +import os import sys import argparse from utils import Path_utils @@ -136,7 +136,19 @@ if __name__ == "__main__": arguments.blur_mask_modifier = int ( input ("Choose blur mask modifier [-100..200] (default 0) : ") ) except: arguments.blur_mask_modifier = 0 - + + if (arguments.alpha == False): #if argument was not passed in command line, ask it + try: + arguments.alpha = bool ( {"1":True,"0":False}[input("Export png with alpha channel? [0..1] (default 0) : ").lower()] ) + except: + arguments.alpha = False + + if (arguments.transfercolor == False): #if argument was not passed in command line, ask it + try: + arguments.transfercolor = bool ( {"1":True,"0":False}[input("Transfer color from original DST image? [0..1] (default 0) : ").lower()] ) + except: + arguments.transfercolor = False + 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) @@ -152,7 +164,9 @@ if __name__ == "__main__": masked_hist_match = arguments.masked_hist_match, erode_mask_modifier = arguments.erode_mask_modifier, blur_mask_modifier = arguments.blur_mask_modifier, - force_best_gpu_idx = arguments.force_best_gpu_idx + force_best_gpu_idx = arguments.force_best_gpu_idx, + alpha = arguments.alpha, + transfercolor = arguments.transfercolor, ) convert_parser = subparsers.add_parser( "convert", help="Converter") @@ -167,6 +181,8 @@ if __name__ == "__main__": 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('--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.") convert_parser.add_argument('--force-best-gpu-idx', type=int, dest="force_best_gpu_idx", default=-1, help="Force to choose this GPU idx as best.") convert_parser.set_defaults(func=process_convert) @@ -185,4 +201,4 @@ if __name__ == "__main__": ''' import code code.interact(local=dict(globals(), **locals())) -''' \ No newline at end of file +''' From 6bbb8ad59b54e4391d4b5f97d9bb007ad59a2527 Mon Sep 17 00:00:00 2001 From: MaksV79 <38731398+MaksV79@users.noreply.github.com> Date: Sat, 17 Nov 2018 18:14:17 +0300 Subject: [PATCH 2/4] Added option --alpha and --transfercolor --- models/ConverterMasked.py | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/models/ConverterMasked.py b/models/ConverterMasked.py index daba910..92e5296 100644 --- a/models/ConverterMasked.py +++ b/models/ConverterMasked.py @@ -24,7 +24,9 @@ class ConverterMasked(ConverterBase): masked_hist_match = False, mode='seamless', erode_mask_modifier=0, - blur_mask_modifier=0, + blur_mask_modifier=0, + alpha=False, + transfercolor=False, **in_options): super().__init__(predictor) @@ -39,6 +41,8 @@ class ConverterMasked(ConverterBase): self.mode = mode self.erode_mask_modifier = erode_mask_modifier self.blur_mask_modifier = blur_mask_modifier + self.alpha = alpha + self.transfercolor = transfercolor if self.erode_mask_modifier != 0 and not self.erode_mask: print ("Erode mask modifier not used in this model.") @@ -186,9 +190,28 @@ class ConverterMasked(ConverterBase): 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 ) - + + if self.transfercolor: #making transfer color from original DST image to fake + from skimage import io, color + lab_clr = color.rgb2lab(img_bgr) #original DST, converting RGB to LAB color space + lab_bw = color.rgb2lab(out_img) #fake, converting RGB to LAB color space + tmp_channel, a_channel, b_channel = cv2.split(lab_clr) #taking color channel A and B from original dst image + l_channel, tmp2_channel, tmp3_channel = cv2.split(lab_bw) #taking lightness channel L from merged fake + img_LAB = cv2.merge((l_channel,a_channel, b_channel)) #merging light and color + out_img = color.lab2rgb(img_LAB) #converting LAB to RGB + + if self.alpha: + new_image = out_img.copy() + new_image = (new_image*255).astype(np.uint8) #convert image to int + b_channel, g_channel, r_channel = cv2.split(new_image) #splitting RGB + alpha_channel = img_mask_blurry_aaa.copy() #making copy of alpha channel + alpha_channel = (alpha_channel*255).astype(np.uint8) + alpha_channel, tmp2, tmp3 = cv2.split(alpha_channel) #splitting alpha to three channels, they all same in original alpha channel, we need just one + out_img = cv2.merge((b_channel,g_channel, r_channel, alpha_channel)) #mergin RGB with alpha + out_img = out_img.astype(np.float32) / 255.0 + if debug: debugs += [out_img.copy()] return debugs if debug else out_img - \ No newline at end of file + From b4af9d3c36ea0b0e9a111b106b275033a540301a Mon Sep 17 00:00:00 2001 From: MaksV79 <38731398+MaksV79@users.noreply.github.com> Date: Sat, 17 Nov 2018 18:20:11 +0300 Subject: [PATCH 3/4] added skimage --- requirements-gpu-cuda9-cudnn7.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/requirements-gpu-cuda9-cudnn7.txt b/requirements-gpu-cuda9-cudnn7.txt index 3a2c906..ce57662 100644 --- a/requirements-gpu-cuda9-cudnn7.txt +++ b/requirements-gpu-cuda9-cudnn7.txt @@ -7,4 +7,5 @@ tensorflow-gpu==1.8.0 scikit-image dlib==19.10.0 tqdm -git+https://www.github.com/keras-team/keras-contrib.git \ No newline at end of file +git+https://www.github.com/keras-team/keras-contrib.git +skimage From d47ef23df1821ee03756569ab8e6257ae54a51ec Mon Sep 17 00:00:00 2001 From: MaksV79 <38731398+MaksV79@users.noreply.github.com> Date: Mon, 19 Nov 2018 23:31:06 +0300 Subject: [PATCH 4/4] Fix options --alpha and --transfercolor Always use params from dialog --- main.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/main.py b/main.py index 1aae01e..9122cc5 100644 --- a/main.py +++ b/main.py @@ -137,17 +137,15 @@ if __name__ == "__main__": except: arguments.blur_mask_modifier = 0 - if (arguments.alpha == False): #if argument was not passed in command line, ask it - try: - arguments.alpha = bool ( {"1":True,"0":False}[input("Export png with alpha channel? [0..1] (default 0) : ").lower()] ) - except: - arguments.alpha = False + try: + arguments.alpha = bool ( {"1":True,"0":False}[input("Export png with alpha channel? [0..1] (default 0) : ").lower()] ) + except: + arguments.alpha = False - if (arguments.transfercolor == False): #if argument was not passed in command line, ask it - try: - arguments.transfercolor = bool ( {"1":True,"0":False}[input("Transfer color from original DST image? [0..1] (default 0) : ").lower()] ) - except: - arguments.transfercolor = False + try: + arguments.transfercolor = bool ( {"1":True,"0":False}[input("Transfer color from original DST image? [0..1] (default 0) : ").lower()] ) + except: + arguments.transfercolor = False 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)