Ii
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -37,39 +37,30 @@ args = parser.parse_args()
|
|
37 |
|
38 |
# Initialize the Refacer class
|
39 |
refacer = Refacer(force_cpu=args.force_cpu, colab_performance=args.colab_performance)
|
|
|
40 |
num_faces = args.max_num_faces
|
41 |
|
42 |
-
#
|
43 |
def run(*vars):
|
44 |
-
video_path = vars[0]
|
45 |
-
if not video_path:
|
46 |
-
raise ValueError("Video file not found. Please upload a valid video.")
|
47 |
-
|
48 |
origins = vars[1:(num_faces+1)]
|
49 |
destinations = vars[(num_faces+1):(num_faces*2)+1]
|
50 |
thresholds = vars[(num_faces*2)+1:]
|
51 |
|
52 |
faces = []
|
53 |
-
for k in range(num_faces):
|
54 |
-
if origins[k] and destinations[k]:
|
55 |
faces.append({
|
56 |
'origin': origins[k],
|
57 |
'destination': destinations[k],
|
58 |
'threshold': thresholds[k]
|
59 |
})
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
with open(refaced_video_path, "rb") as f:
|
65 |
-
video_bytes = f.read()
|
66 |
|
67 |
-
|
68 |
-
os.remove(refaced_video_path)
|
69 |
-
return video_bytes
|
70 |
-
except Exception as e:
|
71 |
-
print(f"Error during refacing: {e}")
|
72 |
-
raise RuntimeError("Video refacing failed. Check logs.")
|
73 |
|
74 |
# Prepare Gradio components
|
75 |
origin = []
|
@@ -80,22 +71,21 @@ with gr.Blocks() as demo:
|
|
80 |
with gr.Row():
|
81 |
gr.Markdown("# Refacer")
|
82 |
with gr.Row():
|
83 |
-
video = gr.Video(label="Original
|
84 |
-
video2 = gr.Video(label="Refaced
|
85 |
|
86 |
-
for i in range(num_faces):
|
87 |
with gr.Tab(f"Face #{i+1}"):
|
88 |
with gr.Row():
|
89 |
-
origin.append(gr.Image(label="Face to
|
90 |
-
destination.append(gr.Image(label="Destination
|
91 |
with gr.Row():
|
92 |
thresholds.append(gr.Slider(label="Threshold", minimum=0.0, maximum=1.0, value=0.2))
|
93 |
|
94 |
with gr.Row():
|
95 |
button = gr.Button("Reface", variant="primary")
|
96 |
|
97 |
-
# Configure button click
|
98 |
button.click(fn=run, inputs=[video] + origin + destination + thresholds, outputs=[video2])
|
99 |
|
100 |
-
# Launch Gradio app
|
101 |
demo.queue().launch(show_error=True, share=args.share_gradio, server_name="0.0.0.0", server_port=args.server_port)
|
|
|
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 = []
|
|
|
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)
|