Ii
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,10 @@
|
|
1 |
import gradio as gr
|
2 |
from refacer import Refacer
|
|
|
3 |
import os
|
4 |
import requests
|
5 |
|
6 |
-
#
|
7 |
model_url = "https://huggingface.co/ofter/4x-UltraSharp/resolve/main/inswapper_128.onnx"
|
8 |
model_path = "./inswapper_128.onnx"
|
9 |
|
@@ -21,72 +22,70 @@ def download_model():
|
|
21 |
else:
|
22 |
print("Model already exists.")
|
23 |
|
24 |
-
# Download the model
|
25 |
download_model()
|
26 |
|
27 |
-
#
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
#
|
31 |
-
|
32 |
-
try:
|
33 |
-
# Prepare face data
|
34 |
-
face_data = [
|
35 |
-
{
|
36 |
-
"origin": face["origin"],
|
37 |
-
"destination": face["destination"],
|
38 |
-
"threshold": face.get("threshold", 0.2),
|
39 |
-
}
|
40 |
-
for face in faces if face.get("origin") and face.get("destination")
|
41 |
-
]
|
42 |
|
43 |
-
|
44 |
-
print("Processing video...")
|
45 |
-
refaced_video_path = refacer.reface(video_path, face_data)
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
51 |
|
52 |
-
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
|
60 |
-
|
61 |
-
input_video = gr.Video(label="Upload a video", source="upload") # Corrected the keyword to 'source'
|
62 |
-
output_video = gr.Video(label="Refaced video", interactive=False)
|
63 |
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
with gr.Row():
|
69 |
-
origin = gr.Image(label="Origin Face", type="filepath")
|
70 |
-
destination = gr.Image(label="Destination Face", type="filepath")
|
71 |
-
threshold = gr.Slider(
|
72 |
-
label="Threshold", minimum=0.0, maximum=1.0, value=0.2
|
73 |
-
)
|
74 |
-
faces.append({"origin": origin, "destination": destination, "threshold": threshold})
|
75 |
|
76 |
-
|
77 |
-
|
|
|
|
|
|
|
|
|
78 |
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
|
|
|
|
|
|
86 |
|
87 |
-
|
88 |
|
89 |
# Launch the Gradio app
|
90 |
-
|
91 |
-
ui = build_ui()
|
92 |
-
ui.queue().launch(server_name="0.0.0.0", server_port=7860, debug=True)
|
|
|
1 |
import gradio as gr
|
2 |
from refacer import Refacer
|
3 |
+
import argparse
|
4 |
import os
|
5 |
import requests
|
6 |
|
7 |
+
# Hugging Face URL to download the model
|
8 |
model_url = "https://huggingface.co/ofter/4x-UltraSharp/resolve/main/inswapper_128.onnx"
|
9 |
model_path = "./inswapper_128.onnx"
|
10 |
|
|
|
22 |
else:
|
23 |
print("Model already exists.")
|
24 |
|
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 |
+
return refaced_video_path # Return the file path to show in Gradio output
|
|
|
|
|
64 |
|
65 |
+
# Prepare Gradio components
|
66 |
+
origin = []
|
67 |
+
destination = []
|
68 |
+
thresholds = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
|
70 |
+
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(0, num_faces):
|
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 |
+
|
85 |
+
with gr.Row():
|
86 |
+
button = gr.Button("Reface", variant="primary")
|
87 |
|
88 |
+
button.click(fn=run, inputs=[video] + origin + destination + thresholds, outputs=[video2])
|
89 |
|
90 |
# Launch the Gradio app
|
91 |
+
demo.queue().launch(show_error=True, share=args.share_gradio, server_name="0.0.0.0", server_port=args.server_port)
|
|
|
|