Ii
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
from refacer import Refacer
|
|
|
3 |
import os
|
4 |
import requests
|
5 |
|
@@ -24,60 +25,69 @@ def download_model():
|
|
24 |
# Download the model when the script runs
|
25 |
download_model()
|
26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
# Initialize the Refacer class
|
28 |
-
refacer = Refacer(force_cpu=
|
29 |
|
30 |
-
|
31 |
-
num_faces = 5
|
32 |
|
33 |
# Run function for refacing video
|
34 |
-
def run(
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
38 |
|
39 |
faces = []
|
40 |
-
for
|
41 |
-
if origins[
|
42 |
faces.append({
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
})
|
47 |
|
48 |
-
#
|
49 |
-
refaced_video_path = refacer.reface(video_path, faces)
|
50 |
-
|
51 |
|
52 |
-
#
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
55 |
|
56 |
-
|
|
|
|
|
57 |
with gr.Row():
|
58 |
-
|
59 |
-
|
60 |
|
61 |
-
|
62 |
-
origins = []
|
63 |
-
destinations = []
|
64 |
-
thresholds = []
|
65 |
-
for i in range(num_faces):
|
66 |
with gr.Tab(f"Face #{i+1}"):
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
|
|
72 |
with gr.Row():
|
73 |
-
|
74 |
|
75 |
-
#
|
76 |
-
|
77 |
-
fn=run,
|
78 |
-
inputs=[input_video] + origins + destinations + thresholds,
|
79 |
-
outputs=output_video
|
80 |
-
)
|
81 |
|
82 |
# Launch the Gradio app
|
83 |
-
demo.queue().launch(share=
|
|
|
1 |
import gradio as gr
|
2 |
from refacer import Refacer
|
3 |
+
import argparse
|
4 |
import os
|
5 |
import requests
|
6 |
|
|
|
25 |
# Download the model when the script runs
|
26 |
download_model()
|
27 |
|
28 |
+
# Argument parser
|
29 |
+
parser = argparse.ArgumentParser(description='Refacer')
|
30 |
+
parser.add_argument("--max_num_faces", type=int, help="Max number of faces on UI", default=5)
|
31 |
+
parser.add_argument("--force_cpu", help="Force CPU mode", default=False, action="store_true")
|
32 |
+
parser.add_argument("--share_gradio", help="Share Gradio", default=False, action="store_true")
|
33 |
+
parser.add_argument("--server_name", type=str, help="Server IP address", default="127.0.0.1")
|
34 |
+
parser.add_argument("--server_port", type=int, help="Server port", default=7860)
|
35 |
+
parser.add_argument("--colab_performance", help="Use in colab for better performance", default=False, action="store_true")
|
36 |
+
args = parser.parse_args()
|
37 |
+
|
38 |
# Initialize the Refacer class
|
39 |
+
refacer = Refacer(force_cpu=args.force_cpu, colab_performance=args.colab_performance)
|
40 |
|
41 |
+
num_faces = args.max_num_faces
|
|
|
42 |
|
43 |
# Run function for refacing video
|
44 |
+
def run(*vars):
|
45 |
+
video_path = vars[0]
|
46 |
+
origins = vars[1:(num_faces+1)]
|
47 |
+
destinations = vars[(num_faces+1):(num_faces*2)+1]
|
48 |
+
thresholds = vars[(num_faces*2)+1:]
|
49 |
|
50 |
faces = []
|
51 |
+
for k in range(0, num_faces):
|
52 |
+
if origins[k] is not None and destinations[k] is not None:
|
53 |
faces.append({
|
54 |
+
'origin': origins[k],
|
55 |
+
'destination': destinations[k],
|
56 |
+
'threshold': thresholds[k]
|
57 |
})
|
58 |
|
59 |
+
# Call refacer to process video and get file path
|
60 |
+
refaced_video_path = refacer.reface(video_path, faces) # refaced video path
|
61 |
+
print(f"Refaced video can be found at {refaced_video_path}")
|
62 |
|
63 |
+
# Returning the video file path directly for Gradio to show it
|
64 |
+
return refaced_video_path # Gradio will handle it and show directly in the UI
|
65 |
+
|
66 |
+
# Prepare Gradio components
|
67 |
+
origin = []
|
68 |
+
destination = []
|
69 |
+
thresholds = []
|
70 |
|
71 |
+
with gr.Blocks() as demo:
|
72 |
+
with gr.Row():
|
73 |
+
gr.Markdown("# Refacer")
|
74 |
with gr.Row():
|
75 |
+
video = gr.Video(label="Original video", format="mp4")
|
76 |
+
video2 = gr.Video(label="Refaced video", interactive=False, format="mp4")
|
77 |
|
78 |
+
for i in range(0, num_faces):
|
|
|
|
|
|
|
|
|
79 |
with gr.Tab(f"Face #{i+1}"):
|
80 |
+
with gr.Row():
|
81 |
+
origin.append(gr.Image(label="Face to replace"))
|
82 |
+
destination.append(gr.Image(label="Destination face"))
|
83 |
+
with gr.Row():
|
84 |
+
thresholds.append(gr.Slider(label="Threshold", minimum=0.0, maximum=1.0, value=0.2))
|
85 |
+
|
86 |
with gr.Row():
|
87 |
+
button = gr.Button("Reface", variant="primary")
|
88 |
|
89 |
+
# Click event: Refacing the video and showing the refaced video in Gradio
|
90 |
+
button.click(fn=run, inputs=[video] + origin + destination + thresholds, outputs=[video2])
|
|
|
|
|
|
|
|
|
91 |
|
92 |
# Launch the Gradio app
|
93 |
+
demo.queue().launch(show_error=True, share=args.share_gradio, server_name="0.0.0.0", server_port=args.server_port)
|