Moibe commited on
Commit
c11624a
1 Parent(s): 640ac93

User persistance ready OK!

Browse files
Files changed (5) hide show
  1. app.py +5 -9
  2. auth.py +1 -1
  3. funciones.py +119 -122
  4. roop/core.py +224 -224
  5. roop/processors/frame/face_swapper.py +111 -113
app.py CHANGED
@@ -14,14 +14,9 @@ import ast
14
  def perform(input1, input2, request: gr.Request):
15
 
16
  print("5.- Entré a Perform, checando persistencia...")
17
- print("Estando en perform, la cantidad de tokens es (de gr.State): ", gr.State.tokens)
18
- print("Estando en perform, el usuario es (de gr.State): ", gr.State.usuario)
19
-
20
- #Probablemente a partir de aquí traeremos los datos con request y no con gr.State.
21
-
22
  print("Y desde dentro de Perform, ésta es la impresión del request (de request):")
23
  print(request.username)
24
- #Revisaremos de nuevo:
25
  gr.State.tokens = sulkuPypi.getTokens(sulkuPypi.encripta(request.username).decode("utf-8")) #Todo en una línea.
26
  print("Ahora tienes 555: ", gr.State.tokens)
27
 
@@ -55,8 +50,7 @@ def perform(input1, input2, request: gr.Request):
55
  #IMPORTANTE: Tienes que reconstruir capsule ahora que ya se obtiene del request, sino, capsule sera un State para el uso...
56
  #...de todos y es ahí donde radica el problema:
57
  capsule = sulkuPypi.encripta(request.username).decode("utf-8") #decode es para quitarle el 'b
58
- gr.State.capsule = capsule
59
- gr.State.tokens = sulkuPypi.debitTokens(gr.State.capsule, "picswap")
60
  print(f"Y ahora tienes: {gr.State.tokens} tokens.")
61
  html_credits = tools.actualizar_creditos(gr.State.tokens)
62
  print(f"html credits quedó como : {html_credits} y es del tipo: {type(html_credits)}")
@@ -76,10 +70,12 @@ def display_tokens(request: gr.Request):
76
 
77
  print("182: Checando la persistencia de la información cada vez...")
78
  print("Ejecutando display_tokens, tienes (de gr.State): ", gr.State.tokens)
79
- print("Ejecutando display_tokens, eres (de gr.State): ", gr.State.usuario)
 
80
  display = tools.actualizar_creditos(gr.State.tokens)
81
  print("Y ésta es la impresión del request (de request):")
82
  print(request.username)
 
83
  return display
84
 
85
  #Inputs
 
14
  def perform(input1, input2, request: gr.Request):
15
 
16
  print("5.- Entré a Perform, checando persistencia...")
17
+
 
 
 
 
18
  print("Y desde dentro de Perform, ésta es la impresión del request (de request):")
19
  print(request.username)
 
20
  gr.State.tokens = sulkuPypi.getTokens(sulkuPypi.encripta(request.username).decode("utf-8")) #Todo en una línea.
21
  print("Ahora tienes 555: ", gr.State.tokens)
22
 
 
50
  #IMPORTANTE: Tienes que reconstruir capsule ahora que ya se obtiene del request, sino, capsule sera un State para el uso...
51
  #...de todos y es ahí donde radica el problema:
52
  capsule = sulkuPypi.encripta(request.username).decode("utf-8") #decode es para quitarle el 'b
53
+ gr.State.tokens = sulkuPypi.debitTokens(capsule, "picswap")
 
54
  print(f"Y ahora tienes: {gr.State.tokens} tokens.")
55
  html_credits = tools.actualizar_creditos(gr.State.tokens)
56
  print(f"html credits quedó como : {html_credits} y es del tipo: {type(html_credits)}")
 
70
 
71
  print("182: Checando la persistencia de la información cada vez...")
72
  print("Ejecutando display_tokens, tienes (de gr.State): ", gr.State.tokens)
73
+
74
+ gr.State.tokens = sulkuPypi.getTokens(sulkuPypi.encripta(request.username).decode("utf-8"))
75
  display = tools.actualizar_creditos(gr.State.tokens)
76
  print("Y ésta es la impresión del request (de request):")
77
  print(request.username)
78
+
79
  return display
80
 
81
  #Inputs
auth.py CHANGED
@@ -4,7 +4,7 @@ import sulkuPypi
4
  import time
5
  import ast
6
 
7
- def authenticate(username, password, request: gr.Request) :
8
 
9
  print("3.- Entré a authenticate...")
10
 
 
4
  import time
5
  import ast
6
 
7
+ def authenticate(username, password) :
8
 
9
  print("3.- Entré a authenticate...")
10
 
