import gradio as gr import io import requests import os import base64 import cv2 from PIL import Image import json import numpy as np import rsa import insightface from insightface.app import FaceAnalysis def FaceSwap(source_img, dest_img): swapper = insightface.model_zoo.get_model('./inswapper_128.onnx', download=False, download_zip=False) app = FaceAnalysis(name="buffalo_l") app.prepare(ctx_id=0, det_size=(640, 640)) s_faces = app.get(source_img) if len(s_faces) == 0: raise gr.Error("No Face Detected in Source Image") d_faces = app.get(dest_img) if len(d_faces) == 0: raise gr.Error("No Face Detected in Destination Image") source_face = s_faces[0] dest_face = d_faces[0] res = dest_img.copy() res = swapper.get(res, dest_face, source_face, paste_back=True) return res if __name__ == "__main__": with gr.Blocks(title="Face Swap | Meet With Your new Personality") as demo: gr.Interface(fn=FaceSwap, inputs=["image","image"], outputs=["image"], allow_flagging="never") demo.launch(show_api=False, favicon_path="./favicon.ico")