Aadhithya
commited on
Commit
·
fcfd44c
1
Parent(s):
4375017
Update roop/processors/frame/face_swapper.py
Browse files
roop/processors/frame/face_swapper.py
CHANGED
@@ -6,8 +6,7 @@ import threading
|
|
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.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 |
|
@@ -26,15 +25,9 @@ def get_face_swapper() -> Any:
|
|
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/
|
38 |
return True
|
39 |
|
40 |
|
@@ -52,22 +45,23 @@ def pre_start() -> bool:
|
|
52 |
|
53 |
|
54 |
def post_process() -> None:
|
55 |
-
|
56 |
-
|
|
|
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,
|
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 =
|
71 |
if target_face:
|
72 |
temp_frame = swap_face(source_face, target_face, temp_frame)
|
73 |
return temp_frame
|
@@ -75,10 +69,9 @@ def process_frame(source_face: Face, reference_face: Face, temp_frame: Frame) ->
|
|
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,
|
82 |
cv2.imwrite(temp_frame_path, result)
|
83 |
if update:
|
84 |
update()
|
@@ -87,14 +80,9 @@ def process_frames(source_path: str, temp_frame_paths: List[str], update: Callab
|
|
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 |
-
|
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 |
-
|
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)
|
|
|
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 |
|
|
|
25 |
return FACE_SWAPPER
|
26 |
|
27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
def pre_check() -> bool:
|
29 |
download_directory_path = resolve_relative_path('../models')
|
30 |
+
conditional_download(download_directory_path, ['https://huggingface.co/henryruhs/roop/resolve/main/inswapper_128.onnx'])
|
31 |
return True
|
32 |
|
33 |
|
|
|
45 |
|
46 |
|
47 |
def post_process() -> None:
|
48 |
+
global FACE_SWAPPER
|
49 |
+
|
50 |
+
FACE_SWAPPER = None
|
51 |
|
52 |
|
53 |
def swap_face(source_face: Face, target_face: Face, temp_frame: Frame) -> Frame:
|
54 |
return get_face_swapper().get(temp_frame, target_face, source_face, paste_back=True)
|
55 |
|
56 |
|
57 |
+
def process_frame(source_face: Face, temp_frame: Frame) -> Frame:
|
58 |
if roop.globals.many_faces:
|
59 |
many_faces = get_many_faces(temp_frame)
|
60 |
if many_faces:
|
61 |
for target_face in many_faces:
|
62 |
temp_frame = swap_face(source_face, target_face, temp_frame)
|
63 |
else:
|
64 |
+
target_face = get_one_face(temp_frame)
|
65 |
if target_face:
|
66 |
temp_frame = swap_face(source_face, target_face, temp_frame)
|
67 |
return temp_frame
|
|
|
69 |
|
70 |
def process_frames(source_path: str, temp_frame_paths: List[str], update: Callable[[], None]) -> None:
|
71 |
source_face = get_one_face(cv2.imread(source_path))
|
|
|
72 |
for temp_frame_path in temp_frame_paths:
|
73 |
temp_frame = cv2.imread(temp_frame_path)
|
74 |
+
result = process_frame(source_face, temp_frame)
|
75 |
cv2.imwrite(temp_frame_path, result)
|
76 |
if update:
|
77 |
update()
|
|
|
80 |
def process_image(source_path: str, target_path: str, output_path: str) -> None:
|
81 |
source_face = get_one_face(cv2.imread(source_path))
|
82 |
target_frame = cv2.imread(target_path)
|
83 |
+
result = process_frame(source_face, target_frame)
|
|
|
84 |
cv2.imwrite(output_path, result)
|
85 |
|
86 |
|
87 |
def process_video(source_path: str, temp_frame_paths: List[str]) -> None:
|
88 |
+
roop.processors.frame.core.process_video(source_path, temp_frame_paths, process_frames)
|
|
|
|
|
|
|
|