diff --git a/mainscripts/Merger.py b/mainscripts/Merger.py index 98a4187..0f1b072 100644 --- a/mainscripts/Merger.py +++ b/mainscripts/Merger.py @@ -190,12 +190,11 @@ class MergeSubprocessor(Subprocessor): self.predictor_input_shape = predictor_input_shape self.face_enhancer = None - def superres_func(mode, face_bgr): - if mode == 1: - if self.face_enhancer is None: - self.face_enhancer = FaceEnhancer(place_model_on_cpu=True) + def superres_func(face_bgr): + if self.face_enhancer is None: + self.face_enhancer = FaceEnhancer(place_model_on_cpu=True) - return self.face_enhancer.enhance (face_bgr, is_tanh=True, preserve_size=False) + return self.face_enhancer.enhance (face_bgr, is_tanh=True, preserve_size=False) self.superres_host, self.superres_func = SubprocessFunctionCaller.make_pair(superres_func) @@ -372,6 +371,8 @@ class MergeSubprocessor(Subprocessor): 'd' : lambda cfg,shift_pressed: cfg.add_blur_mask_modifier(-1 if not shift_pressed else -5), 'r' : lambda cfg,shift_pressed: cfg.add_motion_blur_power(1 if not shift_pressed else 5), 'f' : lambda cfg,shift_pressed: cfg.add_motion_blur_power(-1 if not shift_pressed else -5), + 't' : lambda cfg,shift_pressed: cfg.add_super_resolution_power(1 if not shift_pressed else 5), + 'g' : lambda cfg,shift_pressed: cfg.add_super_resolution_power(-1 if not shift_pressed else -5), 'y' : lambda cfg,shift_pressed: cfg.add_blursharpen_amount(1 if not shift_pressed else 5), 'h' : lambda cfg,shift_pressed: cfg.add_blursharpen_amount(-1 if not shift_pressed else -5), 'u' : lambda cfg,shift_pressed: cfg.add_output_face_scale(1 if not shift_pressed else 5), @@ -386,7 +387,6 @@ class MergeSubprocessor(Subprocessor): 'z' : lambda cfg,shift_pressed: cfg.toggle_masked_hist_match(), 'x' : lambda cfg,shift_pressed: cfg.toggle_mask_mode(), 'c' : lambda cfg,shift_pressed: cfg.toggle_color_transfer_mode(), - 'v' : lambda cfg,shift_pressed: cfg.toggle_super_resolution_mode(), 'n' : lambda cfg,shift_pressed: cfg.toggle_sharpen_mode(), } self.masked_keys = list(self.masked_keys_funcs.keys()) @@ -523,7 +523,7 @@ class MergeSubprocessor(Subprocessor): self.screen_manager.get_current().diff_scale(-0.1) elif chr_key == '=': self.screen_manager.get_current().diff_scale(0.1) - elif chr_key == 'b': + elif chr_key == 'v': self.screen_manager.get_current().toggle_show_checker_board() if go_prev_frame: diff --git a/mainscripts/gfx/help_merger_masked.jpg b/mainscripts/gfx/help_merger_masked.jpg index 926cf28..f3c31e0 100644 Binary files a/mainscripts/gfx/help_merger_masked.jpg and b/mainscripts/gfx/help_merger_masked.jpg differ diff --git a/mainscripts/gfx/help_merger_masked_source.psd b/mainscripts/gfx/help_merger_masked_source.psd index 800cb71..25a440f 100644 Binary files a/mainscripts/gfx/help_merger_masked_source.psd and b/mainscripts/gfx/help_merger_masked_source.psd differ diff --git a/merger/MergeAvatar.py b/merger/MergeAvatar.py index 2f3a000..cc59d23 100644 --- a/merger/MergeAvatar.py +++ b/merger/MergeAvatar.py @@ -26,8 +26,8 @@ def MergeFaceAvatar (predictor_func, predictor_input_shape, cfg, prev_temporal_f prd_f = predictor_func ( prev_imgs, img, next_imgs ) - if cfg.super_resolution_mode != 0: - prd_f = cfg.superres_func(cfg.super_resolution_mode, prd_f) + #if cfg.super_resolution_mode != 0: + # prd_f = cfg.superres_func(cfg.super_resolution_mode, prd_f) if cfg.sharpen_mode != 0 and cfg.sharpen_amount != 0: prd_f = cfg.sharpen_func ( prd_f, cfg.sharpen_mode, 3, cfg.sharpen_amount) diff --git a/merger/MergeMasked.py b/merger/MergeMasked.py index 1a74abc..4719e04 100644 --- a/merger/MergeMasked.py +++ b/merger/MergeMasked.py @@ -22,7 +22,7 @@ def MergeMaskedFace (predictor_func, predictor_input_shape, cfg, frame_info, img input_size = predictor_input_shape[0] mask_subres_size = input_size*4 output_size = input_size - if cfg.super_resolution_mode != 0: + if cfg.super_resolution_power != 0: output_size *= 4 face_mat = LandmarksProcessor.get_transform_mat (img_face_landmarks, output_size, face_type=cfg.face_type) @@ -53,8 +53,12 @@ def MergeMaskedFace (predictor_func, predictor_input_shape, cfg, frame_info, img prd_face_mask_a_0 = cv2.resize (dst_face_mask_a_0, (input_size,input_size) ) predictor_masked = False - if cfg.super_resolution_mode != 0: - prd_face_bgr = cfg.superres_func(cfg.super_resolution_mode, prd_face_bgr) + if cfg.super_resolution_power != 0: + prd_face_bgr_enhanced = cfg.superres_func(prd_face_bgr) + mod = cfg.super_resolution_power / 100.0 + + prd_face_bgr = cv2.resize(prd_face_bgr, (output_size,output_size))*(1.0-mod) + \ + prd_face_bgr_enhanced*mod prd_face_bgr = np.clip(prd_face_bgr, 0, 1) if predictor_masked: @@ -123,7 +127,7 @@ def MergeMaskedFace (predictor_func, predictor_input_shape, cfg, frame_info, img # clip eroded/dilated mask in actual predict area # pad with half blur size in order to accuratelly fade to zero at the boundary clip_size = input_size + blur // 2 - + prd_face_mask_a_0[:clip_size,:] = 0 prd_face_mask_a_0[-clip_size:,:] = 0 prd_face_mask_a_0[:,:clip_size] = 0 @@ -132,7 +136,7 @@ def MergeMaskedFace (predictor_func, predictor_input_shape, cfg, frame_info, img if blur > 0: blur = blur + (1-blur % 2) prd_face_mask_a_0 = cv2.GaussianBlur(prd_face_mask_a_0, (blur, blur) , 0) - + prd_face_mask_a_0 = prd_face_mask_a_0[input_size:-input_size,input_size:-input_size] prd_face_mask_a_0 = np.clip(prd_face_mask_a_0, 0, 1) @@ -141,7 +145,7 @@ def MergeMaskedFace (predictor_func, predictor_input_shape, cfg, frame_info, img img_face_mask_a = np.clip (img_face_mask_a, 0.0, 1.0) img_face_mask_a [ img_face_mask_a < (1.0/255.0) ] = 0.0 # get rid of noise - + if prd_face_mask_a_0.shape[0] != output_size: prd_face_mask_a_0 = cv2.resize (prd_face_mask_a_0, (output_size,output_size), cv2.INTER_CUBIC) @@ -277,7 +281,7 @@ def MergeMaskedFace (predictor_func, predictor_input_shape, cfg, frame_info, img k_size = int(frame_info.motion_power*cfg_mp) if k_size >= 1: k_size = np.clip (k_size+1, 2, 50) - if cfg.super_resolution_mode != 0: + if cfg.super_resolution_power != 0: k_size *= 2 out_face_bgr = imagelib.LinearMotionBlur (out_face_bgr, k_size , frame_info.motion_deg) diff --git a/merger/MergerConfig.py b/merger/MergerConfig.py index 8f689c7..11822a4 100644 --- a/merger/MergerConfig.py +++ b/merger/MergerConfig.py @@ -15,8 +15,6 @@ class MergerConfig(object): TYPE_IMAGE_WITH_LANDMARKS = 4 def __init__(self, type=0, - - super_resolution_mode=0, sharpen_mode=0, blursharpen_amount=0, **kwargs @@ -28,11 +26,9 @@ class MergerConfig(object): self.fanseg_input_size = None self.fanseg_extract_func = None - self.super_res_dict = {0:"None", 1:'FaceEnhancer'} self.sharpen_dict = {0:"None", 1:'box', 2:'gaussian'} #default changeable params - self.super_resolution_mode = super_resolution_mode self.sharpen_mode = sharpen_mode self.blursharpen_amount = blursharpen_amount @@ -50,12 +46,6 @@ class MergerConfig(object): if self.sharpen_mode != 0: self.blursharpen_amount = np.clip ( io.input_int ("Choose blur/sharpen amount", 0, add_info="-100..100"), -100, 100 ) - s = """Choose super resolution mode: \n""" - for key in self.super_res_dict.keys(): - s += f"""({key}) {self.super_res_dict[key]}\n""" - io.log_info(s) - self.super_resolution_mode = io.input_int ("", 0, valid_list=self.super_res_dict.keys(), help_message="Enhance details by applying superresolution network.") - def toggle_sharpen_mode(self): a = list( self.sharpen_dict.keys() ) self.sharpen_mode = a[ (a.index(self.sharpen_mode)+1) % len(a) ] @@ -63,19 +53,11 @@ class MergerConfig(object): def add_blursharpen_amount(self, diff): self.blursharpen_amount = np.clip ( self.blursharpen_amount+diff, -100, 100) - def toggle_super_resolution_mode(self): - a = list( self.super_res_dict.keys() ) - self.super_resolution_mode = a[ (a.index(self.super_resolution_mode)+1) % len(a) ] - #overridable def get_config(self): d = self.__dict__.copy() d.pop('type') return d - return {'sharpen_mode':self.sharpen_mode, - 'blursharpen_amount':self.blursharpen_amount, - 'super_resolution_mode':self.super_resolution_mode - } #overridable def __eq__(self, other): @@ -83,8 +65,7 @@ class MergerConfig(object): if isinstance(other, MergerConfig): return self.sharpen_mode == other.sharpen_mode and \ - self.blursharpen_amount == other.blursharpen_amount and \ - self.super_resolution_mode == other.super_resolution_mode + self.blursharpen_amount == other.blursharpen_amount return False @@ -93,7 +74,6 @@ class MergerConfig(object): r = "" r += f"sharpen_mode : {self.sharpen_dict[self.sharpen_mode]}\n" r += f"blursharpen_amount : {self.blursharpen_amount}\n" - r += f"super_resolution_mode : {self.super_res_dict[self.super_resolution_mode]}\n" return r mode_dict = {0:'original', @@ -137,6 +117,7 @@ class MergerConfigMasked(MergerConfig): blur_mask_modifier = 0, motion_blur_power = 0, output_face_scale = 0, + super_resolution_power = 0, color_transfer_mode = ctm_str_dict['rct'], image_denoise_power = 0, bicubic_degrade_power = 0, @@ -165,6 +146,7 @@ class MergerConfigMasked(MergerConfig): self.blur_mask_modifier = blur_mask_modifier self.motion_blur_power = motion_blur_power self.output_face_scale = output_face_scale + self.super_resolution_power = super_resolution_power self.color_transfer_mode = color_transfer_mode self.image_denoise_power = image_denoise_power self.bicubic_degrade_power = bicubic_degrade_power @@ -206,6 +188,9 @@ class MergerConfigMasked(MergerConfig): def toggle_color_transfer_mode(self): self.color_transfer_mode = (self.color_transfer_mode+1) % ( max(ctm_dict.keys())+1 ) + def add_super_resolution_power(self, diff): + self.super_resolution_power = np.clip ( self.super_resolution_power+diff , 0, 100) + def add_color_degrade_power(self, diff): self.color_degrade_power = np.clip ( self.color_degrade_power+diff , 0, 100) @@ -257,6 +242,8 @@ class MergerConfigMasked(MergerConfig): self.color_transfer_mode = ctm_str_dict[self.color_transfer_mode] super().ask_settings() + + self.super_resolution_power = np.clip ( io.input_int ("Choose super resolution power", 0, add_info="0..100", help_message="Enhance details by applying superresolution network."), 0, 100) if 'raw' not in self.mode: self.image_denoise_power = np.clip ( io.input_int ("Choose image degrade by denoise power", 0, add_info="0..500"), 0, 500) @@ -279,6 +266,7 @@ class MergerConfigMasked(MergerConfig): self.motion_blur_power == other.motion_blur_power and \ self.output_face_scale == other.output_face_scale and \ self.color_transfer_mode == other.color_transfer_mode and \ + self.super_resolution_power == other.super_resolution_power and \ self.image_denoise_power == other.image_denoise_power and \ self.bicubic_degrade_power == other.bicubic_degrade_power and \ self.color_degrade_power == other.color_degrade_power @@ -313,7 +301,8 @@ class MergerConfigMasked(MergerConfig): r += f"""color_transfer_mode: { ctm_dict[self.color_transfer_mode]}\n""" r += super().to_string(filename) - + r += f"""super_resolution_power: {self.super_resolution_power}\n""" + if 'raw' not in self.mode: r += (f"""image_denoise_power: {self.image_denoise_power}\n""" f"""bicubic_degrade_power: {self.bicubic_degrade_power}\n"""