duyv commited on
Commit
898516b
·
verified ·
1 Parent(s): a926f18

Upload 24 files

Browse files
FaceSwapping/models/GFPGAN/GFPGANv1.4.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e2cd4703ab14f4d01fd1383a8a8b266f9a5833dacee8e6a79d3bf21a1b6be5ad
3
+ size 348632874
FaceSwapping/models/inswapper/__init__.py ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from app.config import settings
2
+ import os
3
+ import urllib.request
4
+
5
+ PATH_FOLDER = os.path.join(settings.DIR_ROOT, "FaceSwapping", "models", "inswapper")
6
+ ONNX_FILENAME = "inswapper_128.onnx"
7
+
8
+ ONNX_PATH = os.path.join(PATH_FOLDER, ONNX_FILENAME)
9
+ ONNX_URL = "https://huggingface.co/duyv/MC-AI/resolve/main/FaceSwapping/models/inswapper/inswapper_128.onnx" # Thay URL thật ở đây
10
+
11
+ # Tạo thư mục nếu chưa tồn tại
12
+ os.makedirs(PATH_FOLDER, exist_ok=True)
13
+
14
+ # Kiểm tra nếu không có file .onnx thì tải về
15
+ if not os.path.exists(ONNX_PATH):
16
+ print(f"Tải {ONNX_FILENAME} từ {ONNX_URL}...")
17
+ urllib.request.urlretrieve(ONNX_URL, ONNX_PATH)
18
+ print("Tải xong.")
19
+ else:
20
+ print(f"Đã có file {ONNX_FILENAME} tại {ONNX_PATH}")
FaceSwapping/models/inswapper/inswapper_128.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e4a3f08c753cb72d04e10aa0f7dbe3deebbf39567d4ead6dce08e98aa49e16af
3
+ size 554253681
FaceSwapping/models/inswapper/readme.md ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # inswapper_128.onnx
2
+
3
+ ## 🧠 Giới thiệu
4
+
5
+ `inswapper_128.onnx` là một mô hình deep learning được huấn luyện để thực hiện **hoán đổi khuôn mặt (face swapping)** giữa hai ảnh hoặc khung hình. Mô hình này thuộc hệ sinh thái **InsightFace** – một thư viện nổi tiếng trong lĩnh vực nhận diện và xử lý khuôn mặt.
6
+
7
+ Mô hình sử dụng định dạng `ONNX`, giúp dễ dàng tích hợp với các framework khác nhau như PyTorch, OpenCV, hoặc ONNX Runtime.
8
+
9
+ ---
10
+
11
+ ## 🚀 Tính năng chính
12
+
13
+ - Hoán đổi khuôn mặt từ ảnh nguồn sang ảnh đích.
14
+ - Giữ nguyên biểu cảm, ánh sáng, góc nhìn từ ảnh đích.
15
+ - Cho kết quả mượt mà và tự nhiên.
16
+ - Tối ưu cho input kích thước **128x128 pixels**.
17
+
18
+ ---
19
+
20
+ ## 🛠️ Yêu cầu
21
+
22
+ - Python 3.7+
23
+ - `onnxruntime`
24
+ - `numpy`
25
+ - `opencv-python`
26
+ - `insightface` (thư viện gốc)
27
+
28
+ Cài đặt bằng pip:
29
+
30
+ ```bash
31
+ pip install onnxruntime numpy opencv-python insightface
FaceSwapping/roop/__init__.py ADDED
File without changes
FaceSwapping/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
FaceSwapping/roop/core.py ADDED
@@ -0,0 +1,315 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+
3
+ import os
4
+ import sys
5
+ import json
6
+ from pathlib import Path
7
+
8
+ # single thread doubles cuda performance - needs to be set before torch import
9
+ if any(arg.startswith("--execution-provider") for arg in sys.argv):
10
+ os.environ["OMP_NUM_THREADS"] = "1"
11
+ # reduce tensorflow log level
12
+ os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
13
+ import warnings
14
+ from typing import List
15
+ import platform
16
+ import signal
17
+ import shutil
18
+ import argparse
19
+ import onnxruntime
20
+ import tensorflow
21
+ import roop.globals
22
+ import roop.metadata
23
+ # import roop.ui as ui
24
+ from roop.predictor import predict_image, predict_video
25
+ from roop.processors.frame.core import get_frame_processors_modules
26
+ from roop.utilities import (
27
+ has_image_extension,
28
+ is_image,
29
+ is_video,
30
+ detect_fps,
31
+ create_video,
32
+ extract_frames,
33
+ get_temp_frame_paths,
34
+ restore_audio,
35
+ create_temp,
36
+ move_temp,
37
+ clean_temp,
38
+ normalize_output_path,
39
+ resolve_relative_path,
40
+ )
41
+
42
+ warnings.filterwarnings("ignore", category=FutureWarning, module="insightface")
43
+ warnings.filterwarnings("ignore", category=UserWarning, module="torchvision")
44
+
45
+ CONFIG_PATH = Path(__file__).parent / "model_config.json"
46
+
47
+
48
+ def load_model_path():
49
+ default_model_path = resolve_relative_path("../models/inswapper/inswapper_128.onnx")
50
+
51
+ if CONFIG_PATH.exists():
52
+ try:
53
+ with CONFIG_PATH.open("r") as f:
54
+ config = json.load(f)
55
+ model_path = config.get("model_path")
56
+ if model_path and os.path.exists(model_path):
57
+ print(f"[CORE] Loaded model path from config: {model_path}")
58
+ return model_path
59
+ else:
60
+ print(f"[CORE] Invalid model path in config: {model_path}, using default: {default_model_path}")
61
+ except Exception as e:
62
+ print(f"[CORE] Error reading model config: {str(e)}, using default: {default_model_path}")
63
+ else:
64
+ print(f"[CORE] Model config not found at {CONFIG_PATH}, using default: {default_model_path}")
65
+
66
+ return default_model_path
67
+
68
+
69
+ def parse_args() -> None:
70
+ signal.signal(signal.SIGINT, lambda signal_number, frame: destroy())
71
+ program = argparse.ArgumentParser(formatter_class=lambda prog: argparse.HelpFormatter(prog, max_help_position=100))
72
+ program.add_argument("-s", "--source", help="select an source image", dest="source_path")
73
+ program.add_argument("-t", "--target", help="select an target image or video", dest="target_path")
74
+ program.add_argument("-o", "--output", help="select output file or directory", dest="output_path")
75
+ program.add_argument(
76
+ "--frame-processor",
77
+ help="frame processors (choices: face_swapper, face_enhancer, ...)",
78
+ dest="frame_processor",
79
+ default=["face_swapper"],
80
+ nargs="+",
81
+ )
82
+ program.add_argument("--keep-fps", help="keep target fps", dest="keep_fps", action="store_true")
83
+ program.add_argument("--keep-frames", help="keep temporary frames", dest="keep_frames", action="store_true")
84
+ program.add_argument("--skip-audio", help="skip target audio", dest="skip_audio", action="store_true")
85
+ program.add_argument("--many-faces", help="process every face", dest="many_faces", action="store_true")
86
+ program.add_argument(
87
+ "--reference-face-position", help="position of the reference face", dest="reference_face_position", type=int, default=0
88
+ )
89
+ program.add_argument(
90
+ "--reference-frame-number", help="number of the reference frame", dest="reference_frame_number", type=int, default=0
91
+ )
92
+ program.add_argument(
93
+ "--similar-face-distance", help="face distance used for recognition", dest="similar_face_distance", type=float, default=0.85
94
+ )
95
+ program.add_argument(
96
+ "--temp-frame-format",
97
+ help="image format used for frame extraction",
98
+ dest="temp_frame_format",
99
+ default="png",
100
+ choices=["jpg", "png"],
101
+ )
102
+ program.add_argument(
103
+ "--temp-frame-quality",
104
+ help="image quality used for frame extraction",
105
+ dest="temp_frame_quality",
106
+ type=int,
107
+ default=0,
108
+ choices=range(101),
109
+ metavar="[0-100]",
110
+ )
111
+ program.add_argument(
112
+ "--output-video-encoder",
113
+ help="encoder used for the output video",
114
+ dest="output_video_encoder",
115
+ default="libx264",
116
+ choices=["libx264", "libx265", "libvpx-vp9", "h264_nvenc", "hevc_nvenc"],
117
+ )
118
+ program.add_argument(
119
+ "--output-video-quality",
120
+ help="quality used for the output video",
121
+ dest="output_video_quality",
122
+ type=int,
123
+ default=35,
124
+ choices=range(101),
125
+ metavar="[0-100]",
126
+ )
127
+ program.add_argument("--max-memory", help="maximum amount of RAM in GB", dest="max_memory", type=int)
128
+ program.add_argument(
129
+ "--execution-provider",
130
+ help="available execution provider (choices: cpu, ...)",
131
+ dest="execution_provider",
132
+ default=["cpu"],
133
+ choices=suggest_execution_providers(),
134
+ nargs="+",
135
+ )
136
+ program.add_argument(
137
+ "--execution-threads", help="number of execution threads", dest="execution_threads", type=int, default=suggest_execution_threads()
138
+ )
139
+ program.add_argument("--model-path", help="path to face swapper model", dest="model_path")
140
+ program.add_argument("-v", "--version", action="version", version=f"{roop.metadata.name} {roop.metadata.version}")
141
+
142
+ args = program.parse_args()
143
+
144
+ roop.globals.source_path = args.source_path
145
+ roop.globals.target_path = args.target_path
146
+ roop.globals.output_path = normalize_output_path(roop.globals.source_path, roop.globals.target_path, args.output_path)
147
+ roop.globals.headless = (
148
+ roop.globals.source_path is not None and roop.globals.target_path is not None and roop.globals.output_path is not None
149
+ )
150
+ roop.globals.frame_processors = args.frame_processor
151
+ roop.globals.keep_fps = args.keep_fps
152
+ roop.globals.keep_frames = args.keep_frames
153
+ roop.globals.skip_audio = args.skip_audio
154
+ roop.globals.many_faces = args.many_faces
155
+ roop.globals.reference_face_position = args.reference_face_position
156
+ roop.globals.reference_frame_number = args.reference_frame_number
157
+ roop.globals.similar_face_distance = args.similar_face_distance
158
+ roop.globals.temp_frame_format = args.temp_frame_format
159
+ roop.globals.temp_frame_quality = args.temp_frame_quality
160
+ roop.globals.output_video_encoder = args.output_video_encoder
161
+ roop.globals.output_video_quality = args.output_video_quality
162
+ roop.globals.max_memory = args.max_memory
163
+ roop.globals.execution_providers = decode_execution_providers(args.execution_provider)
164
+ roop.globals.execution_threads = args.execution_threads
165
+
166
+ # Thiết lập model_path: ưu tiên tham số dòng lệnh, nếu không thì đọc từ config
167
+ if args.model_path and os.path.exists(args.model_path):
168
+ roop.globals.model_path = args.model_path
169
+ print(f"[CORE] Using model path from command line: {roop.globals.model_path}")
170
+ else:
171
+ roop.globals.model_path = load_model_path()
172
+
173
+
174
+ def encode_execution_providers(execution_providers: List[str]) -> List[str]:
175
+ return [execution_provider.replace("ExecutionProvider", "").lower() for execution_provider in execution_providers]
176
+
177
+
178
+ def decode_execution_providers(execution_providers: List[str]) -> List[str]:
179
+ return [
180
+ provider
181
+ for provider, encoded_execution_provider in zip(
182
+ onnxruntime.get_available_providers(), encode_execution_providers(onnxruntime.get_available_providers())
183
+ )
184
+ if any(execution_provider in encoded_execution_provider for execution_provider in execution_providers)
185
+ ]
186
+
187
+
188
+ def suggest_execution_providers() -> List[str]:
189
+ return encode_execution_providers(onnxruntime.get_available_providers())
190
+
191
+
192
+ def suggest_execution_threads() -> int:
193
+ if "CUDAExecutionProvider" in onnxruntime.get_available_providers():
194
+ return 8
195
+ return 1
196
+
197
+
198
+ def limit_resources() -> None:
199
+ gpus = tensorflow.config.experimental.list_physical_devices("GPU")
200
+ for gpu in gpus:
201
+ tensorflow.config.experimental.set_virtual_device_configuration(
202
+ gpu, [tensorflow.config.experimental.VirtualDeviceConfiguration(memory_limit=1024)]
203
+ )
204
+ if roop.globals.max_memory:
205
+ memory = roop.globals.max_memory * 1024**3
206
+ if platform.system().lower() == "darwin":
207
+ memory = roop.globals.max_memory * 1024**6
208
+ if platform.system().lower() == "windows":
209
+ import ctypes
210
+
211
+ kernel32 = ctypes.windll.kernel32
212
+ kernel32.SetProcessWorkingSetSize(-1, ctypes.c_size_t(memory), ctypes.c_size_t(memory))
213
+ else:
214
+ import resource
215
+
216
+ resource.setrlimit(resource.RLIMIT_DATA, (memory, memory))
217
+
218
+
219
+ def pre_check() -> bool:
220
+ if sys.version_info < (3, 9):
221
+ update_status("Python version is not supported - please upgrade to 3.9 or higher.")
222
+ return False
223
+ if not shutil.which("ffmpeg"):
224
+ update_status("ffmpeg is not installed.")
225
+ return False
226
+ return True
227
+
228
+
229
+ def update_status(message: str, scope: str = "ROOP.CORE") -> None:
230
+ print(f"[{scope}] {message}")
231
+ # if not roop.globals.headless:
232
+ # ui.update_status(message)
233
+
234
+
235
+ def start() -> None:
236
+ print(f"[CORE] Starting with model: {roop.globals.model_path}")
237
+ for frame_processor in get_frame_processors_modules(roop.globals.frame_processors):
238
+ if not frame_processor.pre_start():
239
+ return
240
+ if has_image_extension(roop.globals.target_path):
241
+ if predict_image(roop.globals.target_path):
242
+ destroy()
243
+ shutil.copy2(roop.globals.target_path, roop.globals.output_path)
244
+ for frame_processor in get_frame_processors_modules(roop.globals.frame_processors):
245
+ update_status("Progressing...", frame_processor.NAME)
246
+ frame_processor.process_image(roop.globals.source_path, roop.globals.output_path, roop.globals.output_path)
247
+ frame_processor.post_process()
248
+ if is_image(roop.globals.output_path):
249
+ update_status("Processing to image succeed!")
250
+ else:
251
+ update_status("Processing to image failed!")
252
+ return
253
+ if predict_video(roop.globals.target_path):
254
+ destroy()
255
+ update_status("Creating temporary resources...")
256
+ create_temp(roop.globals.target_path)
257
+ if roop.globals.keep_fps:
258
+ fps = detect_fps(roop.globals.target_path)
259
+ update_status(f"Extracting frames with {fps} FPS...")
260
+ extract_frames(roop.globals.target_path, fps)
261
+ else:
262
+ update_status("Extracting frames with 30 FPS...")
263
+ extract_frames(roop.globals.target_path)
264
+ temp_frame_paths = get_temp_frame_paths(roop.globals.target_path)
265
+ if temp_frame_paths:
266
+ for frame_processor in get_frame_processors_modules(roop.globals.frame_processors):
267
+ update_status("Progressing...", frame_processor.NAME)
268
+ frame_processor.process_video(roop.globals.source_path, temp_frame_paths)
269
+ frame_processor.post_process()
270
+ else:
271
+ update_status("Frames not found...")
272
+ return
273
+ if roop.globals.keep_fps:
274
+ fps = detect_fps(roop.globals.target_path)
275
+ update_status(f"Creating video with {fps} FPS...")
276
+ create_video(roop.globals.target_path, fps)
277
+ else:
278
+ update_status("Creating video with 30 FPS...")
279
+ create_video(roop.globals.target_path)
280
+ if roop.globals.skip_audio:
281
+ move_temp(roop.globals.target_path, roop.globals.output_path)
282
+ update_status("Skipping audio...")
283
+ else:
284
+ if roop.globals.keep_fps:
285
+ update_status("Restoring audio...")
286
+ else:
287
+ update_status("Restoring audio might cause issues as fps are not kept...")
288
+ restore_audio(roop.globals.target_path, roop.globals.output_path)
289
+ update_status("Cleaning temporary resources...")
290
+ clean_temp(roop.globals.target_path)
291
+ if is_video(roop.globals.output_path):
292
+ update_status("Processing to video succeed!")
293
+ else:
294
+ update_status("Processing to video failed!")
295
+
296
+
297
+ def destroy() -> None:
298
+ if roop.globals.target_path:
299
+ clean_temp(roop.globals.target_path)
300
+ sys.exit()
301
+
302
+
303
+ def run() -> None:
304
+ parse_args()
305
+ if not pre_check():
306
+ return
307
+ for frame_processor in get_frame_processors_modules(roop.globals.frame_processors):
308
+ if not frame_processor.pre_check():
309
+ return
310
+ limit_resources()
311
+ if roop.globals.headless:
312
+ start()
313
+ # else:
314
+ # window = ui.init(start, destroy)
315
+ # window.mainloop()
FaceSwapping/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
FaceSwapping/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
FaceSwapping/roop/globals.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import List, Optional
2
+
3
+ model_path = None
4
+ source_path: Optional[str] = None
5
+ target_path: Optional[str] = None
6
+ output_path: Optional[str] = None
7
+ headless: Optional[bool] = None
8
+ frame_processors: List[str] = []
9
+ keep_fps: Optional[bool] = None
10
+ keep_frames: Optional[bool] = None
11
+ skip_audio: Optional[bool] = None
12
+ many_faces: Optional[bool] = None
13
+ reference_face_position: Optional[int] = None
14
+ reference_frame_number: Optional[int] = None
15
+ similar_face_distance: Optional[float] = None
16
+ temp_frame_format: Optional[str] = None
17
+ temp_frame_quality: Optional[int] = None
18
+ output_video_encoder: Optional[str] = None
19
+ output_video_quality: Optional[int] = None
20
+ max_memory: Optional[int] = None
21
+ execution_providers: List[str] = []
22
+ execution_threads: Optional[int] = None
23
+ log_level: str = 'error'
FaceSwapping/roop/metadata.py ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ name = 'roop'
2
+ version = '1.3.2'
FaceSwapping/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)
FaceSwapping/roop/processors/__init__.py ADDED
File without changes
FaceSwapping/roop/processors/frame/__init__.py ADDED
File without changes
FaceSwapping/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)
FaceSwapping/roop/processors/frame/face_enhancer.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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/GFPGAN")
46
+ conditional_download(
47
+ download_directory_path, ["https://huggingface.co/duyv/MC-AI/resolve/main/FaceSwapping/models/GFPGAN/GFPGANv1.4.pth"]
48
+ )
49
+ return True
50
+
51
+
52
+ def pre_start() -> bool:
53
+ if not is_image(roop.globals.target_path) and not is_video(roop.globals.target_path):
54
+ update_status('Select an image or video for target path.', NAME)
55
+ return False
56
+ return True
57
+
58
+
59
+ def post_process() -> None:
60
+ clear_face_enhancer()
61
+
62
+
63
+ def enhance_face(target_face: Face, temp_frame: Frame) -> Frame:
64
+ start_x, start_y, end_x, end_y = map(int, target_face['bbox'])
65
+ padding_x = int((end_x - start_x) * 0.5)
66
+ padding_y = int((end_y - start_y) * 0.5)
67
+ start_x = max(0, start_x - padding_x)
68
+ start_y = max(0, start_y - padding_y)
69
+ end_x = max(0, end_x + padding_x)
70
+ end_y = max(0, end_y + padding_y)
71
+ temp_face = temp_frame[start_y:end_y, start_x:end_x]
72
+ if temp_face.size:
73
+ with THREAD_SEMAPHORE:
74
+ _, _, temp_face = get_face_enhancer().enhance(
75
+ temp_face,
76
+ paste_back=True
77
+ )
78
+ temp_frame[start_y:end_y, start_x:end_x] = temp_face
79
+ return temp_frame
80
+
81
+
82
+ def process_frame(source_face: Face, reference_face: Face, temp_frame: Frame) -> Frame:
83
+ many_faces = get_many_faces(temp_frame)
84
+ if many_faces:
85
+ for target_face in many_faces:
86
+ temp_frame = enhance_face(target_face, temp_frame)
87
+ return temp_frame
88
+
89
+
90
+ def process_frames(source_path: str, temp_frame_paths: List[str], update: Callable[[], None]) -> None:
91
+ for temp_frame_path in temp_frame_paths:
92
+ temp_frame = cv2.imread(temp_frame_path)
93
+ result = process_frame(None, None, temp_frame)
94
+ cv2.imwrite(temp_frame_path, result)
95
+ if update:
96
+ update()
97
+
98
+
99
+ def process_image(source_path: str, target_path: str, output_path: str) -> None:
100
+ target_frame = cv2.imread(target_path)
101
+ result = process_frame(None, None, target_frame)
102
+ cv2.imwrite(output_path, result)
103
+
104
+
105
+ def process_video(source_path: str, temp_frame_paths: List[str]) -> None:
106
+ roop.processors.frame.core.process_video(None, temp_frame_paths, process_frames)
FaceSwapping/roop/processors/frame/face_swapper.py ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Any, List, Callable
2
+ import cv2
3
+ import insightface
4
+ import threading
5
+ import roop.globals
6
+ import roop.globals
7
+ import roop.processors.frame.core
8
+ import os
9
+ from roop.core import update_status
10
+ from roop.face_analyser import get_one_face, get_many_faces, find_similar_face
11
+ from roop.face_reference import get_face_reference, set_face_reference, clear_face_reference
12
+ from roop.typing import Face, Frame
13
+ from roop.utilities import conditional_download, resolve_relative_path, is_image, is_video
14
+
15
+ FACE_SWAPPER = None
16
+ THREAD_LOCK = threading.Lock()
17
+ NAME = "ROOP.FACE-SWAPPER"
18
+
19
+
20
+ def get_face_swapper() -> Any:
21
+ global FACE_SWAPPER
22
+
23
+ with THREAD_LOCK:
24
+ if FACE_SWAPPER is None:
25
+ # Sử dụng roop.globals.model_path nếu có, nếu không thì dùng mặc định
26
+ model_path = (
27
+ roop.globals.model_path
28
+ if hasattr(roop.globals, "model_path") and roop.globals.model_path and os.path.exists(roop.globals.model_path)
29
+ else resolve_relative_path("../models/inswapper_128.onnx")
30
+ )
31
+ print(f"[FACE_SWAPPER] Đang tải mô hình face swapper từ: {model_path}")
32
+ try:
33
+ # Chỉ định rõ ràng nhà cung cấp thực thi
34
+ providers = (
35
+ roop.globals.execution_providers
36
+ if hasattr(roop.globals, "execution_providers") and roop.globals.execution_providers
37
+ else ["CPUExecutionProvider"]
38
+ )
39
+ FACE_SWAPPER = insightface.model_zoo.get_model(model_path, providers=providers)
40
+ print(f"[FACE_SWAPPER] Tải mô hình thành công: {model_path} với nhà cung cấp: {providers}")
41
+ except Exception as e:
42
+ print(f"[FACE_SWAPPER] Lỗi khi tải mô hình {model_path}: {str(e)}")
43
+ raise
44
+ return FACE_SWAPPER
45
+
46
+
47
+ def clear_face_swapper() -> None:
48
+ global FACE_SWAPPER
49
+
50
+ FACE_SWAPPER = None
51
+
52
+
53
+ def pre_check() -> bool:
54
+ download_directory_path = resolve_relative_path("../models/inswapper")
55
+ conditional_download(
56
+ download_directory_path, ["https://huggingface.co/duyv/MC-AI/resolve/main/FaceSwapping/models/inswapper/inswapper_128.onnx"]
57
+ )
58
+ return True
59
+
60
+
61
+ def pre_start() -> bool:
62
+ if not is_image(roop.globals.source_path):
63
+ update_status("Select an image for source path.", NAME)
64
+ return False
65
+ elif not get_one_face(cv2.imread(roop.globals.source_path)):
66
+ update_status("No face in source path detected.", NAME)
67
+ return False
68
+ if not is_image(roop.globals.target_path) and not is_video(roop.globals.target_path):
69
+ update_status("Select an image or video for target path.", NAME)
70
+ return False
71
+ return True
72
+
73
+
74
+ def post_process() -> None:
75
+ clear_face_swapper()
76
+ clear_face_reference()
77
+
78
+
79
+ def swap_face(source_face: Face, target_face: Face, temp_frame: Frame) -> Frame:
80
+ return get_face_swapper().get(temp_frame, target_face, source_face, paste_back=True)
81
+
82
+
83
+ def process_frame(source_face: Face, reference_face: Face, temp_frame: Frame) -> Frame:
84
+ if roop.globals.many_faces:
85
+ many_faces = get_many_faces(temp_frame)
86
+ if many_faces:
87
+ for target_face in many_faces:
88
+ temp_frame = swap_face(source_face, target_face, temp_frame)
89
+ else:
90
+ target_face = find_similar_face(temp_frame, reference_face)
91
+ if target_face:
92
+ temp_frame = swap_face(source_face, target_face, temp_frame)
93
+ return temp_frame
94
+
95
+
96
+ def process_frames(source_path: str, temp_frame_paths: List[str], update: Callable[[], None]) -> None:
97
+ source_face = get_one_face(cv2.imread(source_path))
98
+ reference_face = None if roop.globals.many_faces else get_face_reference()
99
+ for temp_frame_path in temp_frame_paths:
100
+ temp_frame = cv2.imread(temp_frame_path)
101
+ result = process_frame(source_face, reference_face, temp_frame)
102
+ cv2.imwrite(temp_frame_path, result)
103
+ if update:
104
+ update()
105
+
106
+
107
+ def process_image(source_path: str, target_path: str, output_path: str) -> None:
108
+ source_face = get_one_face(cv2.imread(source_path))
109
+ target_frame = cv2.imread(target_path)
110
+ reference_face = None if roop.globals.many_faces else get_one_face(target_frame, roop.globals.reference_face_position)
111
+ result = process_frame(source_face, reference_face, target_frame)
112
+ cv2.imwrite(output_path, result)
113
+
114
+
115
+ def process_video(source_path: str, temp_frame_paths: List[str]) -> None:
116
+ if not roop.globals.many_faces and not get_face_reference():
117
+ reference_frame = cv2.imread(temp_frame_paths[roop.globals.reference_frame_number])
118
+ reference_face = get_one_face(reference_frame, roop.globals.reference_face_position)
119
+ set_face_reference(reference_face)
120
+ roop.processors.frame.core.process_video(source_path, temp_frame_paths, process_frames)
FaceSwapping/roop/readme.md ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ # PyTorch CPU
2
+ # torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
3
+
4
+
5
+ # GPU
6
+ `pip install torch==2.5.0 torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118`
FaceSwapping/roop/requirements-cpu.txt ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ numpy==1.24.3
2
+ opencv-python==4.8.0.74
3
+ onnx==1.14.0
4
+ insightface==0.7.3
5
+ psutil==5.9.5
6
+ tk==0.1.0
7
+ customtkinter==5.2.0
8
+ tkinterdnd2==0.3.0; sys_platform != 'darwin' and platform_machine != 'arm64'
9
+ tkinterdnd2-universal==1.7.3; sys_platform == 'darwin' and platform_machine == 'arm64'
10
+ pillow==10.0.0
11
+ onnxruntime
12
+ onnxruntime-coreml==1.13.1; python_version == '3.9' and sys_platform == 'darwin' and platform_machine != 'arm64'
13
+ onnxruntime-silicon==1.13.1; sys_platform == 'darwin' and platform_machine == 'arm64'
14
+ tensorflow==2.13.0
15
+ opennsfw2==0.10.2
16
+ protobuf==4.23.4
17
+ tqdm==4.65.0
18
+ gfpgan==1.3.8
19
+
FaceSwapping/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]
FaceSwapping/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
+ }
FaceSwapping/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())
FaceSwapping/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))
FaceSwapping/run.py ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ # import os
2
+ from FaceSwapping.roop import core
3
+
4
+
5
+ # os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
6
+
7
+ if __name__ == "__main__":
8
+ core.run()