Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,46 +1,68 @@
|
|
| 1 |
import spaces
|
| 2 |
import gradio as gr
|
| 3 |
import cv2
|
| 4 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
@spaces.GPU
|
| 6 |
def swap_face(source_img, target_img):
|
|
|
|
| 7 |
import insightface
|
| 8 |
from insightface.app import FaceAnalysis
|
| 9 |
try:
|
| 10 |
-
|
|
|
|
|
|
|
| 11 |
try:
|
| 12 |
-
app.prepare(ctx_id=0, det_size=(640,640))
|
| 13 |
-
except:
|
| 14 |
-
app.prepare(ctx_id=-1, det_size=(640,640))
|
| 15 |
import insightface.model_zoo
|
| 16 |
-
swapper = insightface.model_zoo.get_model(
|
| 17 |
-
src = cv2.imread(source_img)
|
| 18 |
-
tgt = cv2.imread(target_img)
|
| 19 |
s_faces = app.get(src)
|
| 20 |
t_faces = app.get(tgt)
|
| 21 |
-
if len(s_faces)==0 or len(t_faces)==0:
|
| 22 |
-
app2 = FaceAnalysis(name=
|
| 23 |
-
app2.prepare(ctx_id=-1, det_size=(1024,1024))
|
| 24 |
-
if len(s_faces)==0:
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
return out
|
| 31 |
res = swapper.get(tgt, t_faces[0], s_faces[0], paste_back=True)
|
| 32 |
-
out="/tmp/swapped.jpg"
|
| 33 |
-
cv2.imwrite(out,res)
|
| 34 |
return out
|
| 35 |
-
except
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
| 38 |
with gr.Blocks() as demo:
|
| 39 |
gr.Markdown("# FaceFusion Zero")
|
| 40 |
with gr.Row():
|
| 41 |
-
src = gr.Image(
|
| 42 |
-
tgt = gr.Image(
|
| 43 |
-
out = gr.Image(
|
| 44 |
btn = gr.Button("Swap")
|
| 45 |
btn.click(swap_face, inputs=[src, tgt], outputs=out)
|
|
|
|
| 46 |
demo.launch()
|
|
|
|
| 1 |
import spaces
|
| 2 |
import gradio as gr
|
| 3 |
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def _to_bgr(img):
|
| 8 |
+
if img is None:
|
| 9 |
+
raise ValueError("no image received")
|
| 10 |
+
if isinstance(img, str):
|
| 11 |
+
im = cv2.imread(img)
|
| 12 |
+
if im is None:
|
| 13 |
+
raise ValueError("unreadable path: %s" % img)
|
| 14 |
+
return im
|
| 15 |
+
if hasattr(img, "convert"):
|
| 16 |
+
return cv2.cvtColor(np.array(img.convert("RGB")), cv2.COLOR_RGB2BGR)
|
| 17 |
+
a = np.array(img)
|
| 18 |
+
if a.ndim == 3 and a.shape[2] >= 3:
|
| 19 |
+
return cv2.cvtColor(a[:, :, :3], cv2.COLOR_RGB2BGR)
|
| 20 |
+
return a
|
| 21 |
+
|
| 22 |
+
|
| 23 |
@spaces.GPU
|
| 24 |
def swap_face(source_img, target_img):
|
| 25 |
+
import traceback
|
| 26 |
import insightface
|
| 27 |
from insightface.app import FaceAnalysis
|
| 28 |
try:
|
| 29 |
+
src = _to_bgr(source_img)
|
| 30 |
+
tgt = _to_bgr(target_img)
|
| 31 |
+
app = FaceAnalysis(name="buffalo_l")
|
| 32 |
try:
|
| 33 |
+
app.prepare(ctx_id=0, det_size=(640, 640))
|
| 34 |
+
except Exception:
|
| 35 |
+
app.prepare(ctx_id=-1, det_size=(640, 640))
|
| 36 |
import insightface.model_zoo
|
| 37 |
+
swapper = insightface.model_zoo.get_model("inswapper_128.onnx", download=True)
|
|
|
|
|
|
|
| 38 |
s_faces = app.get(src)
|
| 39 |
t_faces = app.get(tgt)
|
| 40 |
+
if len(s_faces) == 0 or len(t_faces) == 0:
|
| 41 |
+
app2 = FaceAnalysis(name="buffalo_l")
|
| 42 |
+
app2.prepare(ctx_id=-1, det_size=(1024, 1024))
|
| 43 |
+
if len(s_faces) == 0:
|
| 44 |
+
s_faces = app2.get(src)
|
| 45 |
+
if len(t_faces) == 0:
|
| 46 |
+
t_faces = app2.get(tgt)
|
| 47 |
+
if len(s_faces) == 0 or len(t_faces) == 0:
|
| 48 |
+
raise gr.Error("no faces detected s:%d t:%d" % (len(s_faces), len(t_faces)))
|
|
|
|
| 49 |
res = swapper.get(tgt, t_faces[0], s_faces[0], paste_back=True)
|
| 50 |
+
out = "/tmp/swapped.jpg"
|
| 51 |
+
cv2.imwrite(out, res)
|
| 52 |
return out
|
| 53 |
+
except gr.Error:
|
| 54 |
+
raise
|
| 55 |
+
except Exception:
|
| 56 |
+
raise gr.Error(traceback.format_exc()[-1500:])
|
| 57 |
+
|
| 58 |
+
|
| 59 |
with gr.Blocks() as demo:
|
| 60 |
gr.Markdown("# FaceFusion Zero")
|
| 61 |
with gr.Row():
|
| 62 |
+
src = gr.Image(label="Source")
|
| 63 |
+
tgt = gr.Image(label="Target")
|
| 64 |
+
out = gr.Image(label="Result")
|
| 65 |
btn = gr.Button("Swap")
|
| 66 |
btn.click(swap_face, inputs=[src, tgt], outputs=out)
|
| 67 |
+
|
| 68 |
demo.launch()
|