Jonny001 commited on
Commit
19136f3
·
verified ·
1 Parent(s): 0bc8459

Update roop/processors/frame/face_swapper.py

Browse files
Files changed (1) hide show
  1. roop/processors/frame/face_swapper.py +85 -42
roop/processors/frame/face_swapper.py CHANGED
@@ -2,6 +2,8 @@ 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
@@ -14,6 +16,8 @@ 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
@@ -21,68 +25,107 @@ def get_face_swapper() -> Any:
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/countfloyd/deepfake/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)
 
 
 
 
 
 
 
 
 
 
 
 
37
  return False
38
- elif not get_one_face(cv2.imread(roop.globals.source_path)):
39
- update_status('No face in source path detected.', NAME)
40
- return False
41
- if not is_image(roop.globals.target_path) and not is_video(roop.globals.target_path):
42
- update_status('Select an image or video for target path.', NAME)
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)
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
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:
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()
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
- 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)
 
 
 
 
 
2
  import cv2
3
  import insightface
4
  import threading
5
+ import os
6
+ import logging
7
 
8
  import roop.globals
9
  import roop.processors.frame.core
 
16
  THREAD_LOCK = threading.Lock()
17
  NAME = 'ROOP.FACE-SWAPPER'
18
 
19
+ # Configure logging
20
+ logging.basicConfig(level=logging.INFO)
21
 
22
  def get_face_swapper() -> Any:
23
  global FACE_SWAPPER
 
25
  with THREAD_LOCK:
26
  if FACE_SWAPPER is None:
27
  model_path = resolve_relative_path('../models/inswapper_128.onnx')
28
+ try:
29
+ FACE_SWAPPER = insightface.model_zoo.get_model(model_path, providers=roop.globals.execution_providers)
30
+ logging.info(f"Loaded face swapper model from {model_path}")
31
+ except Exception as e:
32
+ logging.error(f"Failed to load face swapper model: {e}")
33
+ FACE_SWAPPER = None
34
  return FACE_SWAPPER
35
 
 
36
  def pre_check() -> bool:
37
  download_directory_path = resolve_relative_path('../models')
38
+ try:
39
+ conditional_download(download_directory_path, ['https://huggingface.co/countfloyd/deepfake/resolve/main/inswapper_128.onnx'])
40
+ logging.info("Pre-check completed successfully.")
41
+ return True
42
+ except Exception as e:
43
+ logging.error(f"Pre-check failed: {e}")
44
+ return False
45
 
46
  def pre_start() -> bool:
47
+ try:
48
+ if not is_image(roop.globals.source_path):
49
+ update_status('Select an image for source path.', NAME)
50
+ return False
51
+ elif not get_one_face(cv2.imread(roop.globals.source_path)):
52
+ update_status('No face in source path detected.', NAME)
53
+ return False
54
+ if not is_image(roop.globals.target_path) and not is_video(roop.globals.target_path):
55
+ update_status('Select an image or video for target path.', NAME)
56
+ return False
57
+ logging.info("Pre-start checks passed.")
58
+ return True
59
+ except Exception as e:
60
+ logging.error(f"Pre-start check failed: {e}")
61
  return False
 
 
 
 
 
 
 
 
62
 
63
  def post_process() -> None:
64
  global FACE_SWAPPER
65
 
66
  FACE_SWAPPER = None
67
+ logging.info("Post-process cleanup done.")
68
 
69
  def swap_face(source_face: Face, target_face: Face, temp_frame: Frame) -> Frame:
70
+ try:
71
+ result = get_face_swapper().get(temp_frame, target_face, source_face, paste_back=True)
72
+ return result
73
+ except Exception as e:
74
+ logging.error(f"Error swapping face: {e}")
75
+ return temp_frame # Return the unmodified frame in case of error
76
 
77
  def process_frame(source_face: Face, temp_frame: Frame) -> Frame:
78
+ try:
79
+ if roop.globals.many_faces:
80
+ many_faces = get_many_faces(temp_frame)
81
+ if many_faces:
82
+ for target_face in many_faces:
83
+ temp_frame = swap_face(source_face, target_face, temp_frame)
84
+ else:
85
+ target_face = get_one_face(temp_frame)
86
+ if target_face:
87
  temp_frame = swap_face(source_face, target_face, temp_frame)
88
+ return temp_frame
89
+ except Exception as e:
90
+ logging.error(f"Error processing frame: {e}")
91
+ return temp_frame # Return the unmodified frame in case of error
 
 
92
 
93
  def process_frames(source_path: str, temp_frame_paths: List[str], update: Callable[[], None]) -> None:
94
+ try:
95
+ source_face = get_one_face(cv2.imread(source_path))
96
+ if source_face is None:
97
+ raise ValueError("No face detected in source image.")
98
+
99
+ for temp_frame_path in temp_frame_paths:
100
+ temp_frame = cv2.imread(temp_frame_path)
101
+ if temp_frame is None:
102
+ raise ValueError(f"Failed to read frame from path: {temp_frame_path}")
103
+
104
+ result = process_frame(source_face, temp_frame)
105
+ cv2.imwrite(temp_frame_path, result)
106
+ if update:
107
+ update()
108
+ logging.info("Frames processed successfully.")
109
+ except Exception as e:
110
+ logging.error(f"Error processing frames: {e}")
111
 
112
  def process_image(source_path: str, target_path: str, output_path: str) -> None:
113
+ try:
114
+ source_face = get_one_face(cv2.imread(source_path))
115
+ target_frame = cv2.imread(target_path)
116
+ if source_face is None:
117
+ raise ValueError("No face detected in source image.")
118
+ if target_frame is None:
119
+ raise ValueError("Failed to read target frame.")
120
+ result = process_frame(source_face, target_frame)
121
+ cv2.imwrite(output_path, result)
122
+ logging.info(f"Image processed and saved to {output_path}.")
123
+ except Exception as e:
124
+ logging.error(f"Error processing image: {e}")
125
 
126
  def process_video(source_path: str, temp_frame_paths: List[str]) -> None:
127
+ try:
128
+ roop.processors.frame.core.process_video(source_path, temp_frame_paths, process_frames)
129
+ logging.info("Video processing completed.")
130
+ except Exception as e:
131
+ logging.error(f"Error processing video: {e}")