File size: 2,506 Bytes
0669ea3
 
 
 
 
 
 
 
e1bb76a
0669ea3
 
 
 
 
494f391
e1bb76a
0669ea3
 
 
 
 
e1bb76a
0669ea3
 
 
 
 
e1bb76a
0669ea3
 
494f391
0669ea3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
494f391
0669ea3
 
 
 
203f163
 
0669ea3
494f391
 
0669ea3
 
 
 
 
494f391
 
0669ea3
 
 
 
 
 
494f391
 
0669ea3
494f391
0669ea3
494f391
0669ea3
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
import os
import shutil
import zipfile
from PIL import Image
import gradio as gr
import uuid
import subprocess

# Crear carpetas necesarias
INPUT_DIR = "batch_images"
OUTPUT_DIR = "batch_results"
os.makedirs(INPUT_DIR, exist_ok=True)
os.makedirs(OUTPUT_DIR, exist_ok=True)

def face_swap_batch(rb_img, lote_imgs, usar_zip):
    # Limpiar carpetas anteriores
    shutil.rmtree(INPUT_DIR, ignore_errors=True)
    shutil.rmtree(OUTPUT_DIR, ignore_errors=True)
    os.makedirs(INPUT_DIR, exist_ok=True)
    os.makedirs(OUTPUT_DIR, exist_ok=True)

    # Guardar rostro base
    rb_path = os.path.join(INPUT_DIR, "RB.jpg")
    rb_img.save(rb_path)

    # Guardar imágenes por lote
    img_paths = []
    for i, file in enumerate(lote_imgs):
        name = f"img_{i}.jpg"
        path = os.path.join(INPUT_DIR, name)
        img = Image.open(file)
        img.save(path)
        img_paths.append(path)

    # Procesar imágenes con Roop
    for path in img_paths:
        filename = os.path.basename(path)
        output_img = os.path.join(OUTPUT_DIR, f"swapped_{filename}")
        subprocess.run([
            "python", "run.py",
            "-s", rb_path,
            "-t", path,
            "-o", output_img,
            "--execution-provider", "cpu",
            "--frame-processor", "face_swapper", "face_enhancer"
        ])

    # Recolectar resultados
    resultados = []
    for path in img_paths:
        filename = os.path.basename(path)
        generado_path = os.path.join(OUTPUT_DIR, f"swapped_{filename}")
        if os.path.exists(generado_path):
            resultados.append(generado_path)

    # ZIP opcional
    if usar_zip or len(resultados) > 1:
        zip_path = f"resultados_{uuid.uuid4().hex[:6]}.zip"
        with zipfile.ZipFile(zip_path, 'w') as zipf:
            for file in resultados:
                zipf.write(file, os.path.basename(file))
        return zip_path
    else:
        return resultados[0]

# Interfaz Gradio
gr.Interface(
    fn=face_swap_batch,
    inputs=[
        gr.Image(label="🧠 Rostro base", type="pil"),
        gr.File(label="🖼️ Imágenes por lote", file_types=["image"], file_count="multiple"),
        gr.Checkbox(label="📦 Descargar como ZIP", value=False)
    ],
    outputs=gr.File(label="⬇ Resultado procesado"),
    title="Deep Fake Lote - Yepo Hz",
    description="Sube una imagen de rostro base y varias imágenes objetivo. El sistema aplicará el cambio de rostro. Puedes elegir si descargar como ZIP o imagen directa."
).launch()