Ii
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from refacer import Refacer
|
| 3 |
-
import argparse
|
| 4 |
import os
|
| 5 |
import requests
|
| 6 |
|
|
@@ -25,65 +24,60 @@ def download_model():
|
|
| 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=
|
| 40 |
|
| 41 |
-
|
|
|
|
| 42 |
|
| 43 |
# Run function for refacing video
|
| 44 |
-
def run(video_path, *
|
| 45 |
-
origins =
|
| 46 |
-
destinations =
|
| 47 |
-
thresholds =
|
| 48 |
|
| 49 |
faces = []
|
| 50 |
-
for
|
| 51 |
-
if origins[
|
| 52 |
faces.append({
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
})
|
| 57 |
|
| 58 |
-
#
|
| 59 |
-
|
| 60 |
-
return
|
| 61 |
-
|
| 62 |
-
# Prepare Gradio components
|
| 63 |
-
origin = []
|
| 64 |
-
destination = []
|
| 65 |
-
thresholds = []
|
| 66 |
|
|
|
|
| 67 |
with gr.Blocks() as demo:
|
|
|
|
|
|
|
|
|
|
| 68 |
with gr.Row():
|
| 69 |
-
gr.
|
| 70 |
-
|
| 71 |
-
video = gr.Video(label="Original Video", format="mp4")
|
| 72 |
-
video2 = gr.Video(label="Refaced Video", format="mp4", interactive=False)
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
for i in range(num_faces):
|
| 75 |
with gr.Tab(f"Face #{i+1}"):
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
with gr.Row():
|
| 83 |
-
|
| 84 |
|
| 85 |
-
#
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
# Launch the Gradio app
|
| 89 |
-
demo.queue().launch(
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from refacer import Refacer
|
|
|
|
| 3 |
import os
|
| 4 |
import requests
|
| 5 |
|
|
|
|
| 24 |
# Download the model when the script runs
|
| 25 |
download_model()
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
# Initialize the Refacer class
|
| 28 |
+
refacer = Refacer(force_cpu=False, colab_performance=False)
|
| 29 |
|
| 30 |
+
# Number of faces to handle
|
| 31 |
+
num_faces = 5
|
| 32 |
|
| 33 |
# Run function for refacing video
|
| 34 |
+
def run(video_path, *inputs):
|
| 35 |
+
origins = inputs[:num_faces]
|
| 36 |
+
destinations = inputs[num_faces:2*num_faces]
|
| 37 |
+
thresholds = inputs[2*num_faces:]
|
| 38 |
|
| 39 |
faces = []
|
| 40 |
+
for i in range(num_faces):
|
| 41 |
+
if origins[i] is not None and destinations[i] is not None:
|
| 42 |
faces.append({
|
| 43 |
+
"origin": origins[i],
|
| 44 |
+
"destination": destinations[i],
|
| 45 |
+
"threshold": thresholds[i]
|
| 46 |
})
|
| 47 |
|
| 48 |
+
# Perform refacing
|
| 49 |
+
refaced_video_path = refacer.reface(video_path, faces)
|
| 50 |
+
return refaced_video_path # Return the path of the refaced video
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
# Define Gradio UI
|
| 53 |
with gr.Blocks() as demo:
|
| 54 |
+
gr.Markdown("# Refacer - Replace Faces in Videos")
|
| 55 |
+
|
| 56 |
+
# Input and output components
|
| 57 |
with gr.Row():
|
| 58 |
+
input_video = gr.Video(label="Original Video", format="mp4")
|
| 59 |
+
output_video = gr.Video(label="Refaced Video", interactive=False, format="mp4")
|
|
|
|
|
|
|
| 60 |
|
| 61 |
+
# Face replacement tabs
|
| 62 |
+
origins = []
|
| 63 |
+
destinations = []
|
| 64 |
+
thresholds = []
|
| 65 |
for i in range(num_faces):
|
| 66 |
with gr.Tab(f"Face #{i+1}"):
|
| 67 |
+
origins.append(gr.Image(label="Origin Face"))
|
| 68 |
+
destinations.append(gr.Image(label="Destination Face"))
|
| 69 |
+
thresholds.append(gr.Slider(label="Threshold", minimum=0.0, maximum=1.0, value=0.2))
|
| 70 |
+
|
| 71 |
+
# Reface button
|
|
|
|
| 72 |
with gr.Row():
|
| 73 |
+
reface_button = gr.Button("Reface")
|
| 74 |
|
| 75 |
+
# Connect the function to the Gradio UI
|
| 76 |
+
reface_button.click(
|
| 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=True)
|