File size: 2,873 Bytes
2bf015e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c0d77bd
2bf015e
5a0145c
2bf015e
 
 
7b2746c
2bf015e
5156ad0
2bf015e
 
 
d99e673
 
2bf015e
54dee3b
5a0145c
c0d77bd
5156ad0
 
2bf015e
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
import gradio as gr
import insightface
from insightface.app import FaceAnalysis

assert insightface.__version__ >= '0.7'

def prepare_app():
    app = FaceAnalysis(name='buffalo_l')
    app.prepare(ctx_id=0, det_size=(640, 640))
    swapper = insightface.model_zoo.get_model('inswapper_128.onnx', download=True, download_zip=True)
    return app, swapper

def sort_faces(faces):
    return sorted(faces, key=lambda x: x.bbox[0])

def get_face(faces, face_id):
    try:
        if len(faces) < face_id or face_id < 1:
            raise gr.Error(f"The image includes only {len(faces)} faces, however, you asked for face {face_id}")
        return faces[face_id-1]
    except Exception as e:
        raise gr.Error(f"An error occurred: {str(e)}")

app, swapper = prepare_app()

def swap_faces(sourceImage, sourceFaceIndex, destinationImage, destinationFaceIndex):
    """Swaps faces between the source and destination images based on the specified face indices."""
    faces = sort_faces(app.get(sourceImage))
    source_face = get_face(faces, sourceFaceIndex)
    
    res_faces = sort_faces(app.get(destinationImage))
    res_face = get_face(res_faces, destinationFaceIndex)

    result = swapper.get(destinationImage, res_face, source_face, paste_back=True)
    return result


gr.Interface(
    
    swap_faces, 
    [
        gr.Image(label="Source Image (the image with the face that you want to use)"), 
        gr.Number(precision=0, value=1, label='Source Face Position', info='In case there are multiple faces on the image specify which should be used from the left, starting at 1', visible=False,), 
        gr.Image(label="Destination Image (the image with the face that you want to replace)"), 
        gr.Number(precision=0, value=1, label='Destination Face Position', info='In case there are multiple faces on the image specify which should be replaced from the left, starting at 1' ,visible=False,)
    ],
    gr.Image(),
    examples=[
        ['./examples/rihanna.jpg', None, './examples/margaret_thatcher.jpg', None],
        ['./examples/game_of_thrones.jpg', None, './examples/game_of_thrones.jpg', None],
    ],
    theme="syddharth/gray-minimal",
    title="<h1 style='color: black; text-decoration: underline;'>AI Face Swapping - Free Face Swap</h1>",
    description="<span>● Swap faces with anyone in seconds - photos or AI images!,</span><br><span>● A gradio demo for  <a href='https://aiconvert.online/ai-face-merge/' style='color: blue; text-decoration: none;'>Mergify's Ai Face Swap  </a></span><br><span>● Full features and Multiple Face Swapping For Free with no limitations at at <a href='https://aiconvert.online' style='color: crimson; text-decoration: underline;'> <strong style='color: crimson;'>AIconvert.online</strong></a></span>",
    thumbnail='./examples/rihatcher.jpg', 
    css=" footer{display:none !important;}"
).launch()