funciones.py CHANGED
@@ -1,123 +1,120 @@
1
- import os
2
- import time
3
- import pathlib
4
- from PIL import Image
5
- #import envCharger
6
-
7
-
8
- def mass(input1, input2):
9
-
10
- # envCharger.load_env("local")
11
- # PLATAFORMA = os.getenv('plataforma')
12
- # print("PLATAFORMA: ", PLATAFORMA)
13
- # time.sleep(5)
14
-
15
- #video o cualquier otro sería para imagenes.
16
- modo = "pic"
17
- #local o huggingface
18
- plataforma = "huggingface"
19
- #face_swapper o face_enhancer o la combinación de ellos.
20
- procesador = "face_swapper"
21
-
22
- print(f"Inicio: Estamos en modo {modo}, plataforma: {plataforma} y procesador: {procesador}.")
23
-
24
- path_video = input2
25
- print("Path_video es:", path_video)
26
-
27
- if modo == "video":
28
-
29
- if plataforma == "local":
30
- #Para local.
31
- path_parts = path_video.split("\\")
32
- else:
33
- #Para HuggingFace
34
- #Creo que no va en imagen.
35
- print("La plataforma en la que basaremos la división es HuggingFace.")
36
- path_parts = path_video.split("/")
37
-
38
- #Aquí obtendremos nom_video
39
- #Creo no va en imagen
40
- filename = path_parts[-1]
41
- nom_video = filename[:-4]
42
- print("Esto es filename alias nom_video: ", nom_video)
43
- path_particular = "/".join(path_parts[0:len(path_parts) - 1])
44
- path_general = "/".join(path_parts[0:len(path_parts) - 2])
45
- path_general = path_general.replace("\\", "/")
46
- path_particular = path_particular.replace("\\", "/")
47
- print("Path general: ", path_general)
48
- print("Path general: ", path_particular)
49
- path = pathlib.Path("result.mp4")
50
- files = os.listdir(path_general)
51
-
52
- print("Estos son los files que hay:")
53
- print(files)
54
-
55
- ext_imagen = "png"
56
- ext_video = "mp4"
57
-
58
- #Selector de modo.
59
- if modo == "video":
60
- print("Se asignó la extensión de video:", ext_video)
61
- extension = ext_video
62
- else:
63
- print("Se asignó la extensión de imagen:", ext_imagen)
64
- extension = ext_imagen
65
-
66
- #El source siempre es una imagen.
67
- source_path = "source.png"
68
- target_path = "target." + extension
69
- result_path = "result." + extension
70
-
71
- #La primera siempre será una imagen, por eso no entra en el modo selector.
72
- source_image = Image.fromarray(input1)
73
- print("Esto es source_image: ", source_image)
74
- source_image.save(source_path)
75
-
76
- #Aquí trabajaremos solo el target.
77
- if modo == "video":
78
- #Para Video
79
- target_path = input2
80
- else:
81
- #Es decir si es modo imagen
82
- #Para Imagenes
83
- target_image = Image.fromarray(input2)
84
- print("Esto es target_image: ", target_image)
85
- target_image.save(target_path)
86
-
87
- print("Después de los selectores de modo los paths quedaron así:")
88
- print("source_path: ", source_path)
89
- print("target_path: ", target_path)
90
-
91
- #FUTURE: Agrega por parámetro o mejor aún por enviroment el hecho de si es compu para usar cpu o si es hf para usar cuda o azure?
92
- #(choose from 'tensorrt', 'cuda', 'cpu')
93
- command = f"python run.py -s {source_path} -t {target_path} -o {result_path} --frame-processor {procesador} --execution-provider cpu"
94
- print(command)
95
- proc = os.popen(command)
96
- output = proc.read()
97
- print("Output (resultado de la ejecución del código):")
98
- print(output)
99
- print("Y el tipo del output es: ", type(output))
100
- time.sleep(18)
101
-
102
- print("Terminó la impresión del output...")
103
-
104
- if "No face in source path detected" in output:
105
- #Si no se detecta un rostro, pondremos un placeholder, ésto evita que se despliegue el último result obtenido antes...
106
- #...de la operación fallida.
107
- print("No se detectó ninguna cara en la ruta de origen.")
108
- result_path = "no-source-face.png"
109
-
110
- else:
111
- print("Si se detecto un rostro...")
112
- #Si sí se detectó un rostro, sigue su camino normal.
113
-
114
- print("Éste es el momento en el que se creo result, revisar...")
115
-
116
- path = pathlib.Path(result_path)
117
- path_abs = os.path.abspath(path)
118
- print("Éste es el path:", path)
119
- print("Y su ruta absoluta es: ", path_abs)
120
- print("Listo! Gracias!")
121
- return path
122
-
123
 
 
1
+ import os
2
+ import time
3
+ import pathlib
4
+ from PIL import Image
5
+ #import envCharger
6
+
7
+
8
+ def mass(input1, input2):
9
+
10
+ # envCharger.load_env("local")
11
+ # PLATAFORMA = os.getenv('plataforma')
12
+ # print("PLATAFORMA: ", PLATAFORMA)
13
+
14
+ #video o cualquier otro sería para imagenes.
15
+ modo = "pic"
16
+ #local o huggingface
17
+ plataforma = "huggingface"
18
+ #face_swapper o face_enhancer o la combinación de ellos.
19
+ procesador = "face_swapper"
20
+
21
+ print(f"Inicio: Estamos en modo {modo}, plataforma: {plataforma} y procesador: {procesador}.")
22
+
23
+ path_video = input2
24
+ print("Path_video es:", path_video)
25
+
26
+ if modo == "video":
27
+
28
+ if plataforma == "local":
29
+ #Para local.
30
+ path_parts = path_video.split("\\")
31
+ else:
32
+ #Para HuggingFace
33
+ #Creo que no va en imagen.
34
+ print("La plataforma en la que basaremos la división es HuggingFace.")
35
+ path_parts = path_video.split("/")
36
+
37
+ #Aquí obtendremos nom_video
38
+ #Creo no va en imagen
39
+ filename = path_parts[-1]
40
+ nom_video = filename[:-4]
41
+ print("Esto es filename alias nom_video: ", nom_video)
42
+ path_particular = "/".join(path_parts[0:len(path_parts) - 1])
43
+ path_general = "/".join(path_parts[0:len(path_parts) - 2])
44
+ path_general = path_general.replace("\\", "/")
45
+ path_particular = path_particular.replace("\\", "/")
46
+ print("Path general: ", path_general)
47
+ print("Path general: ", path_particular)
48
+ path = pathlib.Path("result.mp4")
49
+ files = os.listdir(path_general)
50
+
51
+ print("Estos son los files que hay:")
52
+ print(files)
53
+
54
+ ext_imagen = "png"
55
+ ext_video = "mp4"
56
+
57
+ #Selector de modo.
58
+ if modo == "video":
59
+ print("Se asignó la extensión de video:", ext_video)
60
+ extension = ext_video
61
+ else:
62
+ print("Se asignó la extensión de imagen:", ext_imagen)
63
+ extension = ext_imagen
64
+
65
+ #El source siempre es una imagen.
66
+ source_path = "source.png"
67
+ target_path = "target." + extension
68
+ result_path = "result." + extension
69
+
70
+ #La primera siempre será una imagen, por eso no entra en el modo selector.
71
+ source_image = Image.fromarray(input1)
72
+ print("Esto es source_image: ", source_image)
73
+ source_image.save(source_path)
74
+
75
+ #Aquí trabajaremos solo el target.
76
+ if modo == "video":
77
+ #Para Video
78
+ target_path = input2
79
+ else:
80
+ #Es decir si es modo imagen
81
+ #Para Imagenes
82
+ target_image = Image.fromarray(input2)
83
+ print("Esto es target_image: ", target_image)
84
+ target_image.save(target_path)
85
+
86
+ print("Después de los selectores de modo los paths quedaron así:")
87
+ print("source_path: ", source_path)
88
+ print("target_path: ", target_path)
89
+
90
+ #FUTURE: Agrega por parámetro o mejor aún por enviroment el hecho de si es compu para usar cpu o si es hf para usar cuda o azure?
91
+ #(choose from 'tensorrt', 'cuda', 'cpu')
92
+ command = f"python run.py -s {source_path} -t {target_path} -o {result_path} --frame-processor {procesador} --execution-provider cpu"
93
+ print(command)
94
+ proc = os.popen(command)
95
+ output = proc.read()
96
+ print("Output (resultado de la ejecución del código):")
97
+ print(output)
98
+
99
+ print("Terminó la impresión del output...")
100
+
101
+ if "No face in source path detected" in output:
102
+ #Si no se detecta un rostro, pondremos un placeholder, ésto evita que se despliegue el último result obtenido antes...
103
+ #...de la operación fallida.
104
+ print("No se detectó ninguna cara en la ruta de origen.")
105
+ result_path = "no-source-face.png"
106
+
107
+ else:
108
+ print("Si se detecto un rostro...")
109
+ #Si sí se detectó un rostro, sigue su camino normal.
110
+
111
+ print("Éste es el momento en el que se creo result, revisar...")
112
+
113
+ path = pathlib.Path(result_path)
114
+ path_abs = os.path.abspath(path)
115
+ print("Éste es el path:", path)
116
+ print("Y su ruta absoluta es: ", path_abs)
117
+ print("Listo! Gracias!")
118
+ return path
119
+
 
 
 
