Update app.py
Browse files
app.py
CHANGED
|
@@ -1,54 +1,48 @@
|
|
| 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 |
-
|
| 47 |
-
|
| 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://dentroai.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()
|
|
|
|
| 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 |
+
theme=gr.themes.Base(),
|
| 47 |
+
title="Face Swapper App 🔄",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
).launch()
|