victorisgeek commited on
Commit
392e900
Β·
verified Β·
1 Parent(s): e17aae4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -116
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='⏳running face swap')
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
- def swap_face(input_path, dst_path, src_path, use_enhancer, use_sr, scale,
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:power by Gradio')
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
- label="πŸ› οΈ Super resolution",
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
- value=0.6,
78
- label='🧲 face similarity threshold',
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
- label="πŸ”‹ Super Resolution",
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 power by Gradio')
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
- label="πŸ› οΈ Super Resolution",
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
+