FaceSwapAll-moaad1 / SinglePhoto.py
dosesnrolls1's picture
Update SinglePhoto.py
9b725c5 verified
Raw
History Blame Contribute Delete
1.75 kB
import cv2
import insightface
from insightface.app import FaceAnalysis
import os
class FaceSwapper:
def __init__(self):
# Optimized for GPU
self.app = FaceAnalysis(
name='buffalo_l',
providers=['CUDAExecutionProvider', 'CPUExecutionProvider']
)
self.app.prepare(ctx_id=0, det_size=(640, 640)) # ctx_id=0 forces GPU
self.swapper = insightface.model_zoo.get_model(
'inswapper_128.onnx',
download=True,
download_zip=True
)
print("✅ FaceSwapper initialized with GPU support")
def swap_faces(self, source_path, source_face_idx, target_path, target_face_idx):
source_img = cv2.imread(source_path)
target_img = cv2.imread(target_path)
if source_img is None or target_img is None:
raise ValueError("Could not read one or both images")
source_faces = self.app.get(source_img)
target_faces = self.app.get(target_img)
source_faces = sorted(source_faces, key=lambda x: x.bbox[0])
target_faces = sorted(target_faces, key=lambda x: x.bbox[0])
if len(source_faces) < source_face_idx or source_face_idx < 1:
raise ValueError(f"Source image contains {len(source_faces)} faces, but requested face {source_face_idx}")
if len(target_faces) < target_face_idx or target_face_idx < 1:
raise ValueError(f"Target image contains {len(target_faces)} faces, but requested face {target_face_idx}")
source_face = source_faces[source_face_idx - 1]
target_face = target_faces[target_face_idx - 1]
result = self.swapper.get(target_img, target_face, source_face, paste_back=True)
return result