Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,146 +1,77 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
import numpy as np
|
3 |
-
import
|
4 |
-
from diffusers import DiffusionPipeline
|
5 |
-
import torch
|
6 |
|
7 |
-
|
|
|
|
|
|
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
else
|
15 |
-
pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", use_safetensors=True)
|
16 |
-
pipe = pipe.to(device)
|
17 |
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
|
|
|
|
|
|
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
generator = torch.Generator().manual_seed(seed)
|
27 |
-
|
28 |
-
image = pipe(
|
29 |
-
prompt = prompt,
|
30 |
-
negative_prompt = negative_prompt,
|
31 |
-
guidance_scale = guidance_scale,
|
32 |
-
num_inference_steps = num_inference_steps,
|
33 |
-
width = width,
|
34 |
-
height = height,
|
35 |
-
generator = generator
|
36 |
-
).images[0]
|
37 |
-
|
38 |
-
return image
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
"An astronaut riding a green horse",
|
43 |
-
"A delicious ceviche cheesecake slice",
|
44 |
-
]
|
45 |
|
46 |
-
|
47 |
-
|
48 |
-
margin: 0 auto;
|
49 |
-
max-width: 520px;
|
50 |
-
}
|
51 |
-
"""
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
else:
|
56 |
-
power_device = "CPU"
|
57 |
|
58 |
-
|
59 |
-
|
60 |
-
with gr.Column(elem_id="col-container"):
|
61 |
-
gr.Markdown(f"""
|
62 |
-
# Text-to-Image Gradio Template
|
63 |
-
Currently running on {power_device}.
|
64 |
-
""")
|
65 |
-
|
66 |
-
with gr.Row():
|
67 |
-
|
68 |
-
prompt = gr.Text(
|
69 |
-
label="Prompt",
|
70 |
-
show_label=False,
|
71 |
-
max_lines=1,
|
72 |
-
placeholder="Enter your prompt",
|
73 |
-
container=False,
|
74 |
-
)
|
75 |
-
|
76 |
-
run_button = gr.Button("Run", scale=0)
|
77 |
-
|
78 |
-
result = gr.Image(label="Result", show_label=False)
|
79 |
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
label="Negative prompt",
|
84 |
-
max_lines=1,
|
85 |
-
placeholder="Enter a negative prompt",
|
86 |
-
visible=False,
|
87 |
-
)
|
88 |
-
|
89 |
-
seed = gr.Slider(
|
90 |
-
label="Seed",
|
91 |
-
minimum=0,
|
92 |
-
maximum=MAX_SEED,
|
93 |
-
step=1,
|
94 |
-
value=0,
|
95 |
-
)
|
96 |
-
|
97 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
98 |
-
|
99 |
-
with gr.Row():
|
100 |
-
|
101 |
-
width = gr.Slider(
|
102 |
-
label="Width",
|
103 |
-
minimum=256,
|
104 |
-
maximum=MAX_IMAGE_SIZE,
|
105 |
-
step=32,
|
106 |
-
value=512,
|
107 |
-
)
|
108 |
-
|
109 |
-
height = gr.Slider(
|
110 |
-
label="Height",
|
111 |
-
minimum=256,
|
112 |
-
maximum=MAX_IMAGE_SIZE,
|
113 |
-
step=32,
|
114 |
-
value=512,
|
115 |
-
)
|
116 |
-
|
117 |
-
with gr.Row():
|
118 |
-
|
119 |
-
guidance_scale = gr.Slider(
|
120 |
-
label="Guidance scale",
|
121 |
-
minimum=0.0,
|
122 |
-
maximum=10.0,
|
123 |
-
step=0.1,
|
124 |
-
value=0.0,
|
125 |
-
)
|
126 |
-
|
127 |
-
num_inference_steps = gr.Slider(
|
128 |
-
label="Number of inference steps",
|
129 |
-
minimum=1,
|
130 |
-
maximum=12,
|
131 |
-
step=1,
|
132 |
-
value=2,
|
133 |
-
)
|
134 |
-
|
135 |
-
gr.Examples(
|
136 |
-
examples = examples,
|
137 |
-
inputs = [prompt]
|
138 |
-
)
|
139 |
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
145 |
|
146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
import insightface
|
4 |
import numpy as np
|
5 |
+
import gradio.components as gr_comp
|
|
|
|
|
6 |
|
7 |
+
# Load the insightface model once
|
8 |
+
providers = ["CUDAExecutionProvider", "CPUExecutionProvider"] if cv2.cuda.getCudaEnabledDeviceCount() > 0 else ["CPUExecutionProvider"]
|
9 |
+
model_path = '/content/inswapper_128.onnx'
|
10 |
+
model_swap_insightface = insightface.model_zoo.get_model(model_path, providers=providers)
|
11 |
|
12 |
+
# Prepare the FaceAnalysis model once
|
13 |
+
FACE_ANALYSER = insightface.app.FaceAnalysis(
|
14 |
+
name="buffalo_l",
|
15 |
+
root=".", providers=providers, allowed_modules=["landmark_3d_68", "landmark_2d_106", "detection", "recognition"]
|
16 |
+
)
|
17 |
+
FACE_ANALYSER.prepare(ctx_id=0 if cv2.cuda.getCudaEnabledDeviceCount() > 0 else -1, det_size=(768, 512))
|
|
|
|
|
18 |
|
19 |
+
# Function to update template choices based on gender
|
20 |
+
def update_templates(gender):
|
21 |
+
if gender == 'Male':
|
22 |
+
return gr.Dropdown.update(choices=['Boy Template 1.JPG', 'Boy Template 2.JPG', 'Boy Template 3.JPG'])
|
23 |
+
elif gender == 'Female':
|
24 |
+
return gr.Dropdown.update(choices=['Girl Template 1.JPG', 'Girl Template 2.JPG', 'Girl Template 3.JPG'])
|
25 |
+
else:
|
26 |
+
return gr.Dropdown.update(choices=[])
|
27 |
|
28 |
+
# Main function for face swapping
|
29 |
+
def face_swap_and_merge(src_image, gender, template_choice):
|
30 |
+
# Resize the source image to 700x400 for uniform processing
|
31 |
+
src_image = cv2.resize(src_image, (400, 600))
|
32 |
|
33 |
+
template_image_path = f'/content/{template_choice}'
|
34 |
+
template_image = cv2.imread(template_image_path, cv2.IMREAD_UNCHANGED)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
+
# Resize the template image to 700x400 for uniform processing
|
37 |
+
template_image = cv2.resize(template_image, (400, 600))
|
|
|
|
|
|
|
38 |
|
39 |
+
src_faces = FACE_ANALYSER.get(src_image)
|
40 |
+
template_faces = FACE_ANALYSER.get(template_image)
|
|
|
|
|
|
|
|
|
41 |
|
42 |
+
img_fake = model_swap_insightface.get(img=template_image, target_face=template_faces[0], source_face=src_faces[0], paste_back=True)
|
43 |
+
img_fake_rgb = cv2.cvtColor(img_fake, cv2.COLOR_BGR2RGB)
|
|
|
|
|
44 |
|
45 |
+
return img_fake_rgb
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
+
# Function to clear only the output image
|
48 |
+
def clear_output():
|
49 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
+
# Function to clear all inputs and outputs
|
52 |
+
def clear_all():
|
53 |
+
return None, None, None, None
|
54 |
+
|
55 |
+
# Create Gradio interface with title and theme
|
56 |
+
with gr.Blocks(theme='upsatwal/mlsc_tiet') as iface:
|
57 |
+
gr.Markdown("# Face Changer")
|
58 |
+
gr.Markdown(" ")
|
59 |
+
|
60 |
+
src_image = gr.Image(type="numpy", label="Input Image")
|
61 |
+
gender = gr.Dropdown(["Male", "Female"], label="Select Gender", interactive=True)
|
62 |
+
template_choice = gr.Dropdown([], label="Select Template", interactive=True)
|
63 |
+
|
64 |
+
gender.change(fn=update_templates, inputs=gender, outputs=template_choice)
|
65 |
+
|
66 |
+
result_image = gr.Image(label="Output Image")
|
67 |
+
|
68 |
+
submit_button = gr.Button("Submit")
|
69 |
+
submit_button.click(fn=face_swap_and_merge, inputs=[src_image, gender, template_choice], outputs=result_image)
|
70 |
|
71 |
+
clear_output_button = gr.Button("Clear Output")
|
72 |
+
clear_output_button.click(fn=clear_output, inputs=[], outputs=result_image)
|
73 |
+
|
74 |
+
clear_all_button = gr.Button("Clear All")
|
75 |
+
clear_all_button.click(fn=clear_all, inputs=[], outputs=[src_image, gender, template_choice, result_image])
|
76 |
+
|
77 |
+
iface.launch(debug=True)
|