Spaces:
Runtime error
Runtime error
chukwurah
commited on
Commit
·
88baf91
1
Parent(s):
3a2c844
ew
Browse files
app.py
CHANGED
@@ -1,79 +1,50 @@
|
|
1 |
-
import
|
2 |
import gradio as gr
|
3 |
-
import
|
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 |
-
processed_images = []
|
16 |
-
|
17 |
-
def swap_face(source_file, target_files, doFaceEnhancer):
|
18 |
-
source_path = "input.jpg"
|
19 |
-
source_image = Image.fromarray(source_file)
|
20 |
-
source_image.save(source_path)
|
21 |
-
|
22 |
-
results = []
|
23 |
-
for i, target_file in enumerate(target_files):
|
24 |
-
target_path = f"target_{i}.jpg"
|
25 |
-
target_image = Image.fromarray(target_file)
|
26 |
-
target_image.save(target_path)
|
27 |
-
|
28 |
-
roop.globals.source_path = source_path
|
29 |
-
roop.globals.target_path = target_path
|
30 |
-
output_path = f"output_{i}.jpg"
|
31 |
-
roop.globals.output_path = normalize_output_path(
|
32 |
-
roop.globals.source_path, roop.globals.target_path, output_path
|
33 |
-
)
|
34 |
-
if doFaceEnhancer:
|
35 |
-
roop.globals.frame_processors = ["face_swapper", "face_enhancer"]
|
36 |
-
else:
|
37 |
-
roop.globals.frame_processors = ["face_swapper"]
|
38 |
-
roop.globals.headless = True
|
39 |
-
roop.globals.keep_fps = True
|
40 |
-
roop.globals.keep_audio = True
|
41 |
-
roop.globals.keep_frames = False
|
42 |
-
roop.globals.many_faces = False
|
43 |
-
roop.globals.video_encoder = "libx264"
|
44 |
-
roop.globals.video_quality = 18
|
45 |
-
roop.globals.max_memory = suggest_max_memory()
|
46 |
-
roop.globals.execution_providers = decode_execution_providers(["cuda"])
|
47 |
-
roop.globals.execution_threads = suggest_execution_threads()
|
48 |
-
|
49 |
-
for frame_processor in get_frame_processors_modules(
|
50 |
-
roop.globals.frame_processors
|
51 |
-
):
|
52 |
-
if not frame_processor.pre_check():
|
53 |
-
return
|
54 |
-
|
55 |
-
start()
|
56 |
-
results.append(output_path)
|
57 |
-
processed_images.append(output_path)
|
58 |
|
59 |
-
|
60 |
|
61 |
-
html_section_1 = "<div><h1>Welcome to the Face Swap App</h1></div>"
|
62 |
-
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>'
|
63 |
|
64 |
app = gr.Blocks()
|
65 |
|
66 |
with app:
|
67 |
gr.HTML(html_section_1)
|
68 |
gr.HTML(html_section_2)
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
|
79 |
app.launch()
|
|
|
1 |
+
import os
|
2 |
import gradio as gr
|
3 |
+
import numpy as np
|
4 |
+
from roop.core import start, decode_execution_providers, suggest_max_memory, suggest_execution_threads
|
|
|
|
|
|
|
|
|
|
|
5 |
from roop.processors.frame.core import get_frame_processors_modules
|
6 |
from roop.utilities import normalize_output_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
+
html_section_1 = '<div><h1>Face Swap App</h1></div>'
|
9 |
|
|
|
|
|
10 |
|
11 |
app = gr.Blocks()
|
12 |
|
13 |
with app:
|
14 |
gr.HTML(html_section_1)
|
15 |
gr.HTML(html_section_2)
|
16 |
+
source_image = gr.Image(label="Source Image")
|
17 |
+
target_images = gr.File(label="Target Images", file_count="multiple")
|
18 |
+
do_face_enhancer = gr.Checkbox(label="Face Enhancer")
|
19 |
+
with gr.Tab("Result"):
|
20 |
+
result_images = gr.Gallery(label="Result Images")
|
21 |
+
with gr.Tab("History"):
|
22 |
+
history_images = gr.Gallery(label="Processed Images")
|
23 |
+
|
24 |
+
def face_swap(source_image, target_images, do_face_enhancer):
|
25 |
+
# Your face swap function here
|
26 |
+
# Assuming you have a function that takes source image, target images, and do_face_enhancer as input and returns a list of result images
|
27 |
+
result_images = []
|
28 |
+
for target_image in target_images:
|
29 |
+
# Call your face swap function for each target image
|
30 |
+
result_image = face_swap_function(source_image, target_image, do_face_enhancer)
|
31 |
+
result_images.append(result_image)
|
32 |
+
return result_images
|
33 |
+
|
34 |
+
gr.Button("Swap Faces").click(face_swap, inputs=[source_image, target_images, do_face_enhancer], outputs=result_images)
|
35 |
+
|
36 |
+
def save_history(result_images):
|
37 |
+
# Save the result images to a history folder
|
38 |
+
# You can use a database or a file system to store the images
|
39 |
+
# For simplicity, let's assume we're using a file system
|
40 |
+
history_folder = "history"
|
41 |
+
if not os.path.exists(history_folder):
|
42 |
+
os.makedirs(history_folder)
|
43 |
+
for i, result_image in enumerate(result_images):
|
44 |
+
filename = f"result_{i}.jpg"
|
45 |
+
result_image.save(os.path.join(history_folder, filename))
|
46 |
+
return result_images
|
47 |
+
|
48 |
+
gr.Button("Save to History").click(save_history, inputs=result_images, outputs=history_images)
|
49 |
|
50 |
app.launch()
|