Spaces:
Sleeping
Sleeping
Official AnyPose prompt+0.7; pose canvas; rembg person onto image-1 room
Browse files- face_lock.py +72 -1
face_lock.py
CHANGED
|
@@ -11,7 +11,7 @@ import os
|
|
| 11 |
from typing import Optional
|
| 12 |
|
| 13 |
import numpy as np
|
| 14 |
-
from PIL import Image
|
| 15 |
|
| 16 |
SKIN, NOSE, EYE_G = 1, 2, 3
|
| 17 |
L_EYE, R_EYE, L_BROW, R_BROW = 4, 5, 6, 7
|
|
@@ -410,6 +410,77 @@ def try_lock_background(src: Image.Image, dst: Image.Image) -> Image.Image:
|
|
| 410 |
return dst.convert("RGB") if isinstance(dst, Image.Image) else dst
|
| 411 |
|
| 412 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 413 |
def try_lock_face_hair(src: Image.Image, dst: Image.Image) -> Image.Image:
|
| 414 |
try:
|
| 415 |
return lock_face_hair(src, dst)
|
|
|
|
| 11 |
from typing import Optional
|
| 12 |
|
| 13 |
import numpy as np
|
| 14 |
+
from PIL import Image, ImageFilter
|
| 15 |
|
| 16 |
SKIN, NOSE, EYE_G = 1, 2, 3
|
| 17 |
L_EYE, R_EYE, L_BROW, R_BROW = 4, 5, 6, 7
|
|
|
|
| 410 |
return dst.convert("RGB") if isinstance(dst, Image.Image) else dst
|
| 411 |
|
| 412 |
|
| 413 |
+
_REMBG = None
|
| 414 |
+
|
| 415 |
+
|
| 416 |
+
def _rembg_session():
|
| 417 |
+
global _REMBG
|
| 418 |
+
if _REMBG is None:
|
| 419 |
+
from rembg import new_session
|
| 420 |
+
_REMBG = new_session("u2net")
|
| 421 |
+
print("[matte] rembg u2net ready", flush=True)
|
| 422 |
+
return _REMBG
|
| 423 |
+
|
| 424 |
+
|
| 425 |
+
def fit_contain(im: Image.Image, size: tuple[int, int]) -> Image.Image:
|
| 426 |
+
"""Scale image 1 to fit inside the pose canvas; pad with blurred room."""
|
| 427 |
+
tw, th = size
|
| 428 |
+
im = im.convert("RGB")
|
| 429 |
+
w, h = im.size
|
| 430 |
+
scale = min(tw / float(w), th / float(h))
|
| 431 |
+
nw, nh = max(1, int(w * scale)), max(1, int(h * scale))
|
| 432 |
+
resized = im.resize((nw, nh), Image.LANCZOS)
|
| 433 |
+
pad = im.resize((tw, th), Image.LANCZOS).filter(ImageFilter.GaussianBlur(28))
|
| 434 |
+
pad.paste(resized, ((tw - nw) // 2, (th - nh) // 2))
|
| 435 |
+
return pad
|
| 436 |
+
|
| 437 |
+
|
| 438 |
+
def fit_cover(im: Image.Image, size: tuple[int, int]) -> Image.Image:
|
| 439 |
+
"""Fill the pose canvas with image 1's room (center crop)."""
|
| 440 |
+
tw, th = size
|
| 441 |
+
im = im.convert("RGB")
|
| 442 |
+
w, h = im.size
|
| 443 |
+
scale = max(tw / float(w), th / float(h))
|
| 444 |
+
nw, nh = max(1, int(w * scale)), max(1, int(h * scale))
|
| 445 |
+
resized = im.resize((nw, nh), Image.LANCZOS)
|
| 446 |
+
left = max(0, (nw - tw) // 2)
|
| 447 |
+
top = max(0, (nh - th) // 2)
|
| 448 |
+
return resized.crop((left, top, left + tw, top + th))
|
| 449 |
+
|
| 450 |
+
|
| 451 |
+
def composite_person_on_bg(person: Image.Image, bg: Image.Image) -> Image.Image:
|
| 452 |
+
"""Cut the posed person (rembg) and paste onto the user-photo room."""
|
| 453 |
+
from rembg import remove
|
| 454 |
+
person = person.convert("RGB")
|
| 455 |
+
bg = bg.convert("RGB")
|
| 456 |
+
if bg.size != person.size:
|
| 457 |
+
bg = fit_cover(bg, person.size)
|
| 458 |
+
rgba = remove(person, session=_rembg_session())
|
| 459 |
+
alpha = np.asarray(rgba.split()[-1]).astype(np.float32) / 255.0
|
| 460 |
+
if float(alpha.mean()) < 0.02:
|
| 461 |
+
print("[matte] empty alpha — skip", flush=True)
|
| 462 |
+
return person
|
| 463 |
+
# slight erode so leftover ref-room fringe is dropped
|
| 464 |
+
import cv2
|
| 465 |
+
a8 = (alpha * 255).astype(np.uint8)
|
| 466 |
+
a8 = cv2.erode(a8, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)))
|
| 467 |
+
a8 = cv2.GaussianBlur(a8, (7, 7), 0)
|
| 468 |
+
a = (a8.astype(np.float32) / 255.0)[..., None]
|
| 469 |
+
fg = np.asarray(person).astype(np.float32)
|
| 470 |
+
bg_n = np.asarray(bg).astype(np.float32)
|
| 471 |
+
out = fg * a + bg_n * (1.0 - a)
|
| 472 |
+
print("[matte] posed person on image-1 room", flush=True)
|
| 473 |
+
return Image.fromarray(np.clip(out, 0, 255).astype(np.uint8))
|
| 474 |
+
|
| 475 |
+
|
| 476 |
+
def try_composite_person_on_bg(person: Image.Image, bg: Image.Image) -> Image.Image:
|
| 477 |
+
try:
|
| 478 |
+
return composite_person_on_bg(person, bg)
|
| 479 |
+
except Exception as e:
|
| 480 |
+
print(f"[matte] skipped: {type(e).__name__}: {e}", flush=True)
|
| 481 |
+
return person.convert("RGB")
|
| 482 |
+
|
| 483 |
+
|
| 484 |
def try_lock_face_hair(src: Image.Image, dst: Image.Image) -> Image.Image:
|
| 485 |
try:
|
| 486 |
return lock_face_hair(src, dst)
|