Spaces:
Running
Running
kiran chauhan
commited on
Commit
•
26cfabd
1
Parent(s):
638028d
Update app.py
Browse files
app.py
CHANGED
@@ -1,93 +1,84 @@
|
|
1 |
-
import numpy as np
|
2 |
-
import gradio as gr
|
3 |
-
import roop.globals
|
4 |
-
from roop.core import (
|
5 |
-
start,
|
6 |
-
decode_execution_providers,
|
7 |
-
suggest_max_memory,
|
8 |
-
suggest_execution_threads,
|
9 |
-
)
|
10 |
-
from roop.processors.frame.core import get_frame_processors_modules
|
11 |
-
from roop.utilities import normalize_output_path
|
12 |
-
import os
|
13 |
-
from PIL import Image
|
14 |
-
|
15 |
-
|
16 |
-
def swap_face(source_file, target_file, doFaceEnhancer):
|
17 |
-
|
18 |
-
source_path = "input.jpg"
|
19 |
-
target_path = "target.jpg"
|
20 |
-
|
21 |
-
source_image = Image.fromarray(source_file)
|
22 |
-
source_image.save(source_path)
|
23 |
-
target_image = Image.fromarray(target_file)
|
24 |
-
target_image.save(target_path)
|
25 |
-
|
26 |
-
print("source_path: ", source_path)
|
27 |
-
print("target_path: ", target_path)
|
28 |
-
|
29 |
-
roop.globals.source_path = source_path
|
30 |
-
roop.globals.target_path = target_path
|
31 |
-
output_path = "output.jpg"
|
32 |
-
roop.globals.output_path = normalize_output_path(
|
33 |
-
roop.globals.source_path, roop.globals.target_path, output_path
|
34 |
-
)
|
35 |
-
if doFaceEnhancer:
|
36 |
-
roop.globals.frame_processors = ["face_swapper", "face_enhancer"]
|
37 |
-
else:
|
38 |
-
roop.globals.frame_processors = ["face_swapper"]
|
39 |
-
roop.globals.headless = True
|
40 |
-
roop.globals.keep_fps = True
|
41 |
-
roop.globals.keep_audio = True
|
42 |
-
roop.globals.keep_frames = False
|
43 |
-
roop.globals.many_faces = False
|
44 |
-
roop.globals.video_encoder = "libx264"
|
45 |
-
roop.globals.video_quality = 18
|
46 |
-
roop.globals.max_memory = suggest_max_memory()
|
47 |
-
roop.globals.execution_providers = decode_execution_providers(["cuda"])
|
48 |
-
roop.globals.execution_threads = suggest_execution_threads()
|
49 |
-
|
50 |
-
print(
|
51 |
-
"start process",
|
52 |
-
roop.globals.source_path,
|
53 |
-
roop.globals.target_path,
|
54 |
-
roop.globals.output_path,
|
55 |
-
)
|
56 |
-
|
57 |
-
for frame_processor in get_frame_processors_modules(
|
58 |
-
roop.globals.frame_processors
|
59 |
-
):
|
60 |
-
if not frame_processor.pre_check():
|
61 |
-
return
|
62 |
-
|
63 |
-
start()
|
64 |
-
return output_path
|
65 |
-
|
66 |
-
|
67 |
-
html_section_1 = "<div><h1>Welcome to the Face Swap App</h1></div>"
|
68 |
-
html_section_2 = "<div><p>Upload your source and target images to swap faces. Optionally, use the face enhancer feature.</p></div>"
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
gr.HTML(html_section_2)
|
86 |
-
gr.HTML(html_section_3)
|
87 |
-
gr.Interface(
|
88 |
-
fn=swap_face,
|
89 |
-
inputs=[gr.Image(), gr.Image(), gr.Checkbox(label="face_enhancer?", info="do face enhancer?")],
|
90 |
-
outputs="image"
|
91 |
-
)
|
92 |
-
|
93 |
-
app.launch()
|
|
|
1 |
+
import numpy as np
|
2 |
+
import gradio as gr
|
3 |
+
import roop.globals
|
4 |
+
from roop.core import (
|
5 |
+
start,
|
6 |
+
decode_execution_providers,
|
7 |
+
suggest_max_memory,
|
8 |
+
suggest_execution_threads,
|
9 |
+
)
|
10 |
+
from roop.processors.frame.core import get_frame_processors_modules
|
11 |
+
from roop.utilities import normalize_output_path
|
12 |
+
import os
|
13 |
+
from PIL import Image
|
14 |
+
|
15 |
+
|
16 |
+
def swap_face(source_file, target_file, doFaceEnhancer):
|
17 |
+
|
18 |
+
source_path = "input.jpg"
|
19 |
+
target_path = "target.jpg"
|
20 |
+
|
21 |
+
source_image = Image.fromarray(source_file)
|
22 |
+
source_image.save(source_path)
|
23 |
+
target_image = Image.fromarray(target_file)
|
24 |
+
target_image.save(target_path)
|
25 |
+
|
26 |
+
print("source_path: ", source_path)
|
27 |
+
print("target_path: ", target_path)
|
28 |
+
|
29 |
+
roop.globals.source_path = source_path
|
30 |
+
roop.globals.target_path = target_path
|
31 |
+
output_path = "output.jpg"
|
32 |
+
roop.globals.output_path = normalize_output_path(
|
33 |
+
roop.globals.source_path, roop.globals.target_path, output_path
|
34 |
+
)
|
35 |
+
if doFaceEnhancer:
|
36 |
+
roop.globals.frame_processors = ["face_swapper", "face_enhancer"]
|
37 |
+
else:
|
38 |
+
roop.globals.frame_processors = ["face_swapper"]
|
39 |
+
roop.globals.headless = True
|
40 |
+
roop.globals.keep_fps = True
|
41 |
+
roop.globals.keep_audio = True
|
42 |
+
roop.globals.keep_frames = False
|
43 |
+
roop.globals.many_faces = False
|
44 |
+
roop.globals.video_encoder = "libx264"
|
45 |
+
roop.globals.video_quality = 18
|
46 |
+
roop.globals.max_memory = suggest_max_memory()
|
47 |
+
roop.globals.execution_providers = decode_execution_providers(["cuda"])
|
48 |
+
roop.globals.execution_threads = suggest_execution_threads()
|
49 |
+
|
50 |
+
print(
|
51 |
+
"start process",
|
52 |
+
roop.globals.source_path,
|
53 |
+
roop.globals.target_path,
|
54 |
+
roop.globals.output_path,
|
55 |
+
)
|
56 |
+
|
57 |
+
for frame_processor in get_frame_processors_modules(
|
58 |
+
roop.globals.frame_processors
|
59 |
+
):
|
60 |
+
if not frame_processor.pre_check():
|
61 |
+
return
|
62 |
+
|
63 |
+
start()
|
64 |
+
return output_path
|
65 |
+
|
66 |
+
|
67 |
+
html_section_1 = "<div><h1>Welcome to the Face Swap App</h1></div>"
|
68 |
+
html_section_2 = "<div><p>Upload your source and target images to swap faces. Optionally, use the face enhancer feature.</p></div>"
|
69 |
+
</a>
|
70 |
+
</div>"""
|
71 |
+
|
72 |
+
app = gr.Blocks()
|
73 |
+
|
74 |
+
with app:
|
75 |
+
gr.HTML(html_section_1)
|
76 |
+
gr.HTML(html_section_2)
|
77 |
+
gr.HTML(html_section_3)
|
78 |
+
gr.Interface(
|
79 |
+
fn=swap_face,
|
80 |
+
inputs=[gr.Image(), gr.Image(), gr.Checkbox(label="face_enhancer?", info="do face enhancer?")],
|
81 |
+
outputs="image"
|
82 |
+
)
|
83 |
+
|
84 |
+
app.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|