Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -11,100 +11,75 @@ 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 |
-
|
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 |
-
roop.globals.
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
start()
|
77 |
-
return output_path
|
78 |
-
|
79 |
-
except Exception as e:
|
80 |
-
return f"An error occurred: {e}"
|
81 |
-
|
82 |
-
# HTML content for UI information
|
83 |
-
html_section_1 = "<div><p>This model is running on the selected hardware (CPU/GPU). Processing might take some time.</p></div>"
|
84 |
-
|
85 |
-
# Create the Gradio interface
|
86 |
app = gr.Blocks()
|
87 |
|
88 |
with app:
|
89 |
-
gr.HTML(
|
90 |
-
gr.Markdown("## Face Swap Application")
|
91 |
-
gr.Markdown("Upload a source and target image to swap faces. Optionally, enhance the swapped face.")
|
92 |
-
|
93 |
-
# Inputs and Interface
|
94 |
-
inputs = [
|
95 |
-
gr.Image(label="Source Image"),
|
96 |
-
gr.Image(label="Target Image"),
|
97 |
-
gr.Checkbox(label="Enhance", info="Use Face Enhancer"),
|
98 |
-
gr.Checkbox(label="Use GPU (if available)", value=True)
|
99 |
-
]
|
100 |
-
|
101 |
-
outputs = gr.Image(label="Swapped Image")
|
102 |
-
|
103 |
-
# Interface function call
|
104 |
gr.Interface(
|
105 |
fn=swap_face,
|
106 |
-
inputs=
|
107 |
-
outputs=
|
108 |
-
)
|
109 |
|
110 |
app.launch()
|
|
|
11 |
from roop.utilities import normalize_output_path
|
12 |
import os
|
13 |
from PIL import Image
|
14 |
+
|
15 |
+
def swap_face(source_file, target_file, doFaceEnhancer):
|
16 |
+
|
17 |
+
# Resize images to reduce processing load
|
18 |
+
source_image = Image.fromarray(source_file)
|
19 |
+
target_image = Image.fromarray(target_file)
|
20 |
+
|
21 |
+
# Resize to a smaller dimension if necessary
|
22 |
+
source_image.thumbnail((512, 512))
|
23 |
+
target_image.thumbnail((512, 512))
|
24 |
+
|
25 |
+
source_path = "input.jpg"
|
26 |
+
target_path = "target.jpg"
|
27 |
+
|
28 |
+
source_image.save(source_path, format="JPEG")
|
29 |
+
target_image.save(target_path, format="JPEG")
|
30 |
+
|
31 |
+
print("source_path: ", source_path)
|
32 |
+
print("target_path: ", target_path)
|
33 |
+
|
34 |
+
roop.globals.source_path = source_path
|
35 |
+
roop.globals.target_path = target_path
|
36 |
+
output_path = "output.jpg"
|
37 |
+
roop.globals.output_path = normalize_output_path(
|
38 |
+
roop.globals.source_path, roop.globals.target_path, output_path
|
39 |
+
)
|
40 |
+
|
41 |
+
# Limit processing to what is necessary for CPU
|
42 |
+
if doFaceEnhancer:
|
43 |
+
roop.globals.frame_processors = ["face_swapper", "face_enhancer"]
|
44 |
+
else:
|
45 |
+
roop.globals.frame_processors = ["face_swapper"]
|
46 |
+
|
47 |
+
# Optimize for CPU
|
48 |
+
roop.globals.headless = True
|
49 |
+
roop.globals.keep_fps = False # Disable to reduce processing
|
50 |
+
roop.globals.keep_audio = False # Disable to reduce processing
|
51 |
+
roop.globals.keep_frames = False
|
52 |
+
roop.globals.many_faces = False
|
53 |
+
roop.globals.video_encoder = "libx264"
|
54 |
+
roop.globals.video_quality = 24 # Lower quality for faster processing
|
55 |
+
roop.globals.max_memory = suggest_max_memory() # Use all available memory
|
56 |
+
roop.globals.execution_providers = decode_execution_providers(["cpu"]) # Use CPU only
|
57 |
+
roop.globals.execution_threads = suggest_execution_threads() # Utilize all CPU cores
|
58 |
+
|
59 |
+
print(
|
60 |
+
"start process",
|
61 |
+
roop.globals.source_path,
|
62 |
+
roop.globals.target_path,
|
63 |
+
roop.globals.output_path,
|
64 |
+
)
|
65 |
+
|
66 |
+
for frame_processor in get_frame_processors_modules(roop.globals.frame_processors):
|
67 |
+
if not frame_processor.pre_check():
|
68 |
+
return
|
69 |
+
|
70 |
+
start()
|
71 |
+
return output_path
|
72 |
+
|
73 |
+
html_section_2 = "<div><p>This model is running on CPU and might be slow.</p></div>"
|
74 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
app = gr.Blocks()
|
76 |
|
77 |
with app:
|
78 |
+
gr.HTML(html_section_2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
gr.Interface(
|
80 |
fn=swap_face,
|
81 |
+
inputs=[gr.Image(), gr.Image(), gr.Checkbox(label="Enhance", info="Face Enhancer")],
|
82 |
+
outputs="image"
|
83 |
+
)
|
84 |
|
85 |
app.launch()
|