ubuntu
commited on
Commit
•
46088e2
1
Parent(s):
dd1d998
Initial Commit
Browse files- app.py +117 -0
- requirements.txt +13 -0
- roop/__init__.py +0 -0
- roop/capturer.py +22 -0
- roop/core.py +220 -0
- roop/face_analyser.py +54 -0
- roop/face_reference.py +21 -0
- roop/globals.py +22 -0
- roop/metadata.py +2 -0
- roop/predictor.py +43 -0
- roop/processors/__init__.py +0 -0
- roop/processors/frame/__init__.py +0 -0
- roop/processors/frame/core.py +91 -0
- roop/processors/frame/face_enhancer.py +104 -0
- roop/processors/frame/face_swapper.py +100 -0
- roop/typing.py +7 -0
- roop/ui.json +161 -0
- roop/ui.py +285 -0
- roop/utilities.py +149 -0
- run.py +6 -0
app.py
ADDED
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 the solution to the Graph Edit Distance problem.
|
59 |
+
## How to use this Space?
|
60 |
+
- Upload two '.gexf' files.
|
61 |
+
- The images of the GED problem and solution will be shown after you click the solve button.
|
62 |
+
- Click the 'clear' button to clear all the files.
|
63 |
+
## Examples
|
64 |
+
- You can get the test examples from our [GED Dataset Repo.](https://huggingface.co/datasets/SJTU-TES/Graph-Edit-Distance)
|
65 |
+
'''
|
66 |
+
)
|
67 |
+
|
68 |
+
with gr.Row(variant="panel"):
|
69 |
+
with gr.Column(scale=2):
|
70 |
+
with gr.Row():
|
71 |
+
upload_video = gr.Video(
|
72 |
+
label="Upload .mp4 Vide0",
|
73 |
+
format="mp4",
|
74 |
+
)
|
75 |
+
upload_img = gr.Image(
|
76 |
+
label="Upload .png or .jpg File",
|
77 |
+
type="filepath",
|
78 |
+
min_width=40,
|
79 |
+
)
|
80 |
+
info = gr.Textbox(
|
81 |
+
value="",
|
82 |
+
label="Log",
|
83 |
+
scale=4,
|
84 |
+
)
|
85 |
+
with gr.Row():
|
86 |
+
with gr.Column(scale=1, min_width=100):
|
87 |
+
solve_button = gr.Button(
|
88 |
+
value="Solve",
|
89 |
+
variant="primary",
|
90 |
+
scale=1
|
91 |
+
)
|
92 |
+
with gr.Column(scale=1, min_width=100):
|
93 |
+
clear_button = gr.Button(
|
94 |
+
"Clear",
|
95 |
+
variant="secondary",
|
96 |
+
scale=1
|
97 |
+
)
|
98 |
+
with gr.Column(scale=8):
|
99 |
+
pass
|
100 |
+
with gr.Column(scale=2):
|
101 |
+
output_video = gr.Video(height=405, width=720)
|
102 |
+
|
103 |
+
solve_button.click(
|
104 |
+
handle_roop,
|
105 |
+
[upload_video, upload_img],
|
106 |
+
outputs=[info, output_video]
|
107 |
+
)
|
108 |
+
|
109 |
+
clear_button.click(
|
110 |
+
handle_roop_clear,
|
111 |
+
inputs=None,
|
112 |
+
outputs=[info, output_video]
|
113 |
+
)
|
114 |
+
|
115 |
+
|
116 |
+
if __name__ == "__main__":
|
117 |
+
ged_page.launch(debug = True)
|
requirements.txt
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio==3.40.0
|
2 |
+
onnxruntime==1.17.1
|
3 |
+
tensorflow==2.15.0
|
4 |
+
customtkinter==5.2.0
|
5 |
+
tkinterdnd2==0.3.0
|
6 |
+
opencv-python==4.8.0.74
|
7 |
+
insightface==0.7.3
|
8 |
+
numpy==1.24.3
|
9 |
+
opennsfw2==0.9.2
|
10 |
+
ffmpeg
|
11 |
+
tk==0.1.0
|
12 |
+
tkinterdnd2-universal==1.7.3
|
13 |
+
onnxruntime-gpu==1.17.1
|
roop/__init__.py
ADDED
File without changes
|
roop/capturer.py
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Optional
|
2 |
+
import cv2
|
3 |
+
|
4 |
+
from roop.typing import Frame
|
5 |
+
|
6 |
+
|
7 |
+
def get_video_frame(video_path: str, frame_number: int = 0) -> Optional[Frame]:
|
8 |
+
capture = cv2.VideoCapture(video_path)
|
9 |
+
frame_total = capture.get(cv2.CAP_PROP_FRAME_COUNT)
|
10 |
+
capture.set(cv2.CAP_PROP_POS_FRAMES, min(frame_total, frame_number - 1))
|
11 |
+
has_frame, frame = capture.read()
|
12 |
+
capture.release()
|
13 |
+
if has_frame:
|
14 |
+
return frame
|
15 |
+
return None
|
16 |
+
|
17 |
+
|
18 |
+
def get_video_frame_total(video_path: str) -> int:
|
19 |
+
capture = cv2.VideoCapture(video_path)
|
20 |
+
video_frame_total = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
|
21 |
+
capture.release()
|
22 |
+
return video_frame_total
|
roop/core.py
ADDED
@@ -0,0 +1,220 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python3
|
2 |
+
|
3 |
+
import os
|
4 |
+
import sys
|
5 |
+
# single thread doubles cuda performance - needs to be set before torch import
|
6 |
+
if any(arg.startswith('--execution-provider') for arg in sys.argv):
|
7 |
+
os.environ['OMP_NUM_THREADS'] = '1'
|
8 |
+
# reduce tensorflow log level
|
9 |
+
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
|
10 |
+
import warnings
|
11 |
+
from typing import List
|
12 |
+
import platform
|
13 |
+
import signal
|
14 |
+
import shutil
|
15 |
+
import argparse
|
16 |
+
import onnxruntime
|
17 |
+
import tensorflow
|
18 |
+
import roop.globals as globals
|
19 |
+
import roop.metadata as metadata
|
20 |
+
import roop.ui as ui
|
21 |
+
from roop.predictor import predict_image, predict_video
|
22 |
+
from roop.processors.frame.core import get_frame_processors_modules
|
23 |
+
from roop.utilities import has_image_extension, is_image, is_video, detect_fps, create_video, extract_frames, get_temp_frame_paths, restore_audio, create_temp, move_temp, clean_temp, normalize_output_path
|
24 |
+
|
25 |
+
warnings.filterwarnings('ignore', category=FutureWarning, module='insightface')
|
26 |
+
warnings.filterwarnings('ignore', category=UserWarning, module='torchvision')
|
27 |
+
|
28 |
+
|
29 |
+
def parse_args() -> None:
|
30 |
+
signal.signal(signal.SIGINT, lambda signal_number, frame: destroy())
|
31 |
+
program = argparse.ArgumentParser(formatter_class=lambda prog: argparse.HelpFormatter(prog, max_help_position=100))
|
32 |
+
program.add_argument('-s', '--source', help='select an source image', dest='source_path')
|
33 |
+
program.add_argument('-t', '--target', help='select an target image or video', dest='target_path')
|
34 |
+
program.add_argument('-o', '--output', help='select output file or directory', dest='output_path')
|
35 |
+
program.add_argument('--frame-processor', help='frame processors (choices: face_swapper, face_enhancer, ...)', dest='frame_processor', default=['face_swapper'], nargs='+')
|
36 |
+
program.add_argument('--keep-fps', help='keep target fps', dest='keep_fps', action='store_true')
|
37 |
+
program.add_argument('--keep-frames', help='keep temporary frames', dest='keep_frames', action='store_true')
|
38 |
+
program.add_argument('--skip-audio', help='skip target audio', dest='skip_audio', action='store_true')
|
39 |
+
program.add_argument('--many-faces', help='process every face', dest='many_faces', action='store_true')
|
40 |
+
program.add_argument('--reference-face-position', help='position of the reference face', dest='reference_face_position', type=int, default=0)
|
41 |
+
program.add_argument('--reference-frame-number', help='number of the reference frame', dest='reference_frame_number', type=int, default=0)
|
42 |
+
program.add_argument('--similar-face-distance', help='face distance used for recognition', dest='similar_face_distance', type=float, default=0.85)
|
43 |
+
program.add_argument('--temp-frame-format', help='image format used for frame extraction', dest='temp_frame_format', default='png', choices=['jpg', 'png'])
|
44 |
+
program.add_argument('--temp-frame-quality', help='image quality used for frame extraction', dest='temp_frame_quality', type=int, default=0, choices=range(101), metavar='[0-100]')
|
45 |
+
program.add_argument('--output-video-encoder', help='encoder used for the output video', dest='output_video_encoder', default='libx264', choices=['libx264', 'libx265', 'libvpx-vp9', 'h264_nvenc', 'hevc_nvenc'])
|
46 |
+
program.add_argument('--output-video-quality', help='quality used for the output video', dest='output_video_quality', type=int, default=35, choices=range(101), metavar='[0-100]')
|
47 |
+
program.add_argument('--max-memory', help='maximum amount of RAM in GB', dest='max_memory', type=int)
|
48 |
+
program.add_argument('--execution-provider', help='available execution provider (choices: cpu, ...)', dest='execution_provider', default=['cpu'], choices=suggest_execution_providers(), nargs='+')
|
49 |
+
program.add_argument('--execution-threads', help='number of execution threads', dest='execution_threads', type=int, default=suggest_execution_threads())
|
50 |
+
program.add_argument('-v', '--version', action='version', version=f'{metadata.name} {metadata.version}')
|
51 |
+
|
52 |
+
args = program.parse_args()
|
53 |
+
|
54 |
+
globals.source_path = args.source_path
|
55 |
+
globals.target_path = args.target_path
|
56 |
+
globals.output_path = normalize_output_path(globals.source_path, globals.target_path, args.output_path)
|
57 |
+
globals.headless = globals.source_path is not None and globals.target_path is not None and globals.output_path is not None
|
58 |
+
globals.frame_processors = args.frame_processor
|
59 |
+
globals.keep_fps = args.keep_fps
|
60 |
+
globals.keep_frames = args.keep_frames
|
61 |
+
globals.skip_audio = args.skip_audio
|
62 |
+
globals.many_faces = args.many_faces
|
63 |
+
globals.reference_face_position = args.reference_face_position
|
64 |
+
globals.reference_frame_number = args.reference_frame_number
|
65 |
+
globals.similar_face_distance = args.similar_face_distance
|
66 |
+
globals.temp_frame_format = args.temp_frame_format
|
67 |
+
globals.temp_frame_quality = args.temp_frame_quality
|
68 |
+
globals.output_video_encoder = args.output_video_encoder
|
69 |
+
globals.output_video_quality = args.output_video_quality
|
70 |
+
globals.max_memory = args.max_memory
|
71 |
+
globals.execution_providers = decode_execution_providers(args.execution_provider)
|
72 |
+
globals.execution_threads = args.execution_threads
|
73 |
+
|
74 |
+
|
75 |
+
def encode_execution_providers(execution_providers: List[str]) -> List[str]:
|
76 |
+
return [execution_provider.replace('ExecutionProvider', '').lower() for execution_provider in execution_providers]
|
77 |
+
|
78 |
+
|
79 |
+
def decode_execution_providers(execution_providers: List[str]) -> List[str]:
|
80 |
+
return [provider for provider, encoded_execution_provider in zip(onnxruntime.get_available_providers(), encode_execution_providers(onnxruntime.get_available_providers()))
|
81 |
+
if any(execution_provider in encoded_execution_provider for execution_provider in execution_providers)]
|
82 |
+
|
83 |
+
|
84 |
+
def suggest_execution_providers() -> List[str]:
|
85 |
+
return encode_execution_providers(onnxruntime.get_available_providers())
|
86 |
+
|
87 |
+
|
88 |
+
def suggest_execution_threads() -> int:
|
89 |
+
if 'CUDAExecutionProvider' in onnxruntime.get_available_providers():
|
90 |
+
return 8
|
91 |
+
return 1
|
92 |
+
|
93 |
+
|
94 |
+
def limit_resources() -> None:
|
95 |
+
# prevent tensorflow memory leak
|
96 |
+
gpus = tensorflow.config.experimental.list_physical_devices('GPU')
|
97 |
+
for gpu in gpus:
|
98 |
+
tensorflow.config.experimental.set_virtual_device_configuration(gpu, [
|
99 |
+
tensorflow.config.experimental.VirtualDeviceConfiguration(memory_limit=1024)
|
100 |
+
])
|
101 |
+
# limit memory usage
|
102 |
+
if globals.max_memory:
|
103 |
+
memory = globals.max_memory * 1024 ** 3
|
104 |
+
if platform.system().lower() == 'darwin':
|
105 |
+
memory = globals.max_memory * 1024 ** 6
|
106 |
+
if platform.system().lower() == 'windows':
|
107 |
+
import ctypes
|
108 |
+
kernel32 = ctypes.windll.kernel32 # type: ignore[attr-defined]
|
109 |
+
kernel32.SetProcessWorkingSetSize(-1, ctypes.c_size_t(memory), ctypes.c_size_t(memory))
|
110 |
+
else:
|
111 |
+
import resource
|
112 |
+
resource.setrlimit(resource.RLIMIT_DATA, (memory, memory))
|
113 |
+
|
114 |
+
|
115 |
+
def pre_check() -> bool:
|
116 |
+
if sys.version_info < (3, 9):
|
117 |
+
update_status('Python version is not supported - please upgrade to 3.9 or higher.')
|
118 |
+
return False
|
119 |
+
if not shutil.which('ffmpeg'):
|
120 |
+
update_status('ffmpeg is not installed.')
|
121 |
+
return False
|
122 |
+
return True
|
123 |
+
|
124 |
+
|
125 |
+
def update_status(message: str, scope: str = 'ROOP.CORE') -> None:
|
126 |
+
print(f'[{scope}] {message}')
|
127 |
+
if not globals.headless:
|
128 |
+
ui.update_status(message)
|
129 |
+
|
130 |
+
|
131 |
+
def start() -> None:
|
132 |
+
for frame_processor in get_frame_processors_modules(globals.frame_processors):
|
133 |
+
if not frame_processor.pre_start():
|
134 |
+
return
|
135 |
+
# process image to image
|
136 |
+
if has_image_extension(globals.target_path):
|
137 |
+
if predict_image(globals.target_path):
|
138 |
+
destroy()
|
139 |
+
shutil.copy2(globals.target_path, globals.output_path)
|
140 |
+
# process frame
|
141 |
+
for frame_processor in get_frame_processors_modules(globals.frame_processors):
|
142 |
+
update_status('Progressing...', frame_processor.NAME)
|
143 |
+
frame_processor.process_image(globals.source_path, globals.output_path, globals.output_path)
|
144 |
+
frame_processor.post_process()
|
145 |
+
# validate image
|
146 |
+
if is_image(globals.target_path):
|
147 |
+
update_status('Processing to image succeed!')
|
148 |
+
else:
|
149 |
+
update_status('Processing to image failed!')
|
150 |
+
return
|
151 |
+
# process image to videos
|
152 |
+
if predict_video(globals.target_path):
|
153 |
+
destroy()
|
154 |
+
update_status('Creating temporary resources...')
|
155 |
+
create_temp(globals.target_path)
|
156 |
+
# extract frames
|
157 |
+
if globals.keep_fps:
|
158 |
+
fps = detect_fps(globals.target_path)
|
159 |
+
update_status(f'Extracting frames with {fps} FPS...')
|
160 |
+
extract_frames(globals.target_path, fps)
|
161 |
+
else:
|
162 |
+
update_status('Extracting frames with 30 FPS...')
|
163 |
+
extract_frames(globals.target_path)
|
164 |
+
# process frame
|
165 |
+
temp_frame_paths = get_temp_frame_paths(globals.target_path)
|
166 |
+
if temp_frame_paths:
|
167 |
+
for frame_processor in get_frame_processors_modules(globals.frame_processors):
|
168 |
+
update_status('Progressing...', frame_processor.NAME)
|
169 |
+
frame_processor.process_video(globals.source_path, temp_frame_paths)
|
170 |
+
frame_processor.post_process()
|
171 |
+
else:
|
172 |
+
update_status('Frames not found...')
|
173 |
+
return
|
174 |
+
# create video
|
175 |
+
if globals.keep_fps:
|
176 |
+
fps = detect_fps(globals.target_path)
|
177 |
+
update_status(f'Creating video with {fps} FPS...')
|
178 |
+
create_video(globals.target_path, fps)
|
179 |
+
else:
|
180 |
+
update_status('Creating video with 30 FPS...')
|
181 |
+
create_video(globals.target_path)
|
182 |
+
# handle audio
|
183 |
+
if globals.skip_audio:
|
184 |
+
move_temp(globals.target_path, globals.output_path)
|
185 |
+
update_status('Skipping audio...')
|
186 |
+
else:
|
187 |
+
if globals.keep_fps:
|
188 |
+
update_status('Restoring audio...')
|
189 |
+
else:
|
190 |
+
update_status('Restoring audio might cause issues as fps are not kept...')
|
191 |
+
restore_audio(globals.target_path, globals.output_path)
|
192 |
+
# clean temp
|
193 |
+
update_status('Cleaning temporary resources...')
|
194 |
+
clean_temp(globals.target_path)
|
195 |
+
# validate video
|
196 |
+
if is_video(globals.target_path):
|
197 |
+
update_status('Processing to video succeed!')
|
198 |
+
else:
|
199 |
+
update_status('Processing to video failed!')
|
200 |
+
|
201 |
+
|
202 |
+
def destroy() -> None:
|
203 |
+
if globals.target_path:
|
204 |
+
clean_temp(globals.target_path)
|
205 |
+
sys.exit()
|
206 |
+
|
207 |
+
|
208 |
+
def run() -> None:
|
209 |
+
parse_args()
|
210 |
+
if not pre_check():
|
211 |
+
return
|
212 |
+
for frame_processor in get_frame_processors_modules(globals.frame_processors):
|
213 |
+
if not frame_processor.pre_check():
|
214 |
+
return
|
215 |
+
limit_resources()
|
216 |
+
if globals.headless:
|
217 |
+
start()
|
218 |
+
else:
|
219 |
+
window = ui.init(start, destroy)
|
220 |
+
window.mainloop()
|
roop/face_analyser.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import threading
|
2 |
+
from typing import Any, Optional, List
|
3 |
+
import insightface
|
4 |
+
import numpy
|
5 |
+
|
6 |
+
import roop.globals
|
7 |
+
from roop.typing import Frame, Face
|
8 |
+
|
9 |
+
FACE_ANALYSER = None
|
10 |
+
THREAD_LOCK = threading.Lock()
|
11 |
+
|
12 |
+
|
13 |
+
def get_face_analyser() -> Any:
|
14 |
+
global FACE_ANALYSER
|
15 |
+
|
16 |
+
with THREAD_LOCK:
|
17 |
+
if FACE_ANALYSER is None:
|
18 |
+
FACE_ANALYSER = insightface.app.FaceAnalysis(name='buffalo_l', providers=roop.globals.execution_providers)
|
19 |
+
FACE_ANALYSER.prepare(ctx_id=0)
|
20 |
+
return FACE_ANALYSER
|
21 |
+
|
22 |
+
|
23 |
+
def clear_face_analyser() -> Any:
|
24 |
+
global FACE_ANALYSER
|
25 |
+
|
26 |
+
FACE_ANALYSER = None
|
27 |
+
|
28 |
+
|
29 |
+
def get_one_face(frame: Frame, position: int = 0) -> Optional[Face]:
|
30 |
+
many_faces = get_many_faces(frame)
|
31 |
+
if many_faces:
|
32 |
+
try:
|
33 |
+
return many_faces[position]
|
34 |
+
except IndexError:
|
35 |
+
return many_faces[-1]
|
36 |
+
return None
|
37 |
+
|
38 |
+
|
39 |
+
def get_many_faces(frame: Frame) -> Optional[List[Face]]:
|
40 |
+
try:
|
41 |
+
return get_face_analyser().get(frame)
|
42 |
+
except ValueError:
|
43 |
+
return None
|
44 |
+
|
45 |
+
|
46 |
+
def find_similar_face(frame: Frame, reference_face: Face) -> Optional[Face]:
|
47 |
+
many_faces = get_many_faces(frame)
|
48 |
+
if many_faces:
|
49 |
+
for face in many_faces:
|
50 |
+
if hasattr(face, 'normed_embedding') and hasattr(reference_face, 'normed_embedding'):
|
51 |
+
distance = numpy.sum(numpy.square(face.normed_embedding - reference_face.normed_embedding))
|
52 |
+
if distance < roop.globals.similar_face_distance:
|
53 |
+
return face
|
54 |
+
return None
|
roop/face_reference.py
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Optional
|
2 |
+
|
3 |
+
from roop.typing import Face
|
4 |
+
|
5 |
+
FACE_REFERENCE = None
|
6 |
+
|
7 |
+
|
8 |
+
def get_face_reference() -> Optional[Face]:
|
9 |
+
return FACE_REFERENCE
|
10 |
+
|
11 |
+
|
12 |
+
def set_face_reference(face: Face) -> None:
|
13 |
+
global FACE_REFERENCE
|
14 |
+
|
15 |
+
FACE_REFERENCE = face
|
16 |
+
|
17 |
+
|
18 |
+
def clear_face_reference() -> None:
|
19 |
+
global FACE_REFERENCE
|
20 |
+
|
21 |
+
FACE_REFERENCE = None
|
roop/globals.py
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import List, Optional
|
2 |
+
|
3 |
+
source_path: Optional[str] = None
|
4 |
+
target_path: Optional[str] = None
|
5 |
+
output_path: Optional[str] = None
|
6 |
+
headless: Optional[bool] = None
|
7 |
+
frame_processors: List[str] = []
|
8 |
+
keep_fps: Optional[bool] = None
|
9 |
+
keep_frames: Optional[bool] = None
|
10 |
+
skip_audio: Optional[bool] = None
|
11 |
+
many_faces: Optional[bool] = None
|
12 |
+
reference_face_position: Optional[int] = None
|
13 |
+
reference_frame_number: Optional[int] = None
|
14 |
+
similar_face_distance: Optional[float] = None
|
15 |
+
temp_frame_format: Optional[str] = None
|
16 |
+
temp_frame_quality: Optional[int] = None
|
17 |
+
output_video_encoder: Optional[str] = None
|
18 |
+
output_video_quality: Optional[int] = None
|
19 |
+
max_memory: Optional[int] = None
|
20 |
+
execution_providers: List[str] = []
|
21 |
+
execution_threads: Optional[int] = None
|
22 |
+
log_level: str = 'error'
|
roop/metadata.py
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
name = 'roop'
|
2 |
+
version = '1.3.2'
|
roop/predictor.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import threading
|
2 |
+
import numpy
|
3 |
+
import opennsfw2
|
4 |
+
from PIL import Image
|
5 |
+
from keras import Model
|
6 |
+
|
7 |
+
from roop.typing import Frame
|
8 |
+
|
9 |
+
PREDICTOR = None
|
10 |
+
THREAD_LOCK = threading.Lock()
|
11 |
+
MAX_PROBABILITY = 0.85
|
12 |
+
|
13 |
+
|
14 |
+
def get_predictor() -> Model:
|
15 |
+
global PREDICTOR
|
16 |
+
|
17 |
+
with THREAD_LOCK:
|
18 |
+
if PREDICTOR is None:
|
19 |
+
PREDICTOR = opennsfw2.make_open_nsfw_model()
|
20 |
+
return PREDICTOR
|
21 |
+
|
22 |
+
|
23 |
+
def clear_predictor() -> None:
|
24 |
+
global PREDICTOR
|
25 |
+
|
26 |
+
PREDICTOR = None
|
27 |
+
|
28 |
+
|
29 |
+
def predict_frame(target_frame: Frame) -> bool:
|
30 |
+
image = Image.fromarray(target_frame)
|
31 |
+
image = opennsfw2.preprocess_image(image, opennsfw2.Preprocessing.YAHOO)
|
32 |
+
views = numpy.expand_dims(image, axis=0)
|
33 |
+
_, probability = get_predictor().predict(views)[0]
|
34 |
+
return probability > MAX_PROBABILITY
|
35 |
+
|
36 |
+
|
37 |
+
def predict_image(target_path: str) -> bool:
|
38 |
+
return opennsfw2.predict_image(target_path) > MAX_PROBABILITY
|
39 |
+
|
40 |
+
|
41 |
+
def predict_video(target_path: str) -> bool:
|
42 |
+
_, probabilities = opennsfw2.predict_video_frames(video_path=target_path, frame_interval=100)
|
43 |
+
return any(probability > MAX_PROBABILITY for probability in probabilities)
|
roop/processors/__init__.py
ADDED
File without changes
|
roop/processors/frame/__init__.py
ADDED
File without changes
|
roop/processors/frame/core.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import sys
|
3 |
+
import importlib
|
4 |
+
import psutil
|
5 |
+
from concurrent.futures import ThreadPoolExecutor, as_completed
|
6 |
+
from queue import Queue
|
7 |
+
from types import ModuleType
|
8 |
+
from typing import Any, List, Callable
|
9 |
+
from tqdm import tqdm
|
10 |
+
|
11 |
+
import roop
|
12 |
+
|
13 |
+
FRAME_PROCESSORS_MODULES: List[ModuleType] = []
|
14 |
+
FRAME_PROCESSORS_INTERFACE = [
|
15 |
+
'pre_check',
|
16 |
+
'pre_start',
|
17 |
+
'process_frame',
|
18 |
+
'process_frames',
|
19 |
+
'process_image',
|
20 |
+
'process_video',
|
21 |
+
'post_process'
|
22 |
+
]
|
23 |
+
|
24 |
+
|
25 |
+
def load_frame_processor_module(frame_processor: str) -> Any:
|
26 |
+
try:
|
27 |
+
frame_processor_module = importlib.import_module(f'roop.processors.frame.{frame_processor}')
|
28 |
+
for method_name in FRAME_PROCESSORS_INTERFACE:
|
29 |
+
if not hasattr(frame_processor_module, method_name):
|
30 |
+
raise NotImplementedError
|
31 |
+
except ModuleNotFoundError:
|
32 |
+
sys.exit(f'Frame processor {frame_processor} not found.')
|
33 |
+
except NotImplementedError:
|
34 |
+
sys.exit(f'Frame processor {frame_processor} not implemented correctly.')
|
35 |
+
return frame_processor_module
|
36 |
+
|
37 |
+
|
38 |
+
def get_frame_processors_modules(frame_processors: List[str]) -> List[ModuleType]:
|
39 |
+
global FRAME_PROCESSORS_MODULES
|
40 |
+
|
41 |
+
if not FRAME_PROCESSORS_MODULES:
|
42 |
+
for frame_processor in frame_processors:
|
43 |
+
frame_processor_module = load_frame_processor_module(frame_processor)
|
44 |
+
FRAME_PROCESSORS_MODULES.append(frame_processor_module)
|
45 |
+
return FRAME_PROCESSORS_MODULES
|
46 |
+
|
47 |
+
|
48 |
+
def multi_process_frame(source_path: str, temp_frame_paths: List[str], process_frames: Callable[[str, List[str], Any], None], update: Callable[[], None]) -> None:
|
49 |
+
with ThreadPoolExecutor(max_workers=roop.globals.execution_threads) as executor:
|
50 |
+
futures = []
|
51 |
+
queue = create_queue(temp_frame_paths)
|
52 |
+
queue_per_future = max(len(temp_frame_paths) // roop.globals.execution_threads, 1)
|
53 |
+
while not queue.empty():
|
54 |
+
future = executor.submit(process_frames, source_path, pick_queue(queue, queue_per_future), update)
|
55 |
+
futures.append(future)
|
56 |
+
for future in as_completed(futures):
|
57 |
+
future.result()
|
58 |
+
|
59 |
+
|
60 |
+
def create_queue(temp_frame_paths: List[str]) -> Queue[str]:
|
61 |
+
queue: Queue[str] = Queue()
|
62 |
+
for frame_path in temp_frame_paths:
|
63 |
+
queue.put(frame_path)
|
64 |
+
return queue
|
65 |
+
|
66 |
+
|
67 |
+
def pick_queue(queue: Queue[str], queue_per_future: int) -> List[str]:
|
68 |
+
queues = []
|
69 |
+
for _ in range(queue_per_future):
|
70 |
+
if not queue.empty():
|
71 |
+
queues.append(queue.get())
|
72 |
+
return queues
|
73 |
+
|
74 |
+
|
75 |
+
def process_video(source_path: str, frame_paths: list[str], process_frames: Callable[[str, List[str], Any], None]) -> None:
|
76 |
+
progress_bar_format = '{l_bar}{bar}| {n_fmt}/{total_fmt} [{elapsed}<{remaining}, {rate_fmt}{postfix}]'
|
77 |
+
total = len(frame_paths)
|
78 |
+
with tqdm(total=total, desc='Processing', unit='frame', dynamic_ncols=True, bar_format=progress_bar_format) as progress:
|
79 |
+
multi_process_frame(source_path, frame_paths, process_frames, lambda: update_progress(progress))
|
80 |
+
|
81 |
+
|
82 |
+
def update_progress(progress: Any = None) -> None:
|
83 |
+
process = psutil.Process(os.getpid())
|
84 |
+
memory_usage = process.memory_info().rss / 1024 / 1024 / 1024
|
85 |
+
progress.set_postfix({
|
86 |
+
'memory_usage': '{:.2f}'.format(memory_usage).zfill(5) + 'GB',
|
87 |
+
'execution_providers': roop.globals.execution_providers,
|
88 |
+
'execution_threads': roop.globals.execution_threads
|
89 |
+
})
|
90 |
+
progress.refresh()
|
91 |
+
progress.update(1)
|
roop/processors/frame/face_enhancer.py
ADDED
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Any, List, Callable
|
2 |
+
import cv2
|
3 |
+
import threading
|
4 |
+
from gfpgan.utils import GFPGANer
|
5 |
+
|
6 |
+
import roop.globals
|
7 |
+
import roop.processors.frame.core
|
8 |
+
from roop.core import update_status
|
9 |
+
from roop.face_analyser import get_many_faces
|
10 |
+
from roop.typing import Frame, Face
|
11 |
+
from roop.utilities import conditional_download, resolve_relative_path, is_image, is_video
|
12 |
+
|
13 |
+
FACE_ENHANCER = None
|
14 |
+
THREAD_SEMAPHORE = threading.Semaphore()
|
15 |
+
THREAD_LOCK = threading.Lock()
|
16 |
+
NAME = 'ROOP.FACE-ENHANCER'
|
17 |
+
|
18 |
+
|
19 |
+
def get_face_enhancer() -> Any:
|
20 |
+
global FACE_ENHANCER
|
21 |
+
|
22 |
+
with THREAD_LOCK:
|
23 |
+
if FACE_ENHANCER is None:
|
24 |
+
model_path = resolve_relative_path('../models/GFPGANv1.4.pth')
|
25 |
+
# todo: set models path -> https://github.com/TencentARC/GFPGAN/issues/399
|
26 |
+
FACE_ENHANCER = GFPGANer(model_path=model_path, upscale=1, device=get_device())
|
27 |
+
return FACE_ENHANCER
|
28 |
+
|
29 |
+
|
30 |
+
def get_device() -> str:
|
31 |
+
if 'CUDAExecutionProvider' in roop.globals.execution_providers:
|
32 |
+
return 'cuda'
|
33 |
+
if 'CoreMLExecutionProvider' in roop.globals.execution_providers:
|
34 |
+
return 'mps'
|
35 |
+
return 'cpu'
|
36 |
+
|
37 |
+
|
38 |
+
def clear_face_enhancer() -> None:
|
39 |
+
global FACE_ENHANCER
|
40 |
+
|
41 |
+
FACE_ENHANCER = None
|
42 |
+
|
43 |
+
|
44 |
+
def pre_check() -> bool:
|
45 |
+
download_directory_path = resolve_relative_path('../models')
|
46 |
+
conditional_download(download_directory_path, ['https://github.com/TencentARC/GFPGAN/releases/download/v1.3.4/GFPGANv1.4.pth'])
|
47 |
+
return True
|
48 |
+
|
49 |
+
|
50 |
+
def pre_start() -> bool:
|
51 |
+
if not is_image(roop.globals.target_path) and not is_video(roop.globals.target_path):
|
52 |
+
update_status('Select an image or video for target path.', NAME)
|
53 |
+
return False
|
54 |
+
return True
|
55 |
+
|
56 |
+
|
57 |
+
def post_process() -> None:
|
58 |
+
clear_face_enhancer()
|
59 |
+
|
60 |
+
|
61 |
+
def enhance_face(target_face: Face, temp_frame: Frame) -> Frame:
|
62 |
+
start_x, start_y, end_x, end_y = map(int, target_face['bbox'])
|
63 |
+
padding_x = int((end_x - start_x) * 0.5)
|
64 |
+
padding_y = int((end_y - start_y) * 0.5)
|
65 |
+
start_x = max(0, start_x - padding_x)
|
66 |
+
start_y = max(0, start_y - padding_y)
|
67 |
+
end_x = max(0, end_x + padding_x)
|
68 |
+
end_y = max(0, end_y + padding_y)
|
69 |
+
temp_face = temp_frame[start_y:end_y, start_x:end_x]
|
70 |
+
if temp_face.size:
|
71 |
+
with THREAD_SEMAPHORE:
|
72 |
+
_, _, temp_face = get_face_enhancer().enhance(
|
73 |
+
temp_face,
|
74 |
+
paste_back=True
|
75 |
+
)
|
76 |
+
temp_frame[start_y:end_y, start_x:end_x] = temp_face
|
77 |
+
return temp_frame
|
78 |
+
|
79 |
+
|
80 |
+
def process_frame(source_face: Face, reference_face: Face, temp_frame: Frame) -> Frame:
|
81 |
+
many_faces = get_many_faces(temp_frame)
|
82 |
+
if many_faces:
|
83 |
+
for target_face in many_faces:
|
84 |
+
temp_frame = enhance_face(target_face, temp_frame)
|
85 |
+
return temp_frame
|
86 |
+
|
87 |
+
|
88 |
+
def process_frames(source_path: str, temp_frame_paths: List[str], update: Callable[[], None]) -> None:
|
89 |
+
for temp_frame_path in temp_frame_paths:
|
90 |
+
temp_frame = cv2.imread(temp_frame_path)
|
91 |
+
result = process_frame(None, None, temp_frame)
|
92 |
+
cv2.imwrite(temp_frame_path, result)
|
93 |
+
if update:
|
94 |
+
update()
|
95 |
+
|
96 |
+
|
97 |
+
def process_image(source_path: str, target_path: str, output_path: str) -> None:
|
98 |
+
target_frame = cv2.imread(target_path)
|
99 |
+
result = process_frame(None, None, target_frame)
|
100 |
+
cv2.imwrite(output_path, result)
|
101 |
+
|
102 |
+
|
103 |
+
def process_video(source_path: str, temp_frame_paths: List[str]) -> None:
|
104 |
+
roop.processors.frame.core.process_video(None, temp_frame_paths, process_frames)
|
roop/processors/frame/face_swapper.py
ADDED
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Any, List, Callable
|
2 |
+
import cv2
|
3 |
+
import insightface
|
4 |
+
import threading
|
5 |
+
|
6 |
+
import roop.globals
|
7 |
+
import roop.processors.frame.core
|
8 |
+
from roop.core import update_status
|
9 |
+
from roop.face_analyser import get_one_face, get_many_faces, find_similar_face
|
10 |
+
from roop.face_reference import get_face_reference, set_face_reference, clear_face_reference
|
11 |
+
from roop.typing import Face, Frame
|
12 |
+
from roop.utilities import conditional_download, resolve_relative_path, is_image, is_video
|
13 |
+
|
14 |
+
FACE_SWAPPER = None
|
15 |
+
THREAD_LOCK = threading.Lock()
|
16 |
+
NAME = 'ROOP.FACE-SWAPPER'
|
17 |
+
|
18 |
+
|
19 |
+
def get_face_swapper() -> Any:
|
20 |
+
global FACE_SWAPPER
|
21 |
+
|
22 |
+
with THREAD_LOCK:
|
23 |
+
if FACE_SWAPPER is None:
|
24 |
+
model_path = resolve_relative_path('../models/inswapper_128.onnx')
|
25 |
+
FACE_SWAPPER = insightface.model_zoo.get_model(model_path, providers=roop.globals.execution_providers)
|
26 |
+
return FACE_SWAPPER
|
27 |
+
|
28 |
+
|
29 |
+
def clear_face_swapper() -> None:
|
30 |
+
global FACE_SWAPPER
|
31 |
+
|
32 |
+
FACE_SWAPPER = None
|
33 |
+
|
34 |
+
|
35 |
+
def pre_check() -> bool:
|
36 |
+
download_directory_path = resolve_relative_path('../models')
|
37 |
+
conditional_download(download_directory_path, ['https://huggingface.co/CountFloyd/deepfake/resolve/main/inswapper_128.onnx'])
|
38 |
+
return True
|
39 |
+
|
40 |
+
|
41 |
+
def pre_start() -> bool:
|
42 |
+
if not is_image(roop.globals.source_path):
|
43 |
+
update_status('Select an image for source path.', NAME)
|
44 |
+
return False
|
45 |
+
elif not get_one_face(cv2.imread(roop.globals.source_path)):
|
46 |
+
update_status('No face in source path detected.', NAME)
|
47 |
+
return False
|
48 |
+
if not is_image(roop.globals.target_path) and not is_video(roop.globals.target_path):
|
49 |
+
update_status('Select an image or video for target path.', NAME)
|
50 |
+
return False
|
51 |
+
return True
|
52 |
+
|
53 |
+
|
54 |
+
def post_process() -> None:
|
55 |
+
clear_face_swapper()
|
56 |
+
clear_face_reference()
|
57 |
+
|
58 |
+
|
59 |
+
def swap_face(source_face: Face, target_face: Face, temp_frame: Frame) -> Frame:
|
60 |
+
return get_face_swapper().get(temp_frame, target_face, source_face, paste_back=True)
|
61 |
+
|
62 |
+
|
63 |
+
def process_frame(source_face: Face, reference_face: Face, temp_frame: Frame) -> Frame:
|
64 |
+
if roop.globals.many_faces:
|
65 |
+
many_faces = get_many_faces(temp_frame)
|
66 |
+
if many_faces:
|
67 |
+
for target_face in many_faces:
|
68 |
+
temp_frame = swap_face(source_face, target_face, temp_frame)
|
69 |
+
else:
|
70 |
+
target_face = find_similar_face(temp_frame, reference_face)
|
71 |
+
if target_face:
|
72 |
+
temp_frame = swap_face(source_face, target_face, temp_frame)
|
73 |
+
return temp_frame
|
74 |
+
|
75 |
+
|
76 |
+
def process_frames(source_path: str, temp_frame_paths: List[str], update: Callable[[], None]) -> None:
|
77 |
+
source_face = get_one_face(cv2.imread(source_path))
|
78 |
+
reference_face = None if roop.globals.many_faces else get_face_reference()
|
79 |
+
for temp_frame_path in temp_frame_paths:
|
80 |
+
temp_frame = cv2.imread(temp_frame_path)
|
81 |
+
result = process_frame(source_face, reference_face, temp_frame)
|
82 |
+
cv2.imwrite(temp_frame_path, result)
|
83 |
+
if update:
|
84 |
+
update()
|
85 |
+
|
86 |
+
|
87 |
+
def process_image(source_path: str, target_path: str, output_path: str) -> None:
|
88 |
+
source_face = get_one_face(cv2.imread(source_path))
|
89 |
+
target_frame = cv2.imread(target_path)
|
90 |
+
reference_face = None if roop.globals.many_faces else get_one_face(target_frame, roop.globals.reference_face_position)
|
91 |
+
result = process_frame(source_face, reference_face, target_frame)
|
92 |
+
cv2.imwrite(output_path, result)
|
93 |
+
|
94 |
+
|
95 |
+
def process_video(source_path: str, temp_frame_paths: List[str]) -> None:
|
96 |
+
if not roop.globals.many_faces and not get_face_reference():
|
97 |
+
reference_frame = cv2.imread(temp_frame_paths[roop.globals.reference_frame_number])
|
98 |
+
reference_face = get_one_face(reference_frame, roop.globals.reference_face_position)
|
99 |
+
set_face_reference(reference_face)
|
100 |
+
roop.processors.frame.core.process_video(source_path, temp_frame_paths, process_frames)
|
roop/typing.py
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Any
|
2 |
+
|
3 |
+
from insightface.app.common import Face
|
4 |
+
import numpy
|
5 |
+
|
6 |
+
Face = Face
|
7 |
+
Frame = numpy.ndarray[Any, Any]
|
roop/ui.json
ADDED
@@ -0,0 +1,161 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"CTk": {
|
3 |
+
"fg_color": ["gray95", "gray10"]
|
4 |
+
},
|
5 |
+
"CTkToplevel": {
|
6 |
+
"fg_color": ["gray95", "gray10"]
|
7 |
+
},
|
8 |
+
"CTkFrame": {
|
9 |
+
"corner_radius": 6,
|
10 |
+
"border_width": 0,
|
11 |
+
"fg_color": ["gray90", "gray13"],
|
12 |
+
"top_fg_color": ["gray85", "gray16"],
|
13 |
+
"border_color": ["gray65", "gray28"]
|
14 |
+
},
|
15 |
+
"CTkButton": {
|
16 |
+
"corner_radius": 6,
|
17 |
+
"border_width": 0,
|
18 |
+
"fg_color": ["#3a7ebf", "#1f538d"],
|
19 |
+
"hover_color": ["#325882", "#14375e"],
|
20 |
+
"border_color": ["#3E454A", "#949A9F"],
|
21 |
+
"text_color": ["#DCE4EE", "#DCE4EE"],
|
22 |
+
"text_color_disabled": ["gray74", "gray60"]
|
23 |
+
},
|
24 |
+
"CTkLabel": {
|
25 |
+
"corner_radius": 0,
|
26 |
+
"fg_color": "transparent",
|
27 |
+
"text_color": ["gray14", "gray84"]
|
28 |
+
},
|
29 |
+
"CTkEntry": {
|
30 |
+
"corner_radius": 6,
|
31 |
+
"border_width": 2,
|
32 |
+
"fg_color": ["#F9F9FA", "#343638"],
|
33 |
+
"border_color": ["#979DA2", "#565B5E"],
|
34 |
+
"text_color": ["gray14", "gray84"],
|
35 |
+
"placeholder_text_color": ["gray52", "gray62"]
|
36 |
+
},
|
37 |
+
"CTkCheckbox": {
|
38 |
+
"corner_radius": 6,
|
39 |
+
"border_width": 3,
|
40 |
+
"fg_color": ["#3a7ebf", "#1f538d"],
|
41 |
+
"border_color": ["#3E454A", "#949A9F"],
|
42 |
+
"hover_color": ["#325882", "#14375e"],
|
43 |
+
"checkmark_color": ["#DCE4EE", "gray90"],
|
44 |
+
"text_color": ["gray14", "gray84"],
|
45 |
+
"text_color_disabled": ["gray60", "gray45"]
|
46 |
+
},
|
47 |
+
"CTkSwitch": {
|
48 |
+
"corner_radius": 1000,
|
49 |
+
"border_width": 3,
|
50 |
+
"button_length": 0,
|
51 |
+
"fg_color": ["#939BA2", "#4A4D50"],
|
52 |
+
"progress_color": ["#3a7ebf", "#1f538d"],
|
53 |
+
"button_color": ["gray36", "#D5D9DE"],
|
54 |
+
"button_hover_color": ["gray20", "gray100"],
|
55 |
+
"text_color": ["gray14", "gray84"],
|
56 |
+
"text_color_disabled": ["gray60", "gray45"]
|
57 |
+
},
|
58 |
+
"CTkRadiobutton": {
|
59 |
+
"corner_radius": 1000,
|
60 |
+
"border_width_checked": 6,
|
61 |
+
"border_width_unchecked": 3,
|
62 |
+
"fg_color": ["#3a7ebf", "#1f538d"],
|
63 |
+
"border_color": ["#3E454A", "#949A9F"],
|
64 |
+
"hover_color": ["#325882", "#14375e"],
|
65 |
+
"text_color": ["gray14", "gray84"],
|
66 |
+
"text_color_disabled": ["gray60", "gray45"]
|
67 |
+
},
|
68 |
+
"CTkProgressBar": {
|
69 |
+
"corner_radius": 1000,
|
70 |
+
"border_width": 0,
|
71 |
+
"fg_color": ["#939BA2", "#4A4D50"],
|
72 |
+
"progress_color": ["#3a7ebf", "#1f538d"],
|
73 |
+
"border_color": ["gray", "gray"]
|
74 |
+
},
|
75 |
+
"CTkSlider": {
|
76 |
+
"corner_radius": 1000,
|
77 |
+
"button_corner_radius": 1000,
|
78 |
+
"border_width": 6,
|
79 |
+
"button_length": 0,
|
80 |
+
"fg_color": ["#939BA2", "#4A4D50"],
|
81 |
+
"progress_color": ["gray40", "#AAB0B5"],
|
82 |
+
"button_color": ["#3a7ebf", "#1f538d"],
|
83 |
+
"button_hover_color": ["#325882", "#14375e"]
|
84 |
+
},
|
85 |
+
"CTkOptionMenu": {
|
86 |
+
"corner_radius": 6,
|
87 |
+
"fg_color": ["#3a7ebf", "#1f538d"],
|
88 |
+
"button_color": ["#325882", "#14375e"],
|
89 |
+
"button_hover_color": ["#234567", "#1e2c40"],
|
90 |
+
"text_color": ["#DCE4EE", "#DCE4EE"],
|
91 |
+
"text_color_disabled": ["gray74", "gray60"]
|
92 |
+
},
|
93 |
+
"CTkComboBox": {
|
94 |
+
"corner_radius": 6,
|
95 |
+
"border_width": 2,
|
96 |
+
"fg_color": ["#F9F9FA", "#343638"],
|
97 |
+
"border_color": ["#979DA2", "#565B5E"],
|
98 |
+
"button_color": ["#979DA2", "#565B5E"],
|
99 |
+
"button_hover_color": ["#6E7174", "#7A848D"],
|
100 |
+
"text_color": ["gray14", "gray84"],
|
101 |
+
"text_color_disabled": ["gray50", "gray45"]
|
102 |
+
},
|
103 |
+
"CTkScrollbar": {
|
104 |
+
"corner_radius": 1000,
|
105 |
+
"border_spacing": 4,
|
106 |
+
"fg_color": "transparent",
|
107 |
+
"button_color": ["gray55", "gray41"],
|
108 |
+
"button_hover_color": ["gray40", "gray53"]
|
109 |
+
},
|
110 |
+
"CTkSegmentedButton": {
|
111 |
+
"corner_radius": 6,
|
112 |
+
"border_width": 2,
|
113 |
+
"fg_color": ["#979DA2", "gray29"],
|
114 |
+
"selected_color": ["#3a7ebf", "#1f538d"],
|
115 |
+
"selected_hover_color": ["#325882", "#14375e"],
|
116 |
+
"unselected_color": ["#979DA2", "gray29"],
|
117 |
+
"unselected_hover_color": ["gray70", "gray41"],
|
118 |
+
"text_color": ["#DCE4EE", "#DCE4EE"],
|
119 |
+
"text_color_disabled": ["gray74", "gray60"]
|
120 |
+
},
|
121 |
+
"CTkTextbox": {
|
122 |
+
"corner_radius": 6,
|
123 |
+
"border_width": 0,
|
124 |
+
"fg_color": ["gray100", "gray20"],
|
125 |
+
"border_color": ["#979DA2", "#565B5E"],
|
126 |
+
"text_color": ["gray14", "gray84"],
|
127 |
+
"scrollbar_button_color": ["gray55", "gray41"],
|
128 |
+
"scrollbar_button_hover_color": ["gray40", "gray53"]
|
129 |
+
},
|
130 |
+
"CTkScrollableFrame": {
|
131 |
+
"label_fg_color": ["gray80", "gray21"]
|
132 |
+
},
|
133 |
+
"DropdownMenu": {
|
134 |
+
"fg_color": ["gray90", "gray20"],
|
135 |
+
"hover_color": ["gray75", "gray28"],
|
136 |
+
"text_color": ["gray14", "gray84"]
|
137 |
+
},
|
138 |
+
"CTkFont": {
|
139 |
+
"macOS": {
|
140 |
+
"family": "Avenir",
|
141 |
+
"size": 12,
|
142 |
+
"weight": "normal"
|
143 |
+
},
|
144 |
+
"Windows": {
|
145 |
+
"family": "Corbel",
|
146 |
+
"size": 12,
|
147 |
+
"weight": "normal"
|
148 |
+
},
|
149 |
+
"Linux": {
|
150 |
+
"family": "Montserrat",
|
151 |
+
"size": 12,
|
152 |
+
"weight": "normal"
|
153 |
+
}
|
154 |
+
},
|
155 |
+
"RoopDropArea": {
|
156 |
+
"fg_color": ["gray90", "gray13"]
|
157 |
+
},
|
158 |
+
"RoopDonate": {
|
159 |
+
"text_color": ["#3a7ebf", "gray60"]
|
160 |
+
}
|
161 |
+
}
|
roop/ui.py
ADDED
@@ -0,0 +1,285 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import sys
|
3 |
+
import webbrowser
|
4 |
+
import customtkinter as ctk
|
5 |
+
from tkinterdnd2 import TkinterDnD, DND_ALL
|
6 |
+
from typing import Any, Callable, Tuple, Optional
|
7 |
+
import cv2
|
8 |
+
from PIL import Image, ImageOps
|
9 |
+
|
10 |
+
import roop.globals
|
11 |
+
import roop.metadata
|
12 |
+
from roop.face_analyser import get_one_face
|
13 |
+
from roop.capturer import get_video_frame, get_video_frame_total
|
14 |
+
from roop.face_reference import get_face_reference, set_face_reference, clear_face_reference
|
15 |
+
from roop.predictor import predict_frame, clear_predictor
|
16 |
+
from roop.processors.frame.core import get_frame_processors_modules
|
17 |
+
from roop.utilities import is_image, is_video, resolve_relative_path
|
18 |
+
|
19 |
+
ROOT = None
|
20 |
+
ROOT_HEIGHT = 700
|
21 |
+
ROOT_WIDTH = 600
|
22 |
+
|
23 |
+
PREVIEW = None
|
24 |
+
PREVIEW_MAX_HEIGHT = 700
|
25 |
+
PREVIEW_MAX_WIDTH = 1200
|
26 |
+
|
27 |
+
RECENT_DIRECTORY_SOURCE = None
|
28 |
+
RECENT_DIRECTORY_TARGET = None
|
29 |
+
RECENT_DIRECTORY_OUTPUT = None
|
30 |
+
|
31 |
+
preview_label = None
|
32 |
+
preview_slider = None
|
33 |
+
source_label = None
|
34 |
+
target_label = None
|
35 |
+
status_label = None
|
36 |
+
|
37 |
+
|
38 |
+
# todo: remove by native support -> https://github.com/TomSchimansky/CustomTkinter/issues/934
|
39 |
+
class CTk(ctk.CTk, TkinterDnD.DnDWrapper):
|
40 |
+
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
41 |
+
super().__init__(*args, **kwargs)
|
42 |
+
self.TkdndVersion = TkinterDnD._require(self)
|
43 |
+
|
44 |
+
|
45 |
+
def init(start: Callable[[], None], destroy: Callable[[], None]) -> ctk.CTk:
|
46 |
+
global ROOT, PREVIEW
|
47 |
+
|
48 |
+
ROOT = create_root(start, destroy)
|
49 |
+
PREVIEW = create_preview(ROOT)
|
50 |
+
|
51 |
+
return ROOT
|
52 |
+
|
53 |
+
|
54 |
+
def create_root(start: Callable[[], None], destroy: Callable[[], None]) -> ctk.CTk:
|
55 |
+
global source_label, target_label, status_label
|
56 |
+
|
57 |
+
ctk.deactivate_automatic_dpi_awareness()
|
58 |
+
ctk.set_appearance_mode('system')
|
59 |
+
ctk.set_default_color_theme(resolve_relative_path('ui.json'))
|
60 |
+
|
61 |
+
root = CTk()
|
62 |
+
root.minsize(ROOT_WIDTH, ROOT_HEIGHT)
|
63 |
+
root.title(f'{roop.metadata.name} {roop.metadata.version}')
|
64 |
+
root.configure()
|
65 |
+
root.protocol('WM_DELETE_WINDOW', lambda: destroy())
|
66 |
+
|
67 |
+
source_label = ctk.CTkLabel(root, text=None, fg_color=ctk.ThemeManager.theme.get('RoopDropArea').get('fg_color'))
|
68 |
+
source_label.place(relx=0.1, rely=0.1, relwidth=0.3, relheight=0.25)
|
69 |
+
source_label.drop_target_register(DND_ALL)
|
70 |
+
source_label.dnd_bind('<<Drop>>', lambda event: select_source_path(event.data))
|
71 |
+
if roop.globals.source_path:
|
72 |
+
select_source_path(roop.globals.source_path)
|
73 |
+
|
74 |
+
target_label = ctk.CTkLabel(root, text=None, fg_color=ctk.ThemeManager.theme.get('RoopDropArea').get('fg_color'))
|
75 |
+
target_label.place(relx=0.6, rely=0.1, relwidth=0.3, relheight=0.25)
|
76 |
+
target_label.drop_target_register(DND_ALL)
|
77 |
+
target_label.dnd_bind('<<Drop>>', lambda event: select_target_path(event.data))
|
78 |
+
if roop.globals.target_path:
|
79 |
+
select_target_path(roop.globals.target_path)
|
80 |
+
|
81 |
+
source_button = ctk.CTkButton(root, text='Select a face', cursor='hand2', command=lambda: select_source_path())
|
82 |
+
source_button.place(relx=0.1, rely=0.4, relwidth=0.3, relheight=0.1)
|
83 |
+
|
84 |
+
target_button = ctk.CTkButton(root, text='Select a target', cursor='hand2', command=lambda: select_target_path())
|
85 |
+
target_button.place(relx=0.6, rely=0.4, relwidth=0.3, relheight=0.1)
|
86 |
+
|
87 |
+
keep_fps_value = ctk.BooleanVar(value=roop.globals.keep_fps)
|
88 |
+
keep_fps_checkbox = ctk.CTkSwitch(root, text='Keep target fps', variable=keep_fps_value, cursor='hand2', command=lambda: setattr(roop.globals, 'keep_fps', not roop.globals.keep_fps))
|
89 |
+
keep_fps_checkbox.place(relx=0.1, rely=0.6)
|
90 |
+
|
91 |
+
keep_frames_value = ctk.BooleanVar(value=roop.globals.keep_frames)
|
92 |
+
keep_frames_switch = ctk.CTkSwitch(root, text='Keep temporary frames', variable=keep_frames_value, cursor='hand2', command=lambda: setattr(roop.globals, 'keep_frames', keep_frames_value.get()))
|
93 |
+
keep_frames_switch.place(relx=0.1, rely=0.65)
|
94 |
+
|
95 |
+
skip_audio_value = ctk.BooleanVar(value=roop.globals.skip_audio)
|
96 |
+
skip_audio_switch = ctk.CTkSwitch(root, text='Skip target audio', variable=skip_audio_value, cursor='hand2', command=lambda: setattr(roop.globals, 'skip_audio', skip_audio_value.get()))
|
97 |
+
skip_audio_switch.place(relx=0.6, rely=0.6)
|
98 |
+
|
99 |
+
many_faces_value = ctk.BooleanVar(value=roop.globals.many_faces)
|
100 |
+
many_faces_switch = ctk.CTkSwitch(root, text='Many faces', variable=many_faces_value, cursor='hand2', command=lambda: setattr(roop.globals, 'many_faces', many_faces_value.get()))
|
101 |
+
many_faces_switch.place(relx=0.6, rely=0.65)
|
102 |
+
|
103 |
+
start_button = ctk.CTkButton(root, text='Start', cursor='hand2', command=lambda: select_output_path(start))
|
104 |
+
start_button.place(relx=0.15, rely=0.75, relwidth=0.2, relheight=0.05)
|
105 |
+
|
106 |
+
stop_button = ctk.CTkButton(root, text='Destroy', cursor='hand2', command=lambda: destroy())
|
107 |
+
stop_button.place(relx=0.4, rely=0.75, relwidth=0.2, relheight=0.05)
|
108 |
+
|
109 |
+
preview_button = ctk.CTkButton(root, text='Preview', cursor='hand2', command=lambda: toggle_preview())
|
110 |
+
preview_button.place(relx=0.65, rely=0.75, relwidth=0.2, relheight=0.05)
|
111 |
+
|
112 |
+
status_label = ctk.CTkLabel(root, text=None, justify='center')
|
113 |
+
status_label.place(relx=0.1, rely=0.9, relwidth=0.8)
|
114 |
+
|
115 |
+
donate_label = ctk.CTkLabel(root, text='^_^ Donate to project ^_^', justify='center', cursor='hand2')
|
116 |
+
donate_label.place(relx=0.1, rely=0.95, relwidth=0.8)
|
117 |
+
donate_label.configure(text_color=ctk.ThemeManager.theme.get('RoopDonate').get('text_color'))
|
118 |
+
donate_label.bind('<Button>', lambda event: webbrowser.open('https://github.com/sponsors/s0md3v'))
|
119 |
+
|
120 |
+
return root
|
121 |
+
|
122 |
+
|
123 |
+
def create_preview(parent: ctk.CTkToplevel) -> ctk.CTkToplevel:
|
124 |
+
global preview_label, preview_slider
|
125 |
+
|
126 |
+
preview = ctk.CTkToplevel(parent)
|
127 |
+
preview.withdraw()
|
128 |
+
preview.configure()
|
129 |
+
preview.protocol('WM_DELETE_WINDOW', lambda: toggle_preview())
|
130 |
+
preview.resizable(width=False, height=False)
|
131 |
+
|
132 |
+
preview_label = ctk.CTkLabel(preview, text=None)
|
133 |
+
preview_label.pack(fill='both', expand=True)
|
134 |
+
|
135 |
+
preview_slider = ctk.CTkSlider(preview, from_=0, to=0, command=lambda frame_value: update_preview(frame_value))
|
136 |
+
|
137 |
+
preview.bind('<Up>', lambda event: update_face_reference(1))
|
138 |
+
preview.bind('<Down>', lambda event: update_face_reference(-1))
|
139 |
+
return preview
|
140 |
+
|
141 |
+
|
142 |
+
def update_status(text: str) -> None:
|
143 |
+
status_label.configure(text=text)
|
144 |
+
ROOT.update()
|
145 |
+
|
146 |
+
|
147 |
+
def select_source_path(source_path: Optional[str] = None) -> None:
|
148 |
+
global RECENT_DIRECTORY_SOURCE
|
149 |
+
|
150 |
+
if PREVIEW:
|
151 |
+
PREVIEW.withdraw()
|
152 |
+
if source_path is None:
|
153 |
+
source_path = ctk.filedialog.askopenfilename(title='select an source image', initialdir=RECENT_DIRECTORY_SOURCE)
|
154 |
+
if is_image(source_path):
|
155 |
+
roop.globals.source_path = source_path
|
156 |
+
RECENT_DIRECTORY_SOURCE = os.path.dirname(roop.globals.source_path)
|
157 |
+
image = render_image_preview(roop.globals.source_path, (200, 200))
|
158 |
+
source_label.configure(image=image)
|
159 |
+
else:
|
160 |
+
roop.globals.source_path = None
|
161 |
+
source_label.configure(image=None)
|
162 |
+
|
163 |
+
|
164 |
+
def select_target_path(target_path: Optional[str] = None) -> None:
|
165 |
+
global RECENT_DIRECTORY_TARGET
|
166 |
+
|
167 |
+
if PREVIEW:
|
168 |
+
PREVIEW.withdraw()
|
169 |
+
clear_face_reference()
|
170 |
+
if target_path is None:
|
171 |
+
target_path = ctk.filedialog.askopenfilename(title='select an target image or video', initialdir=RECENT_DIRECTORY_TARGET)
|
172 |
+
if is_image(target_path):
|
173 |
+
roop.globals.target_path = target_path
|
174 |
+
RECENT_DIRECTORY_TARGET = os.path.dirname(roop.globals.target_path)
|
175 |
+
image = render_image_preview(roop.globals.target_path, (200, 200))
|
176 |
+
target_label.configure(image=image)
|
177 |
+
elif is_video(target_path):
|
178 |
+
roop.globals.target_path = target_path
|
179 |
+
RECENT_DIRECTORY_TARGET = os.path.dirname(roop.globals.target_path)
|
180 |
+
video_frame = render_video_preview(target_path, (200, 200))
|
181 |
+
target_label.configure(image=video_frame)
|
182 |
+
else:
|
183 |
+
roop.globals.target_path = None
|
184 |
+
target_label.configure(image=None)
|
185 |
+
|
186 |
+
|
187 |
+
def select_output_path(start: Callable[[], None]) -> None:
|
188 |
+
global RECENT_DIRECTORY_OUTPUT
|
189 |
+
|
190 |
+
if is_image(roop.globals.target_path):
|
191 |
+
output_path = ctk.filedialog.asksaveasfilename(title='save image output file', defaultextension='.png', initialfile='output.png', initialdir=RECENT_DIRECTORY_OUTPUT)
|
192 |
+
elif is_video(roop.globals.target_path):
|
193 |
+
output_path = ctk.filedialog.asksaveasfilename(title='save video output file', defaultextension='.mp4', initialfile='output.mp4', initialdir=RECENT_DIRECTORY_OUTPUT)
|
194 |
+
else:
|
195 |
+
output_path = None
|
196 |
+
if output_path:
|
197 |
+
roop.globals.output_path = output_path
|
198 |
+
RECENT_DIRECTORY_OUTPUT = os.path.dirname(roop.globals.output_path)
|
199 |
+
start()
|
200 |
+
|
201 |
+
|
202 |
+
def render_image_preview(image_path: str, size: Tuple[int, int]) -> ctk.CTkImage:
|
203 |
+
image = Image.open(image_path)
|
204 |
+
if size:
|
205 |
+
image = ImageOps.fit(image, size, Image.LANCZOS)
|
206 |
+
return ctk.CTkImage(image, size=image.size)
|
207 |
+
|
208 |
+
|
209 |
+
def render_video_preview(video_path: str, size: Tuple[int, int], frame_number: int = 0) -> ctk.CTkImage:
|
210 |
+
capture = cv2.VideoCapture(video_path)
|
211 |
+
if frame_number:
|
212 |
+
capture.set(cv2.CAP_PROP_POS_FRAMES, frame_number)
|
213 |
+
has_frame, frame = capture.read()
|
214 |
+
if has_frame:
|
215 |
+
image = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
216 |
+
if size:
|
217 |
+
image = ImageOps.fit(image, size, Image.LANCZOS)
|
218 |
+
return ctk.CTkImage(image, size=image.size)
|
219 |
+
capture.release()
|
220 |
+
cv2.destroyAllWindows()
|
221 |
+
|
222 |
+
|
223 |
+
def toggle_preview() -> None:
|
224 |
+
if PREVIEW.state() == 'normal':
|
225 |
+
PREVIEW.unbind('<Right>')
|
226 |
+
PREVIEW.unbind('<Left>')
|
227 |
+
PREVIEW.withdraw()
|
228 |
+
clear_predictor()
|
229 |
+
elif roop.globals.source_path and roop.globals.target_path:
|
230 |
+
init_preview()
|
231 |
+
update_preview(roop.globals.reference_frame_number)
|
232 |
+
PREVIEW.deiconify()
|
233 |
+
|
234 |
+
|
235 |
+
def init_preview() -> None:
|
236 |
+
PREVIEW.title('Preview [ ↕ Reference face ]')
|
237 |
+
if is_image(roop.globals.target_path):
|
238 |
+
preview_slider.pack_forget()
|
239 |
+
if is_video(roop.globals.target_path):
|
240 |
+
video_frame_total = get_video_frame_total(roop.globals.target_path)
|
241 |
+
if video_frame_total > 0:
|
242 |
+
PREVIEW.title('Preview [ ↕ Reference face ] [ ↔ Frame number ]')
|
243 |
+
PREVIEW.bind('<Right>', lambda event: update_frame(int(video_frame_total / 20)))
|
244 |
+
PREVIEW.bind('<Left>', lambda event: update_frame(int(video_frame_total / -20)))
|
245 |
+
preview_slider.configure(to=video_frame_total)
|
246 |
+
preview_slider.pack(fill='x')
|
247 |
+
preview_slider.set(roop.globals.reference_frame_number)
|
248 |
+
|
249 |
+
|
250 |
+
def update_preview(frame_number: int = 0) -> None:
|
251 |
+
if roop.globals.source_path and roop.globals.target_path:
|
252 |
+
temp_frame = get_video_frame(roop.globals.target_path, frame_number)
|
253 |
+
if predict_frame(temp_frame):
|
254 |
+
sys.exit()
|
255 |
+
source_face = get_one_face(cv2.imread(roop.globals.source_path))
|
256 |
+
if not get_face_reference():
|
257 |
+
reference_frame = get_video_frame(roop.globals.target_path, roop.globals.reference_frame_number)
|
258 |
+
reference_face = get_one_face(reference_frame, roop.globals.reference_face_position)
|
259 |
+
set_face_reference(reference_face)
|
260 |
+
else:
|
261 |
+
reference_face = get_face_reference()
|
262 |
+
for frame_processor in get_frame_processors_modules(roop.globals.frame_processors):
|
263 |
+
temp_frame = frame_processor.process_frame(
|
264 |
+
source_face,
|
265 |
+
reference_face,
|
266 |
+
temp_frame
|
267 |
+
)
|
268 |
+
image = Image.fromarray(cv2.cvtColor(temp_frame, cv2.COLOR_BGR2RGB))
|
269 |
+
image = ImageOps.contain(image, (PREVIEW_MAX_WIDTH, PREVIEW_MAX_HEIGHT), Image.LANCZOS)
|
270 |
+
image = ctk.CTkImage(image, size=image.size)
|
271 |
+
preview_label.configure(image=image)
|
272 |
+
|
273 |
+
|
274 |
+
def update_face_reference(steps: int) -> None:
|
275 |
+
clear_face_reference()
|
276 |
+
reference_frame_number = int(preview_slider.get())
|
277 |
+
roop.globals.reference_face_position += steps
|
278 |
+
roop.globals.reference_frame_number = reference_frame_number
|
279 |
+
update_preview(reference_frame_number)
|
280 |
+
|
281 |
+
|
282 |
+
def update_frame(steps: int) -> None:
|
283 |
+
frame_number = preview_slider.get() + steps
|
284 |
+
preview_slider.set(frame_number)
|
285 |
+
update_preview(preview_slider.get())
|
roop/utilities.py
ADDED
@@ -0,0 +1,149 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import glob
|
2 |
+
import mimetypes
|
3 |
+
import os
|
4 |
+
import platform
|
5 |
+
import shutil
|
6 |
+
import ssl
|
7 |
+
import subprocess
|
8 |
+
import urllib
|
9 |
+
from pathlib import Path
|
10 |
+
from typing import List, Optional
|
11 |
+
from tqdm import tqdm
|
12 |
+
|
13 |
+
import roop.globals
|
14 |
+
|
15 |
+
TEMP_DIRECTORY = 'temp'
|
16 |
+
TEMP_VIDEO_FILE = 'temp.mp4'
|
17 |
+
|
18 |
+
# monkey patch ssl for mac
|
19 |
+
if platform.system().lower() == 'darwin':
|
20 |
+
ssl._create_default_https_context = ssl._create_unverified_context
|
21 |
+
|
22 |
+
|
23 |
+
def run_ffmpeg(args: List[str]) -> bool:
|
24 |
+
commands = ['ffmpeg', '-hide_banner', '-loglevel', roop.globals.log_level]
|
25 |
+
commands.extend(args)
|
26 |
+
try:
|
27 |
+
subprocess.check_output(commands, stderr=subprocess.STDOUT)
|
28 |
+
return True
|
29 |
+
except Exception:
|
30 |
+
pass
|
31 |
+
return False
|
32 |
+
|
33 |
+
|
34 |
+
def detect_fps(target_path: str) -> float:
|
35 |
+
command = ['ffprobe', '-v', 'error', '-select_streams', 'v:0', '-show_entries', 'stream=r_frame_rate', '-of', 'default=noprint_wrappers=1:nokey=1', target_path]
|
36 |
+
output = subprocess.check_output(command).decode().strip().split('/')
|
37 |
+
try:
|
38 |
+
numerator, denominator = map(int, output)
|
39 |
+
return numerator / denominator
|
40 |
+
except Exception:
|
41 |
+
pass
|
42 |
+
return 30
|
43 |
+
|
44 |
+
|
45 |
+
def extract_frames(target_path: str, fps: float = 30) -> bool:
|
46 |
+
temp_directory_path = get_temp_directory_path(target_path)
|
47 |
+
temp_frame_quality = roop.globals.temp_frame_quality * 31 // 100
|
48 |
+
return run_ffmpeg(['-hwaccel', 'auto', '-i', target_path, '-q:v', str(temp_frame_quality), '-pix_fmt', 'rgb24', '-vf', 'fps=' + str(fps), os.path.join(temp_directory_path, '%04d.' + roop.globals.temp_frame_format)])
|
49 |
+
|
50 |
+
|
51 |
+
def create_video(target_path: str, fps: float = 30) -> bool:
|
52 |
+
temp_output_path = get_temp_output_path(target_path)
|
53 |
+
temp_directory_path = get_temp_directory_path(target_path)
|
54 |
+
output_video_quality = (roop.globals.output_video_quality + 1) * 51 // 100
|
55 |
+
commands = ['-hwaccel', 'auto', '-r', str(fps), '-i', os.path.join(temp_directory_path, '%04d.' + roop.globals.temp_frame_format), '-c:v', roop.globals.output_video_encoder]
|
56 |
+
if roop.globals.output_video_encoder in ['libx264', 'libx265', 'libvpx']:
|
57 |
+
commands.extend(['-crf', str(output_video_quality)])
|
58 |
+
if roop.globals.output_video_encoder in ['h264_nvenc', 'hevc_nvenc']:
|
59 |
+
commands.extend(['-cq', str(output_video_quality)])
|
60 |
+
commands.extend(['-pix_fmt', 'yuv420p', '-vf', 'colorspace=bt709:iall=bt601-6-625:fast=1', '-y', temp_output_path])
|
61 |
+
return run_ffmpeg(commands)
|
62 |
+
|
63 |
+
|
64 |
+
def restore_audio(target_path: str, output_path: str) -> None:
|
65 |
+
temp_output_path = get_temp_output_path(target_path)
|
66 |
+
done = run_ffmpeg(['-i', temp_output_path, '-i', target_path, '-c:v', 'copy', '-map', '0:v:0', '-map', '1:a:0', '-y', output_path])
|
67 |
+
if not done:
|
68 |
+
move_temp(target_path, output_path)
|
69 |
+
|
70 |
+
|
71 |
+
def get_temp_frame_paths(target_path: str) -> List[str]:
|
72 |
+
temp_directory_path = get_temp_directory_path(target_path)
|
73 |
+
return glob.glob((os.path.join(glob.escape(temp_directory_path), '*.' + roop.globals.temp_frame_format)))
|
74 |
+
|
75 |
+
|
76 |
+
def get_temp_directory_path(target_path: str) -> str:
|
77 |
+
target_name, _ = os.path.splitext(os.path.basename(target_path))
|
78 |
+
target_directory_path = os.path.dirname(target_path)
|
79 |
+
return os.path.join(target_directory_path, TEMP_DIRECTORY, target_name)
|
80 |
+
|
81 |
+
|
82 |
+
def get_temp_output_path(target_path: str) -> str:
|
83 |
+
temp_directory_path = get_temp_directory_path(target_path)
|
84 |
+
return os.path.join(temp_directory_path, TEMP_VIDEO_FILE)
|
85 |
+
|
86 |
+
|
87 |
+
def normalize_output_path(source_path: str, target_path: str, output_path: str) -> Optional[str]:
|
88 |
+
if source_path and target_path and output_path:
|
89 |
+
source_name, _ = os.path.splitext(os.path.basename(source_path))
|
90 |
+
target_name, target_extension = os.path.splitext(os.path.basename(target_path))
|
91 |
+
if os.path.isdir(output_path):
|
92 |
+
return os.path.join(output_path, source_name + '-' + target_name + target_extension)
|
93 |
+
return output_path
|
94 |
+
|
95 |
+
|
96 |
+
def create_temp(target_path: str) -> None:
|
97 |
+
temp_directory_path = get_temp_directory_path(target_path)
|
98 |
+
Path(temp_directory_path).mkdir(parents=True, exist_ok=True)
|
99 |
+
|
100 |
+
|
101 |
+
def move_temp(target_path: str, output_path: str) -> None:
|
102 |
+
temp_output_path = get_temp_output_path(target_path)
|
103 |
+
if os.path.isfile(temp_output_path):
|
104 |
+
if os.path.isfile(output_path):
|
105 |
+
os.remove(output_path)
|
106 |
+
shutil.move(temp_output_path, output_path)
|
107 |
+
|
108 |
+
|
109 |
+
def clean_temp(target_path: str) -> None:
|
110 |
+
temp_directory_path = get_temp_directory_path(target_path)
|
111 |
+
parent_directory_path = os.path.dirname(temp_directory_path)
|
112 |
+
if not roop.globals.keep_frames and os.path.isdir(temp_directory_path):
|
113 |
+
shutil.rmtree(temp_directory_path)
|
114 |
+
if os.path.exists(parent_directory_path) and not os.listdir(parent_directory_path):
|
115 |
+
os.rmdir(parent_directory_path)
|
116 |
+
|
117 |
+
|
118 |
+
def has_image_extension(image_path: str) -> bool:
|
119 |
+
return image_path.lower().endswith(('png', 'jpg', 'jpeg', 'webp'))
|
120 |
+
|
121 |
+
|
122 |
+
def is_image(image_path: str) -> bool:
|
123 |
+
if image_path and os.path.isfile(image_path):
|
124 |
+
mimetype, _ = mimetypes.guess_type(image_path)
|
125 |
+
return bool(mimetype and mimetype.startswith('image/'))
|
126 |
+
return False
|
127 |
+
|
128 |
+
|
129 |
+
def is_video(video_path: str) -> bool:
|
130 |
+
if video_path and os.path.isfile(video_path):
|
131 |
+
mimetype, _ = mimetypes.guess_type(video_path)
|
132 |
+
return bool(mimetype and mimetype.startswith('video/'))
|
133 |
+
return False
|
134 |
+
|
135 |
+
|
136 |
+
def conditional_download(download_directory_path: str, urls: List[str]) -> None:
|
137 |
+
if not os.path.exists(download_directory_path):
|
138 |
+
os.makedirs(download_directory_path)
|
139 |
+
for url in urls:
|
140 |
+
download_file_path = os.path.join(download_directory_path, os.path.basename(url))
|
141 |
+
if not os.path.exists(download_file_path):
|
142 |
+
request = urllib.request.urlopen(url) # type: ignore[attr-defined]
|
143 |
+
total = int(request.headers.get('Content-Length', 0))
|
144 |
+
with tqdm(total=total, desc='Downloading', unit='B', unit_scale=True, unit_divisor=1024) as progress:
|
145 |
+
urllib.request.urlretrieve(url, download_file_path, reporthook=lambda count, block_size, total_size: progress.update(block_size)) # type: ignore[attr-defined]
|
146 |
+
|
147 |
+
|
148 |
+
def resolve_relative_path(path: str) -> str:
|
149 |
+
return os.path.abspath(os.path.join(os.path.dirname(__file__), path))
|
run.py
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python3
|
2 |
+
|
3 |
+
from roop import core
|
4 |
+
|
5 |
+
if __name__ == '__main__':
|
6 |
+
core.run()
|