File size: 1,781 Bytes
0420d7a
 
 
 
c374d31
0420d7a
 
 
2b1c750
0420d7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c2b7113
2b1c750
 
9ff0691
0420d7a
 
 
 
 
 
 
2046c5d
0420d7a
4df9e66
0420d7a
 
 
 
ee44c1a
0420d7a
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
import gradio as gr
import insightface
from insightface.app import FaceAnalysis

wellcomingMessage = """"""

assert insightface.__version__>='0.7'

value = 0
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)

def swap_faces(faceSource, sourceFaceId, faceDestination, destFaceId):
    faces = app.get(faceSource)
    faces = sorted(faces, key = lambda x : x.bbox[0])
    if len(faces) < sourceFaceId or sourceFaceId < 1:
        raise gr.Error(f"Source image only contains {len(faces)} faces, but you requested face {sourceFaceId}")
        
    source_face = faces[sourceFaceId-1]

    res_faces = app.get(faceDestination)
    res_faces = sorted(res_faces, key = lambda x : x.bbox[0])
    if len(res_faces) < destFaceId or destFaceId < 1:
        raise gr.Error(f"Destination image only contains {len(res_faces)} faces, but you requested face {destFaceId}")
    res_face = res_faces[destFaceId-1]

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

    global value
    value = value + 1
    print(f"processed: {value}...")

    # for face in faces:
    #     res = swapper.get(res, face, source_face, paste_back=True)
    # cv2.imwrite("./t1_swapped.jpg", res)
    return result

gr.Interface(swap_faces, 
    [
        gr.Image(type="filepath", label="Input Image", sources=["upload", "clipboard"]),
        gr.Number(precision=0, value=1, info='face position (from left, starting at 1)'), 
        gr.Image(sources=["clipboard", "upload"]),
        gr.Number(precision=0, value=1, info='face position (from left, starting at 1)')
    ],
     gr.Image(),
     description=wellcomingMessage,
     examples=[],
).launch()