Spaces:
Paused
Paused
Official AnyPose prompt+0.7; pose canvas; rembg person onto image-1 room
Browse files10 unmodified lines11121314151617392 unmodified lines41041141241341441510 unmodified linesfrom typing import Optionalimport numpy as npfrom PIL import ImageSKIN, NOSE, EYE_G = 1, 2, 3L_EYE, R_EYE, L_BROW, R_BROW = 4, 5, 6, 7392 unmodified linesreturn dst.convert("RGB") if isinstance(dst, Image.Image) else dstdef try_lock_face_hair(src: Image.Image, dst: Image.Image) -> Image.Image:try:return lock_face_hair(src, dst)10 unmodified lines11121314151617392 unmodified lines41041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548610 unmodified linesfrom typing import Optionalimport numpy as npfrom PIL import Image, ImageFilterSKIN, NOSE, EYE_G = 1, 2, 3L_EYE, R_EYE, L_BROW, R_BROW = 4, 5, 6, 7392 unmodified linesreturn dst.convert("RGB") if isinstance(dst, Image.Image) else dst_REMBG = Nonedef _rembg_session():global _REMBGif _REMBG is None:from rembg import new_session_REMBG = new_session("u2net")print("[matte] rembg u2net ready", flush=True)return _REMBGdef fit_contain(im: Image.Image, size: tuple[int, int]) -> Image.Image:"""Scale image 1 to fit inside the pose canvas; pad with blurred room."""tw, th = sizeim = im.convert("RGB")w, h = im.sizescale = min(tw / float(w), th / float(h))nw, nh = max(1, int(w * scale)), max(1, int(h * scale))resized = im.resize((nw, nh), Image.LANCZOS)pad = im.resize((tw, th), Image.LANCZOS).filter(ImageFilter.GaussianBlur(28))pad.paste(resized, ((tw - nw) // 2, (th - nh) // 2))return paddef fit_cover(im: Image.Image, size: tuple[int, int]) -> Image.Image:"""Fill the pose canvas with image 1's room (center crop)."""tw, th = sizeim = im.convert("RGB")w, h = im.sizescale = max(tw / float(w), th / float(h))nw, nh = max(1, int(w * scale)), max(1, int(h * scale))resized = im.resize((nw, nh), Image.LANCZOS)left = max(0, (nw - tw) // 2)top = max(0, (nh - th) // 2)return resized.crop((left, top, left + tw, top + th))def composite_person_on_bg(person: Image.Image, bg: Image.Image) -> Image.Image:"""Cut the posed person (rembg) and paste onto the user-photo room."""from rembg import removeperson = person.convert("RGB")bg = bg.convert("RGB")if bg.size != person.size:bg = fit_cover(bg, person.size)rgba = remove(person, session=_rembg_session())alpha = np.asarray(rgba.split()[-1]).astype(np.float32) / 255.0if float(alpha.mean()) < 0.02:print("[matte] empty alpha — skip", flush=True)return person# slight erode so leftover ref-room fringe is droppedimport cv2a8 = (alpha * 255).astype(np.uint8)a8 = cv2.erode(a8, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)))a8 = cv2.GaussianBlur(a8, (7, 7), 0)a = (a8.astype(np.float32) / 255.0)[..., None]fg = np.asarray(person).astype(np.float32)bg_n = np.asarray(bg).astype(np.float32)out = fg * a + bg_n * (1.0 - a)print("[matte] posed person on image-1 room", flush=True)return Image.fromarray(np.clip(out, 0, 255).astype(np.uint8))def try_composite_person_on_bg(person: Image.Image, bg: Image.Image) -> Image.Image:try:return composite_person_on_bg(person, bg)except Exception as e:print(f"[matte] skipped: {type(e).__name__}: {e}", flush=True)return person.convert("RGB")def try_lock_face_hair(src: Image.Image, dst: Image.Image) -> Image.Image:try:return lock_face_hair(src, dst)
face_lock.py+72-1