Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,60 +1,48 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
-
import
|
| 4 |
-
import cv2
|
| 5 |
-
from PIL import Image, ImageDraw
|
| 6 |
import insightface
|
| 7 |
from insightface.app import FaceAnalysis
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
app = FaceAnalysis(name=
|
| 11 |
app.prepare(ctx_id=0, det_size=(640, 640))
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
def generate_image(src_img, dest_img):
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
# Convert to array
|
| 21 |
-
#src_img_arr = np.asarray(src_img)
|
| 22 |
-
#dest_img_arr = np.asarray(dest_img)
|
| 23 |
-
src_img_arr = src_img
|
| 24 |
-
dest_img_arr = dest_img
|
| 25 |
-
|
| 26 |
-
# Face detection
|
| 27 |
-
src_faces = app.get(src_img_arr)
|
| 28 |
-
dest_faces = app.get(dest_img_arr)
|
| 29 |
-
|
| 30 |
-
# Initialize swapper
|
| 31 |
-
#swapper = insightface.model_zoo.get_model('./inswapper_128.onnx', download=False, download_zip=False)
|
| 32 |
-
#swapper = insightface.model_zoo.get_model('./inswapper_128.onnx')
|
| 33 |
|
| 34 |
-
|
| 35 |
-
# Swap face
|
| 36 |
-
res = dest_img_arr.copy()
|
| 37 |
for face in dest_faces:
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
# Convert to PIL image
|
| 41 |
-
final_image = Image.fromarray(np.uint8(res)).convert('RGB')
|
| 42 |
|
| 43 |
-
return
|
| 44 |
|
| 45 |
-
|
| 46 |
-
iface = gr.Interface(
|
| 47 |
-
|
| 48 |
-
gr.Image(label="Destination Image")],
|
| 49 |
outputs=gr.Image(label="Final Image"),
|
| 50 |
-
theme=gr.themes.Base(primary_hue=gr.themes.colors.teal, font=["helvetica"]),
|
| 51 |
examples=[["Images/kim.jpg", "Images/marilyn.jpg"]],
|
| 52 |
-
description=
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
+
from PIL import Image
|
|
|
|
|
|
|
| 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 |
+
# ---- Gradio <4.39 monkey-patch for bool schema bug ----
|
| 14 |
+
import gradio_client.utils as gu
|
| 15 |
+
if hasattr(gu, "get_type"):
|
| 16 |
+
_old_get_type = gu.get_type
|
| 17 |
+
def _safe_get_type(schema):
|
| 18 |
+
if isinstance(schema, bool): # <-- avoid "bool is not iterable"
|
| 19 |
+
return "bool"
|
| 20 |
+
return _old_get_type(schema)
|
| 21 |
+
gu.get_type = _safe_get_type
|
| 22 |
+
|
| 23 |
+
# ---- Main function ----
|
| 24 |
def generate_image(src_img, dest_img):
|
| 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 |
for face in dest_faces:
|
| 31 |
+
res = swapper.get(res, face, src_faces[0], paste_back=True)
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
return Image.fromarray(np.uint8(res)).convert("RGB")
|
| 34 |
|
| 35 |
+
# ---- Interface ----
|
| 36 |
+
iface = gr.Interface(
|
| 37 |
+
fn=generate_image,
|
| 38 |
+
inputs=[gr.Image(label="Source Image"), gr.Image(label="Destination Image")],
|
| 39 |
outputs=gr.Image(label="Final Image"),
|
|
|
|
| 40 |
examples=[["Images/kim.jpg", "Images/marilyn.jpg"]],
|
| 41 |
+
description="""
|
| 42 |
+
<h1>Face Swap</h1>
|
| 43 |
+
by <a href="https://www.tonyassi.com/" target="_blank" style="color:#97d8be;">Tony Assi</a><br>
|
| 44 |
+
Try out <a href="https://huggingface.co/spaces/tonyassi/video-face-swap">Video Face Swap</a> ❤️
|
| 45 |
+
"""
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
iface.launch()
|
|
|