Initial face swap app with TinyFace + Gradio
Browse files- README.md +4 -6
- app.py +45 -0
- requirements.txt +3 -0
README.md
CHANGED
|
@@ -1,12 +1,10 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 6.13.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
-
|
| 12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Face Swap
|
| 3 |
+
emoji: 🔄
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: purple
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 6.13.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
|
|
|
|
|
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import numpy as np
|
| 4 |
+
from tinyface import TinyFace
|
| 5 |
+
|
| 6 |
+
# Initialize TinyFace once at startup
|
| 7 |
+
tinyface = TinyFace()
|
| 8 |
+
tinyface.prepare()
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def swap_face(original_image: np.ndarray, reference_image: np.ndarray) -> np.ndarray:
|
| 12 |
+
# Gradio provides RGB numpy arrays; TinyFace/OpenCV expects BGR
|
| 13 |
+
original_bgr = cv2.cvtColor(original_image, cv2.COLOR_RGB2BGR)
|
| 14 |
+
reference_bgr = cv2.cvtColor(reference_image, cv2.COLOR_RGB2BGR)
|
| 15 |
+
|
| 16 |
+
# Detect the face in the original image (used to identify which face to replace)
|
| 17 |
+
source_face = tinyface.get_one_face(original_bgr)
|
| 18 |
+
if source_face is None:
|
| 19 |
+
raise gr.Error("No face detected in the original image.")
|
| 20 |
+
|
| 21 |
+
# Detect the new face to apply (from the reference image)
|
| 22 |
+
new_face = tinyface.get_one_face(reference_bgr)
|
| 23 |
+
if new_face is None:
|
| 24 |
+
raise gr.Error("No face detected in the reference image.")
|
| 25 |
+
|
| 26 |
+
# swap_face(image, reference_face=face to match, destination_face=new face to apply)
|
| 27 |
+
result_bgr = tinyface.swap_face(original_bgr, source_face, new_face)
|
| 28 |
+
|
| 29 |
+
# Convert back to RGB for Gradio
|
| 30 |
+
return cv2.cvtColor(result_bgr, cv2.COLOR_BGR2RGB)
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
demo = gr.Interface(
|
| 34 |
+
fn=swap_face,
|
| 35 |
+
inputs=[
|
| 36 |
+
gr.Image(label="Original Image (face to replace)", type="numpy"),
|
| 37 |
+
gr.Image(label="Reference Image (new face to apply)", type="numpy"),
|
| 38 |
+
],
|
| 39 |
+
outputs=gr.Image(label="Result"),
|
| 40 |
+
title="Face Swap",
|
| 41 |
+
description="Upload an original image and a reference image. The face in the original image will be replaced with the face from the reference image.",
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
if __name__ == "__main__":
|
| 45 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
tinyface
|
| 2 |
+
gradio
|
| 3 |
+
opencv-python
|