120
 
roop/core.py CHANGED
@@ -1,224 +1,224 @@
1
- #!/usr/bin/env python3
2
- import time
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 onnxruntime
17
- import tensorflow
18
- import roop.globals
19
- import roop.metadata
20
- import roop.ui as ui
21
- from roop.predictor import predict_image, predict_video
22
- from roop.processors.frame.core import get_frame_processors_modules
23
- 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
24
-
25
- warnings.filterwarnings('ignore', category=FutureWarning, module='insightface')
26
- warnings.filterwarnings('ignore', category=UserWarning, module='torchvision')
27
-
28
-
29
- def parse_args() -> None:
30
- signal.signal(signal.SIGINT, lambda signal_number, frame: destroy())
31
- program = argparse.ArgumentParser(formatter_class=lambda prog: argparse.HelpFormatter(prog, max_help_position=100))
32
- program.add_argument('-s', '--source', help='select an source image', dest='source_path')
33
- program.add_argument('-t', '--target', help='select an target image or video', dest='target_path')
34
- program.add_argument('-o', '--output', help='select output file or directory', dest='output_path')
35
- program.add_argument('--frame-processor', help='frame processors (choices: face_swapper, face_enhancer, ...)', dest='frame_processor', default=['face_swapper'], nargs='+')
36
- program.add_argument('--keep-fps', help='keep target fps', dest='keep_fps', action='store_true')
37
- program.add_argument('--keep-frames', help='keep temporary frames', dest='keep_frames', action='store_true')
38
- program.add_argument('--skip-audio', help='skip target audio', dest='skip_audio', action='store_true')
39
- program.add_argument('--many-faces', help='process every face', dest='many_faces', action='store_true')
40
- program.add_argument('--reference-face-position', help='position of the reference face', dest='reference_face_position', type=int, default=0)
41
- program.add_argument('--reference-frame-number', help='number of the reference frame', dest='reference_frame_number', type=int, default=0)
42
- program.add_argument('--similar-face-distance', help='face distance used for recognition', dest='similar_face_distance', type=float, default=0.85)
43
- program.add_argument('--temp-frame-format', help='image format used for frame extraction', dest='temp_frame_format', default='png', choices=['jpg', 'png'])
44
- 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]')
45
- 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'])
46
- 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]')
47
- program.add_argument('--max-memory', help='maximum amount of RAM in GB', dest='max_memory', type=int)
48
- program.add_argument('--execution-provider', help='available execution provider (choices: cpu, ...)', dest='execution_provider', default=['cpu'], choices=suggest_execution_providers(), nargs='+')
49
- program.add_argument('--execution-threads', help='number of execution threads', dest='execution_threads', type=int, default=suggest_execution_threads())
50
- program.add_argument('-v', '--version', action='version', version=f'{roop.metadata.name} {roop.metadata.version}')
51
-
52
- args = program.parse_args()
53
-
54
- roop.globals.source_path = args.source_path
55
- roop.globals.target_path = args.target_path
56
- roop.globals.output_path = normalize_output_path(roop.globals.source_path, roop.globals.target_path, args.output_path)
57
- 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
58
- roop.globals.frame_processors = args.frame_processor
59
- roop.globals.keep_fps = args.keep_fps
60
- roop.globals.keep_frames = args.keep_frames
61
- roop.globals.skip_audio = args.skip_audio
62
- roop.globals.many_faces = args.many_faces
63
- roop.globals.reference_face_position = args.reference_face_position
64
- roop.globals.reference_frame_number = args.reference_frame_number
65
- roop.globals.similar_face_distance = args.similar_face_distance
66
- roop.globals.temp_frame_format = args.temp_frame_format
67
- roop.globals.temp_frame_quality = args.temp_frame_quality
68
- roop.globals.output_video_encoder = args.output_video_encoder
69
- roop.globals.output_video_quality = args.output_video_quality
70
- roop.globals.max_memory = args.max_memory
71
- roop.globals.execution_providers = decode_execution_providers(args.execution_provider)
72
- roop.globals.execution_threads = args.execution_threads
73
-
74
-
75
- def encode_execution_providers(execution_providers: List[str]) -> List[str]:
76
- return [execution_provider.replace('ExecutionProvider', '').lower() for execution_provider in execution_providers]
77
-
78
-
79
- def decode_execution_providers(execution_providers: List[str]) -> List[str]:
80
- return [provider for provider, encoded_execution_provider in zip(onnxruntime.get_available_providers(), encode_execution_providers(onnxruntime.get_available_providers()))
81
- if any(execution_provider in encoded_execution_provider for execution_provider in execution_providers)]
82
-
83
-
84
- def suggest_execution_providers() -> List[str]:
85
- return encode_execution_providers(onnxruntime.get_available_providers())
86
-
87
-
88
- def suggest_execution_threads() -> int:
89
- if 'CUDAExecutionProvider' in onnxruntime.get_available_providers():
90
- return 8
91
- return 1
92
-
93
-
94
- def limit_resources() -> None:
95
- # prevent tensorflow memory leak
96
- gpus = tensorflow.config.experimental.list_physical_devices('GPU')
97
- for gpu in gpus:
98
- tensorflow.config.experimental.set_virtual_device_configuration(gpu, [
99
- tensorflow.config.experimental.VirtualDeviceConfiguration(memory_limit=1024)
100
- ])
101
- # limit memory usage
102
- if roop.globals.max_memory:
103
- memory = roop.globals.max_memory * 1024 ** 3
104
- if platform.system().lower() == 'darwin':
105
- memory = roop.globals.max_memory * 1024 ** 6
106
- if platform.system().lower() == 'windows':
107
- import ctypes
108
- kernel32 = ctypes.windll.kernel32 # type: ignore[attr-defined]
109
- kernel32.SetProcessWorkingSetSize(-1, ctypes.c_size_t(memory), ctypes.c_size_t(memory))
110
- else:
111
- import resource
112
- resource.setrlimit(resource.RLIMIT_DATA, (memory, memory))
113
-
114
-
115
- def pre_check() -> bool:
116
- if sys.version_info < (3, 9):
117
- update_status('Python version is not supported - please upgrade to 3.9 or higher.')
118
- return False
119
- if not shutil.which('ffmpeg'):
120
- update_status('ffmpeg is not installed.')
121
- return False
122
- return True
123
-
124
-
125
- def update_status(message: str, scope: str = 'ROOP.CORE') -> None:
126
- print(f'[{scope}] {message}')
127
- if not roop.globals.headless:
128
- ui.update_status(message)
129
-
130
-
131
- def start() -> None:
132
- for frame_processor in get_frame_processors_modules(roop.globals.frame_processors):
133
- if not frame_processor.pre_start():
134
- return
135
- # process image to image
136
- if has_image_extension(roop.globals.target_path):
137
- if predict_image(roop.globals.target_path):
138
- destroy()
139
- shutil.copy2(roop.globals.target_path, roop.globals.output_path)
140
- # process frame
141
- for frame_processor in get_frame_processors_modules(roop.globals.frame_processors):
142
- update_status('Progressing...', frame_processor.NAME)
143
- frame_processor.process_image(roop.globals.source_path, roop.globals.output_path, roop.globals.output_path)
144
- print("MOI: roop.globals.source_path es: ", roop.globals.source_path)
145
- time.sleep(30)
146
- frame_processor.post_process()
147
- # validate image
148
- if is_image(roop.globals.target_path):
149
- update_status('Processing to image succeed!')
150
- else:
151
- update_status('Processing to image failed!')
152
- return
153
- # process image to videos
154
- if predict_video(roop.globals.target_path):
155
- destroy()
156
- update_status('Creating temporary resources...')
157
- create_temp(roop.globals.target_path)
158
- # extract frames
159
- if roop.globals.keep_fps:
160
- fps = detect_fps(roop.globals.target_path)
161
- update_status(f'Extracting frames with {fps} FPS...')
162
- extract_frames(roop.globals.target_path, fps)
163
- else:
164
- update_status('Extracting frames with 30 FPS...')
165
- extract_frames(roop.globals.target_path)
166
- # process frame
167
- temp_frame_paths = get_temp_frame_paths(roop.globals.target_path)
168
- if temp_frame_paths:
169
- for frame_processor in get_frame_processors_modules(roop.globals.frame_processors):
170
- update_status('Progressing...', frame_processor.NAME)
171
- frame_processor.process_video(roop.globals.source_path, temp_frame_paths)
172
- frame_processor.post_process()
173
- else:
174
- update_status('Frames not found...')
175
- return
176
- # create video
177
- if roop.globals.keep_fps:
178
- fps = detect_fps(roop.globals.target_path)
179
- update_status(f'Creating video with {fps} FPS...')
180
- create_video(roop.globals.target_path, fps)
181
- else:
182
- update_status('Creating video with 30 FPS...')
183
- create_video(roop.globals.target_path)
184
- # handle audio
185
- if roop.globals.skip_audio:
186
- move_temp(roop.globals.target_path, roop.globals.output_path)
187
- update_status('Skipping audio...')
188
- else:
189
- if roop.globals.keep_fps:
190
- update_status('Restoring audio...')
191
- else:
192
- update_status('Restoring audio might cause issues as fps are not kept...')
193
- restore_audio(roop.globals.target_path, roop.globals.output_path)
194
- # clean temp
195
- update_status('Cleaning temporary resources...')
196
- print("No voy a limpiar el temp...")
197
- time.sleep(3)
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()
 
