File size: 4,716 Bytes
d12923a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83d8d3c
 
d12923a
 
83d8d3c
 
d12923a
83d8d3c
d12923a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83d8d3c
 
d12923a
83d8d3c
 
d12923a
 
83d8d3c
d12923a
83d8d3c
d12923a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83d8d3c
d12923a
 
 
 
 
 
 
 
 
83d8d3c
d12923a
 
 
 
 
 
 
 
 
 
1
2
3
4
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#######################################################################################
#
# MIT License
#
# Copyright (c) [2025] [leonelhs@gmail.com]
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
#######################################################################################
#
# Source code is based on or inspired by several projects.
# For more details and proper attribution, please refer to the following resources:
#
# - [hyxue] - [https://huggingface.co/spaces/hyxue/HiFiFace-inference-demo]
# - [maum-ai] [https://github.com/maum-ai/hififace]
#

import gradio as gr
import torch
from huggingface_hub import hf_hub_download

from benchmark.app_image import ImageSwap
from models.model import HifiFaceST, HifiFaceWGM

REPO_ID = "leonelhs/HiFiFace"

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

gen_st_path = hf_hub_download(repo_id=REPO_ID,
                              filename="hififace_pretrained/standard_model/generator_320000.pth")

gen_wgm_path = hf_hub_download(repo_id=REPO_ID,
                               filename="hififace_pretrained/with_gaze_and_mouth/generator_190000.pth")

fade_detector_path = hf_hub_download(repo_id=REPO_ID,
                                     filename="face_detector/face_detector_scrfd_10g_bnkps.onnx")

identity_extractor_config = {
    "f_3d_checkpoint_path": hf_hub_download(repo_id=REPO_ID, filename="Deep3DFaceRecon/epoch_20.pth"),
    "f_id_checkpoint_path": hf_hub_download(repo_id=REPO_ID, filename="arcface/ms1mv3_arcface_r100_fp16_backbone.pth")
}

class ConfigPath:
    face_detector_weights = fade_detector_path
    model_path = ""
    model_idx = 80000
    ffmpeg_device = device
    device = device

cfg = ConfigPath()

model_standard = HifiFaceST(identity_extractor_config, device=device, generator_path=gen_st_path)

model_wgm = HifiFaceWGM(identity_extractor_config, device=device, generator_path=gen_wgm_path)

image_infer_standard = ImageSwap(cfg, model_standard)
image_infer_wgm = ImageSwap(cfg, model_wgm)

MODELS = {
    "Standard model": "standard",
    "Eye and mouth hm loss": "eyeandmouth",
}

def inference_image(source_face, target_face, method="standard", shape_rate=1.0, id_rate=1.0, iterations=1):
    if method == "standard":
        return target_face, image_infer_standard.inference(source_face, target_face, shape_rate, id_rate, int(iterations))
    return target_face, image_infer_wgm.inference(source_face, target_face, shape_rate, id_rate, int(iterations))


with gr.Blocks(title="FaceSwap") as app:
    gr.Markdown("## HiFiFace image swap")
    with gr.Row():
        with gr.Column(scale=1):
            with gr.Row():
                source_image = gr.Image(type="numpy", label="Face image")
                target_image = gr.Image(type="numpy", label="Body image")
            mod = gr.Dropdown(choices=list(MODELS.items()), label="Model generator", value="standard")
            image_btn = gr.Button("Swap image")
            with gr.Accordion("Fine tunes", open=False):
                structure_sim = gr.Slider(minimum=0.0, maximum=1.0, value=1.0, step=0.1, label="3d similarity")
                id_sim = gr.Slider(minimum=0.0, maximum=1.0, value=1.0, step=0.1, label="id similarity")
                iters = gr.Slider(minimum=1, maximum=10, value=1, step=1, label="iters")
        with gr.Column(scale=1):
            with gr.Row():
                output_image = gr.ImageSlider(label="Swapped image", type="pil")

    image_btn.click(
        fn=inference_image,
        inputs=[source_image, target_image, mod, structure_sim, id_sim, iters],
        outputs=output_image,
    )

app.launch(share=False, debug=True, show_error=True, mcp_server=True, pwa=True)
app.queue()