Spaces:
Runtime error
Runtime error
chukwurah
commited on
Commit
·
c4f1a34
1
Parent(s):
49c7285
cline
Browse files
app.py
CHANGED
@@ -1,83 +1,113 @@
|
|
1 |
-
import
|
2 |
import gradio as gr
|
3 |
-
|
4 |
from roop.core import (
|
|
|
5 |
decode_execution_providers,
|
6 |
suggest_max_memory,
|
7 |
suggest_execution_threads,
|
8 |
)
|
9 |
from roop.processors.frame.core import get_frame_processors_modules
|
10 |
from roop.utilities import normalize_output_path
|
|
|
|
|
|
|
11 |
|
|
|
|
|
12 |
|
|
|
|
|
|
|
13 |
|
14 |
-
|
|
|
|
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
do_face_enhancer = gr.Checkbox(label="Face Enhancer")
|
21 |
-
with gr.Tab("Result"):
|
22 |
-
result_images = gr.Gallery(label="Result Images")
|
23 |
-
with gr.Tab("History"):
|
24 |
-
history_images = gr.Gallery(label="Processed Images")
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
source_path = "input.jpg"
|
30 |
-
target_path = "target.jpg"
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
39 |
|
40 |
-
|
41 |
-
roop.globals.
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
roop.globals.frame_processors = ["face_swapper", "face_enhancer"]
|
46 |
-
else:
|
47 |
-
roop.globals.frame_processors = ["face_swapper"]
|
48 |
-
roop.globals.headless = True
|
49 |
-
roop.globals.keep_fps = True
|
50 |
-
roop.globals.keep_audio = True
|
51 |
-
roop.globals.keep_frames = False
|
52 |
-
roop.globals.many_faces = False
|
53 |
-
roop.globals.video_encoder = "libx264"
|
54 |
-
roop.globals.video_quality = 18
|
55 |
-
roop.globals.max_memory = suggest_max_memory()
|
56 |
-
roop.globals.execution_providers = decode_execution_providers(["cuda"])
|
57 |
-
roop.globals.execution_threads = suggest_execution_threads()
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
):
|
62 |
-
if not frame_processor.pre_check():
|
63 |
-
return
|
64 |
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
|
70 |
-
|
|
|
71 |
|
72 |
-
|
73 |
-
|
74 |
-
if not os.path.exists(history_folder):
|
75 |
-
os.makedirs(history_folder)
|
76 |
-
for i, result_image in enumerate(result_images):
|
77 |
-
filename = f"result_{i}.jpg"
|
78 |
-
result_image.save(os.path.join(history_folder, filename))
|
79 |
-
return result_images
|
80 |
|
81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
|
83 |
-
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 |
+
import uuid
|
15 |
|
16 |
+
# Global variable to store history
|
17 |
+
history = []
|
18 |
|
19 |
+
def swap_face(source_file, target_files, doFaceEnhancer):
|
20 |
+
global history
|
21 |
+
output_paths = []
|
22 |
|
23 |
+
source_path = f"input_{uuid.uuid4()}.jpg"
|
24 |
+
source_image = Image.fromarray(source_file)
|
25 |
+
source_image.save(source_path)
|
26 |
|
27 |
+
for idx, target_file in enumerate(target_files):
|
28 |
+
target_path = f"target_{uuid.uuid4()}.jpg"
|
29 |
+
target_image = Image.fromarray(target_file)
|
30 |
+
target_image.save(target_path)
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
+
print(f"Processing image {idx + 1}")
|
33 |
+
print("source_path: ", source_path)
|
34 |
+
print("target_path: ", target_path)
|
|
|
|
|
35 |
|
36 |
+
roop.globals.source_path = source_path
|
37 |
+
roop.globals.target_path = target_path
|
38 |
+
output_path = f"output_{uuid.uuid4()}.jpg"
|
39 |
+
roop.globals.output_path = normalize_output_path(
|
40 |
+
roop.globals.source_path, roop.globals.target_path, output_path
|
41 |
+
)
|
42 |
+
if doFaceEnhancer:
|
43 |
+
roop.globals.frame_processors = ["face_swapper", "face_enhancer"]
|
44 |
+
else:
|
45 |
+
roop.globals.frame_processors = ["face_swapper"]
|
46 |
+
roop.globals.headless = True
|
47 |
+
roop.globals.keep_fps = True
|
48 |
+
roop.globals.keep_audio = True
|
49 |
+
roop.globals.keep_frames = False
|
50 |
+
roop.globals.many_faces = False
|
51 |
+
roop.globals.video_encoder = "libx264"
|
52 |
+
roop.globals.video_quality = 18
|
53 |
+
roop.globals.max_memory = suggest_max_memory()
|
54 |
+
roop.globals.execution_providers = decode_execution_providers(["cuda"])
|
55 |
+
roop.globals.execution_threads = suggest_execution_threads()
|
56 |
|
57 |
+
print(
|
58 |
+
"start process",
|
59 |
+
roop.globals.source_path,
|
60 |
+
roop.globals.target_path,
|
61 |
+
roop.globals.output_path,
|
62 |
+
)
|
63 |
|
64 |
+
for frame_processor in get_frame_processors_modules(
|
65 |
+
roop.globals.frame_processors
|
66 |
+
):
|
67 |
+
if not frame_processor.pre_check():
|
68 |
+
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
|
70 |
+
start()
|
71 |
+
output_paths.append(output_path)
|
|
|
|
|
|
|
72 |
|
73 |
+
# Add to history
|
74 |
+
history.extend(output_paths)
|
75 |
+
|
76 |
+
return output_paths, gr.update(value=history)
|
77 |
|
78 |
+
def get_history():
|
79 |
+
return history
|
80 |
|
81 |
+
html_section_1 = "<div><h1>Welcome to the Face Swap App</h1></div>"
|
82 |
+
html_section_2 = '<div><p>Upload your source and target images to swap faces. Optionally, use the face enhancer feature for HD Results.</p><h2><br /><strong>For fast and bulk Swap:</strong> <a href="https://picfy.xyz/" target="_blank" rel="noopener">https://picfy.xyz/</a><br /> <strong>Support me USDT (TRC-20): TAe7hsSVWtMEYz3G5V1UiUdYPQVqm28bKx</h2></div>'
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
|
84 |
+
with gr.Blocks() as app:
|
85 |
+
gr.HTML(html_section_1)
|
86 |
+
gr.HTML(html_section_2)
|
87 |
+
|
88 |
+
with gr.Tab("Face Swap"):
|
89 |
+
with gr.Row():
|
90 |
+
source_input = gr.Image(label="Source Image")
|
91 |
+
target_input = gr.File(label="Target Image(s)", file_count="multiple")
|
92 |
+
|
93 |
+
enhance_checkbox = gr.Checkbox(label="Use Face Enhancer", info="Apply face enhancement")
|
94 |
+
swap_button = gr.Button("Swap Faces")
|
95 |
+
output_gallery = gr.Gallery(label="Output Images")
|
96 |
+
|
97 |
+
with gr.Tab("History"):
|
98 |
+
history_gallery = gr.Gallery(label="Previous Outputs")
|
99 |
+
refresh_button = gr.Button("Refresh History")
|
100 |
+
|
101 |
+
swap_button.click(
|
102 |
+
fn=swap_face,
|
103 |
+
inputs=[source_input, target_input, enhance_checkbox],
|
104 |
+
outputs=[output_gallery, history_gallery]
|
105 |
+
)
|
106 |
+
|
107 |
+
refresh_button.click(
|
108 |
+
fn=get_history,
|
109 |
+
inputs=[],
|
110 |
+
outputs=[history_gallery]
|
111 |
+
)
|
112 |
|
113 |
+
app.launch()
|