Spaces:
Runtime error
Runtime error
Update src/gradio_pipeline.py
Browse files- src/gradio_pipeline.py +30 -23
src/gradio_pipeline.py
CHANGED
|
@@ -92,6 +92,35 @@ class GradioPipeline(LivePortraitPipeline):
|
|
| 92 |
return out, out_to_ori_blend
|
| 93 |
|
| 94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
def prepare_retargeting(self, input_image, flag_do_crop = True):
|
| 96 |
""" for single image retargeting
|
| 97 |
"""
|
|
@@ -119,26 +148,4 @@ class GradioPipeline(LivePortraitPipeline):
|
|
| 119 |
# when press the clear button, go here
|
| 120 |
raise gr.Error("Please upload a source portrait as the retargeting input 🤗🤗🤗", duration=5)
|
| 121 |
|
| 122 |
-
|
| 123 |
-
""" initialize the retargeting slider
|
| 124 |
-
"""
|
| 125 |
-
if input_image != None:
|
| 126 |
-
args_user = {'scale': retargeting_source_scale}
|
| 127 |
-
self.args = update_args(self.args, args_user)
|
| 128 |
-
self.cropper.update_config(self.args.__dict__)
|
| 129 |
-
# inference_cfg = self.live_portrait_wrapper.inference_cfg
|
| 130 |
-
######## process source portrait ########
|
| 131 |
-
img_rgb = load_img_online(input_image, mode='rgb', max_dim=1280, n=16)
|
| 132 |
-
log(f"Load source image from {input_image}.")
|
| 133 |
-
#crop_info = self.cropper.crop_source_image(img_rgb, self.cropper.crop_cfg)
|
| 134 |
-
crop_info = self.cropper.crop_single_image(img_rgb)
|
| 135 |
-
if crop_info is None:
|
| 136 |
-
raise gr.Error("Source portrait NO face detected", duration=2)
|
| 137 |
-
source_eye_ratio = calc_eye_close_ratio(crop_info['lmk_crop'][None])
|
| 138 |
-
source_lip_ratio = calc_lip_close_ratio(crop_info['lmk_crop'][None])
|
| 139 |
-
self.source_eye_ratio = round(float(source_eye_ratio.mean()), 2)
|
| 140 |
-
self.source_lip_ratio = round(float(source_lip_ratio[0][0]), 2)
|
| 141 |
-
log("Calculating eyes-open and lip-open ratios successfully!")
|
| 142 |
-
return self.source_eye_ratio, self.source_lip_ratio
|
| 143 |
-
else:
|
| 144 |
-
return source_eye_ratio, source_lip_ratio
|
|
|
|
| 92 |
return out, out_to_ori_blend
|
| 93 |
|
| 94 |
|
| 95 |
+
def execute_image_lip(self, input_lip_ratio: float, input_image, flag_do_crop = True):
|
| 96 |
+
""" for single image retargeting
|
| 97 |
+
"""
|
| 98 |
+
# disposable feature
|
| 99 |
+
f_s_user, x_s_user, source_lmk_user, crop_M_c2o, mask_ori, img_rgb = \
|
| 100 |
+
self.prepare_retargeting(input_image, flag_do_crop)
|
| 101 |
+
|
| 102 |
+
if input_lip_ratio is None:
|
| 103 |
+
raise gr.Error("Invalid ratio input 💥!", duration=5)
|
| 104 |
+
else:
|
| 105 |
+
x_s_user = x_s_user.to("cuda")
|
| 106 |
+
f_s_user = f_s_user.to("cuda")
|
| 107 |
+
|
| 108 |
+
combined_lip_ratio_tensor = self.live_portrait_wrapper.calc_combined_lip_ratio([[input_lip_ratio]], source_lmk_user)
|
| 109 |
+
lip_delta = self.live_portrait_wrapper.retarget_lip(x_s_user, combined_lip_ratio_tensor)
|
| 110 |
+
num_kp = x_s_user.shape[1]
|
| 111 |
+
|
| 112 |
+
# default: use x_s
|
| 113 |
+
x_d_new = x_s_user + lip_delta.reshape(-1, num_kp, 3)
|
| 114 |
+
|
| 115 |
+
# D(W(f_s; x_s, x′_d))
|
| 116 |
+
out = self.live_portrait_wrapper.warp_decode(f_s_user, x_s_user, x_d_new)
|
| 117 |
+
out = self.live_portrait_wrapper.parse_output(out['out'])[0]
|
| 118 |
+
out_to_ori_blend = paste_back(out, crop_M_c2o, img_rgb, mask_ori)
|
| 119 |
+
|
| 120 |
+
# gr.Info("Run successfully!", duration=2)
|
| 121 |
+
return out_to_ori_blend
|
| 122 |
+
|
| 123 |
+
|
| 124 |
def prepare_retargeting(self, input_image, flag_do_crop = True):
|
| 125 |
""" for single image retargeting
|
| 126 |
"""
|
|
|
|
| 148 |
# when press the clear button, go here
|
| 149 |
raise gr.Error("Please upload a source portrait as the retargeting input 🤗🤗🤗", duration=5)
|
| 150 |
|
| 151 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|