Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,45 +4,38 @@ from PIL import Image
|
|
| 4 |
import insightface
|
| 5 |
from insightface.app import FaceAnalysis
|
| 6 |
|
| 7 |
-
#
|
| 8 |
app = FaceAnalysis(name="buffalo_l")
|
| 9 |
app.prepare(ctx_id=0, det_size=(640, 640))
|
| 10 |
-
|
| 11 |
-
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
return "bool"
|
| 20 |
-
return _old_get_type(schema)
|
| 21 |
-
gu.get_type = _safe_get_type
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
def
|
| 25 |
-
# src_img, dest_img arrive as numpy arrays
|
| 26 |
src_faces = app.get(src_img)
|
| 27 |
dest_faces = app.get(dest_img)
|
| 28 |
|
| 29 |
res = dest_img.copy()
|
| 30 |
-
|
| 31 |
-
res = swapper.get(res,
|
| 32 |
|
| 33 |
return Image.fromarray(np.uint8(res)).convert("RGB")
|
| 34 |
|
| 35 |
-
#
|
| 36 |
-
|
| 37 |
-
fn=
|
| 38 |
-
inputs=[gr.Image(
|
| 39 |
-
outputs=gr.Image(
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
)
|
| 47 |
-
|
| 48 |
-
iface.launch()
|
|
|
|
| 4 |
import insightface
|
| 5 |
from insightface.app import FaceAnalysis
|
| 6 |
|
| 7 |
+
# ---------- Load models once ----------
|
| 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",
|
| 11 |
+
download=False, download_zip=False)
|
| 12 |
|
| 13 |
+
# ---------- HTML description ----------
|
| 14 |
+
description_html = """
|
| 15 |
+
<h1>Face Swap</h1>
|
| 16 |
+
<p>by <a href="https://www.tonyassi.com/" target="_blank" style="color:#97d8be;">Tony Assi</a></p>
|
| 17 |
+
<p>Try out <a href="https://huggingface.co/spaces/tonyassi/video-face-swap" target="_blank">Video Face Swap</a> ❤️</p>
|
| 18 |
+
"""
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
# ---------- Swap logic ----------
|
| 21 |
+
def swap_faces(src_img, dest_img):
|
|
|
|
| 22 |
src_faces = app.get(src_img)
|
| 23 |
dest_faces = app.get(dest_img)
|
| 24 |
|
| 25 |
res = dest_img.copy()
|
| 26 |
+
if src_faces and dest_faces:
|
| 27 |
+
res = swapper.get(res, dest_faces[0], src_faces[0], paste_back=True)
|
| 28 |
|
| 29 |
return Image.fromarray(np.uint8(res)).convert("RGB")
|
| 30 |
|
| 31 |
+
# ---------- Interface ----------
|
| 32 |
+
gr.Interface(
|
| 33 |
+
fn=swap_faces,
|
| 34 |
+
inputs=[gr.Image(), gr.Image()],
|
| 35 |
+
outputs=gr.Image(),
|
| 36 |
+
description=description_html,
|
| 37 |
+
examples=[
|
| 38 |
+
['./Images/kim.jpg', './Images/marilyn.jpg'],
|
| 39 |
+
['./Images/friends.jpg', './Images/friends.jpg']
|
| 40 |
+
],
|
| 41 |
+
).launch()
|
|
|
|
|
|
|
|
|