Aadhithya commited on
Commit
79546b5
·
1 Parent(s): ab428df

Delete roop/core.py

Browse files
Files changed (1) hide show
  1. roop/core.py +0 -224
roop/core.py DELETED
@@ -1,224 +0,0 @@
1
- #!/usr/bin/env python3
2
-
3
- import os
4
- import sys
5
- # single thread doubles cuda performance - needs to be set before torch import
6
- if any(arg.startswith('--execution-provider') for arg in sys.argv):
7
- os.environ['OMP_NUM_THREADS'] = '1'
8
- # reduce tensorflow log level
9
- os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
10
- import warnings
11
- from typing import List
12
- import platform
13
- import signal
14
- import shutil
15
- import argparse
16
- import torch
17
- import onnxruntime
18
- if not 'CUDAExecutionProvider' in onnxruntime.get_available_providers():
19
- del torch
20
- import tensorflow
21
-
22
- import roop.globals
23
- import roop.metadata
24
- import roop.ui as ui
25
- from roop.predictor import predict_image, predict_video
26
- from roop.processors.frame.core import get_frame_processors_modules
27
- from roop.utilities import has_image_extension, is_image, is_video, detect_fps, create_video, extract_frames, get_temp_frame_paths, restore_audio, create_temp, move_temp, clean_temp, normalize_output_path
28
-
29
- warnings.filterwarnings('ignore', category=FutureWarning, module='insightface')
30
- warnings.filterwarnings('ignore', category=UserWarning, module='torchvision')
31
-
32
-
33
- def parse_args() -> None:
34
- signal.signal(signal.SIGINT, lambda signal_number, frame: destroy())
35
- program = argparse.ArgumentParser(formatter_class=lambda prog: argparse.HelpFormatter(prog, max_help_position=100))
36
- program.add_argument('-s', '--source', help='select an source image', dest='source_path')
37
- program.add_argument('-t', '--target', help='select an target image or video', dest='target_path')
38
- program.add_argument('-o', '--output', help='select output file or directory', dest='output_path')
39
- program.add_argument('--frame-processor', help='frame processors (choices: face_swapper, face_enhancer, ...)', dest='frame_processor', default=['face_swapper'], nargs='+')
40
- program.add_argument('--keep-fps', help='keep target fps', dest='keep_fps', action='store_true')
41
- program.add_argument('--keep-frames', help='keep temporary frames', dest='keep_frames', action='store_true')
42
- program.add_argument('--skip-audio', help='skip target audio', dest='skip_audio', action='store_true')
43
- program.add_argument('--many-faces', help='process every face', dest='many_faces', action='store_true')
44
- program.add_argument('--reference-face-position', help='position of the reference face', dest='reference_face_position', type=int, default=0)
45
- program.add_argument('--reference-frame-number', help='number of the reference frame', dest='reference_frame_number', type=int, default=0)
46
- program.add_argument('--similar-face-distance', help='face distance used for recognition', dest='similar_face_distance', type=float, default=0.85)
47
- program.add_argument('--temp-frame-format', help='image format used for frame extraction', dest='temp_frame_format', default='png', choices=['jpg', 'png'])
48
- program.add_argument('--temp-frame-quality', help='image quality used for frame extraction', dest='temp_frame_quality', type=int, default=0, choices=range(101), metavar='[0-100]')
49
- program.add_argument('--output-video-encoder', help='encoder used for the output video', dest='output_video_encoder', default='libx264', choices=['libx264', 'libx265', 'libvpx-vp9', 'h264_nvenc', 'hevc_nvenc'])
50
- program.add_argument('--output-video-quality', help='quality used for the output video', dest='output_video_quality', type=int, default=35, choices=range(101), metavar='[0-100]')
51
- program.add_argument('--max-memory', help='maximum amount of RAM in GB', dest='max_memory', type=int)
52
- program.add_argument('--execution-provider', help='available execution provider (choices: cpu, ...)', dest='execution_provider', default=['cpu'], choices=suggest_execution_providers(), nargs='+')
53
- program.add_argument('--execution-threads', help='number of execution threads', dest='execution_threads', type=int, default=suggest_execution_threads())
54
- program.add_argument('-v', '--version', action='version', version=f'{roop.metadata.name} {roop.metadata.version}')
55
-
56
- args = program.parse_args()
57
-
58
- roop.globals.source_path = args.source_path
59
- roop.globals.target_path = args.target_path
60
- roop.globals.output_path = normalize_output_path(roop.globals.source_path, roop.globals.target_path, args.output_path)
61
- roop.globals.headless = roop.globals.source_path is not None and roop.globals.target_path is not None and roop.globals.output_path is not None
62
- roop.globals.frame_processors = args.frame_processor
63
- roop.globals.keep_fps = args.keep_fps
64
- roop.globals.keep_frames = args.keep_frames
65
- roop.globals.skip_audio = args.skip_audio
66
- roop.globals.many_faces = args.many_faces
67
- roop.globals.reference_face_position = args.reference_face_position
68
- roop.globals.reference_frame_number = args.reference_frame_number
69
- roop.globals.similar_face_distance = args.similar_face_distance
70
- roop.globals.temp_frame_format = args.temp_frame_format
71
- roop.globals.temp_frame_quality = args.temp_frame_quality
72
- roop.globals.output_video_encoder = args.output_video_encoder
73
- roop.globals.output_video_quality = args.output_video_quality
74
- roop.globals.max_memory = args.max_memory
75
- roop.globals.execution_providers = decode_execution_providers(args.execution_provider)
76
- roop.globals.execution_threads = args.execution_threads
77
-
78
-
79
- def encode_execution_providers(execution_providers: List[str]) -> List[str]:
80
- return [execution_provider.replace('ExecutionProvider', '').lower() for execution_provider in execution_providers]
81
-
82
-
83
- def decode_execution_providers(execution_providers: List[str]) -> List[str]:
84
- return [provider for provider, encoded_execution_provider in zip(onnxruntime.get_available_providers(), encode_execution_providers(onnxruntime.get_available_providers()))
85
- if any(execution_provider in encoded_execution_provider for execution_provider in execution_providers)]
86
-
87
-
88
- def suggest_execution_providers() -> List[str]:
89
- return encode_execution_providers(onnxruntime.get_available_providers())
90
-
91
-
92
- def suggest_execution_threads() -> int:
93
- if 'CUDAExecutionProvider' in onnxruntime.get_available_providers():
94
- return 8
95
- return 1
96
-
97
-
98
- def limit_resources() -> None:
99
- # prevent tensorflow memory leak
100
- gpus = tensorflow.config.experimental.list_physical_devices('GPU')
101
- for gpu in gpus:
102
- tensorflow.config.experimental.set_virtual_device_configuration(gpu, [
103
- tensorflow.config.experimental.VirtualDeviceConfiguration(memory_limit=1024)
104
- ])
105
- # limit memory usage
106
- if roop.globals.max_memory:
107
- memory = roop.globals.max_memory * 1024 ** 3
108
- if platform.system().lower() == 'darwin':
109
- memory = roop.globals.max_memory * 1024 ** 6
110
- if platform.system().lower() == 'windows':
111
- import ctypes
112
- kernel32 = ctypes.windll.kernel32 # type: ignore[attr-defined]
113
- kernel32.SetProcessWorkingSetSize(-1, ctypes.c_size_t(memory), ctypes.c_size_t(memory))
114
- else:
115
- import resource
116
- resource.setrlimit(resource.RLIMIT_DATA, (memory, memory))
117
-
118
-
119
- def pre_check() -> bool:
120
- if sys.version_info < (3, 9):
121
- update_status('Python version is not supported - please upgrade to 3.9 or higher.')
122
- return False
123
- if not shutil.which('ffmpeg'):
124
- update_status('ffmpeg is not installed.')
125
- return False
126
- return True
127
-
128
-
129
- def update_status(message: str, scope: str = 'ROOP.CORE') -> None:
130
- print(f'[{scope}] {message}')
131
- if not roop.globals.headless:
132
- ui.update_status(message)
133
-
134
-
135
- def start() -> None:
136
- for frame_processor in get_frame_processors_modules(roop.globals.frame_processors):
137
- if not frame_processor.pre_start():
138
- return
139
- # process image to image
140
- if has_image_extension(roop.globals.target_path):
141
- if predict_image(roop.globals.target_path):
142
- destroy()
143
- shutil.copy2(roop.globals.target_path, roop.globals.output_path)
144
- # process frame
145
- for frame_processor in get_frame_processors_modules(roop.globals.frame_processors):
146
- update_status('Progressing...', frame_processor.NAME)
147
- frame_processor.process_image(roop.globals.source_path, roop.globals.output_path, roop.globals.output_path)
148
- frame_processor.post_process()
149
- # validate image
150
- if is_image(roop.globals.target_path):
151
- update_status('Processing to image succeed!')
152
- else:
153
- update_status('Processing to image failed!')
154
- return
155
- # process image to videos
156
- if predict_video(roop.globals.target_path):
157
- destroy()
158
- update_status('Creating temporary resources...')
159
- create_temp(roop.globals.target_path)
160
- # extract frames
161
- if roop.globals.keep_fps:
162
- fps = detect_fps(roop.globals.target_path)
163
- update_status(f'Extracting frames with {fps} FPS...')
164
- extract_frames(roop.globals.target_path, fps)
165
- else:
166
- update_status('Extracting frames with 30 FPS...')
167
- extract_frames(roop.globals.target_path)
168
- # process frame
169
- temp_frame_paths = get_temp_frame_paths(roop.globals.target_path)
170
- if temp_frame_paths:
171
- for frame_processor in get_frame_processors_modules(roop.globals.frame_processors):
172
- update_status('Progressing...', frame_processor.NAME)
173
- frame_processor.process_video(roop.globals.source_path, temp_frame_paths)
174
- frame_processor.post_process()
175
- else:
176
- update_status('Frames not found...')
177
- return
178
- # create video
179
- if roop.globals.keep_fps:
180
- fps = detect_fps(roop.globals.target_path)
181
- update_status(f'Creating video with {fps} FPS...')
182
- create_video(roop.globals.target_path, fps)
183
- else:
184
- update_status('Creating video with 30 FPS...')
185
- create_video(roop.globals.target_path)
186
- # handle audio
187
- if roop.globals.skip_audio:
188
- move_temp(roop.globals.target_path, roop.globals.output_path)
189
- update_status('Skipping audio...')
190
- else:
191
- if roop.globals.keep_fps:
192
- update_status('Restoring audio...')
193
- else:
194
- update_status('Restoring audio might cause issues as fps are not kept...')
195
- restore_audio(roop.globals.target_path, roop.globals.output_path)
196
- # clean temp
197
- update_status('Cleaning temporary resources...')
198
- clean_temp(roop.globals.target_path)
199
- # validate video
200
- if is_video(roop.globals.target_path):
201
- update_status('Processing to video succeed!')
202
- else:
203
- update_status('Processing to video failed!')
204
-
205
-
206
- def destroy() -> None:
207
- if roop.globals.target_path:
208
- clean_temp(roop.globals.target_path)
209
- sys.exit()
210
-
211
-
212
- def run() -> None:
213
- parse_args()
214
- if not pre_check():
215
- return
216
- for frame_processor in get_frame_processors_modules(roop.globals.frame_processors):
217
- if not frame_processor.pre_check():
218
- return
219
- limit_resources()
220
- if roop.globals.headless:
221
- start()
222
- else:
223
- window = ui.init(start, destroy)
224
- window.mainloop()