RahulBhalley
commited on
Commit
·
b4c3e2b
1
Parent(s):
b8e5347
sonnet-3.7 optimizes code
Browse files
app.py
CHANGED
@@ -1,5 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import numpy as np
|
2 |
import gradio as gr
|
|
|
|
|
3 |
import roop.globals
|
4 |
from roop.core import (
|
5 |
start,
|
@@ -9,33 +16,36 @@ from roop.core import (
|
|
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
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
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 |
-
|
34 |
)
|
35 |
-
|
36 |
-
|
37 |
-
else
|
38 |
-
|
|
|
39 |
roop.globals.headless = True
|
40 |
roop.globals.keep_fps = True
|
41 |
roop.globals.keep_audio = True
|
@@ -44,38 +54,123 @@ def swap_face(source_file, target_file, doFaceEnhancer):
|
|
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(
|
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 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
-
start()
|
64 |
-
return output_path
|
65 |
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
-
|
68 |
-
|
69 |
|
70 |
-
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
-
with
|
73 |
-
|
74 |
-
gr.
|
75 |
-
gr.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
fn=swap_face,
|
77 |
-
inputs=[
|
78 |
-
outputs=
|
79 |
)
|
|
|
|
|
80 |
|
81 |
-
|
|
|
|
1 |
+
import os
|
2 |
+
import tempfile
|
3 |
+
from pathlib import Path
|
4 |
+
from typing import Tuple, Optional, List
|
5 |
+
|
6 |
import numpy as np
|
7 |
import gradio as gr
|
8 |
+
from PIL import Image
|
9 |
+
|
10 |
import roop.globals
|
11 |
from roop.core import (
|
12 |
start,
|
|
|
16 |
)
|
17 |
from roop.processors.frame.core import get_frame_processors_modules
|
18 |
from roop.utilities import normalize_output_path
|
|
|
|
|
19 |
|
20 |
|
21 |
+
def setup_roop_config(
|
22 |
+
source_path: str,
|
23 |
+
target_path: str,
|
24 |
+
output_path: str,
|
25 |
+
use_face_enhancer: bool = False,
|
26 |
+
execution_providers: List[str] = ["cuda"],
|
27 |
+
) -> None:
|
28 |
+
"""
|
29 |
+
Configure roop global settings for face swapping.
|
30 |
+
|
31 |
+
Args:
|
32 |
+
source_path: Path to the source image
|
33 |
+
target_path: Path to the target image
|
34 |
+
output_path: Path for the output image
|
35 |
+
use_face_enhancer: Whether to use face enhancer
|
36 |
+
execution_providers: List of execution providers
|
37 |
+
"""
|
38 |
+
# Set paths
|
39 |
roop.globals.source_path = source_path
|
40 |
roop.globals.target_path = target_path
|
|
|
41 |
roop.globals.output_path = normalize_output_path(
|
42 |
+
source_path, target_path, output_path
|
43 |
)
|
44 |
+
|
45 |
+
# Set processors
|
46 |
+
roop.globals.frame_processors = ["face_swapper", "face_enhancer"] if use_face_enhancer else ["face_swapper"]
|
47 |
+
|
48 |
+
# Set other configurations
|
49 |
roop.globals.headless = True
|
50 |
roop.globals.keep_fps = True
|
51 |
roop.globals.keep_audio = True
|
|
|
54 |
roop.globals.video_encoder = "libx264"
|
55 |
roop.globals.video_quality = 18
|
56 |
roop.globals.max_memory = suggest_max_memory()
|
57 |
+
roop.globals.execution_providers = decode_execution_providers(execution_providers)
|
58 |
roop.globals.execution_threads = suggest_execution_threads()
|
59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
+
def swap_face(
|
62 |
+
source_img: np.ndarray,
|
63 |
+
target_img: np.ndarray,
|
64 |
+
use_face_enhancer: bool = False,
|
65 |
+
execution_provider: str = "cuda"
|
66 |
+
) -> Optional[np.ndarray]:
|
67 |
+
"""
|
68 |
+
Swap faces between source and target images.
|
69 |
+
|
70 |
+
Args:
|
71 |
+
source_img: Source image as numpy array
|
72 |
+
target_img: Target image as numpy array
|
73 |
+
use_face_enhancer: Whether to enhance the face after swapping
|
74 |
+
execution_provider: Hardware acceleration provider (cuda, cpu, etc.)
|
75 |
+
|
76 |
+
Returns:
|
77 |
+
Resulting image as numpy array or None if processing failed
|
78 |
+
"""
|
79 |
+
try:
|
80 |
+
# Create temporary directory for processing
|
81 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
82 |
+
temp_dir_path = Path(temp_dir)
|
83 |
+
|
84 |
+
# Save input images to temporary files
|
85 |
+
source_path = str(temp_dir_path / "source.jpg")
|
86 |
+
target_path = str(temp_dir_path / "target.jpg")
|
87 |
+
output_path = str(temp_dir_path / "output.jpg")
|
88 |
+
|
89 |
+
Image.fromarray(source_img).save(source_path)
|
90 |
+
Image.fromarray(target_img).save(target_path)
|
91 |
+
|
92 |
+
# Configure roop
|
93 |
+
setup_roop_config(
|
94 |
+
source_path=source_path,
|
95 |
+
target_path=target_path,
|
96 |
+
output_path=output_path,
|
97 |
+
use_face_enhancer=use_face_enhancer,
|
98 |
+
execution_providers=[execution_provider]
|
99 |
+
)
|
100 |
+
|
101 |
+
# Check if processors are available
|
102 |
+
for frame_processor in get_frame_processors_modules(roop.globals.frame_processors):
|
103 |
+
if not frame_processor.pre_check():
|
104 |
+
raise RuntimeError(f"Frame processor {frame_processor.__name__} failed pre-check")
|
105 |
+
|
106 |
+
# Process the face swap
|
107 |
+
start()
|
108 |
+
|
109 |
+
# Return the result if file exists
|
110 |
+
if os.path.isfile(output_path):
|
111 |
+
return np.array(Image.open(output_path))
|
112 |
+
else:
|
113 |
+
raise FileNotFoundError("Output file was not created")
|
114 |
+
|
115 |
+
except Exception as e:
|
116 |
+
gr.Warning(f"Face swap failed: {str(e)}")
|
117 |
+
return None
|
118 |
|
|
|
|
|
119 |
|
120 |
+
# UI Components
|
121 |
+
TITLE = "NSFW Face Swap"
|
122 |
+
DESCRIPTION = """
|
123 |
+
Upload your source and target images to swap faces.
|
124 |
+
Optionally, use the face enhancer feature for HD Results.
|
125 |
|
126 |
+
**For fast bulk swap and API visit:** [https://picfy.xyz/](https://picfy.xyz/)
|
127 |
+
"""
|
128 |
|
129 |
+
FOOTER = """
|
130 |
+
<div style="text-align: center; margin-top: 20px;">
|
131 |
+
<p>Support me USDT (TRC-20): TAe7hsSVWtMEYz3G5V1UiUdYPQVqm28bKx</p>
|
132 |
+
<p>Start Face Swap SaaS on WordPress: <a href="https://www.codester.com/aheed/" target="_blank">https://www.codester.com/aheed/</a></p>
|
133 |
+
</div>
|
134 |
+
"""
|
135 |
|
136 |
+
# Create Gradio app with improved UI
|
137 |
+
with gr.Blocks(title=TITLE, theme=gr.themes.Soft()) as app:
|
138 |
+
gr.Markdown(f"# {TITLE}")
|
139 |
+
gr.Markdown(DESCRIPTION)
|
140 |
+
|
141 |
+
with gr.Row():
|
142 |
+
with gr.Column():
|
143 |
+
source_image = gr.Image(label="Source Face", type="numpy")
|
144 |
+
with gr.Column():
|
145 |
+
target_image = gr.Image(label="Target Image", type="numpy")
|
146 |
+
|
147 |
+
with gr.Row():
|
148 |
+
with gr.Column():
|
149 |
+
use_enhancer = gr.Checkbox(
|
150 |
+
label="Use Face Enhancer",
|
151 |
+
value=False,
|
152 |
+
info="Apply face enhancement for better quality (slower)"
|
153 |
+
)
|
154 |
+
with gr.Column():
|
155 |
+
provider = gr.Dropdown(
|
156 |
+
label="Execution Provider",
|
157 |
+
choices=["cuda", "cpu", "coreml", "directml", "openvino"],
|
158 |
+
value="cuda",
|
159 |
+
info="Hardware acceleration (CUDA recommended for NVIDIA GPUs)"
|
160 |
+
)
|
161 |
+
|
162 |
+
with gr.Row():
|
163 |
+
swap_btn = gr.Button("Swap Face", variant="primary")
|
164 |
+
|
165 |
+
output_image = gr.Image(label="Result")
|
166 |
+
|
167 |
+
swap_btn.click(
|
168 |
fn=swap_face,
|
169 |
+
inputs=[source_image, target_image, use_enhancer, provider],
|
170 |
+
outputs=output_image
|
171 |
)
|
172 |
+
|
173 |
+
gr.HTML(FOOTER)
|
174 |
|
175 |
+
if __name__ == "__main__":
|
176 |
+
app.launch(share=True)
|