File size: 3,065 Bytes
9245934
 
 
 
 
1b6f855
 
9245934
 
 
 
bb968da
9245934
 
bb968da
9245934
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f50a729
d7ae51b
 
 
1b6f855
 
 
 
 
 
d7ae51b
 
c4757ef
9245934
bb968da
 
9245934
 
 
 
bb968da
 
9245934
91ad607
9245934
 
91ad607
9245934
 
 
 
 
 
 
 
 
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
import gradio as gr
from refacer import Refacer
import ngrok
import time
import threading
from gradio.themes.utils.theme_dropdown import create_theme_dropdown # noqa: F401
import json

# Initialize Refacer
def initialize_refacer():
    global refacer
    print("💊Initializing Refacer...")
    start_time = time.time()
    refacer = Refacer(force_cpu=False, colab_performance=False)
    print(f"💚Refacer initialized in {time.time() - start_time:.2f} seconds")

# Ngrok connection
def connect(token, port, options):
    account = None
    if token:
        if ':' in token:
            # token = authtoken:username:password
            token, username, password = token.split(':', 2)
            account = f"{username}:{password}"

    if not options.get('authtoken_from_env'):
        options['authtoken'] = token
    if account:
        options['basic_auth'] = account

    try:
        public_url = ngrok.connect(f"127.0.0.1:{port}", **options).url()
        print(f'ngrok connected to localhost:{port}! URL: {public_url}')
    except Exception as e:
        print(f'ngrok connection aborted: {e}')

# Run reface
def run(*vars):
    video_path = vars[0]
    origins = vars[1:(num_faces + 1)]
    destinations = vars[(num_faces + 1):(num_faces * 2) + 1]
    thresholds = vars[(num_faces * 2) + 1:]

    faces = []
    for k in range(num_faces):
        if origins[k] and destinations[k]:
            faces.append({
                'origin': origins[k],
                'destination': destinations[k],
                'threshold': thresholds[k]
            })

    return refacer.reface(video_path, faces)

# UI setup
origin = []
destination = []
thresholds = []
num_faces = 5

from gradio.themes.utils.theme_dropdown import create_theme_dropdown # noqa: F401

dropdown, js = create_theme_dropdown()


theme_path = 'themes/[email protected]'
with open(theme_path, 'r') as theme_file:
    custom_theme = json.load(theme_file)

with gr.Blocks(theme=custom_theme) as demo:
    with gr.Row(equal_height=True):
        with gr.Column(scale=10):
            gr.Markdown("# 🧸Refacer")
    with gr.Row():
        video = gr.Video(label="👑Original video", format="mp4")
        video2 = gr.Video(label="💎Refaced video", interactive=False, format="mp4")

    for i in range(num_faces):
        with gr.Tab(f"Face #{i + 1}"):
            with gr.Row():
                origin.append(gr.Image(label="🧲Face to replace"))
                destination.append(gr.Image(label="📥Destination face"))
            with gr.Row():
                thresholds.append(gr.Slider(label="Threshold", minimum=0.0, maximum=1.0, value=0.2))

    with gr.Row():
        button = gr.Button("💽Reface", variant="primary")

    button.click(fn=run, inputs=[video] + origin + destination + thresholds, outputs=[video2])

# Start initialization in a separate thread to prevent blocking
init_thread = threading.Thread(target=initialize_refacer)
init_thread.start()

# Launch demo
demo.queue().launch(show_error=True, share=True, server_name="0.0.0.0", server_port=7860)