Update app.py
Browse files
app.py
CHANGED
|
@@ -1,55 +1,70 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import cv2
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
-
from
|
| 5 |
-
|
| 6 |
from gfpgan import GFPGANer
|
| 7 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
# Initialize
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
swapper = INSwapper(model_file=swapper_model_path)
|
| 18 |
|
| 19 |
-
|
| 20 |
-
_, _, output = gfpganer.enhance(img_np, has_aligned=False, only_center_face=False, paste_back=True)
|
| 21 |
-
return output
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
|
| 40 |
-
|
| 41 |
-
result = swap_faces(source, target)
|
| 42 |
-
return result
|
| 43 |
|
|
|
|
| 44 |
iface = gr.Interface(
|
| 45 |
-
fn=
|
| 46 |
inputs=[
|
| 47 |
-
gr.Image(
|
| 48 |
-
gr.Image(label="Target Image
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
],
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
description="Upload a source face and a target image. The app swaps the face and enhances it using GFPGAN."
|
| 53 |
)
|
| 54 |
|
| 55 |
-
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
import cv2
|
| 4 |
+
import torch
|
| 5 |
import numpy as np
|
| 6 |
+
from PIL import Image
|
| 7 |
+
|
| 8 |
from gfpgan import GFPGANer
|
| 9 |
+
from insightface.app import FaceAnalysis
|
| 10 |
+
from insightface.model_zoo import model_zoo
|
| 11 |
+
|
| 12 |
+
# Paths
|
| 13 |
+
MODEL_DIR = "model"
|
| 14 |
+
GFPGAN_MODEL_PATH = os.path.join(MODEL_DIR, "GFPGANv1.4.pth")
|
| 15 |
+
INSWAPPER_PATH = os.path.join(MODEL_DIR, "inswapper_128.onnx")
|
| 16 |
|
| 17 |
+
# Initialize GFPGAN
|
| 18 |
+
gfpganer = GFPGANer(
|
| 19 |
+
model_path=GFPGAN_MODEL_PATH,
|
| 20 |
+
upscale=2,
|
| 21 |
+
arch='clean',
|
| 22 |
+
channel_multiplier=2,
|
| 23 |
+
bg_upsampler=None
|
| 24 |
+
)
|
| 25 |
|
| 26 |
+
# Initialize InsightFace (for face detection + swapping)
|
| 27 |
+
faceapp = FaceAnalysis(name='buffalo_l', providers=['CPUExecutionProvider'])
|
| 28 |
+
faceapp.prepare(ctx_id=0)
|
|
|
|
| 29 |
|
| 30 |
+
swapper = model_zoo.get_model(INSWAPPER_PATH, providers=['CPUExecutionProvider'])
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
# Function: Face Restoration + Swapping
|
| 33 |
+
def restore_and_swap(input_img: Image.Image, target_img: Image.Image):
|
| 34 |
+
input_np = np.array(input_img.convert("RGB"))
|
| 35 |
+
target_np = np.array(target_img.convert("RGB"))
|
| 36 |
|
| 37 |
+
# Step 1: Face restore
|
| 38 |
+
cropped_faces, restored_img = gfpganer.enhance(input_np, has_aligned=False, only_center_face=False, paste_back=True)
|
| 39 |
|
| 40 |
+
# Step 2: Detect face in target
|
| 41 |
+
target_faces = faceapp.get(target_np)
|
| 42 |
+
if len(target_faces) == 0:
|
| 43 |
+
return Image.fromarray(restored_img), "No face found in target image."
|
| 44 |
|
| 45 |
+
# Step 3: Detect face in restored image
|
| 46 |
+
source_faces = faceapp.get(restored_img)
|
| 47 |
+
if len(source_faces) == 0:
|
| 48 |
+
return Image.fromarray(restored_img), "No face found in restored image."
|
| 49 |
|
| 50 |
+
# Step 4: Face swap using first face
|
| 51 |
+
swapped = swapper.get(restored_img, source_faces[0], target_faces[0], paste_back=True)
|
| 52 |
|
| 53 |
+
return Image.fromarray(swapped), "Success! Face restored and swapped."
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
# Gradio Interface
|
| 56 |
iface = gr.Interface(
|
| 57 |
+
fn=restore_and_swap,
|
| 58 |
inputs=[
|
| 59 |
+
gr.Image(type="pil", label="Input Image (to restore + swap)"),
|
| 60 |
+
gr.Image(type="pil", label="Target Image (face to apply)")
|
| 61 |
+
],
|
| 62 |
+
outputs=[
|
| 63 |
+
gr.Image(type="pil", label="Output Image"),
|
| 64 |
+
gr.Textbox(label="Status")
|
| 65 |
],
|
| 66 |
+
title="GFPGAN + InsightFace Face Swapper",
|
| 67 |
+
description="This app restores old faces using GFPGAN and swaps them with another face using InsightFace InSwapper."
|
|
|
|
| 68 |
)
|
| 69 |
|
| 70 |
+
iface.launch()
|