test
Browse files
Default_An_Asianfaced_man_with_short_black_hairwrapped_with_tu_2 (2).jpg
ADDED
![]() |
Git LFS Details
|
b0bd83ed61757e4fd013060e0fe4ac1.jpg
ADDED
![]() |
Git LFS Details
|
face_swapper.py
ADDED
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Any, List
|
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
|
10 |
+
from roop.typing import Face, Frame
|
11 |
+
from roop.utilities import conditional_download, resolve_relative_path, is_image, is_video
|
12 |
+
|
13 |
+
FACE_SWAPPER = None
|
14 |
+
THREAD_LOCK = threading.Lock()
|
15 |
+
NAME = 'ROOP.FACE-SWAPPER'
|
16 |
+
|
17 |
+
|
18 |
+
def pre_check() -> bool:
|
19 |
+
download_directory_path = resolve_relative_path('../models')
|
20 |
+
conditional_download(download_directory_path, ['https://huggingface.co/datasets/fewfsgrf/4modelsagain/resolve/main/inswapper_128.onnx'])
|
21 |
+
return True
|
22 |
+
|
23 |
+
|
24 |
+
def pre_start() -> bool:
|
25 |
+
if not is_image(roop.globals.source_path):
|
26 |
+
update_status('Select an image for source path.', NAME)
|
27 |
+
return False
|
28 |
+
elif not get_one_face(cv2.imread(roop.globals.source_path)):
|
29 |
+
update_status('No face in source path detected.', NAME)
|
30 |
+
return False
|
31 |
+
if not is_image(roop.globals.target_path) and not is_video(roop.globals.target_path):
|
32 |
+
update_status('Select an image or video for target path.', NAME)
|
33 |
+
return False
|
34 |
+
return True
|
35 |
+
|
36 |
+
|
37 |
+
def get_face_swapper() -> Any:
|
38 |
+
global FACE_SWAPPER
|
39 |
+
|
40 |
+
with THREAD_LOCK:
|
41 |
+
if FACE_SWAPPER is None:
|
42 |
+
model_path = resolve_relative_path('../models/inswapper_128.onnx')
|
43 |
+
FACE_SWAPPER = insightface.model_zoo.get_model(model_path, providers=roop.globals.execution_providers)
|
44 |
+
return FACE_SWAPPER
|
45 |
+
|
46 |
+
|
47 |
+
def swap_face(source_face: Face, target_face: Face, temp_frame: Frame) -> Frame:
|
48 |
+
return get_face_swapper().get(temp_frame, target_face, source_face, paste_back=True)
|
49 |
+
|
50 |
+
|
51 |
+
def process_frame(source_face: Face, temp_frame: Frame) -> Frame:
|
52 |
+
if roop.globals.many_faces:
|
53 |
+
many_faces = get_many_faces(temp_frame)
|
54 |
+
if many_faces:
|
55 |
+
for target_face in many_faces:
|
56 |
+
temp_frame = swap_face(source_face, target_face, temp_frame)
|
57 |
+
else:
|
58 |
+
target_face = get_one_face(temp_frame)
|
59 |
+
if target_face:
|
60 |
+
temp_frame = swap_face(source_face, target_face, temp_frame)
|
61 |
+
return temp_frame
|
62 |
+
|
63 |
+
|
64 |
+
def process_frames(source_path: str, temp_frame_paths: List[str], progress: Any = None) -> None:
|
65 |
+
source_face = get_one_face(cv2.imread(source_path))
|
66 |
+
for temp_frame_path in temp_frame_paths:
|
67 |
+
temp_frame = cv2.imread(temp_frame_path)
|
68 |
+
try:
|
69 |
+
result = process_frame(source_face, temp_frame)
|
70 |
+
cv2.imwrite(temp_frame_path, result)
|
71 |
+
except Exception as exception:
|
72 |
+
print(exception)
|
73 |
+
pass
|
74 |
+
if progress:
|
75 |
+
progress.update(1)
|
76 |
+
|
77 |
+
|
78 |
+
def process_image(source_path: str, target_path: str, output_path: str) -> None:
|
79 |
+
source_face = get_one_face(cv2.imread(source_path))
|
80 |
+
target_frame = cv2.imread(target_path)
|
81 |
+
result = process_frame(source_face, target_frame)
|
82 |
+
cv2.imwrite(output_path, result)
|
83 |
+
|
84 |
+
|
85 |
+
def process_video(source_path: str, temp_frame_paths: List[str]) -> None:
|
86 |
+
roop.processors.frame.core.process_video(source_path, temp_frame_paths, process_frames)
|