Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- README.md +16 -3
- app.py +94 -78
- reference.png +0 -0
- requirements.txt +7 -5
README.md
CHANGED
|
@@ -1,6 +1,19 @@
|
|
| 1 |
---
|
| 2 |
-
title: Face Swap
|
|
|
|
|
|
|
|
|
|
| 3 |
sdk: gradio
|
| 4 |
-
|
| 5 |
-
|
|
|
|
|
|
|
| 6 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Face Swap
|
| 3 |
+
emoji: 🎭
|
| 4 |
+
colorFrom: red
|
| 5 |
+
colorTo: indigo
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 4.44.0
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
license: mit
|
| 11 |
---
|
| 12 |
+
|
| 13 |
+
# Face Swap — InsightFace inswapper_128
|
| 14 |
+
|
| 15 |
+
Upload or snap a webcam photo of your face, click **Swap**, and it replaces the face in the target image (default: a Denmark fan). Runs on the free CPU tier.
|
| 16 |
+
|
| 17 |
+
Models:
|
| 18 |
+
- Detection + embedding: `buffalo_l` (auto-downloaded by insightface)
|
| 19 |
+
- Swap: `inswapper_128.onnx` (pulled from `ezioruan/inswapper_128.onnx`)
|
app.py
CHANGED
|
@@ -1,82 +1,98 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
with gr.Blocks() as demo:
|
| 74 |
-
gr.Markdown("# Face Swap (HF Spaces / Python)\nアップロードした2枚の画像から顔を検出し、target側にsource顔を当て込みます。")
|
| 75 |
with gr.Row():
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
-
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Hugging Face Space — Face Swap with InsightFace inswapper_128.
|
| 3 |
+
Gradio app: webcam or upload a source face, swap it onto a target image.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import os
|
| 7 |
import cv2
|
| 8 |
import numpy as np
|
| 9 |
+
import gradio as gr
|
| 10 |
+
import insightface
|
| 11 |
+
from insightface.app import FaceAnalysis
|
| 12 |
+
from huggingface_hub import hf_hub_download
|
| 13 |
+
|
| 14 |
+
HERE = os.path.dirname(os.path.abspath(__file__))
|
| 15 |
+
DEFAULT_TARGET = os.path.join(HERE, "reference.png")
|
| 16 |
+
|
| 17 |
+
print("[boot] loading FaceAnalysis (buffalo_l)…", flush=True)
|
| 18 |
+
face_app = FaceAnalysis(name="buffalo_l", providers=["CPUExecutionProvider"])
|
| 19 |
+
face_app.prepare(ctx_id=0, det_size=(640, 640))
|
| 20 |
+
|
| 21 |
+
print("[boot] downloading inswapper_128…", flush=True)
|
| 22 |
+
MODEL_PATH = hf_hub_download(
|
| 23 |
+
repo_id="ezioruan/inswapper_128.onnx",
|
| 24 |
+
filename="inswapper_128.onnx",
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
print("[boot] loading inswapper_128…", flush=True)
|
| 28 |
+
swapper = insightface.model_zoo.get_model(
|
| 29 |
+
MODEL_PATH, providers=["CPUExecutionProvider"]
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
print("[boot] ready", flush=True)
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def largest_face(faces):
|
| 36 |
+
return max(
|
| 37 |
+
faces,
|
| 38 |
+
key=lambda f: (f.bbox[2] - f.bbox[0]) * (f.bbox[3] - f.bbox[1]),
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def swap(source_img, target_img):
|
| 43 |
+
if source_img is None:
|
| 44 |
+
return None, "Please provide a source face (webcam or upload)."
|
| 45 |
+
|
| 46 |
+
# Gradio gives RGB numpy — convert to BGR for OpenCV/InsightFace
|
| 47 |
+
source_bgr = cv2.cvtColor(source_img, cv2.COLOR_RGB2BGR)
|
| 48 |
+
|
| 49 |
+
if target_img is None:
|
| 50 |
+
target_bgr = cv2.imread(DEFAULT_TARGET)
|
| 51 |
+
if target_bgr is None:
|
| 52 |
+
return None, "Default target image is missing."
|
| 53 |
+
else:
|
| 54 |
+
target_bgr = cv2.cvtColor(target_img, cv2.COLOR_RGB2BGR)
|
| 55 |
+
|
| 56 |
+
source_faces = face_app.get(source_bgr)
|
| 57 |
+
if not source_faces:
|
| 58 |
+
return None, "No face detected in source image."
|
| 59 |
+
src_face = largest_face(source_faces)
|
| 60 |
+
|
| 61 |
+
target_faces = face_app.get(target_bgr)
|
| 62 |
+
if not target_faces:
|
| 63 |
+
return None, "No face detected in target image."
|
| 64 |
+
tgt_face = largest_face(target_faces)
|
| 65 |
+
|
| 66 |
+
result = swapper.get(target_bgr, tgt_face, src_face, paste_back=True)
|
| 67 |
+
return cv2.cvtColor(result, cv2.COLOR_BGR2RGB), "Done."
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
with gr.Blocks(title="Face Swap — InsightFace") as demo:
|
| 71 |
+
gr.Markdown(
|
| 72 |
+
"# Face Swap — InsightFace `inswapper_128`\n"
|
| 73 |
+
"Take a webcam photo (or upload), click **Swap**. "
|
| 74 |
+
"Leave the target empty to use the default Denmark fan, "
|
| 75 |
+
"or upload your own target image."
|
| 76 |
+
)
|
|
|
|
|
|
|
|
|
|
| 77 |
with gr.Row():
|
| 78 |
+
with gr.Column():
|
| 79 |
+
src_in = gr.Image(
|
| 80 |
+
sources=["webcam", "upload"],
|
| 81 |
+
type="numpy",
|
| 82 |
+
label="Your face (webcam or upload)",
|
| 83 |
+
)
|
| 84 |
+
tgt_in = gr.Image(
|
| 85 |
+
sources=["upload"],
|
| 86 |
+
type="numpy",
|
| 87 |
+
label="Target image (optional — defaults to reference.png)",
|
| 88 |
+
value=DEFAULT_TARGET,
|
| 89 |
+
)
|
| 90 |
+
btn = gr.Button("Swap", variant="primary")
|
| 91 |
+
with gr.Column():
|
| 92 |
+
out_img = gr.Image(label="Result", type="numpy")
|
| 93 |
+
status = gr.Textbox(label="Status", interactive=False)
|
| 94 |
+
|
| 95 |
+
btn.click(swap, inputs=[src_in, tgt_in], outputs=[out_img, status])
|
| 96 |
|
| 97 |
+
if __name__ == "__main__":
|
| 98 |
+
demo.launch()
|
reference.png
ADDED
|
requirements.txt
CHANGED
|
@@ -1,5 +1,7 @@
|
|
| 1 |
-
gradio==
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==4.44.0
|
| 2 |
+
insightface==0.7.3
|
| 3 |
+
onnxruntime==1.18.1
|
| 4 |
+
opencv-python-headless==4.10.0.84
|
| 5 |
+
numpy<2
|
| 6 |
+
huggingface_hub>=0.24.0
|
| 7 |
+
cython
|