Spaces:
Sleeping
Sleeping
debug face counts
Browse files
app.py
CHANGED
|
@@ -2,38 +2,68 @@ import spaces
|
|
| 2 |
import gradio as gr
|
| 3 |
import cv2
|
| 4 |
import numpy as np
|
|
|
|
| 5 |
|
| 6 |
@spaces.GPU
|
| 7 |
def swap_face(source_img, target_img):
|
| 8 |
import insightface
|
| 9 |
from insightface.app import FaceAnalysis
|
| 10 |
try:
|
|
|
|
| 11 |
app = FaceAnalysis(name='buffalo_l')
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
| 13 |
import insightface.model_zoo
|
| 14 |
swapper = insightface.model_zoo.get_model('inswapper_128.onnx', download=True)
|
| 15 |
if swapper is None:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
return target_img
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
except Exception as e:
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
with gr.Blocks() as demo:
|
| 31 |
-
gr.Markdown("# FaceFusion Zero (PRO)")
|
| 32 |
with gr.Row():
|
| 33 |
-
src = gr.Image(type="filepath", label="Source (
|
| 34 |
-
tgt = gr.Image(type="filepath", label="Target
|
| 35 |
-
out = gr.Image(type="filepath", label="Result")
|
| 36 |
btn = gr.Button("Swap")
|
| 37 |
btn.click(swap_face, inputs=[src, tgt], outputs=out)
|
| 38 |
-
|
| 39 |
demo.launch()
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import cv2
|
| 4 |
import numpy as np
|
| 5 |
+
import traceback
|
| 6 |
|
| 7 |
@spaces.GPU
|
| 8 |
def swap_face(source_img, target_img):
|
| 9 |
import insightface
|
| 10 |
from insightface.app import FaceAnalysis
|
| 11 |
try:
|
| 12 |
+
# Try GPU ctx 0, fallback to CPU
|
| 13 |
app = FaceAnalysis(name='buffalo_l')
|
| 14 |
+
try:
|
| 15 |
+
app.prepare(ctx_id=0, det_size=(640,640))
|
| 16 |
+
except:
|
| 17 |
+
app.prepare(ctx_id=-1, det_size=(640,640))
|
| 18 |
import insightface.model_zoo
|
| 19 |
swapper = insightface.model_zoo.get_model('inswapper_128.onnx', download=True)
|
| 20 |
if swapper is None:
|
| 21 |
+
# try hf hub download
|
| 22 |
+
from huggingface_hub import hf_hub_download
|
| 23 |
+
p = hf_hub_download(repo_id="facefusion/models", filename="inswapper_128.onnx")
|
| 24 |
+
swapper = insightface.model_zoo.get_model(p)
|
| 25 |
+
src = cv2.imread(source_img)
|
| 26 |
+
tgt = cv2.imread(target_img)
|
| 27 |
+
if src is None or tgt is None:
|
| 28 |
return target_img
|
| 29 |
+
s_faces = app.get(src)
|
| 30 |
+
t_faces = app.get(tgt)
|
| 31 |
+
# debug: if fails try larger det_size
|
| 32 |
+
if len(s_faces)==0 or len(t_faces)==0:
|
| 33 |
+
app2 = FaceAnalysis(name='buffalo_l')
|
| 34 |
+
app2.prepare(ctx_id=-1, det_size=(1024,1024))
|
| 35 |
+
if len(s_faces)==0:
|
| 36 |
+
s_faces = app2.get(src)
|
| 37 |
+
if len(t_faces)==0:
|
| 38 |
+
t_faces = app2.get(tgt)
|
| 39 |
+
if len(s_faces)==0 or len(t_faces)==0:
|
| 40 |
+
# return target with debug text
|
| 41 |
+
cv2.putText(tgt, f"s:{len(s_faces)} t:{len(t_faces)}", (20,40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255),2)
|
| 42 |
+
out="/tmp/debug.jpg"
|
| 43 |
+
cv2.imwrite(out,tgt)
|
| 44 |
+
return out
|
| 45 |
+
res = swapper.get(tgt, t_faces[0], s_faces[0], paste_back=True)
|
| 46 |
+
out="/tmp/swapped.jpg"
|
| 47 |
+
cv2.imwrite(out,res)
|
| 48 |
+
return out
|
| 49 |
except Exception as e:
|
| 50 |
+
tb=traceback.format_exc()[:500]
|
| 51 |
+
# return target with error text
|
| 52 |
+
try:
|
| 53 |
+
tgt2=cv2.imread(target_img)
|
| 54 |
+
cv2.putText(tgt2, str(e)[:40], (20,40), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0,0,255),2)
|
| 55 |
+
out="/tmp/err.jpg"
|
| 56 |
+
cv2.imwrite(out,tgt2)
|
| 57 |
+
return out
|
| 58 |
+
except:
|
| 59 |
+
return target_img
|
| 60 |
|
| 61 |
with gr.Blocks() as demo:
|
| 62 |
+
gr.Markdown("# FaceFusion Zero (PRO) - Debug")
|
| 63 |
with gr.Row():
|
| 64 |
+
src = gr.Image(type="filepath", label="Source (closeup)")
|
| 65 |
+
tgt = gr.Image(type="filepath", label="Target")
|
| 66 |
+
out = gr.Image(type="filepath", label="Result (debug shows s:t face counts)")
|
| 67 |
btn = gr.Button("Swap")
|
| 68 |
btn.click(swap_face, inputs=[src, tgt], outputs=out)
|
|
|
|
| 69 |
demo.launch()
|