File size: 6,970 Bytes
1f5f680
f2c9245
ced9582
 
7310428
8e363d1
1f5f680
14bab5c
372a6ba
8e363d1
159f3d7
8e363d1
 
 
14bab5c
 
 
 
 
8e363d1
 
 
 
 
372a6ba
a781931
f2c9245
3bca149
e756b69
c0d75d4
a781931
e756b69
a781931
1920c57
a781931
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3bca149
e756b69
 
f2c9245
3bca149
e756b69
 
 
 
 
 
c0d75d4
e756b69
4296c87
 
 
 
 
159f3d7
4296c87
e756b69
 
 
 
 
3bca149
e756b69
 
 
 
 
 
3bca149
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
faa2cc3
 
 
3bca149
 
 
 
 
 
 
 
 
 
 
f2c9245
3bca149
faa2cc3
 
f2c9245
5a28353
ced9582
 
 
 
 
3bca149
af881b4
ced9582
3bca149
1f5f680
8e363d1
af881b4
8e363d1
7e398ae
e756b69
7e398ae
 
 
8e363d1
 
 
 
 
 
af881b4
5a2242d
3f770cf
95cbdc8
 
 
 
 
 
 
 
52e1485
95cbdc8
8e363d1
c632e3d
 
0d7bdbf
 
c632e3d
 
0d7bdbf
 
 
 
8e363d1
3bca149
 
4296c87
95cbdc8
4296c87
95cbdc8
c632e3d
 
 
 
 
3bca149
 
 
55ad643
8e363d1
95cbdc8
3bca149
f1a3cbd
faa2cc3
3bca149
faa2cc3
f1a3cbd
95cbdc8
 
 
 
c632e3d
 
 
 
 
 
 
410276e
a781931
410276e
b1b5fc5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import gradio as gr
from PIL import Image
import time
import os
import pathlib
import zipfile36 as zipfile

def save_images_as_zip(path_foto, filename, plataforma):

    print("Entr茅 a la funci贸n que hace los zips...")
        
    with zipfile.ZipFile(filename, "w") as zip_file:
        for foto in os.listdir(path_foto):
            print("La foto en os.listdir es: ", foto)
            #Si Local
            # path_foto_zippable = str(path_foto) + "\\" + foto
            # #Si HuggingFace
            # path_foto_zippable = str(path_foto) + "/" + foto
            path_foto_zippable = str(path_foto) + (os.sep if plataforma == "local" else "/") + foto
            print("La ruta textual final de esa foto en particular es: ", path_foto_zippable)
            # ruta = pathlib.Path(path_foto_zippable)
            # zip_file.write(ruta)
            ruta = os.path.basename(path_foto_zippable)
            zip_file.write(path_foto_zippable, ruta)

def perform(input1, input2, input3):

    modo = "video"
    #local o huggingface
    plataforma = "huggingface"

    #face_swapper o face_enhancer o la combinaci贸n de ellos.
    #El default, por si el usuario no eligiera nada, es:
    procesador = "face_swapper"
    print("Por ahora el procesador es:", procesador)

    print("Esto fue el input 3 recibido...:", input3)
    time.sleep(3)

    longitud = len(input3)

    print(f"El arreglo tiene {longitud} elementos")
    time.sleep(3)

    if longitud == 2:
        procesador = "face_swapper face_enhancer"
    elif longitud == 0:
        print("La longitud si da 0")        
    else:
        procesador = input3[0]

    print("El procesador seleccionado termin贸 siendo...", procesador)
    
    print("Inicio: Estamos en modo ", modo)
    print("Estamos en la plataforma:", plataforma)
    print("El procesador es: ", procesador)

    path_video = input2

    if plataforma == "local":
        #Para local.
        path_parts = path_video.split("\\")
    else:
        #Para HuggingFace
        print("La plataforma en la que basaremos la divisi贸n es HuggingFace.")
        path_parts = path_video.split("/")

    #Aqu铆 obtendremos nom_video
    filename = path_parts[-1]
    nom_video = filename[:-4]
    print("Esto es filename alias nom_video: ", nom_video)
    time.sleep(1)

    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 asigno la extensi贸n de video:", ext_video)
        extension = ext_video
    else:
        print("Se asigno la extensi贸n de video:", ext_video)
        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)

    command = f"python run.py -s {source_path}  -t {target_path} -o {result_path} --frame-processor {procesador} --execution-provider cuda"
    print(command)
    time.sleep(1)
    proc = os.popen(command)
    output = proc.read()

    print("Output (resultado de la ejecuci贸n del c贸digo):")
    time.sleep(1)
    print(output)
    print("Termin贸 la impresi贸n del output...")

    print("脡ste es el momento en el que se creo result, revisar...")
    time.sleep(1)

    print("Ahora estamos imprimiendo las rutas para ver si apareci贸 una nueva:")
    files = os.listdir(path_general)
    print("Estos son los files que hay:")
    print(files)

    #Creaci贸n de la galer铆a:
    images = []
    
    #nom_video = "whitebeauty"
    path_foto = pathlib.Path(path_particular + "/temp/" + nom_video + "/")
    print("脡ste es el path foto: ", path_foto)
    path_result = str(path_foto) + "/temp.mp4"
    print("Y 茅ste es el path del resultado: ", path_result)
 
    #脡ste es el segmento que crea la galer铆a de imagenes, que por el momento no usaremos por rendimiento.
    #Se reintegrar谩 si agregamos interacci贸n de poder borrar cada imagen desde la interfaz web.
    
    # for filename in os.listdir(path_foto):
    #     if filename.endswith(".png"):
    #         path = os.path.join(path_foto, filename)
    #         image = Image.open(path)
    #         images.append(image)

    # print("Esto es la lista de imagenes: ", images)

    #nombre_zip = nom_video + ".zip"
    #print("El nombre del zip ser谩: ", nombre_zip)

    try:
        #save_images_as_zip(path_foto, nombre_zip, plataforma)
        print("脡sta vez no crearemos archivo zip.")

    except Exception as e:
        # c贸digo que se ejecutar谩 si se produce la excepci贸n
        print(e)
    
    if modo == "video":
        #Para video
        path = pathlib.Path("result.mp4")
        path_abs = os.path.abspath(path)
        print("脡ste es el path para video:", path)
        print("Y su ruta absoluta es: ", path_abs)
        #path_zip = pathlib.Path(nombre_zip)
        #path_zip_abs =  os.path.abspath(path_zip)
        #print("Y 茅ste es el path para el zip: ", path_zip)
        #print("Y su ruta absoluta es: ", path_zip_abs)
        return path
    else:
        #Para imagen
        path = pathlib.Path("result.png")
        print("脡ste es el path para imagen:", path)
        return path, images, images
    print("Listo! Gracias!")
     
#As铆 para imagenes
# demo = gr.Interface(
# fn=greet, inputs=[gr.Image(), gr.Image()], outputs=[gr.Image(), gr.Image()]
# )

#As铆 para video y 3 outputs: Video, Galer铆a y Archivo Zip.
# demo = gr.Interface(
# fn=greet, inputs=[gr.Image(), gr.Video()], outputs=[gr.Video(), gr.Gallery(), gr.File()]
# )

#As铆 para 2 outputs, video y zip.
# demo = gr.Interface(
# fn=greet, inputs=[gr.Image(), gr.Video()], outputs=[gr.Video(), gr.File()]
# )

#1 output: video.
demo = gr.Interface(
fn=perform, inputs=[gr.Image(), gr.Video(), gr.CheckboxGroup(["face_swapper","face_enhancer"], label="Processor")], outputs=[gr.Video()]
)
demo.launch()