Dach Stein commited on
Commit
2bf015e
1 Parent(s): 6bfc358

face swapping app initial commit fully working showcase

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ inswapper_128.onnx filter=lfs diff=lfs merge=lfs -text
app.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import insightface
3
+ from insightface.app import FaceAnalysis
4
+
5
+ assert insightface.__version__ >= '0.7'
6
+
7
+ def prepare_app():
8
+ app = FaceAnalysis(name='buffalo_l')
9
+ app.prepare(ctx_id=0, det_size=(640, 640))
10
+ swapper = insightface.model_zoo.get_model('inswapper_128.onnx', download=True, download_zip=True)
11
+ return app, swapper
12
+
13
+ def sort_faces(faces):
14
+ return sorted(faces, key=lambda x: x.bbox[0])
15
+
16
+ def get_face(faces, face_id):
17
+ try:
18
+ if len(faces) < face_id or face_id < 1:
19
+ raise gr.Error(f"The image includes only {len(faces)} faces, however, you asked for face {face_id}")
20
+ return faces[face_id-1]
21
+ except Exception as e:
22
+ raise gr.Error(f"An error occurred: {str(e)}")
23
+
24
+ app, swapper = prepare_app()
25
+
26
+ def swap_faces(sourceImage, sourceFaceIndex, destinationImage, destinationFaceIndex):
27
+ """Swaps faces between the source and destination images based on the specified face indices."""
28
+ faces = sort_faces(app.get(sourceImage))
29
+ source_face = get_face(faces, sourceFaceIndex)
30
+
31
+ res_faces = sort_faces(app.get(destinationImage))
32
+ res_face = get_face(res_faces, destinationFaceIndex)
33
+
34
+ result = swapper.get(destinationImage, res_face, source_face, paste_back=True)
35
+ return result
36
+
37
+ gr.Interface(
38
+ swap_faces,
39
+ [
40
+ gr.Image(label="Source Image (the image with the face that you want to use)"),
41
+ 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'),
42
+ gr.Image(label="Destination Image (the image with the face that you want to replace)"),
43
+ 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')
44
+ ],
45
+ gr.Image(),
46
+ examples=[
47
+ ['./examples/rihanna.jpg', 1, './examples/margaret_thatcher.jpg', 3],
48
+ ['./examples/game_of_thrones.jpg', 5, './examples/game_of_thrones.jpg', 4],
49
+ ],
50
+ theme=gr.themes.Base(),
51
+ title="Face Swapper App 🔄",
52
+ description="🌀 This app allows you to swap faces between images. <br>➡️ Upload a source image and a destination image, and specify the positions of the faces you'd like to swap! <br>⚡️ Try it out quickly by using the examples below. <br>💡 At [Dentro](https://dentro-innovation.com), we help you to discover, develop and implement AI within your organisation! <br>📖 The original authors of the face swap model can be found [here](https://github.com/deepinsight/insightface/blob/master/examples/in_swapper/README.md).<br>❤️ Feel free to like or duplicate this space!",
53
+ thumbnail='./examples/rihatcher.jpg'
54
+ ).launch()
examples/game_of_thrones.jpg ADDED
examples/margaret_thatcher.jpg ADDED
examples/rihanna.jpg ADDED
examples/rihatcher.jpg ADDED
inswapper_128.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e4a3f08c753cb72d04e10aa0f7dbe3deebbf39567d4ead6dce08e98aa49e16af
3
+ size 554253681
requirements.txt ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ aiofiles==23.2.1
2
+ albumentations==1.3.1
3
+ altair==5.1.2
4
+ annotated-types==0.6.0
5
+ anyio==3.7.1
6
+ attrs==23.1.0
7
+ certifi==2023.7.22
8
+ charset-normalizer==3.3.1
9
+ click==8.1.7
10
+ coloredlogs==15.0.1
11
+ contourpy==1.1.1
12
+ cycler==0.12.1
13
+ Cython==3.0.4
14
+ easydict==1.11
15
+ exceptiongroup==1.1.3
16
+ fastapi==0.104.1
17
+ ffmpy==0.3.1
18
+ filelock==3.13.1
19
+ flatbuffers==23.5.26
20
+ fonttools==4.43.1
21
+ fsspec==2023.10.0
22
+ gradio==3.50.2
23
+ gradio_client==0.6.1
24
+ h11==0.14.0
25
+ httpcore==0.18.0
26
+ httpx==0.25.0
27
+ huggingface-hub==0.18.0
28
+ humanfriendly==10.0
29
+ idna==3.4
30
+ imageio==2.31.6
31
+ importlib-resources==6.1.0
32
+ insightface==0.7.3
33
+ Jinja2==3.1.2
34
+ joblib==1.3.2
35
+ jsonschema==4.19.2
36
+ jsonschema-specifications==2023.7.1
37
+ kiwisolver==1.4.5
38
+ lazy_loader==0.3
39
+ MarkupSafe==2.1.3
40
+ matplotlib==3.8.0
41
+ mpmath==1.3.0
42
+ networkx==3.2.1
43
+ numpy==1.26.1
44
+ onnx==1.15.0
45
+ onnxruntime==1.16.1
46
+ opencv-python-headless==4.8.1.78
47
+ orjson==3.9.10
48
+ packaging==23.2
49
+ pandas==2.1.2
50
+ Pillow==10.0.1
51
+ prettytable==3.9.0
52
+ protobuf==4.24.4
53
+ pydantic==2.4.2
54
+ pydantic_core==2.10.1
55
+ pydub==0.25.1
56
+ pyparsing==3.1.1
57
+ python-dateutil==2.8.2
58
+ python-multipart==0.0.6
59
+ pytz==2023.3.post1
60
+ PyYAML==6.0.1
61
+ qudida==0.0.4
62
+ referencing==0.30.2
63
+ requests==2.31.0
64
+ rpds-py==0.10.6
65
+ scikit-image==0.22.0
66
+ scikit-learn==1.3.2
67
+ scipy==1.11.3
68
+ semantic-version==2.10.0
69
+ six==1.16.0
70
+ sniffio==1.3.0
71
+ starlette==0.27.0
72
+ sympy==1.12
73
+ threadpoolctl==3.2.0
74
+ tifffile==2023.9.26
75
+ toolz==0.12.0
76
+ tqdm==4.66.1
77
+ typing_extensions==4.8.0
78
+ tzdata==2023.3
79
+ urllib3==2.0.7
80
+ uvicorn==0.23.2
81
+ wcwidth==0.2.9
82
+ websockets==11.0.3