Apex-X commited on
Commit
7c43b22
·
verified ·
1 Parent(s): c58f203

Update roop/processors/frame/face_swapper.py

Browse files
roop/processors/frame/face_swapper.py CHANGED
@@ -14,23 +14,19 @@ FACE_SWAPPER = None
14
  THREAD_LOCK = threading.Lock()
15
  NAME = 'ROOP.FACE-SWAPPER'
16
 
17
-
18
  def get_face_swapper() -> Any:
19
  global FACE_SWAPPER
20
-
21
  with THREAD_LOCK:
22
  if FACE_SWAPPER is None:
23
  model_path = resolve_relative_path('../models/inswapper_128.onnx')
24
  FACE_SWAPPER = insightface.model_zoo.get_model(model_path, providers=roop.globals.execution_providers)
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/Apex-X/inswapper_128.onnx/resolve/main/inswapper_128.onnx'])
31
  return True
32
 
33
-
34
  def pre_start() -> bool:
35
  if not is_image(roop.globals.source_path):
36
  update_status('Select an image for source path.', NAME)
@@ -43,17 +39,13 @@ def pre_start() -> bool:
43
  return False
44
  return True
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)
@@ -66,7 +58,6 @@ def process_frame(source_face: Face, temp_frame: Frame) -> Frame:
66
  temp_frame = swap_face(source_face, target_face, temp_frame)
67
  return temp_frame
68
 
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:
@@ -76,14 +67,17 @@ def process_frames(source_path: str, temp_frame_paths: List[str], update: Callab
76
  if update:
77
  update()
78
 
79
-
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
- reference_face = None if roop.globals.many_faces else get_one_face(target_frame)
84
- result = process_frame(source_face, reference_face, target_frame)
85
  cv2.imwrite(output_path, result)
86
 
 
 
 
 
 
87
 
88
  def process_video(source_path: str, temp_frame_paths: List[str]) -> None:
89
  if not roop.globals.many_faces and not get_face_reference():
 
14
  THREAD_LOCK = threading.Lock()
15
  NAME = 'ROOP.FACE-SWAPPER'
16
 
 
17
  def get_face_swapper() -> Any:
18
  global FACE_SWAPPER
 
19
  with THREAD_LOCK:
20
  if FACE_SWAPPER is None:
21
  model_path = resolve_relative_path('../models/inswapper_128.onnx')
22
  FACE_SWAPPER = insightface.model_zoo.get_model(model_path, providers=roop.globals.execution_providers)
23
  return FACE_SWAPPER
24
 
 
25
  def pre_check() -> bool:
26
  download_directory_path = resolve_relative_path('../models')
27
  conditional_download(download_directory_path, ['https://huggingface.co/Apex-X/inswapper_128.onnx/resolve/main/inswapper_128.onnx'])
28
  return True
29
 
 
30
  def pre_start() -> bool:
31
  if not is_image(roop.globals.source_path):
32
  update_status('Select an image for source path.', NAME)
 
39
  return False
40
  return True
41
 
 
42
  def post_process() -> None:
43
  global FACE_SWAPPER
 
44
  FACE_SWAPPER = None
45
 
 
46
  def swap_face(source_face: Face, target_face: Face, temp_frame: Frame) -> Frame:
47
  return get_face_swapper().get(temp_frame, target_face, source_face, paste_back=True)
48
 
 
49
  def process_frame(source_face: Face, temp_frame: Frame) -> Frame:
50
  if roop.globals.many_faces:
51
  many_faces = get_many_faces(temp_frame)
 
58
  temp_frame = swap_face(source_face, target_face, temp_frame)
59
  return temp_frame
60
 
 
61
  def process_frames(source_path: str, temp_frame_paths: List[str], update: Callable[[], None]) -> None:
62
  source_face = get_one_face(cv2.imread(source_path))
63
  for temp_frame_path in temp_frame_paths:
 
67
  if update:
68
  update()
69
 
 
70
  def process_image(source_path: str, target_path: str, output_path: str) -> None:
71
  source_face = get_one_face(cv2.imread(source_path))
72
  target_frame = cv2.imread(target_path)
73
+ result = process_frame(source_face, target_frame)
 
74
  cv2.imwrite(output_path, result)
75
 
76
+ def get_face_reference() -> Face:
77
+ return roop.globals.reference_face
78
+
79
+ def set_face_reference(face: Face) -> None:
80
+ roop.globals.reference_face = face
81
 
82
  def process_video(source_path: str, temp_frame_paths: List[str]) -> None:
83
  if not roop.globals.many_faces and not get_face_reference():