Spaces:
Sleeping
Sleeping
revert to working light
Browse files- app.py +7 -18
- requirements.txt +0 -4
app.py
CHANGED
|
@@ -2,7 +2,6 @@ import spaces
|
|
| 2 |
import gradio as gr
|
| 3 |
import cv2
|
| 4 |
import traceback
|
| 5 |
-
|
| 6 |
@spaces.GPU
|
| 7 |
def swap_face(source_img, target_img):
|
| 8 |
import insightface
|
|
@@ -22,39 +21,29 @@ def swap_face(source_img, target_img):
|
|
| 22 |
if len(s_faces)==0 or len(t_faces)==0:
|
| 23 |
app2 = FaceAnalysis(name='buffalo_l')
|
| 24 |
app2.prepare(ctx_id=-1, det_size=(1024,1024))
|
| 25 |
-
if len(s_faces)==0:
|
| 26 |
-
|
| 27 |
-
if len(t_faces)==0:
|
| 28 |
-
t_faces = app2.get(tgt)
|
| 29 |
if len(s_faces)==0 or len(t_faces)==0:
|
| 30 |
cv2.putText(tgt, f"s:{len(s_faces)} t:{len(t_faces)}", (20,40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255),2)
|
| 31 |
out="/tmp/debug.jpg"
|
| 32 |
cv2.imwrite(out,tgt)
|
| 33 |
return out
|
| 34 |
res = swapper.get(tgt, t_faces[0], s_faces[0], paste_back=True)
|
| 35 |
-
|
| 36 |
-
try:
|
| 37 |
-
from gfpgan import GFPGANer
|
| 38 |
-
enhancer = GFPGANer(model_path='https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.3.pth', upscale=1, arch='clean', channel_multiplier=2, bg_upsampler=None)
|
| 39 |
-
_, _, res = enhancer.enhance(res, has_aligned=False, only_center_face=False, paste_back=True)
|
| 40 |
-
except Exception as e:
|
| 41 |
-
print("gfpgan fail",e, flush=True)
|
| 42 |
-
out="/tmp/swapped_gfpgan.jpg"
|
| 43 |
cv2.imwrite(out,res)
|
| 44 |
return out
|
| 45 |
except Exception as e:
|
| 46 |
tgt2=cv2.imread(target_img)
|
| 47 |
-
cv2.putText(tgt2, str(e)[:
|
| 48 |
out="/tmp/err.jpg"
|
| 49 |
cv2.imwrite(out,tgt2)
|
| 50 |
return out
|
| 51 |
-
|
| 52 |
with gr.Blocks() as demo:
|
| 53 |
-
gr.Markdown("# FaceFusion Zero
|
| 54 |
with gr.Row():
|
| 55 |
src = gr.Image(type="filepath", label="Source")
|
| 56 |
tgt = gr.Image(type="filepath", label="Target")
|
| 57 |
-
out = gr.Image(type="filepath", label="
|
| 58 |
-
btn = gr.Button("Swap
|
| 59 |
btn.click(swap_face, inputs=[src, tgt], outputs=out)
|
| 60 |
demo.launch()
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import cv2
|
| 4 |
import traceback
|
|
|
|
| 5 |
@spaces.GPU
|
| 6 |
def swap_face(source_img, target_img):
|
| 7 |
import insightface
|
|
|
|
| 21 |
if len(s_faces)==0 or len(t_faces)==0:
|
| 22 |
app2 = FaceAnalysis(name='buffalo_l')
|
| 23 |
app2.prepare(ctx_id=-1, det_size=(1024,1024))
|
| 24 |
+
if len(s_faces)==0: s_faces = app2.get(src)
|
| 25 |
+
if len(t_faces)==0: t_faces = app2.get(tgt)
|
|
|
|
|
|
|
| 26 |
if len(s_faces)==0 or len(t_faces)==0:
|
| 27 |
cv2.putText(tgt, f"s:{len(s_faces)} t:{len(t_faces)}", (20,40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255),2)
|
| 28 |
out="/tmp/debug.jpg"
|
| 29 |
cv2.imwrite(out,tgt)
|
| 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 Exception as e:
|
| 36 |
tgt2=cv2.imread(target_img)
|
| 37 |
+
cv2.putText(tgt2, str(e)[:50], (20,40), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,0,255),2)
|
| 38 |
out="/tmp/err.jpg"
|
| 39 |
cv2.imwrite(out,tgt2)
|
| 40 |
return out
|
|
|
|
| 41 |
with gr.Blocks() as demo:
|
| 42 |
+
gr.Markdown("# FaceFusion Zero")
|
| 43 |
with gr.Row():
|
| 44 |
src = gr.Image(type="filepath", label="Source")
|
| 45 |
tgt = gr.Image(type="filepath", label="Target")
|
| 46 |
+
out = gr.Image(type="filepath", label="Result")
|
| 47 |
+
btn = gr.Button("Swap")
|
| 48 |
btn.click(swap_face, inputs=[src, tgt], outputs=out)
|
| 49 |
demo.launch()
|
requirements.txt
CHANGED
|
@@ -3,9 +3,5 @@ spaces
|
|
| 3 |
insightface==0.7.3
|
| 4 |
onnxruntime-gpu
|
| 5 |
opencv-python==4.8.0.76
|
| 6 |
-
gfpgan==1.3.8
|
| 7 |
-
basicsr==1.4.2
|
| 8 |
-
facexlib==0.3.0
|
| 9 |
-
realesrgan==0.3.0
|
| 10 |
huggingface_hub
|
| 11 |
hf_transfer
|
|
|
|
| 3 |
insightface==0.7.3
|
| 4 |
onnxruntime-gpu
|
| 5 |
opencv-python==4.8.0.76
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
huggingface_hub
|
| 7 |
hf_transfer
|