Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,51 +1,82 @@
|
|
| 1 |
import argparse
|
| 2 |
-
|
|
|
|
|
|
|
| 3 |
import gradio as gr
|
|
|
|
|
|
|
| 4 |
from dofaker import FaceSwapper, PoseSwapper
|
| 5 |
|
| 6 |
-
|
| 7 |
def parse_args():
|
| 8 |
-
parser = argparse.ArgumentParser(description='β³
|
| 9 |
parser.add_argument(
|
| 10 |
'--inbrowser',
|
| 11 |
-
help=
|
| 12 |
-
'whether to automatically launch the interface in a new tab on the default browser.',
|
| 13 |
dest='inbrowser',
|
| 14 |
default=True)
|
| 15 |
parser.add_argument(
|
| 16 |
'--server_port',
|
| 17 |
-
help=
|
| 18 |
-
'will start gradio app on this port (if available). Can be set by environment variable GRADIO_SERVER_PORT. If None, will search for an available port starting at 7860.',
|
| 19 |
dest='server_port',
|
| 20 |
type=int,
|
| 21 |
default=None)
|
| 22 |
return parser.parse_args()
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
face_sim_thre):
|
| 27 |
-
faker = FaceSwapper(use_enhancer=use_enhancer,
|
| 28 |
-
use_sr=use_sr,
|
| 29 |
-
scale=scale,
|
| 30 |
-
face_sim_thre=face_sim_thre)
|
| 31 |
output_path = faker.run(input_path, dst_path, src_path)
|
| 32 |
return output_path
|
| 33 |
|
| 34 |
-
|
| 35 |
def swap_pose(input_path, target_path, use_enhancer, use_sr, scale):
|
| 36 |
faker = PoseSwapper(use_enhancer=use_enhancer, use_sr=use_sr, scale=scale)
|
| 37 |
output_path = faker.run(input_path, target_path)
|
| 38 |
return output_path
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
def main():
|
| 42 |
args = parse_args()
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
with gr.Blocks(theme='ysharma/huggingface@=0.0.4') as web_ui:
|
| 45 |
gr.Markdown('# π FaceClone')
|
| 46 |
gr.Markdown('### Deepfake Swap Face and Pose.')
|
|
|
|
| 47 |
with gr.Tab('π§© FaceSwapper'):
|
| 48 |
-
gr.Markdown(' π§Έ FaceClone:
|
| 49 |
with gr.Tab('π₯ Face'):
|
| 50 |
with gr.Row():
|
| 51 |
with gr.Column():
|
|
@@ -53,40 +84,21 @@ def main():
|
|
| 53 |
image_input = gr.Image(type='filepath')
|
| 54 |
with gr.Row():
|
| 55 |
with gr.Column():
|
| 56 |
-
gr.Markdown(
|
| 57 |
-
'π Target source included with Face')
|
| 58 |
dst_face_image = gr.Image(type='filepath')
|
| 59 |
with gr.Column():
|
| 60 |
-
gr.Markdown(
|
| 61 |
-
'π§β𦳠Source Face to Replace Target Face')
|
| 62 |
src_face_image = gr.Image(type='filepath')
|
| 63 |
-
|
| 64 |
with gr.Column():
|
| 65 |
output_image = gr.Image(type='filepath')
|
| 66 |
-
use_enhancer = gr.Checkbox(
|
| 67 |
-
label="π Face Enhance",
|
| 68 |
-
info="π§ΏWhether use face enhance model.")
|
| 69 |
with gr.Row():
|
| 70 |
-
use_sr = gr.Checkbox(
|
| 71 |
-
|
| 72 |
-
info="βοΈ Whether use image resolution model.")
|
| 73 |
-
scale = gr.Number(
|
| 74 |
-
value=1, label='πimage super resolution scale')
|
| 75 |
with gr.Row():
|
| 76 |
-
face_sim_thre = gr.Number(
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
minimum=0.0,
|
| 80 |
-
maximum=1.0)
|
| 81 |
-
convert_button = gr.Button('πSwap')
|
| 82 |
-
convert_button.click(fn=swap_face,
|
| 83 |
-
inputs=[
|
| 84 |
-
image_input, dst_face_image,
|
| 85 |
-
src_face_image, use_enhancer,
|
| 86 |
-
use_sr, scale, face_sim_thre
|
| 87 |
-
],
|
| 88 |
-
outputs=[output_image],
|
| 89 |
-
api_name='πImage swap')
|
| 90 |
|
| 91 |
with gr.Tab('π₯οΈ Video'):
|
| 92 |
with gr.Row():
|
|
@@ -95,101 +107,42 @@ def main():
|
|
| 95 |
video_input = gr.Video()
|
| 96 |
with gr.Row():
|
| 97 |
with gr.Column():
|
| 98 |
-
gr.Markdown(
|
| 99 |
-
'π³ Target Face')
|
| 100 |
dst_face_image = gr.Image(type='filepath')
|
| 101 |
with gr.Column():
|
| 102 |
-
gr.Markdown(
|
| 103 |
-
'π«
Source Face')
|
| 104 |
src_face_image = gr.Image(type='filepath')
|
| 105 |
-
|
| 106 |
with gr.Column():
|
| 107 |
output_video = gr.Video()
|
| 108 |
-
use_enhancer = gr.Checkbox(
|
| 109 |
-
label="πΈ Face Enhance",
|
| 110 |
-
info="π· Whether use face enhance model.")
|
| 111 |
with gr.Row():
|
| 112 |
-
use_sr = gr.Checkbox(
|
| 113 |
-
|
| 114 |
-
info="π¦Whether use image resolution model.")
|
| 115 |
-
scale = gr.Number(
|
| 116 |
-
value=1, label='ποΈ Super Resolution Image')
|
| 117 |
with gr.Row():
|
| 118 |
-
face_sim_thre = gr.Number(
|
| 119 |
-
value=0.6,
|
| 120 |
-
label='πface similarity threshold',
|
| 121 |
-
minimum=0.0,
|
| 122 |
-
maximum=1.0)
|
| 123 |
convert_button = gr.Button('π― Swap')
|
| 124 |
-
convert_button.click(fn=swap_face,
|
| 125 |
-
inputs=[
|
| 126 |
-
video_input, dst_face_image,
|
| 127 |
-
src_face_image, use_enhancer,
|
| 128 |
-
use_sr, scale, face_sim_thre
|
| 129 |
-
],
|
| 130 |
-
outputs=[output_video],
|
| 131 |
-
api_name='π½ Video Swap π½')
|
| 132 |
|
| 133 |
with gr.Tab('πͺ© PoseSwapper'):
|
| 134 |
-
gr.Markdown(' π§ΈFaceClone:Deepfake
|
| 135 |
with gr.Tab('π· Image'):
|
| 136 |
with gr.Row():
|
| 137 |
with gr.Column():
|
| 138 |
-
gr.Markdown('π§βπ¦³The source image to be swapped')
|
| 139 |
image_input = gr.Image(type='filepath')
|
| 140 |
gr.Markdown('π³οΈ The target image with pose')
|
| 141 |
target = gr.Image(type='filepath')
|
| 142 |
-
|
| 143 |
with gr.Column():
|
| 144 |
output_image = gr.Image(type='filepath')
|
| 145 |
-
use_enhancer = gr.Checkbox(
|
| 146 |
-
label="π Face Enhance",
|
| 147 |
-
info="π§ΎWhether use face enhance model.")
|
| 148 |
with gr.Row():
|
| 149 |
-
use_sr = gr.Checkbox(
|
| 150 |
-
|
| 151 |
-
info="βWhether use image resolution model.")
|
| 152 |
-
scale = gr.Number(
|
| 153 |
-
value=1, label='πΈ Super resolution scale')
|
| 154 |
convert_button = gr.Button('π― Swap')
|
| 155 |
-
convert_button.click(fn=swap_pose,
|
| 156 |
-
inputs=[
|
| 157 |
-
image_input, target,
|
| 158 |
-
use_enhancer, use_sr, scale
|
| 159 |
-
],
|
| 160 |
-
outputs=[output_image],
|
| 161 |
-
api_name='π Image slSwap π')
|
| 162 |
-
|
| 163 |
-
# with gr.Tab('Video'):
|
| 164 |
-
# with gr.Row():
|
| 165 |
-
# with gr.Column():
|
| 166 |
-
# gr.Markdown('The source video to be swapped')
|
| 167 |
-
# video_input = gr.Image(type='filepath')
|
| 168 |
-
# gr.Markdown('The target image with pose')
|
| 169 |
-
# target = gr.Video()
|
| 170 |
-
|
| 171 |
-
# with gr.Column():
|
| 172 |
-
# output_video = gr.Video()
|
| 173 |
-
# use_enhancer = gr.Checkbox(
|
| 174 |
-
# label="face enhance",
|
| 175 |
-
# info="Whether use face enhance model.")
|
| 176 |
-
# with gr.Row():
|
| 177 |
-
# use_sr = gr.Checkbox(
|
| 178 |
-
# label="super resolution",
|
| 179 |
-
# info="Whether use image resolution model.")
|
| 180 |
-
# scale = gr.Number(value=1,
|
| 181 |
-
# label='image super resolution scale')
|
| 182 |
-
# convert_button = gr.Button('Swap')
|
| 183 |
-
# convert_button.click(fn=swap_pose,
|
| 184 |
-
# inputs=[
|
| 185 |
-
# video_input, target, use_enhancer,
|
| 186 |
-
# use_sr, scale
|
| 187 |
-
# ],
|
| 188 |
-
# outputs=[output_video],
|
| 189 |
-
# api_name='video swap')
|
| 190 |
|
| 191 |
web_ui.launch(inbrowser=args.inbrowser, server_port=args.server_port)
|
| 192 |
|
| 193 |
-
|
| 194 |
if __name__ == '__main__':
|
| 195 |
main()
|
|
|
|
|
|
| 1 |
import argparse
|
| 2 |
+
import numpy as np
|
| 3 |
+
import cv2
|
| 4 |
+
import os
|
| 5 |
import gradio as gr
|
| 6 |
+
import insightface
|
| 7 |
+
from insightface.app import FaceAnalysis
|
| 8 |
from dofaker import FaceSwapper, PoseSwapper
|
| 9 |
|
|
|
|
| 10 |
def parse_args():
|
| 11 |
+
parser = argparse.ArgumentParser(description='β³ Running face swap')
|
| 12 |
parser.add_argument(
|
| 13 |
'--inbrowser',
|
| 14 |
+
help='Automatically launch the interface in a new tab on the default browser.',
|
|
|
|
| 15 |
dest='inbrowser',
|
| 16 |
default=True)
|
| 17 |
parser.add_argument(
|
| 18 |
'--server_port',
|
| 19 |
+
help='Start gradio app on this port (if available).',
|
|
|
|
| 20 |
dest='server_port',
|
| 21 |
type=int,
|
| 22 |
default=None)
|
| 23 |
return parser.parse_args()
|
| 24 |
|
| 25 |
+
def swap_face(input_path, dst_path, src_path, use_enhancer, use_sr, scale, face_sim_thre):
|
| 26 |
+
faker = FaceSwapper(use_enhancer=use_enhancer, use_sr=use_sr, scale=scale, face_sim_thre=face_sim_thre)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
output_path = faker.run(input_path, dst_path, src_path)
|
| 28 |
return output_path
|
| 29 |
|
|
|
|
| 30 |
def swap_pose(input_path, target_path, use_enhancer, use_sr, scale):
|
| 31 |
faker = PoseSwapper(use_enhancer=use_enhancer, use_sr=use_sr, scale=scale)
|
| 32 |
output_path = faker.run(input_path, target_path)
|
| 33 |
return output_path
|
| 34 |
|
| 35 |
+
def add_bbox_padding(bbox, margin=5):
|
| 36 |
+
return [
|
| 37 |
+
bbox[0] - margin,
|
| 38 |
+
bbox[1] - margin,
|
| 39 |
+
bbox[2] + margin,
|
| 40 |
+
bbox[3] + margin
|
| 41 |
+
]
|
| 42 |
+
|
| 43 |
+
def select_handler(img, evt):
|
| 44 |
+
faces = app.get(img)
|
| 45 |
+
faces = sorted(faces, key=lambda x: x.bbox[0])
|
| 46 |
+
cropped_image = []
|
| 47 |
+
face_index = -1
|
| 48 |
+
sel_face_index = 0
|
| 49 |
+
for face in faces:
|
| 50 |
+
box = face.bbox.astype(np.int32)
|
| 51 |
+
face_index += 1
|
| 52 |
+
if point_in_box((box[0], box[1]), (box[2], box[3]), (evt.index[0], evt.index[1])):
|
| 53 |
+
margin = int((box[2] - box[0]) * 0.35)
|
| 54 |
+
box = add_bbox_padding(box, margin)
|
| 55 |
+
box = np.clip(box, 0, None)
|
| 56 |
+
sel_face_index = face_index
|
| 57 |
+
cropped_image = img[box[1]:box[3], box[0]:box[2]]
|
| 58 |
+
return cropped_image, sel_face_index
|
| 59 |
+
|
| 60 |
+
def point_in_box(bl, tr, p):
|
| 61 |
+
return bl[0] < p[0] < tr[0] and bl[1] < p[1] < tr[1]
|
| 62 |
+
|
| 63 |
+
def get_faces(img):
|
| 64 |
+
faces = app.get(img)
|
| 65 |
+
faces = sorted(faces, key=lambda x: x.bbox[0])
|
| 66 |
+
return img, len(faces)
|
| 67 |
|
| 68 |
def main():
|
| 69 |
args = parse_args()
|
| 70 |
+
app = FaceAnalysis(name='buffalo_l')
|
| 71 |
+
app.prepare(ctx_id=0, det_size=(640, 640))
|
| 72 |
+
swapper = insightface.model_zoo.get_model('inswapper_128.onnx', download=True, download_zip=True)
|
| 73 |
|
| 74 |
with gr.Blocks(theme='ysharma/huggingface@=0.0.4') as web_ui:
|
| 75 |
gr.Markdown('# π FaceClone')
|
| 76 |
gr.Markdown('### Deepfake Swap Face and Pose.')
|
| 77 |
+
|
| 78 |
with gr.Tab('π§© FaceSwapper'):
|
| 79 |
+
gr.Markdown(' π§Έ FaceClone: Powered by Gradio')
|
| 80 |
with gr.Tab('π₯ Face'):
|
| 81 |
with gr.Row():
|
| 82 |
with gr.Column():
|
|
|
|
| 84 |
image_input = gr.Image(type='filepath')
|
| 85 |
with gr.Row():
|
| 86 |
with gr.Column():
|
| 87 |
+
gr.Markdown('π Target source included with Face')
|
|
|
|
| 88 |
dst_face_image = gr.Image(type='filepath')
|
| 89 |
with gr.Column():
|
| 90 |
+
gr.Markdown('π§β𦳠Source Face to Replace Target Face')
|
|
|
|
| 91 |
src_face_image = gr.Image(type='filepath')
|
|
|
|
| 92 |
with gr.Column():
|
| 93 |
output_image = gr.Image(type='filepath')
|
| 94 |
+
use_enhancer = gr.Checkbox(label="π Face Enhance", info="π§Ώ Whether use face enhance model.")
|
|
|
|
|
|
|
| 95 |
with gr.Row():
|
| 96 |
+
use_sr = gr.Checkbox(label="π οΈ Super resolution", info="βοΈ Whether use image resolution model.")
|
| 97 |
+
scale = gr.Number(value=1, label='π Image super resolution scale')
|
|
|
|
|
|
|
|
|
|
| 98 |
with gr.Row():
|
| 99 |
+
face_sim_thre = gr.Number(value=0.6, label='π§² Face similarity threshold', minimum=0.0, maximum=1.0)
|
| 100 |
+
convert_button = gr.Button('π Swap')
|
| 101 |
+
convert_button.click(fn=swap_face, inputs=[image_input, dst_face_image, src_face_image, use_enhancer, use_sr, scale, face_sim_thre], outputs=[output_image])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
with gr.Tab('π₯οΈ Video'):
|
| 104 |
with gr.Row():
|
|
|
|
| 107 |
video_input = gr.Video()
|
| 108 |
with gr.Row():
|
| 109 |
with gr.Column():
|
| 110 |
+
gr.Markdown('π³ Target Face')
|
|
|
|
| 111 |
dst_face_image = gr.Image(type='filepath')
|
| 112 |
with gr.Column():
|
| 113 |
+
gr.Markdown('π«
Source Face')
|
|
|
|
| 114 |
src_face_image = gr.Image(type='filepath')
|
|
|
|
| 115 |
with gr.Column():
|
| 116 |
output_video = gr.Video()
|
| 117 |
+
use_enhancer = gr.Checkbox(label="πΈ Face Enhance", info="π· Whether use face enhance model.")
|
|
|
|
|
|
|
| 118 |
with gr.Row():
|
| 119 |
+
use_sr = gr.Checkbox(label="π Super Resolution", info="π¦ Whether use image resolution model.")
|
| 120 |
+
scale = gr.Number(value=1, label='ποΈ Super Resolution Image')
|
|
|
|
|
|
|
|
|
|
| 121 |
with gr.Row():
|
| 122 |
+
face_sim_thre = gr.Number(value=0.6, label='π Face similarity threshold', minimum=0.0, maximum=1.0)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
convert_button = gr.Button('π― Swap')
|
| 124 |
+
convert_button.click(fn=swap_face, inputs=[video_input, dst_face_image, src_face_image, use_enhancer, use_sr, scale, face_sim_thre], outputs=[output_video])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
with gr.Tab('πͺ© PoseSwapper'):
|
| 127 |
+
gr.Markdown(' π§Έ FaceClone: Deepfake powered by Gradio')
|
| 128 |
with gr.Tab('π· Image'):
|
| 129 |
with gr.Row():
|
| 130 |
with gr.Column():
|
| 131 |
+
gr.Markdown('π§β𦳠The source image to be swapped')
|
| 132 |
image_input = gr.Image(type='filepath')
|
| 133 |
gr.Markdown('π³οΈ The target image with pose')
|
| 134 |
target = gr.Image(type='filepath')
|
|
|
|
| 135 |
with gr.Column():
|
| 136 |
output_image = gr.Image(type='filepath')
|
| 137 |
+
use_enhancer = gr.Checkbox(label="π Face Enhance", info="π§Ύ Whether use face enhance model.")
|
|
|
|
|
|
|
| 138 |
with gr.Row():
|
| 139 |
+
use_sr = gr.Checkbox(label="π οΈ Super Resolution", info="β Whether use image resolution model.")
|
| 140 |
+
scale = gr.Number(value=1, label='πΈ Super resolution scale')
|
|
|
|
|
|
|
|
|
|
| 141 |
convert_button = gr.Button('π― Swap')
|
| 142 |
+
convert_button.click(fn=swap_pose, inputs=[image_input, target, use_enhancer, use_sr, scale], outputs=[output_image])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
web_ui.launch(inbrowser=args.inbrowser, server_port=args.server_port)
|
| 145 |
|
|
|
|
| 146 |
if __name__ == '__main__':
|
| 147 |
main()
|
| 148 |
+
|