Spaces:
Build error
Build error
| import os | |
| import time | |
| import pathlib | |
| from PIL import Image | |
| #import envCharger | |
| def mass(input1, input2): | |
| # envCharger.load_env("local") | |
| # PLATAFORMA = os.getenv('plataforma') | |
| # print("PLATAFORMA: ", PLATAFORMA) | |
| #video o cualquier otro ser铆a para imagenes. | |
| modo = "pic" | |
| #local o huggingface | |
| plataforma = "huggingface" | |
| #face_swapper o face_enhancer o la combinaci贸n de ellos. | |
| procesador = "face_swapper" | |
| print(f"Inicio: Estamos en modo {modo}, plataforma: {plataforma} y procesador: {procesador}.") | |
| path_video = input2 | |
| print("Path_video es:", path_video) | |
| if modo == "video": | |
| if plataforma == "local": | |
| #Para local. | |
| path_parts = path_video.split("\\") | |
| else: | |
| #Para HuggingFace | |
| #Creo que no va en imagen. | |
| print("La plataforma en la que basaremos la divisi贸n es HuggingFace.") | |
| path_parts = path_video.split("/") | |
| #Aqu铆 obtendremos nom_video | |
| #Creo no va en imagen | |
| filename = path_parts[-1] | |
| nom_video = filename[:-4] | |
| print("Esto es filename alias nom_video: ", nom_video) | |
| path_particular = "/".join(path_parts[0:len(path_parts) - 1]) | |
| path_general = "/".join(path_parts[0:len(path_parts) - 2]) | |
| path_general = path_general.replace("\\", "/") | |
| path_particular = path_particular.replace("\\", "/") | |
| print("Path general: ", path_general) | |
| print("Path general: ", path_particular) | |
| path = pathlib.Path("result.mp4") | |
| files = os.listdir(path_general) | |
| print("Estos son los files que hay:") | |
| print(files) | |
| ext_imagen = "png" | |
| ext_video = "mp4" | |
| #Selector de modo. | |
| if modo == "video": | |
| print("Se asign贸 la extensi贸n de video:", ext_video) | |
| extension = ext_video | |
| else: | |
| print("Se asign贸 la extensi贸n de imagen:", ext_imagen) | |
| extension = ext_imagen | |
| #El source siempre es una imagen. | |
| source_path = "source.png" | |
| target_path = "target." + extension | |
| result_path = "result." + extension | |
| #La primera siempre ser谩 una imagen, por eso no entra en el modo selector. | |
| source_image = Image.fromarray(input1) | |
| print("Esto es source_image: ", source_image) | |
| source_image.save(source_path) | |
| #Aqu铆 trabajaremos solo el target. | |
| if modo == "video": | |
| #Para Video | |
| target_path = input2 | |
| else: | |
| #Es decir si es modo imagen | |
| #Para Imagenes | |
| target_image = Image.fromarray(input2) | |
| print("Esto es target_image: ", target_image) | |
| target_image.save(target_path) | |
| print("Despu茅s de los selectores de modo los paths quedaron as铆:") | |
| print("source_path: ", source_path) | |
| print("target_path: ", target_path) | |
| #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? | |
| #(choose from 'tensorrt', 'cuda', 'cpu') | |
| command = f"python run.py -s {source_path} -t {target_path} -o {result_path} --frame-processor {procesador} --execution-provider cpu" | |
| print(command) | |
| proc = os.popen(command) | |
| output = proc.read() | |
| print("Output (resultado de la ejecuci贸n del c贸digo):") | |
| print(output) | |
| print("Termin贸 la impresi贸n del output...") | |
| if "No face in source path detected" in output: | |
| #Si no se detecta un rostro, pondremos un placeholder, 茅sto evita que se despliegue el 煤ltimo result obtenido antes... | |
| #...de la operaci贸n fallida. | |
| print("No se detect贸 ninguna cara en la ruta de origen.") | |
| result_path = "no-source-face.png" | |
| else: | |
| print("Si se detecto un rostro...") | |
| #Si s铆 se detect贸 un rostro, sigue su camino normal. | |
| print("脡ste es el momento en el que se creo result, revisar...") | |
| path = pathlib.Path(result_path) | |
| path_abs = os.path.abspath(path) | |
| print("脡ste es el path:", path) | |
| print("Y su ruta absoluta es: ", path_abs) | |
| print("Listo! Gracias!") | |
| return path | |