Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
import roop.globals
|
3 |
from roop.core import (
|
@@ -8,37 +9,33 @@ from roop.core import (
|
|
8 |
)
|
9 |
from roop.processors.frame.core import get_frame_processors_modules
|
10 |
from roop.utilities import normalize_output_path
|
|
|
11 |
from PIL import Image
|
12 |
-
import io
|
13 |
|
14 |
-
def process_face_swap(source_image, target_image, apply_face_enhancer):
|
15 |
-
# Convert images to PIL Image objects and save them to in-memory file-like objects
|
16 |
-
src_image_bytes = io.BytesIO()
|
17 |
-
tgt_image_bytes = io.BytesIO()
|
18 |
-
|
19 |
-
source_image.save(src_image_bytes, format='JPEG')
|
20 |
-
target_image.save(tgt_image_bytes, format='JPEG')
|
21 |
|
22 |
-
|
23 |
-
tgt_image_bytes.seek(0)
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
output_path = "result_image.jpg"
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
f.write(tgt_image_bytes.getvalue())
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
38 |
roop.globals.output_path = normalize_output_path(
|
39 |
roop.globals.source_path, roop.globals.target_path, output_path
|
40 |
)
|
41 |
-
|
|
|
|
|
|
|
42 |
roop.globals.headless = True
|
43 |
roop.globals.keep_fps = True
|
44 |
roop.globals.keep_audio = True
|
@@ -47,38 +44,34 @@ def process_face_swap(source_image, target_image, apply_face_enhancer):
|
|
47 |
roop.globals.video_encoder = "libx264"
|
48 |
roop.globals.video_quality = 18
|
49 |
roop.globals.max_memory = suggest_max_memory()
|
50 |
-
roop.globals.execution_providers = decode_execution_providers(["cuda"
|
51 |
roop.globals.execution_threads = suggest_execution_threads()
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
|
|
|
62 |
return
|
63 |
|
64 |
start()
|
65 |
-
|
66 |
-
# Load and return the output image
|
67 |
-
with open(roop.globals.output_path, 'rb') as f:
|
68 |
-
output_image = f.read()
|
69 |
-
|
70 |
-
return output_image
|
71 |
|
72 |
-
# Create Gradio interface
|
73 |
-
iface = gr.Interface(
|
74 |
-
fn=process_face_swap,
|
75 |
-
inputs=[
|
76 |
-
gr.Image(type="pil", label="Source Image"),
|
77 |
-
gr.Image(type="pil", label="Target Image"),
|
78 |
-
gr.Checkbox(label="Apply Face Enhancer")
|
79 |
-
],
|
80 |
-
outputs=gr.Image(type="pil"),
|
81 |
-
description="Swap faces between two images. Use the face enhancer option to improve the result. Processing may take longer if you use Face Enhancer. Becose this Modle is running using CPU."
|
82 |
-
)
|
83 |
|
84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
import gradio as gr
|
3 |
import roop.globals
|
4 |
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 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
|
|
|
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 |
+
app = gr.Blocks()
|
68 |
+
|
69 |
+
with app:
|
70 |
+
gr.Interface(
|
71 |
+
fn=swap_face,
|
72 |
+
inputs=[gr.Image(), gr.Image(), gr.Checkbox(label="Apply Face Enhancer")],
|
73 |
+
outputs="image",
|
74 |
+
description="Swap faces between two images. Use the Face Enhancer option to improve the result. Processing may take longer if you use Face Enhancer because this model is running using the CPU."
|
75 |
+
)
|
76 |
+
|
77 |
+
app.launch()
|