Ii
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -37,36 +37,39 @@ 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 |
-
#
|
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(
|
52 |
-
if origins[k]
|
53 |
faces.append({
|
54 |
'origin': origins[k],
|
55 |
'destination': destinations[k],
|
56 |
'threshold': thresholds[k]
|
57 |
})
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
video_bytes = f.read()
|
65 |
-
|
66 |
-
# Delete the temporary video file
|
67 |
-
os.remove(refaced_video_path)
|
68 |
|
69 |
-
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
# Prepare Gradio components
|
72 |
origin = []
|
@@ -77,22 +80,22 @@ with gr.Blocks() as demo:
|
|
77 |
with gr.Row():
|
78 |
gr.Markdown("# Refacer")
|
79 |
with gr.Row():
|
80 |
-
video = gr.Video(label="Original
|
81 |
-
video2 = gr.Video(label="Refaced
|
82 |
|
83 |
-
for i in range(
|
84 |
with gr.Tab(f"Face #{i+1}"):
|
85 |
with gr.Row():
|
86 |
-
origin.append(gr.Image(label="Face to
|
87 |
-
destination.append(gr.Image(label="Destination
|
88 |
with gr.Row():
|
89 |
thresholds.append(gr.Slider(label="Threshold", minimum=0.0, maximum=1.0, value=0.2))
|
90 |
|
91 |
with gr.Row():
|
92 |
button = gr.Button("Reface", variant="primary")
|
93 |
|
94 |
-
#
|
95 |
button.click(fn=run, inputs=[video] + origin + destination + thresholds, outputs=[video2])
|
96 |
|
97 |
-
# Launch
|
98 |
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 |
num_faces = args.max_num_faces
|
41 |
|
42 |
+
# Function to process refacing
|
43 |
def run(*vars):
|
44 |
+
video_path = vars[0] # Original video path
|
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 |
+
try:
|
62 |
+
# Call refacer to process video and return bytes
|
63 |
+
refaced_video_path = refacer.reface(video_path, faces)
|
64 |
+
with open(refaced_video_path, "rb") as f:
|
65 |
+
video_bytes = f.read()
|
|
|
|
|
|
|
|
|
66 |
|
67 |
+
# Cleanup temporary file
|
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 |
with gr.Row():
|
81 |
gr.Markdown("# Refacer")
|
82 |
with gr.Row():
|
83 |
+
video = gr.Video(label="Original Video", format="mp4")
|
84 |
+
video2 = gr.Video(label="Refaced Video", interactive=False, format="mp4")
|
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 Replace"))
|
90 |
+
destination.append(gr.Image(label="Destination Face"))
|
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)
|