Spaces:
Running
Running
AshanGimhana
commited on
Commit
•
97885f2
1
Parent(s):
87cda33
Update app.py
Browse files
app.py
CHANGED
@@ -14,6 +14,17 @@ import cv2
|
|
14 |
import insightface
|
15 |
import numpy as np
|
16 |
import gradio.components as gr_comp
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
# Load the insightface model once
|
19 |
providers = ["CUDAExecutionProvider", "CPUExecutionProvider"] if cv2.cuda.getCudaEnabledDeviceCount() > 0 else ["CPUExecutionProvider"]
|
@@ -37,23 +48,34 @@ def update_templates(gender):
|
|
37 |
return gr.Dropdown.update(choices=[])
|
38 |
|
39 |
# Main function for face swapping
|
40 |
-
def face_swap_and_merge(src_image, gender, template_choice):
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
# Function to clear only the output image
|
59 |
def clear_output():
|
@@ -67,23 +89,23 @@ def clear_all():
|
|
67 |
with gr.Blocks(theme='upsatwal/mlsc_tiet') as iface:
|
68 |
gr.Markdown("# Face Changer")
|
69 |
gr.Markdown(" ")
|
70 |
-
|
71 |
src_image = gr.Image(type="numpy", label="Input Image")
|
72 |
gender = gr.Dropdown(["Male", "Female"], label="Select Gender", interactive=True)
|
73 |
template_choice = gr.Dropdown([], label="Select Template", interactive=True)
|
74 |
-
|
75 |
gender.change(fn=update_templates, inputs=gender, outputs=template_choice)
|
76 |
-
|
77 |
result_image = gr.Image(label="Output Image")
|
78 |
-
|
79 |
submit_button = gr.Button("Submit")
|
80 |
submit_button.click(fn=face_swap_and_merge, inputs=[src_image, gender, template_choice], outputs=result_image)
|
81 |
|
82 |
clear_output_button = gr.Button("Clear Output")
|
83 |
clear_output_button.click(fn=clear_output, inputs=[], outputs=result_image)
|
84 |
-
|
85 |
clear_all_button = gr.Button("Clear All")
|
86 |
clear_all_button.click(fn=clear_all, inputs=[], outputs=[src_image, gender, template_choice, result_image])
|
87 |
-
|
88 |
iface.launch(debug=True)
|
89 |
|
|
|
14 |
import insightface
|
15 |
import numpy as np
|
16 |
import gradio.components as gr_comp
|
17 |
+
import asyncio
|
18 |
+
import logging
|
19 |
+
from PIL import Image
|
20 |
+
|
21 |
+
# Setup logging
|
22 |
+
logging.basicConfig(level=logging.INFO)
|
23 |
+
|
24 |
+
# Constants for DPI and pixel dimensions
|
25 |
+
DPI = 300
|
26 |
+
WIDTH_PIXELS = 1182
|
27 |
+
HEIGHT_PIXELS = 1773
|
28 |
|
29 |
# Load the insightface model once
|
30 |
providers = ["CUDAExecutionProvider", "CPUExecutionProvider"] if cv2.cuda.getCudaEnabledDeviceCount() > 0 else ["CPUExecutionProvider"]
|
|
|
48 |
return gr.Dropdown.update(choices=[])
|
49 |
|
50 |
# Main function for face swapping
|
51 |
+
async def face_swap_and_merge(src_image, gender, template_choice):
|
52 |
+
try:
|
53 |
+
logging.info("Starting face swap and merge process")
|
54 |
+
|
55 |
+
# Resize the source image to the required dimensions for uniform processing
|
56 |
+
src_image = cv2.resize(src_image, (WIDTH_PIXELS, HEIGHT_PIXELS))
|
57 |
+
|
58 |
+
template_image_path = f'{template_choice}'
|
59 |
+
template_image = cv2.imread(template_image_path, cv2.IMREAD_UNCHANGED)
|
60 |
+
|
61 |
+
# Resize the template image to the required dimensions for uniform processing
|
62 |
+
template_image = cv2.resize(template_image, (WIDTH_PIXELS, HEIGHT_PIXELS))
|
63 |
+
|
64 |
+
src_faces = FACE_ANALYSER.get(src_image)
|
65 |
+
template_faces = FACE_ANALYSER.get(template_image)
|
66 |
+
|
67 |
+
img_fake = model_swap_insightface.get(img=template_image, target_face=template_faces[0], source_face=src_faces[0], paste_back=True)
|
68 |
+
img_fake_rgb = cv2.cvtColor(img_fake, cv2.COLOR_BGR2RGB)
|
69 |
+
|
70 |
+
# Convert to PIL image to set DPI
|
71 |
+
pil_image = Image.fromarray(img_fake_rgb)
|
72 |
+
pil_image.info['dpi'] = (DPI, DPI)
|
73 |
+
|
74 |
+
logging.info("Face swap and merge process completed successfully")
|
75 |
+
return np.array(pil_image)
|
76 |
+
except Exception as e:
|
77 |
+
logging.error(f"Error in face_swap_and_merge: {e}")
|
78 |
+
return None
|
79 |
|
80 |
# Function to clear only the output image
|
81 |
def clear_output():
|
|
|
89 |
with gr.Blocks(theme='upsatwal/mlsc_tiet') as iface:
|
90 |
gr.Markdown("# Face Changer")
|
91 |
gr.Markdown(" ")
|
92 |
+
|
93 |
src_image = gr.Image(type="numpy", label="Input Image")
|
94 |
gender = gr.Dropdown(["Male", "Female"], label="Select Gender", interactive=True)
|
95 |
template_choice = gr.Dropdown([], label="Select Template", interactive=True)
|
96 |
+
|
97 |
gender.change(fn=update_templates, inputs=gender, outputs=template_choice)
|
98 |
+
|
99 |
result_image = gr.Image(label="Output Image")
|
100 |
+
|
101 |
submit_button = gr.Button("Submit")
|
102 |
submit_button.click(fn=face_swap_and_merge, inputs=[src_image, gender, template_choice], outputs=result_image)
|
103 |
|
104 |
clear_output_button = gr.Button("Clear Output")
|
105 |
clear_output_button.click(fn=clear_output, inputs=[], outputs=result_image)
|
106 |
+
|
107 |
clear_all_button = gr.Button("Clear All")
|
108 |
clear_all_button.click(fn=clear_all, inputs=[], outputs=[src_image, gender, template_choice, result_image])
|
109 |
+
|
110 |
iface.launch(debug=True)
|
111 |
|