Update app.py
Browse files
app.py
CHANGED
|
@@ -15,9 +15,14 @@ import numpy as np
|
|
| 15 |
import gradio as gr
|
| 16 |
|
| 17 |
from typing import Sequence, Mapping, Any, Union
|
|
|
|
| 18 |
from PIL import Image
|
| 19 |
from huggingface_hub import hf_hub_download
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
# =========================
|
| 22 |
# DOWNLOAD MODELS (ONLY IF NOT EXISTS)
|
| 23 |
# =========================
|
|
@@ -100,12 +105,146 @@ loadimage = NODE_CLASS_MAPPINGS["LoadImage"]()
|
|
| 100 |
reactorfaceswap = NODE_CLASS_MAPPINGS["ReActorFaceSwap"]()
|
| 101 |
|
| 102 |
# =========================
|
| 103 |
-
#
|
| 104 |
# =========================
|
| 105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
def generate_image(source_files, target_files, target_index,
|
| 107 |
swap_model, face_restore_model, restore_strength):
|
| 108 |
|
|
|
|
|
|
|
| 109 |
os.makedirs("output", exist_ok=True)
|
| 110 |
output_paths = []
|
| 111 |
|
|
@@ -247,6 +386,9 @@ with gr.Blocks() as app:
|
|
| 247 |
|
| 248 |
source_files = gr.File(label="Source Faces", file_count="multiple", interactive=True)
|
| 249 |
target_files = gr.File(label="Target Images / GIFs", file_count="multiple", interactive=True)
|
|
|
|
|
|
|
|
|
|
| 250 |
|
| 251 |
swap_model = gr.Dropdown(
|
| 252 |
choices=["inswapper_128.onnx",
|
|
@@ -264,11 +406,20 @@ with gr.Blocks() as app:
|
|
| 264 |
)
|
| 265 |
|
| 266 |
restore_strength = gr.Slider(0, 1, 0.7, step=0.05)
|
| 267 |
-
target_index = gr.Dropdown([0,1,2,3,4], value=0)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 268 |
|
| 269 |
generate_btn = gr.Button("Generate")
|
| 270 |
output_files = gr.Files(label="Output WebPs")
|
| 271 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
generate_btn.click(
|
| 273 |
fn=generate_image,
|
| 274 |
inputs=[source_files, target_files, target_index,
|
|
|
|
| 15 |
import gradio as gr
|
| 16 |
|
| 17 |
from typing import Sequence, Mapping, Any, Union
|
| 18 |
+
|
| 19 |
from PIL import Image
|
| 20 |
from huggingface_hub import hf_hub_download
|
| 21 |
|
| 22 |
+
import cv2
|
| 23 |
+
from insightface.app import FaceAnalysis
|
| 24 |
+
|
| 25 |
+
|
| 26 |
# =========================
|
| 27 |
# DOWNLOAD MODELS (ONLY IF NOT EXISTS)
|
| 28 |
# =========================
|
|
|
|
| 105 |
reactorfaceswap = NODE_CLASS_MAPPINGS["ReActorFaceSwap"]()
|
| 106 |
|
| 107 |
# =========================
|
| 108 |
+
# FAST FACE PREVIEW
|
| 109 |
# =========================
|
| 110 |
|
| 111 |
+
face_cache = {}
|
| 112 |
+
|
| 113 |
+
face_analyzer = FaceAnalysis(
|
| 114 |
+
name="buffalo_s",
|
| 115 |
+
providers=["CPUExecutionProvider"]
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
face_analyzer.prepare(
|
| 119 |
+
ctx_id=0,
|
| 120 |
+
det_size=(320, 320)
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
def preview_faces(image_path):
|
| 125 |
+
|
| 126 |
+
img = cv2.imread(image_path)
|
| 127 |
+
|
| 128 |
+
if img is None:
|
| 129 |
+
return None
|
| 130 |
+
|
| 131 |
+
h, w = img.shape[:2]
|
| 132 |
+
|
| 133 |
+
max_side = 1280
|
| 134 |
+
|
| 135 |
+
if max(h, w) > max_side:
|
| 136 |
+
|
| 137 |
+
scale = max_side / max(h, w)
|
| 138 |
+
|
| 139 |
+
img_small = cv2.resize(
|
| 140 |
+
img,
|
| 141 |
+
(
|
| 142 |
+
int(w * scale),
|
| 143 |
+
int(h * scale)
|
| 144 |
+
),
|
| 145 |
+
interpolation=cv2.INTER_AREA
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
else:
|
| 149 |
+
|
| 150 |
+
scale = 1.0
|
| 151 |
+
img_small = img
|
| 152 |
+
|
| 153 |
+
cache_key = (
|
| 154 |
+
image_path,
|
| 155 |
+
os.path.getmtime(image_path)
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
+
if cache_key in face_cache:
|
| 159 |
+
|
| 160 |
+
faces = face_cache[cache_key]
|
| 161 |
+
|
| 162 |
+
else:
|
| 163 |
+
|
| 164 |
+
faces = face_analyzer.get(img_small)
|
| 165 |
+
|
| 166 |
+
face_cache[cache_key] = faces
|
| 167 |
+
|
| 168 |
+
for idx, face in enumerate(faces):
|
| 169 |
+
|
| 170 |
+
x1, y1, x2, y2 = face.bbox
|
| 171 |
+
|
| 172 |
+
x1 = int(x1 / scale)
|
| 173 |
+
y1 = int(y1 / scale)
|
| 174 |
+
x2 = int(x2 / scale)
|
| 175 |
+
y2 = int(y2 / scale)
|
| 176 |
+
|
| 177 |
+
cv2.rectangle(
|
| 178 |
+
img,
|
| 179 |
+
(x1, y1),
|
| 180 |
+
(x2, y2),
|
| 181 |
+
(0, 255, 0),
|
| 182 |
+
3
|
| 183 |
+
)
|
| 184 |
+
|
| 185 |
+
cv2.rectangle(
|
| 186 |
+
img,
|
| 187 |
+
(x1, max(y1 - 40, 0)),
|
| 188 |
+
(x1 + 55, y1),
|
| 189 |
+
(0, 255, 0),
|
| 190 |
+
-1
|
| 191 |
+
)
|
| 192 |
+
|
| 193 |
+
cv2.putText(
|
| 194 |
+
img,
|
| 195 |
+
str(idx),
|
| 196 |
+
(x1 + 10, y1 - 8),
|
| 197 |
+
cv2.FONT_HERSHEY_SIMPLEX,
|
| 198 |
+
1,
|
| 199 |
+
(0, 0, 0),
|
| 200 |
+
2,
|
| 201 |
+
cv2.LINE_AA
|
| 202 |
+
)
|
| 203 |
+
|
| 204 |
+
return cv2.cvtColor(
|
| 205 |
+
img,
|
| 206 |
+
cv2.COLOR_BGR2RGB
|
| 207 |
+
)
|
| 208 |
+
|
| 209 |
+
|
| 210 |
+
def show_face_indexes(target_files):
|
| 211 |
+
|
| 212 |
+
if not target_files:
|
| 213 |
+
return None
|
| 214 |
+
|
| 215 |
+
target_path = target_files[0].name
|
| 216 |
+
|
| 217 |
+
if target_path.lower().endswith(".gif"):
|
| 218 |
+
|
| 219 |
+
reader = imageio.get_reader(target_path)
|
| 220 |
+
|
| 221 |
+
first_frame = reader.get_data(0)
|
| 222 |
+
|
| 223 |
+
reader.close()
|
| 224 |
+
|
| 225 |
+
with tempfile.NamedTemporaryFile(
|
| 226 |
+
suffix=".png",
|
| 227 |
+
delete=False
|
| 228 |
+
) as tmp:
|
| 229 |
+
|
| 230 |
+
Image.fromarray(first_frame).save(tmp.name)
|
| 231 |
+
|
| 232 |
+
preview = preview_faces(tmp.name)
|
| 233 |
+
|
| 234 |
+
os.remove(tmp.name)
|
| 235 |
+
|
| 236 |
+
return preview
|
| 237 |
+
|
| 238 |
+
return preview_faces(target_path)
|
| 239 |
+
|
| 240 |
+
# =========================
|
| 241 |
+
# MAIN FUNCTION
|
| 242 |
+
# =========================
|
| 243 |
def generate_image(source_files, target_files, target_index,
|
| 244 |
swap_model, face_restore_model, restore_strength):
|
| 245 |
|
| 246 |
+
target_index = str(target_index).replace(" ", "")
|
| 247 |
+
|
| 248 |
os.makedirs("output", exist_ok=True)
|
| 249 |
output_paths = []
|
| 250 |
|
|
|
|
| 386 |
|
| 387 |
source_files = gr.File(label="Source Faces", file_count="multiple", interactive=True)
|
| 388 |
target_files = gr.File(label="Target Images / GIFs", file_count="multiple", interactive=True)
|
| 389 |
+
preview_btn = gr.Button("Preview Face Index")
|
| 390 |
+
|
| 391 |
+
preview_image = gr.Image(label="Detected Faces",type="numpy")
|
| 392 |
|
| 393 |
swap_model = gr.Dropdown(
|
| 394 |
choices=["inswapper_128.onnx",
|
|
|
|
| 406 |
)
|
| 407 |
|
| 408 |
restore_strength = gr.Slider(0, 1, 0.7, step=0.05)
|
| 409 |
+
#target_index = gr.Dropdown([0,1,2,3,4], value=0)
|
| 410 |
+
target_index = gr.Textbox(
|
| 411 |
+
value="0",
|
| 412 |
+
label="Target Face Index(es)",
|
| 413 |
+
info="Ví dụ: 0 hoặc 0,1,2 phải sang trái")
|
| 414 |
|
| 415 |
generate_btn = gr.Button("Generate")
|
| 416 |
output_files = gr.Files(label="Output WebPs")
|
| 417 |
|
| 418 |
+
preview_btn.click(
|
| 419 |
+
fn=show_face_indexes,
|
| 420 |
+
inputs=[target_files],
|
| 421 |
+
outputs=[preview_image])
|
| 422 |
+
|
| 423 |
generate_btn.click(
|
| 424 |
fn=generate_image,
|
| 425 |
inputs=[source_files, target_files, target_index,
|