1
+ #!/usr/bin/env python3
2
+ import time
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 onnxruntime
17
+ import tensorflow
18
+ import roop.globals
19
+ import roop.metadata
20
+ import roop.ui as ui
21
+ from roop.predictor import predict_image, predict_video
22
+ from roop.processors.frame.core import get_frame_processors_modules
23
+ 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
24
+
25
+ warnings.filterwarnings('ignore', category=FutureWarning, module='insightface')
26
+ warnings.filterwarnings('ignore', category=UserWarning, module='torchvision')
27
+
28
+
29
+ def parse_args() -> None:
30
+ signal.signal(signal.SIGINT, lambda signal_number, frame: destroy())
31
+ program = argparse.ArgumentParser(formatter_class=lambda prog: argparse.HelpFormatter(prog, max_help_position=100))
32
+ program.add_argument('-s', '--source', help='select an source image', dest='source_path')
33
+ program.add_argument('-t', '--target', help='select an target image or video', dest='target_path')
34
+ program.add_argument('-o', '--output', help='select output file or directory', dest='output_path')
35
+ program.add_argument('--frame-processor', help='frame processors (choices: face_swapper, face_enhancer, ...)', dest='frame_processor', default=['face_swapper'], nargs='+')
36
+ program.add_argument('--keep-fps', help='keep target fps', dest='keep_fps', action='store_true')
37
+ program.add_argument('--keep-frames', help='keep temporary frames', dest='keep_frames', action='store_true')
38
+ program.add_argument('--skip-audio', help='skip target audio', dest='skip_audio', action='store_true')
39
+ program.add_argument('--many-faces', help='process every face', dest='many_faces', action='store_true')
40
+ program.add_argument('--reference-face-position', help='position of the reference face', dest='reference_face_position', type=int, default=0)
41
+ program.add_argument('--reference-frame-number', help='number of the reference frame', dest='reference_frame_number', type=int, default=0)
42
+ program.add_argument('--similar-face-distance', help='face distance used for recognition', dest='similar_face_distance', type=float, default=0.85)
43
+ program.add_argument('--temp-frame-format', help='image format used for frame extraction', dest='temp_frame_format', default='png', choices=['jpg', 'png'])
44
+ 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]')
45
+ 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'])
46
+ 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]')
47
+ program.add_argument('--max-memory', help='maximum amount of RAM in GB', dest='max_memory', type=int)
48
+ program.add_argument('--execution-provider', help='available execution provider (choices: cpu, ...)', dest='execution_provider', default=['cpu'], choices=suggest_execution_providers(), nargs='+')
49
+ program.add_argument('--execution-threads', help='number of execution threads', dest='execution_threads', type=int, default=suggest_execution_threads())
50
+ program.add_argument('-v', '--version', action='version', version=f'{roop.metadata.name} {roop.metadata.version}')
51
+
52
+ args = program.parse_args()
53
+
54
+ roop.globals.source_path = args.source_path
55
+ roop.globals.target_path = args.target_path
56
+ roop.globals.output_path = normalize_output_path(roop.globals.source_path, roop.globals.target_path, args.output_path)
57
+ 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
58
+ roop.globals.frame_processors = args.frame_processor
59
+ roop.globals.keep_fps = args.keep_fps
60
+ roop.globals.keep_frames = args.keep_frames
61
+ roop.globals.skip_audio = args.skip_audio
62
+ roop.globals.many_faces = args.many_faces
63
+ roop.globals.reference_face_position = args.reference_face_position
64
+ roop.globals.reference_frame_number = args.reference_frame_number
65
+ roop.globals.similar_face_distance = args.similar_face_distance
66
+ roop.globals.temp_frame_format = args.temp_frame_format
67
+ roop.globals.temp_frame_quality = args.temp_frame_quality
68
+ roop.globals.output_video_encoder = args.output_video_encoder
69
+ roop.globals.output_video_quality = args.output_video_quality
70
+ roop.globals.max_memory = args.max_memory
71
+ roop.globals.execution_providers = decode_execution_providers(args.execution_provider)
72
+ roop.globals.execution_threads = args.execution_threads
73
+
74
+
75
+ def encode_execution_providers(execution_providers: List[str]) -> List[str]:
76
+ return [execution_provider.replace('ExecutionProvider', '').lower() for execution_provider in execution_providers]
77
+
78
+
79
+ def decode_execution_providers(execution_providers: List[str]) -> List[str]:
80
+ return [provider for provider, encoded_execution_provider in zip(onnxruntime.get_available_providers(), encode_execution_providers(onnxruntime.get_available_providers()))
81
+ if any(execution_provider in encoded_execution_provider for execution_provider in execution_providers)]
82
+
83
+
84
+ def suggest_execution_providers() -> List[str]:
85
+ return encode_execution_providers(onnxruntime.get_available_providers())
86
+
87
+
88
+ def suggest_execution_threads() -> int:
89
+ if 'CUDAExecutionProvider' in onnxruntime.get_available_providers():
90
+ return 8
91
+ return 1
92
+
93
+
94
+ def limit_resources() -> None:
95
+ # prevent tensorflow memory leak
96
+ gpus = tensorflow.config.experimental.list_physical_devices('GPU')
97
+ for gpu in gpus:
98
+ tensorflow.config.experimental.set_virtual_device_configuration(gpu, [
99
+ tensorflow.config.experimental.VirtualDeviceConfiguration(memory_limit=1024)
100
+ ])
101
+ # limit memory usage
102
+ if roop.globals.max_memory:
103
+ memory = roop.globals.max_memory * 1024 ** 3
104
+ if platform.system().lower() == 'darwin':
105
+ memory = roop.globals.max_memory * 1024 ** 6
106
+ if platform.system().lower() == 'windows':
107
+ import ctypes
108
+ kernel32 = ctypes.windll.kernel32 # type: ignore[attr-defined]
109
+ kernel32.SetProcessWorkingSetSize(-1, ctypes.c_size_t(memory), ctypes.c_size_t(memory))
110
+ else:
111
+ import resource
112
+ resource.setrlimit(resource.RLIMIT_DATA, (memory, memory))
113
+
114
+
115
+ def pre_check() -> bool:
116
+ if sys.version_info < (3, 9):
117
+ update_status('Python version is not supported - please upgrade to 3.9 or higher.')
118
+ return False
119
+ if not shutil.which('ffmpeg'):
120
+ update_status('ffmpeg is not installed.')
121
+ return False
122
+ return True
123
+
124
+
125
+ def update_status(message: str, scope: str = 'ROOP.CORE') -> None:
126
+ print(f'[{scope}] {message}')
127
+ if not roop.globals.headless:
128
+ ui.update_status(message)
129
+
130
+
131
+ def start() -> None:
132
+ for frame_processor in get_frame_processors_modules(roop.globals.frame_processors):
133
+ if not frame_processor.pre_start():
134
+ return
135
+ # process image to image
136
+ if has_image_extension(roop.globals.target_path):
137
+ if predict_image(roop.globals.target_path):
138
+ destroy()
139
+ shutil.copy2(roop.globals.target_path, roop.globals.output_path)
140
+ # process frame
141
+ for frame_processor in get_frame_processors_modules(roop.globals.frame_processors):
142
+ update_status('Progressing...', frame_processor.NAME)
143
+ frame_processor.process_image(roop.globals.source_path, roop.globals.output_path, roop.globals.output_path)
144
+ print("MOI182: roop.globals.source_path es: ", roop.globals.source_path)
145
+ #time.sleep(30)
146
+ frame_processor.post_process()
147
+ # validate image
148
+ if is_image(roop.globals.target_path):
149
+ update_status('Processing to image succeed!')
150
+ else:
151
+ update_status('Processing to image failed!')
152
+ return
153
+ # process image to videos
154
+ if predict_video(roop.globals.target_path):
155
+ destroy()
156
+ update_status('Creating temporary resources...')
157
+ create_temp(roop.globals.target_path)
158
+ # extract frames
159
+ if roop.globals.keep_fps:
160
+ fps = detect_fps(roop.globals.target_path)
161
+ update_status(f'Extracting frames with {fps} FPS...')
162
+ extract_frames(roop.globals.target_path, fps)
163
+ else:
164
+ update_status('Extracting frames with 30 FPS...')
165
+ extract_frames(roop.globals.target_path)
166
+ # process frame
167
+ temp_frame_paths = get_temp_frame_paths(roop.globals.target_path)
168
+ if temp_frame_paths:
169
+ for frame_processor in get_frame_processors_modules(roop.globals.frame_processors):
170
+ update_status('Progressing...', frame_processor.NAME)
171
+ frame_processor.process_video(roop.globals.source_path, temp_frame_paths)
172
+ frame_processor.post_process()
173
+ else:
174
+ update_status('Frames not found...')
175
+ return
176
+ # create video
177
+ if roop.globals.keep_fps:
178
+ fps = detect_fps(roop.globals.target_path)
179
+ update_status(f'Creating video with {fps} FPS...')
180
+ create_video(roop.globals.target_path, fps)
181
+ else:
182
+ update_status('Creating video with 30 FPS...')
183
+ create_video(roop.globals.target_path)
184
+ # handle audio
185
+ if roop.globals.skip_audio:
186
+ move_temp(roop.globals.target_path, roop.globals.output_path)
187
+ update_status('Skipping audio...')
188
+ else:
189
+ if roop.globals.keep_fps:
190
+ update_status('Restoring audio...')
191
+ else:
192
+ update_status('Restoring audio might cause issues as fps are not kept...')
193
+ restore_audio(roop.globals.target_path, roop.globals.output_path)
194
+ # clean temp
195
+ update_status('Cleaning temporary resources...')
196
+ print("No voy a limpiar el temp...")
197
+ time.sleep(3)
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()
roop/processors/frame/face_swapper.py CHANGED
@@ -1,113 +1,111 @@
1
- from typing import Any, List, Callable
2
- import cv2
3
- import insightface
4
- import threading
5
- import requests
6
- import os
7
- import time
8
-
9
- import roop.globals
10
- import roop.processors.frame.core
11
- from roop.core import update_status
12
- from roop.face_analyser import get_one_face, get_many_faces
13
- from roop.typing import Face, Frame
14
- from roop.utilities import conditional_download, resolve_relative_path, is_image, is_video
15
-
16
- FACE_SWAPPER = None
17
- THREAD_LOCK = threading.Lock()
18
- NAME = 'ROOP.FACE-SWAPPER'
19
-
20
-
21
- def get_face_swapper() -> Any:
22
- global FACE_SWAPPER
23
-
24
- with THREAD_LOCK:
25
- if FACE_SWAPPER is None:
26
-
27
- model_path = resolve_relative_path('../inswapper_128.onnx')
28
- found = os.path.exists(model_path)
29
- print("La ruta se encontró:", found)
30
- time.sleep(4)
31
-
32
- if found is True:
33
- print("Es verdad que la ruta si se encontró...")
34
- else:
35
- print("La ruta no se encontró por cierto...")
36
- time.sleep(5)
37
- print("Lo bajaré remotamente...")
38
- #Ésta es la forma de bajarlo si no se encuentra en su ruta.
39
- url = "https://huggingface.co/countfloyd/deepfake/resolve/main/inswapper_128.onnx"
40
- response = requests.get(url)
41
- content = response.content
42
- path = "inswapper_128.onnx"
43
- with open(path, "wb") as f:
44
- f.write(content)
45
- model_path = os.path.join(os.getcwd(), path)
46
-
47
- #También Probar cuando esté arriba en huggingface, si con la ruta relativa llega.
48
-
49
-
50
- FACE_SWAPPER = insightface.model_zoo.get_model(model_path, providers=roop.globals.execution_providers)
51
-
52
- return FACE_SWAPPER
53
-
54
-
55
- def pre_check() -> bool:
56
- download_directory_path = resolve_relative_path('../content/roop')
57
- #Al parecer ésto no se usa.
58
- conditional_download(download_directory_path, ['https://huggingface.co/countfloyd/deepfake/resolve/main/inswapper_128.onnx'])
59
- return True
60
-
61
-
62
- def pre_start() -> bool:
63
- if not is_image(roop.globals.source_path):
64
- update_status('Select an image for source path.', NAME)
65
- return False
66
- elif not get_one_face(cv2.imread(roop.globals.source_path)):
67
- update_status('No face in source path detected.', NAME)
68
- return False
69
- if not is_image(roop.globals.target_path) and not is_video(roop.globals.target_path):
70
- update_status('Select an image or video for target path.', NAME)
71
- return False
72
- return True
73
-
74
-
75
- def post_process() -> None:
76
- global FACE_SWAPPER
77
-
78
- FACE_SWAPPER = None
79
-
80
-
81
- def swap_face(source_face: Face, target_face: Face, temp_frame: Frame) -> Frame:
82
- return get_face_swapper().get(temp_frame, target_face, source_face, paste_back=True)
83
-
84
-
85
- def process_frame(source_face: Face, temp_frame: Frame) -> Frame:
86
- if roop.globals.many_faces:
87
- if many_faces := get_many_faces(temp_frame):
88
- for target_face in many_faces:
89
- temp_frame = swap_face(source_face, target_face, temp_frame)
90
- elif target_face := get_one_face(temp_frame):
91
- temp_frame = swap_face(source_face, target_face, temp_frame)
92
- return temp_frame
93
-
94
-
95
- def process_frames(source_path: str, temp_frame_paths: List[str], update: Callable[[], None]) -> None:
96
- source_face = get_one_face(cv2.imread(source_path))
97
- for temp_frame_path in temp_frame_paths:
98
- temp_frame = cv2.imread(temp_frame_path)
99
- result = process_frame(source_face, temp_frame)
100
- cv2.imwrite(temp_frame_path, result)
101
- if update:
102
- update()
103
-
104
-
105
- def process_image(source_path: str, target_path: str, output_path: str) -> None:
106
- source_face = get_one_face(cv2.imread(source_path))
107
- target_frame = cv2.imread(target_path)
108
- result = process_frame(source_face, target_frame)
109
- cv2.imwrite(output_path, result)
110
-
111
-
112
- def process_video(source_path: str, temp_frame_paths: List[str]) -> None:
113
- roop.processors.frame.core.process_video(source_path, temp_frame_paths, process_frames)
 
