File size: 1,464 Bytes
452fa15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import gradio as gr
from PIL import Image
import sys
import tempfile
from facefusion import core

def run_cli(cli_args):
  old_argv = sys.argv
  try:
    sys.argv = ['run.py', *cli_args]
    core.cli()
  finally:
    sys.argv = old_argv

def swap_faces(source_image_path, target_image_path, enhance=True):
  provider = 'coreml'

  output_image_file = tempfile.NamedTemporaryFile(suffix='.png')
  output_image_path = output_image_file.name

  print(source_image_path)
  print(target_image_path)
  print(output_image_path)

  cli_args = [
    '--headless',
    '-s', source_image_path,
    '-t', target_image_path,
    '-o', output_image_path,
    '--output-image-quality', '80',
    '--execution-providers', provider,
  ]

  cli_args += [ '--frame-processors', 'face_swapper' ]
  if enhance:
    cli_args += [
      'frame_enhancer',
      'face_enhancer',
    ]

  from facefusion.processors.frame.core import clear_frame_processors_modules
  clear_frame_processors_modules()

  run_cli(cli_args)

  return Image.open(output_image_path)

if __name__ == "__main__":
    demo = gr.Interface(
      fn=swap_faces,
      inputs=[
          gr.Image(type="filepath"),
          gr.Image(type="filepath"),
          gr.Checkbox(label="Enhance Face", value=True),
      ],
      outputs=[
          gr.Image(
            type="pil",
            show_download_button=True,
          )
      ],
      title="Swap Faces",
      allow_flagging="never"
    )
    demo.launch()