Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,67 +2,66 @@ import gradio as gr
|
|
| 2 |
from refacer import Refacer
|
| 3 |
import argparse
|
| 4 |
import ngrok
|
|
|
|
| 5 |
|
|
|
|
| 6 |
parser = argparse.ArgumentParser(description='Refacer')
|
| 7 |
parser.add_argument("--max_num_faces", type=int, help="Max number of faces on UI", default=5)
|
| 8 |
parser.add_argument("--force_cpu", help="Force CPU mode", default=False, action="store_true")
|
| 9 |
parser.add_argument("--share_gradio", help="Share Gradio", default=False, action="store_true")
|
| 10 |
parser.add_argument("--server_name", type=str, help="Server IP address", default="127.0.0.1")
|
| 11 |
parser.add_argument("--server_port", type=int, help="Server port", default=7860)
|
| 12 |
-
parser.add_argument("--colab_performance", help="Use in colab for better performance", default=False,action="store_true")
|
| 13 |
parser.add_argument("--ngrok", type=str, help="Use ngrok", default=None)
|
| 14 |
parser.add_argument("--ngrok_region", type=str, help="ngrok region", default="us")
|
| 15 |
args = parser.parse_args()
|
| 16 |
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
# Connect to ngrok for ingress
|
| 22 |
def connect(token, port, options):
|
| 23 |
account = None
|
| 24 |
-
if token
|
| 25 |
-
token = 'None'
|
| 26 |
-
else:
|
| 27 |
if ':' in token:
|
| 28 |
# token = authtoken:username:password
|
| 29 |
token, username, password = token.split(':', 2)
|
| 30 |
account = f"{username}:{password}"
|
| 31 |
|
| 32 |
-
# For all options see: https://github.com/ngrok/ngrok-py/blob/main/examples/ngrok-connect-full.py
|
| 33 |
if not options.get('authtoken_from_env'):
|
| 34 |
options['authtoken'] = token
|
| 35 |
if account:
|
| 36 |
options['basic_auth'] = account
|
| 37 |
|
| 38 |
-
|
| 39 |
try:
|
| 40 |
public_url = ngrok.connect(f"127.0.0.1:{port}", **options).url()
|
|
|
|
| 41 |
except Exception as e:
|
| 42 |
-
print(f'
|
| 43 |
-
f'Your token: {token}, get the right one on https://dashboard.ngrok.com/get-started/your-authtoken')
|
| 44 |
-
else:
|
| 45 |
-
print(f'ngrok connected to localhost:{port}! URL: {public_url}\n'
|
| 46 |
-
'You can use this link after the launch is complete.')
|
| 47 |
-
|
| 48 |
|
|
|
|
| 49 |
def run(*vars):
|
| 50 |
-
video_path=vars[0]
|
| 51 |
-
origins=vars[1:(num_faces+1)]
|
| 52 |
-
destinations=vars[(num_faces+1):(num_faces*2)+1]
|
| 53 |
-
thresholds=vars[(num_faces*2)+1:]
|
| 54 |
|
| 55 |
faces = []
|
| 56 |
-
for k in range(
|
| 57 |
-
if origins[k]
|
| 58 |
faces.append({
|
| 59 |
-
'origin':origins[k],
|
| 60 |
-
'destination':destinations[k],
|
| 61 |
-
'threshold':thresholds[k]
|
| 62 |
})
|
| 63 |
|
| 64 |
-
return refacer.reface(video_path,faces)
|
| 65 |
|
|
|
|
| 66 |
origin = []
|
| 67 |
destination = []
|
| 68 |
thresholds = []
|
|
@@ -71,23 +70,24 @@ with gr.Blocks() as demo:
|
|
| 71 |
with gr.Row():
|
| 72 |
gr.Markdown("# Refacer")
|
| 73 |
with gr.Row():
|
| 74 |
-
video=gr.Video(label="Original video",format="mp4")
|
| 75 |
-
video2=gr.Video(label="Refaced video",interactive=False,format="mp4")
|
| 76 |
|
| 77 |
-
for i in range(
|
| 78 |
-
with gr.Tab(f"Face #{i+1}"):
|
| 79 |
with gr.Row():
|
| 80 |
origin.append(gr.Image(label="Face to replace"))
|
| 81 |
destination.append(gr.Image(label="Destination face"))
|
| 82 |
with gr.Row():
|
| 83 |
-
thresholds.append(gr.Slider(label="Threshold",minimum=0.0,maximum=1.0,value=0.2))
|
|
|
|
| 84 |
with gr.Row():
|
| 85 |
-
button=gr.Button("Reface", variant="primary")
|
|
|
|
|
|
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
if args.ngrok is not None:
|
| 90 |
connect(args.ngrok, args.server_port, {'region': args.ngrok_region, 'authtoken_from_env': False})
|
| 91 |
-
|
| 92 |
-
#demo
|
| 93 |
-
demo.queue().launch(show_error=True,share=args.share_gradio,server_name=args.server_name,server_port=args.server_port)
|
|
|
|
| 2 |
from refacer import Refacer
|
| 3 |
import argparse
|
| 4 |
import ngrok
|
| 5 |
+
import time
|
| 6 |
|
| 7 |
+
# Argument parser
|
| 8 |
parser = argparse.ArgumentParser(description='Refacer')
|
| 9 |
parser.add_argument("--max_num_faces", type=int, help="Max number of faces on UI", default=5)
|
| 10 |
parser.add_argument("--force_cpu", help="Force CPU mode", default=False, action="store_true")
|
| 11 |
parser.add_argument("--share_gradio", help="Share Gradio", default=False, action="store_true")
|
| 12 |
parser.add_argument("--server_name", type=str, help="Server IP address", default="127.0.0.1")
|
| 13 |
parser.add_argument("--server_port", type=int, help="Server port", default=7860)
|
| 14 |
+
parser.add_argument("--colab_performance", help="Use in colab for better performance", default=False, action="store_true")
|
| 15 |
parser.add_argument("--ngrok", type=str, help="Use ngrok", default=None)
|
| 16 |
parser.add_argument("--ngrok_region", type=str, help="ngrok region", default="us")
|
| 17 |
args = parser.parse_args()
|
| 18 |
|
| 19 |
+
# Initialize Refacer
|
| 20 |
+
print("Initializing Refacer...")
|
| 21 |
+
start_time = time.time()
|
| 22 |
+
refacer = Refacer(force_cpu=args.force_cpu, colab_performance=args.colab_performance)
|
| 23 |
+
num_faces = args.max_num_faces
|
| 24 |
+
print(f"Refacer initialized in {time.time() - start_time:.2f} seconds")
|
| 25 |
|
| 26 |
+
# Ngrok connection
|
|
|
|
|
|
|
| 27 |
def connect(token, port, options):
|
| 28 |
account = None
|
| 29 |
+
if token:
|
|
|
|
|
|
|
| 30 |
if ':' in token:
|
| 31 |
# token = authtoken:username:password
|
| 32 |
token, username, password = token.split(':', 2)
|
| 33 |
account = f"{username}:{password}"
|
| 34 |
|
|
|
|
| 35 |
if not options.get('authtoken_from_env'):
|
| 36 |
options['authtoken'] = token
|
| 37 |
if account:
|
| 38 |
options['basic_auth'] = account
|
| 39 |
|
|
|
|
| 40 |
try:
|
| 41 |
public_url = ngrok.connect(f"127.0.0.1:{port}", **options).url()
|
| 42 |
+
print(f'ngrok connected to localhost:{port}! URL: {public_url}')
|
| 43 |
except Exception as e:
|
| 44 |
+
print(f'ngrok connection aborted: {e}')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
# Run reface
|
| 47 |
def run(*vars):
|
| 48 |
+
video_path = vars[0]
|
| 49 |
+
origins = vars[1:(num_faces + 1)]
|
| 50 |
+
destinations = vars[(num_faces + 1):(num_faces * 2) + 1]
|
| 51 |
+
thresholds = vars[(num_faces * 2) + 1:]
|
| 52 |
|
| 53 |
faces = []
|
| 54 |
+
for k in range(num_faces):
|
| 55 |
+
if origins[k] and destinations[k]:
|
| 56 |
faces.append({
|
| 57 |
+
'origin': origins[k],
|
| 58 |
+
'destination': destinations[k],
|
| 59 |
+
'threshold': thresholds[k]
|
| 60 |
})
|
| 61 |
|
| 62 |
+
return refacer.reface(video_path, faces)
|
| 63 |
|
| 64 |
+
# UI setup
|
| 65 |
origin = []
|
| 66 |
destination = []
|
| 67 |
thresholds = []
|
|
|
|
| 70 |
with gr.Row():
|
| 71 |
gr.Markdown("# Refacer")
|
| 72 |
with gr.Row():
|
| 73 |
+
video = gr.Video(label="Original video", format="mp4")
|
| 74 |
+
video2 = gr.Video(label="Refaced video", interactive=False, format="mp4")
|
| 75 |
|
| 76 |
+
for i in range(num_faces):
|
| 77 |
+
with gr.Tab(f"Face #{i + 1}"):
|
| 78 |
with gr.Row():
|
| 79 |
origin.append(gr.Image(label="Face to replace"))
|
| 80 |
destination.append(gr.Image(label="Destination face"))
|
| 81 |
with gr.Row():
|
| 82 |
+
thresholds.append(gr.Slider(label="Threshold", minimum=0.0, maximum=1.0, value=0.2))
|
| 83 |
+
|
| 84 |
with gr.Row():
|
| 85 |
+
button = gr.Button("Reface", variant="primary")
|
| 86 |
+
|
| 87 |
+
button.click(fn=run, inputs=[video] + origin + destination + thresholds, outputs=[video2])
|
| 88 |
|
| 89 |
+
if args.ngrok:
|
|
|
|
|
|
|
| 90 |
connect(args.ngrok, args.server_port, {'region': args.ngrok_region, 'authtoken_from_env': False})
|
| 91 |
+
|
| 92 |
+
# Launch demo
|
| 93 |
+
demo.queue().launch(show_error=True, share=args.share_gradio, server_name=args.server_name, server_port=args.server_port)
|