Update app.py
Browse files
app.py
CHANGED
|
@@ -167,12 +167,15 @@ def preview_faces(image_path):
|
|
| 167 |
(f.bbox[2]-f.bbox[0]) *
|
| 168 |
(f.bbox[3]-f.bbox[1]),
|
| 169 |
reverse=True
|
|
|
|
| 170 |
|
| 171 |
-
|
| 172 |
|
| 173 |
face_cache[cache_key] = faces
|
| 174 |
|
| 175 |
face_boxes = []
|
|
|
|
|
|
|
| 176 |
for idx, face in enumerate(faces):
|
| 177 |
|
| 178 |
x1, y1, x2, y2 = face.bbox
|
|
@@ -181,44 +184,58 @@ def preview_faces(image_path):
|
|
| 181 |
y1 = int(y1 / scale)
|
| 182 |
x2 = int(x2 / scale)
|
| 183 |
y2 = int(y2 / scale)
|
|
|
|
| 184 |
face_boxes.append([x1, y1, x2, y2])
|
| 185 |
|
|
|
|
| 186 |
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
|
|
|
| 193 |
)
|
| 194 |
|
| 195 |
cv2.rectangle(
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
)
|
| 202 |
|
| 203 |
cv2.putText(
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
cv2.LINE_AA
|
| 212 |
)
|
| 213 |
|
| 214 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
def show_face_indexes(target_files):
|
| 219 |
|
| 220 |
if not target_files:
|
| 221 |
-
return None, []
|
| 222 |
|
| 223 |
target_path = target_files[0].name
|
| 224 |
|
|
@@ -244,25 +261,33 @@ def show_face_indexes(target_files):
|
|
| 244 |
return preview
|
| 245 |
|
| 246 |
return preview_faces(target_path)
|
| 247 |
-
def select_face(evt: gr.SelectData, boxes):
|
| 248 |
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
|
| 254 |
-
|
| 255 |
|
| 256 |
-
|
| 257 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
|
| 259 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
|
| 261 |
-
|
| 262 |
-
# MAIN FUNCTION
|
| 263 |
-
# =========================
|
| 264 |
-
def generate_image(source_files, target_files, selected_face_index,
|
| 265 |
-
swap_model, face_restore_model, restore_strength):
|
| 266 |
|
| 267 |
os.makedirs("output", exist_ok=True)
|
| 268 |
output_paths = []
|
|
@@ -402,7 +427,8 @@ def generate_image(source_files, target_files, selected_face_index,
|
|
| 402 |
# =========================
|
| 403 |
|
| 404 |
with gr.Blocks() as app:
|
| 405 |
-
|
|
|
|
| 406 |
face_boxes = gr.State([])
|
| 407 |
source_files = gr.File(label="Source Faces", file_count="multiple", interactive=True)
|
| 408 |
target_files = gr.File(label="Target Images / GIFs", file_count="multiple", interactive=True)
|
|
@@ -415,20 +441,13 @@ with gr.Blocks() as app:
|
|
| 415 |
height =600
|
| 416 |
|
| 417 |
)
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
preview_image.select(
|
| 423 |
-
fn=select_face,
|
| 424 |
-
inputs=[face_boxes],
|
| 425 |
-
outputs=[
|
| 426 |
-
selected_face_index,
|
| 427 |
-
selected_label
|
| 428 |
-
]
|
| 429 |
)
|
| 430 |
|
| 431 |
-
|
| 432 |
swap_model = gr.Dropdown(
|
| 433 |
choices=["inswapper_128.onnx",
|
| 434 |
"hyperswap_1a_256.onnx",
|
|
@@ -451,25 +470,28 @@ with gr.Blocks() as app:
|
|
| 451 |
output_files = gr.Files(label="Output WebPs")
|
| 452 |
|
| 453 |
preview_btn.click(
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
|
|
|
|
|
|
| 461 |
|
| 462 |
|
| 463 |
generate_btn.click(
|
| 464 |
fn=generate_image,
|
| 465 |
inputs=[
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
|
|
|
| 473 |
outputs=output_files
|
| 474 |
)
|
| 475 |
|
|
|
|
| 167 |
(f.bbox[2]-f.bbox[0]) *
|
| 168 |
(f.bbox[3]-f.bbox[1]),
|
| 169 |
reverse=True
|
| 170 |
+
)
|
| 171 |
|
| 172 |
+
|
| 173 |
|
| 174 |
face_cache[cache_key] = faces
|
| 175 |
|
| 176 |
face_boxes = []
|
| 177 |
+
face_previews = []
|
| 178 |
+
|
| 179 |
for idx, face in enumerate(faces):
|
| 180 |
|
| 181 |
x1, y1, x2, y2 = face.bbox
|
|
|
|
| 184 |
y1 = int(y1 / scale)
|
| 185 |
x2 = int(x2 / scale)
|
| 186 |
y2 = int(y2 / scale)
|
| 187 |
+
|
| 188 |
face_boxes.append([x1, y1, x2, y2])
|
| 189 |
|
| 190 |
+
crop = img[y1:y2, x1:x2]
|
| 191 |
|
| 192 |
+
if crop.size == 0:
|
| 193 |
+
continue
|
| 194 |
+
|
| 195 |
+
crop = cv2.resize(
|
| 196 |
+
crop,
|
| 197 |
+
(180, 180),
|
| 198 |
+
interpolation=cv2.INTER_AREA
|
| 199 |
)
|
| 200 |
|
| 201 |
cv2.rectangle(
|
| 202 |
+
crop,
|
| 203 |
+
(0, 0),
|
| 204 |
+
(179, 35),
|
| 205 |
+
(0, 255, 0),
|
| 206 |
+
-1
|
| 207 |
)
|
| 208 |
|
| 209 |
cv2.putText(
|
| 210 |
+
crop,
|
| 211 |
+
f"FACE {idx}",
|
| 212 |
+
(10, 24),
|
| 213 |
+
cv2.FONT_HERSHEY_SIMPLEX,
|
| 214 |
+
0.7,
|
| 215 |
+
(0, 0, 0),
|
| 216 |
+
2
|
|
|
|
| 217 |
)
|
| 218 |
|
| 219 |
+
face_previews.append(crop)
|
| 220 |
+
|
| 221 |
+
if len(face_previews) == 0:
|
| 222 |
+
return None, []
|
| 223 |
+
|
| 224 |
+
preview = np.hstack(face_previews)
|
| 225 |
+
|
| 226 |
+
preview = cv2.cvtColor(
|
| 227 |
+
preview,
|
| 228 |
+
cv2.COLOR_BGR2RGB
|
| 229 |
+
)
|
| 230 |
+
|
| 231 |
+
return preview, face_boxes
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
def show_face_indexes(target_files):
|
| 236 |
|
| 237 |
if not target_files:
|
| 238 |
+
return None, [],[]
|
| 239 |
|
| 240 |
target_path = target_files[0].name
|
| 241 |
|
|
|
|
| 261 |
return preview
|
| 262 |
|
| 263 |
return preview_faces(target_path)
|
|
|
|
| 264 |
|
| 265 |
+
# =========================
|
| 266 |
+
# MAIN FUNCTION
|
| 267 |
+
# =========================
|
| 268 |
+
def update_face_selector(target_files):
|
| 269 |
|
| 270 |
+
preview, boxes, choices = [str(i) for i in range(len(face_boxes))]
|
| 271 |
|
| 272 |
+
return (
|
| 273 |
+
preview,
|
| 274 |
+
boxes,
|
| 275 |
+
gr.update(
|
| 276 |
+
choices=choices,
|
| 277 |
+
value=choices[0] if choices else None
|
| 278 |
+
)
|
| 279 |
+
)
|
| 280 |
|
| 281 |
+
def generate_image(
|
| 282 |
+
source_files,
|
| 283 |
+
target_files,
|
| 284 |
+
selected_face_index,
|
| 285 |
+
swap_model,
|
| 286 |
+
face_restore_model,
|
| 287 |
+
restore_strength
|
| 288 |
+
):
|
| 289 |
|
| 290 |
+
selected_face_index = int(selected_face_index)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 291 |
|
| 292 |
os.makedirs("output", exist_ok=True)
|
| 293 |
output_paths = []
|
|
|
|
| 427 |
# =========================
|
| 428 |
|
| 429 |
with gr.Blocks() as app:
|
| 430 |
+
|
| 431 |
+
|
| 432 |
face_boxes = gr.State([])
|
| 433 |
source_files = gr.File(label="Source Faces", file_count="multiple", interactive=True)
|
| 434 |
target_files = gr.File(label="Target Images / GIFs", file_count="multiple", interactive=True)
|
|
|
|
| 441 |
height =600
|
| 442 |
|
| 443 |
)
|
| 444 |
+
face_selector = gr.Dropdown(
|
| 445 |
+
choices=[],
|
| 446 |
+
value=None,
|
| 447 |
+
label="Select Face"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 448 |
)
|
| 449 |
|
| 450 |
+
|
| 451 |
swap_model = gr.Dropdown(
|
| 452 |
choices=["inswapper_128.onnx",
|
| 453 |
"hyperswap_1a_256.onnx",
|
|
|
|
| 470 |
output_files = gr.Files(label="Output WebPs")
|
| 471 |
|
| 472 |
preview_btn.click(
|
| 473 |
+
fn=update_face_selector,
|
| 474 |
+
inputs=[target_files],
|
| 475 |
+
outputs=[
|
| 476 |
+
preview_image,
|
| 477 |
+
face_boxes,
|
| 478 |
+
face_selector
|
| 479 |
+
]
|
| 480 |
+
)
|
| 481 |
+
|
| 482 |
|
| 483 |
|
| 484 |
generate_btn.click(
|
| 485 |
fn=generate_image,
|
| 486 |
inputs=[
|
| 487 |
+
source_files,
|
| 488 |
+
target_files,
|
| 489 |
+
face_selector,
|
| 490 |
+
swap_model,
|
| 491 |
+
face_restore_model,
|
| 492 |
+
restore_strength
|
| 493 |
+
]
|
| 494 |
+
|
| 495 |
outputs=output_files
|
| 496 |
)
|
| 497 |
|