Lucas Hansen commited on
Commit
452fa15
1 Parent(s): 9309ab1

Create simple.py

Browse files
Files changed (1) hide show
  1. simple.py +65 -0
simple.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from PIL import Image
3
+ import sys
4
+ import tempfile
5
+ from facefusion import core
6
+
7
+ def run_cli(cli_args):
8
+ old_argv = sys.argv
9
+ try:
10
+ sys.argv = ['run.py', *cli_args]
11
+ core.cli()
12
+ finally:
13
+ sys.argv = old_argv
14
+
15
+ def swap_faces(source_image_path, target_image_path, enhance=True):
16
+ provider = 'coreml'
17
+
18
+ output_image_file = tempfile.NamedTemporaryFile(suffix='.png')
19
+ output_image_path = output_image_file.name
20
+
21
+ print(source_image_path)
22
+ print(target_image_path)
23
+ print(output_image_path)
24
+
25
+ cli_args = [
26
+ '--headless',
27
+ '-s', source_image_path,
28
+ '-t', target_image_path,
29
+ '-o', output_image_path,
30
+ '--output-image-quality', '80',
31
+ '--execution-providers', provider,
32
+ ]
33
+
34
+ cli_args += [ '--frame-processors', 'face_swapper' ]
35
+ if enhance:
36
+ cli_args += [
37
+ 'frame_enhancer',
38
+ 'face_enhancer',
39
+ ]
40
+
41
+ from facefusion.processors.frame.core import clear_frame_processors_modules
42
+ clear_frame_processors_modules()
43
+
44
+ run_cli(cli_args)
45
+
46
+ return Image.open(output_image_path)
47
+
48
+ if __name__ == "__main__":
49
+ demo = gr.Interface(
50
+ fn=swap_faces,
51
+ inputs=[
52
+ gr.Image(type="filepath"),
53
+ gr.Image(type="filepath"),
54
+ gr.Checkbox(label="Enhance Face", value=True),
55
+ ],
56
+ outputs=[
57
+ gr.Image(
58
+ type="pil",
59
+ show_download_button=True,
60
+ )
61
+ ],
62
+ title="Swap Faces",
63
+ allow_flagging="never"
64
+ )
65
+ demo.launch()