Spaces:
Build error
Build error
Update src/components/faceswap.py
Browse files- src/components/faceswap.py +55 -55
src/components/faceswap.py
CHANGED
|
@@ -1,68 +1,68 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
|
| 11 |
-
|
| 12 |
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
|
| 25 |
-
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
|
| 58 |
-
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import cv2
|
| 3 |
+
import gdown
|
| 4 |
+
import insightface
|
| 5 |
+
from insightface.app import FaceAnalysis
|
| 6 |
+
from PIL import Image
|
| 7 |
+
from src.utils.logger import logger
|
| 8 |
+
import warnings
|
| 9 |
+
from src.components.face_enhancer import load_face_enhancer_model
|
| 10 |
|
| 11 |
+
warnings.filterwarnings("ignore", category=FutureWarning)
|
| 12 |
|
| 13 |
|
| 14 |
+
class FaceSwapper:
|
| 15 |
+
def __init__(self, app_name='buffalo_l', det_size=(640, 640), device="cuda"):
|
| 16 |
+
model_dir = "/mannequin_to_model/.insightface/models"
|
| 17 |
+
os.makedirs(model_dir, exist_ok=True)
|
| 18 |
+
self.device = device
|
| 19 |
+
self.app = FaceAnalysis(name=app_name, root=model_dir)
|
| 20 |
+
self.app.prepare(ctx_id=0 if device == device else -1, det_size=det_size)
|
| 21 |
+
self.swapper = None
|
| 22 |
+
self.enhancer_model = None
|
| 23 |
+
self.model_runner = None
|
| 24 |
|
| 25 |
+
logger.info('FaceSwapper initialized')
|
| 26 |
|
| 27 |
+
def load_enhancer_model(self, enhancer, device):
|
| 28 |
+
model, model_runner = load_face_enhancer_model(enhancer, device)
|
| 29 |
+
logger.info(f'{enhancer} model loaded')
|
| 30 |
+
self.enhancer_model = model
|
| 31 |
+
self.model_runner = model_runner
|
| 32 |
+
logger.info('Enhancer model loaded')
|
| 33 |
|
| 34 |
+
def load_swapper_model(self, model_url, model_path):
|
| 35 |
+
# Set up the gdown cache directory
|
| 36 |
+
gdown_cache_dir = "/mannequin_to_model/.cache/gdown"
|
| 37 |
+
os.makedirs(gdown_cache_dir, exist_ok=True)
|
| 38 |
+
os.environ['XDG_CACHE_HOME'] = gdown_cache_dir
|
| 39 |
|
| 40 |
+
# Log the cache directory and cookie path for debugging
|
| 41 |
+
logger.info(f"Setting XDG_CACHE_HOME to: {gdown_cache_dir}")
|
| 42 |
|
| 43 |
+
# Ensure cookies.txt exists
|
| 44 |
+
cookie_path = os.path.join(gdown_cache_dir, "cookies.txt")
|
| 45 |
+
if not os.path.exists(cookie_path):
|
| 46 |
+
with open(cookie_path, 'w') as f: # Create an empty cookies.txt file
|
| 47 |
+
pass
|
| 48 |
+
logger.info(f"Ensured cookies.txt exists at: {cookie_path}")
|
| 49 |
|
| 50 |
+
# Check if model path exists, and download if not
|
| 51 |
+
if not os.path.exists(model_path):
|
| 52 |
+
os.makedirs(os.path.dirname(model_path), exist_ok=True)
|
| 53 |
+
gdown.download(model_url, model_path, quiet=False)
|
| 54 |
|
| 55 |
+
self.swapper = insightface.model_zoo.get_model(model_path, download=False, download_zip=False)
|
| 56 |
+
logger.info('Swapper model loaded')
|
| 57 |
|
| 58 |
+
def face_swap(self, img1, img2, enhance=False):
|
| 59 |
|
| 60 |
+
face1 = self.app.get(img1)[0]
|
| 61 |
+
face2 = self.app.get(img2)[0]
|
| 62 |
|
| 63 |
+
img1_ = img1.copy()
|
| 64 |
+
img1_ = self.swapper.get(img1_, face1, face2, paste_back=True)
|
| 65 |
+
if enhance:
|
| 66 |
+
img1_ = self.model_runner(img1_, self.enhancer_model)
|
| 67 |
+
logger.info('Face swapped')
|
| 68 |
+
return img1_
|