1
+ from typing import Any, List, Callable
2
+ import cv2
3
+ import insightface
4
+ import threading
5
+ import requests
6
+ import os
7
+ import time
8
+
9
+ import roop.globals
10
+ import roop.processors.frame.core
11
+ from roop.core import update_status
12
+ from roop.face_analyser import get_one_face, get_many_faces
13
+ from roop.typing import Face, Frame
14
+ from roop.utilities import conditional_download, resolve_relative_path, is_image, is_video
15
+
16
+ FACE_SWAPPER = None
17
+ THREAD_LOCK = threading.Lock()
18
+ NAME = 'ROOP.FACE-SWAPPER'
19
+
20
+
21
+ def get_face_swapper() -> Any:
22
+ global FACE_SWAPPER
23
+
24
+ with THREAD_LOCK:
25
+ if FACE_SWAPPER is None:
26
+
27
+ model_path = resolve_relative_path('../inswapper_128.onnx')
28
+ found = os.path.exists(model_path)
29
+ print("La ruta se encontró:", found)
30
+
31
+ if found is True:
32
+ print("Es verdad que la ruta si se encontró...")
33
+ else:
34
+ print("La ruta no se encontró por cierto...")
35
+ print("Lo bajaré remotamente...")
36
+ #Ésta es la forma de bajarlo si no se encuentra en su ruta.
37
+ url = "https://huggingface.co/countfloyd/deepfake/resolve/main/inswapper_128.onnx"
38
+ response = requests.get(url)
39
+ content = response.content
40
+ path = "inswapper_128.onnx"
41
+ with open(path, "wb") as f:
42
+ f.write(content)
43
+ model_path = os.path.join(os.getcwd(), path)
44
+
45
+ #También Probar cuando esté arriba en huggingface, si con la ruta relativa llega.
46
+
47
+
48
+ FACE_SWAPPER = insightface.model_zoo.get_model(model_path, providers=roop.globals.execution_providers)
49
+
50
+ return FACE_SWAPPER
51
+
52
+
53
+ def pre_check() -> bool:
54
+ download_directory_path = resolve_relative_path('../content/roop')
55
+ #Al parecer ésto no se usa.
56
+ conditional_download(download_directory_path, ['https://huggingface.co/countfloyd/deepfake/resolve/main/inswapper_128.onnx'])
57
+ return True
58
+
59
+
60
+ def pre_start() -> bool:
61
+ if not is_image(roop.globals.source_path):
62
+ update_status('Select an image for source path.', NAME)
63
+ return False
64
+ elif not get_one_face(cv2.imread(roop.globals.source_path)):
65
+ update_status('No face in source path detected.', NAME)
66
+ return False
67
+ if not is_image(roop.globals.target_path) and not is_video(roop.globals.target_path):
68
+ update_status('Select an image or video for target path.', NAME)
69
+ return False
70
+ return True
71
+
72
+
73
+ def post_process() -> None:
74
+ global FACE_SWAPPER
75
+
76
+ FACE_SWAPPER = None
77
+
78
+
79
+ def swap_face(source_face: Face, target_face: Face, temp_frame: Frame) -> Frame:
80
+ return get_face_swapper().get(temp_frame, target_face, source_face, paste_back=True)
81
+
82
+
83
+ def process_frame(source_face: Face, temp_frame: Frame) -> Frame:
84
+ if roop.globals.many_faces:
85
+ if many_faces := get_many_faces(temp_frame):
86
+ for target_face in many_faces:
87
+ temp_frame = swap_face(source_face, target_face, temp_frame)
88
+ elif target_face := get_one_face(temp_frame):
89
+ temp_frame = swap_face(source_face, target_face, temp_frame)
90
+ return temp_frame
91
+
92
+
93
+ def process_frames(source_path: str, temp_frame_paths: List[str], update: Callable[[], None]) -> None:
94
+ source_face = get_one_face(cv2.imread(source_path))
95
+ for temp_frame_path in temp_frame_paths:
96
+ temp_frame = cv2.imread(temp_frame_path)
97
+ result = process_frame(source_face, temp_frame)
98
+ cv2.imwrite(temp_frame_path, result)
99
+ if update:
100
+ update()
101
+
102
+
103
+ def process_image(source_path: str, target_path: str, output_path: str) -> None:
104
+ source_face = get_one_face(cv2.imread(source_path))
105
+ target_frame = cv2.imread(target_path)
106
+ result = process_frame(source_face, target_frame)
107
+ cv2.imwrite(output_path, result)
108
+
109
+
110
+ def process_video(source_path: str, temp_frame_paths: List[str]) -> None:
111
+ roop.processors.frame.core.process_video(source_path, temp_frame_paths, process_frames)