Update app.py
Browse files
app.py
CHANGED
@@ -1,117 +1,118 @@
|
|
1 |
-
import os
|
2 |
-
import time
|
3 |
-
import shutil
|
4 |
-
import gradio as gr
|
5 |
-
|
6 |
-
|
7 |
-
ROOP_DEFAULT_PATH = "media/roop_default.png"
|
8 |
-
ROOP_OUTPUT_VIDEO_PATH = "media/roop_output.mp4"
|
9 |
-
|
10 |
-
|
11 |
-
def _handle_roop_solve(
|
12 |
-
video_path: str,
|
13 |
-
img_path: str
|
14 |
-
):
|
15 |
-
if video_path is None:
|
16 |
-
raise gr.Error("Please upload source video!")
|
17 |
-
if img_path is None:
|
18 |
-
raise gr.Error("Please upload target image!")
|
19 |
-
|
20 |
-
start_time = time.time()
|
21 |
-
command = f"python run.py -t {video_path} -s {img_path} -o {ROOP_OUTPUT_VIDEO_PATH}"
|
22 |
-
os.system(command)
|
23 |
-
solved_time = time.time() - start_time
|
24 |
-
message = "Successfully solve the GED problem, using time ({:.3f}s).".format(solved_time)
|
25 |
-
|
26 |
-
return message, ROOP_OUTPUT_VIDEO_PATH
|
27 |
-
|
28 |
-
|
29 |
-
def handle_roop(
|
30 |
-
video_path: str,
|
31 |
-
img_path: str
|
32 |
-
):
|
33 |
-
try:
|
34 |
-
message = _handle_roop_solve(
|
35 |
-
video_path=video_path,
|
36 |
-
img_path=img_path
|
37 |
-
)
|
38 |
-
return message
|
39 |
-
except Exception as e:
|
40 |
-
message = str(e)
|
41 |
-
return message, ROOP_OUTPUT_VIDEO_PATH
|
42 |
-
|
43 |
-
|
44 |
-
def handle_roop_clear():
|
45 |
-
shutil.copy(
|
46 |
-
src=ROOP_DEFAULT_PATH,
|
47 |
-
dst=ROOP_OUTPUT_VIDEO_PATH
|
48 |
-
)
|
49 |
-
|
50 |
-
message = "successfully clear the files!"
|
51 |
-
return message, ROOP_OUTPUT_VIDEO_PATH
|
52 |
-
|
53 |
-
|
54 |
-
with gr.Blocks() as ged_page:
|
55 |
-
|
56 |
-
gr.Markdown(
|
57 |
-
'''
|
58 |
-
This space displays
|
59 |
-
## How to use this Space?
|
60 |
-
- Upload
|
61 |
-
-
|
62 |
-
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
|
|
117 |
ged_page.launch(debug = True)
|
|
|
1 |
+
import os
|
2 |
+
import time
|
3 |
+
import shutil
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
|
7 |
+
ROOP_DEFAULT_PATH = "media/roop_default.png"
|
8 |
+
ROOP_OUTPUT_VIDEO_PATH = "media/roop_output.mp4"
|
9 |
+
|
10 |
+
|
11 |
+
def _handle_roop_solve(
|
12 |
+
video_path: str,
|
13 |
+
img_path: str
|
14 |
+
):
|
15 |
+
if video_path is None:
|
16 |
+
raise gr.Error("Please upload source video!")
|
17 |
+
if img_path is None:
|
18 |
+
raise gr.Error("Please upload target image!")
|
19 |
+
|
20 |
+
start_time = time.time()
|
21 |
+
command = f"python run.py -t {video_path} -s {img_path} -o {ROOP_OUTPUT_VIDEO_PATH}"
|
22 |
+
os.system(command)
|
23 |
+
solved_time = time.time() - start_time
|
24 |
+
message = "Successfully solve the GED problem, using time ({:.3f}s).".format(solved_time)
|
25 |
+
|
26 |
+
return message, ROOP_OUTPUT_VIDEO_PATH
|
27 |
+
|
28 |
+
|
29 |
+
def handle_roop(
|
30 |
+
video_path: str,
|
31 |
+
img_path: str
|
32 |
+
):
|
33 |
+
try:
|
34 |
+
message = _handle_roop_solve(
|
35 |
+
video_path=video_path,
|
36 |
+
img_path=img_path
|
37 |
+
)
|
38 |
+
return message
|
39 |
+
except Exception as e:
|
40 |
+
message = str(e)
|
41 |
+
return message, ROOP_OUTPUT_VIDEO_PATH
|
42 |
+
|
43 |
+
|
44 |
+
def handle_roop_clear():
|
45 |
+
shutil.copy(
|
46 |
+
src=ROOP_DEFAULT_PATH,
|
47 |
+
dst=ROOP_OUTPUT_VIDEO_PATH
|
48 |
+
)
|
49 |
+
|
50 |
+
message = "successfully clear the files!"
|
51 |
+
return message, ROOP_OUTPUT_VIDEO_PATH
|
52 |
+
|
53 |
+
|
54 |
+
with gr.Blocks() as ged_page:
|
55 |
+
|
56 |
+
gr.Markdown(
|
57 |
+
'''
|
58 |
+
This space displays how to perform face swapping.
|
59 |
+
## How to use this Space?
|
60 |
+
- Upload a video, preferably with a duration of less than 5 seconds.
|
61 |
+
- Upload a photo of the person you wish to swap with.
|
62 |
+
- You will receive the result of the face swap after 5-10 minutes.
|
63 |
+
- Click the 'clear' button to clear all the files.
|
64 |
+
## Examples
|
65 |
+
- You can get the test examples from our [Roop Dataset Repo.](https://huggingface.co/datasets/SJTU-TES/Roop)
|
66 |
+
'''
|
67 |
+
)
|
68 |
+
|
69 |
+
with gr.Row(variant="panel"):
|
70 |
+
with gr.Column(scale=2):
|
71 |
+
with gr.Row():
|
72 |
+
upload_video = gr.Video(
|
73 |
+
label="Upload .mp4 Vide0",
|
74 |
+
format="mp4",
|
75 |
+
)
|
76 |
+
upload_img = gr.Image(
|
77 |
+
label="Upload .png or .jpg File",
|
78 |
+
type="filepath",
|
79 |
+
min_width=40,
|
80 |
+
)
|
81 |
+
info = gr.Textbox(
|
82 |
+
value="",
|
83 |
+
label="Log",
|
84 |
+
scale=4,
|
85 |
+
)
|
86 |
+
with gr.Row():
|
87 |
+
with gr.Column(scale=1, min_width=100):
|
88 |
+
solve_button = gr.Button(
|
89 |
+
value="Solve",
|
90 |
+
variant="primary",
|
91 |
+
scale=1
|
92 |
+
)
|
93 |
+
with gr.Column(scale=1, min_width=100):
|
94 |
+
clear_button = gr.Button(
|
95 |
+
"Clear",
|
96 |
+
variant="secondary",
|
97 |
+
scale=1
|
98 |
+
)
|
99 |
+
with gr.Column(scale=8):
|
100 |
+
pass
|
101 |
+
with gr.Column(scale=2):
|
102 |
+
output_video = gr.Video(height=405, width=720)
|
103 |
+
|
104 |
+
solve_button.click(
|
105 |
+
handle_roop,
|
106 |
+
[upload_video, upload_img],
|
107 |
+
outputs=[info, output_video]
|
108 |
+
)
|
109 |
+
|
110 |
+
clear_button.click(
|
111 |
+
handle_roop_clear,
|
112 |
+
inputs=None,
|
113 |
+
outputs=[info, output_video]
|
114 |
+
)
|
115 |
+
|
116 |
+
|
117 |
+
if __name__ == "__main__":
|
118 |
ged_page.launch(debug = True)
|