Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import shutil
|
| 3 |
+
import zipfile
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import uuid
|
| 7 |
+
import subprocess
|
| 8 |
+
|
| 9 |
+
# Directorios
|
| 10 |
+
INPUT_DIR = "batch_images"
|
| 11 |
+
OUTPUT_DIR = "batch_results"
|
| 12 |
+
RESULTS_DIR = "comparaciones"
|
| 13 |
+
os.makedirs(INPUT_DIR, exist_ok=True)
|
| 14 |
+
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 15 |
+
os.makedirs(RESULTS_DIR, exist_ok=True)
|
| 16 |
+
|
| 17 |
+
def face_swap_batch(rb_img, lote_imgs):
|
| 18 |
+
# Limpiar directorios
|
| 19 |
+
shutil.rmtree(INPUT_DIR, ignore_errors=True)
|
| 20 |
+
shutil.rmtree(OUTPUT_DIR, ignore_errors=True)
|
| 21 |
+
shutil.rmtree(RESULTS_DIR, ignore_errors=True)
|
| 22 |
+
os.makedirs(INPUT_DIR, exist_ok=True)
|
| 23 |
+
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 24 |
+
os.makedirs(RESULTS_DIR, exist_ok=True)
|
| 25 |
+
|
| 26 |
+
# Guardar imagen de rostro base
|
| 27 |
+
rb_path = os.path.join(INPUT_DIR, "RB.jpg")
|
| 28 |
+
rb_img.save(rb_path)
|
| 29 |
+
|
| 30 |
+
# Guardar imágenes por lote
|
| 31 |
+
img_paths = []
|
| 32 |
+
for i, img in enumerate(lote_imgs):
|
| 33 |
+
name = f"img_{i}.jpg"
|
| 34 |
+
path = os.path.join(INPUT_DIR, name)
|
| 35 |
+
img.save(path)
|
| 36 |
+
img_paths.append(path)
|
| 37 |
+
|
| 38 |
+
# Procesar imágenes con Roop
|
| 39 |
+
for path in img_paths:
|
| 40 |
+
filename = os.path.basename(path)
|
| 41 |
+
output_img = os.path.join(OUTPUT_DIR, f"swapped_{filename}")
|
| 42 |
+
subprocess.run([
|
| 43 |
+
"python", "run.py",
|
| 44 |
+
"-s", rb_path,
|
| 45 |
+
"-t", path,
|
| 46 |
+
"-o", output_img,
|
| 47 |
+
"--execution-provider", "cpu",
|
| 48 |
+
"--frame-processor", "face_swapper", "face_enhancer"
|
| 49 |
+
])
|
| 50 |
+
|
| 51 |
+
# Crear comparaciones lado a lado
|
| 52 |
+
resultados = []
|
| 53 |
+
for path in img_paths:
|
| 54 |
+
filename = os.path.basename(path)
|
| 55 |
+
original = Image.open(path).resize((256, 256))
|
| 56 |
+
generado_path = os.path.join(OUTPUT_DIR, f"swapped_{filename}")
|
| 57 |
+
if not os.path.exists(generado_path):
|
| 58 |
+
continue
|
| 59 |
+
generado = Image.open(generado_path).resize((256, 256))
|
| 60 |
+
combinado = Image.new("RGB", (512, 256))
|
| 61 |
+
combinado.paste(original, (0, 0))
|
| 62 |
+
combinado.paste(generado, (256, 0))
|
| 63 |
+
out_path = os.path.join(RESULTS_DIR, f"comparado_{filename}")
|
| 64 |
+
combinado.save(out_path)
|
| 65 |
+
resultados.append(out_path)
|
| 66 |
+
|
| 67 |
+
# Descarga
|
| 68 |
+
if len(resultados) == 1:
|
| 69 |
+
return resultados[0]
|
| 70 |
+
else:
|
| 71 |
+
zip_path = f"resultados_{uuid.uuid4().hex[:6]}.zip"
|
| 72 |
+
with zipfile.ZipFile(zip_path, 'w') as zipf:
|
| 73 |
+
for file in resultados:
|
| 74 |
+
zipf.write(file, os.path.basename(file))
|
| 75 |
+
return zip_path
|
| 76 |
+
|
| 77 |
+
# Interfaz Gradio
|
| 78 |
+
gr.Interface(
|
| 79 |
+
fn=face_swap_batch,
|
| 80 |
+
inputs=[
|
| 81 |
+
gr.Image(label="🧠 Rostro base", type="pil"),
|
| 82 |
+
gr.File(label="🖼️ Imágenes por lote", file_types=["image"], file_count="multiple")
|
| 83 |
+
],
|
| 84 |
+
outputs=gr.File(label="📦 Descargar ZIP o imagen"),
|
| 85 |
+
title="Deep Fake Lote - Yepo Hz",
|
| 86 |
+
description="Sube una imagen de rostro base y varias imágenes objetivo. El sistema aplicará el cambio de rostro en lote. Resultado visual lado a lado."
|
| 87 |
+
).launch()
|