Files
LC_NeoRefacer/app.py

57 lines
2.0 KiB
Python
Raw Normal View History

2023-06-05 08:27:14 +02:00
import gradio as gr
from refacer import Refacer
import argparse
2023-06-03 08:04:06 +02:00
parser = argparse.ArgumentParser(description='Refacer')
parser.add_argument("--max_num_faces", help="Max number of faces on UI", default=5)
parser.add_argument("--force_cpu", help="Force CPU mode", default=False,action="store_true")
parser.add_argument("--share_gradio", help="Share Gradio", default=False,action="store_true")
parser.add_argument("--colab_performance", help="Use in colab for better performance", default=False,action="store_true")
args = parser.parse_args()
2023-06-05 08:27:14 +02:00
refacer = Refacer(force_cpu=args.force_cpu,colab_performance=args.colab_performance)
2023-06-05 08:27:14 +02:00
num_faces=args.max_num_faces
2023-06-05 08:27:14 +02:00
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:]
2023-06-05 08:27:14 +02:00
faces = []
for k in range(0,num_faces):
2023-06-05 08:27:14 +02:00
if origins[k] is not None and destinations[k] is not None:
faces.append({
'origin':origins[k],
'destination':destinations[k],
'threshold':thresholds[k]
})
return refacer.reface(video_path,faces)
origin = []
destination = []
thresholds = []
with gr.Blocks() as demo:
with gr.Row():
gr.Markdown("# Refacer")
with gr.Row():
video=gr.Video(label="Original video",format="mp4")
video2=gr.Video(label="Refaced video",interactive=False,format="mp4")
2023-06-05 08:27:14 +02:00
for i in range(0,num_faces):
2023-06-05 08:27:14 +02:00
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])
#demo.launch(share=True,server_name="0.0.0.0", show_error=True)
demo.queue().launch(show_error=True,share=args.share_gradio)