Spaces:
Configuration error
Configuration error
File size: 2,906 Bytes
5cc95c0 |
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 |
import os
import shutil
import zipfile
from PIL import Image
import gradio as gr
import uuid
import subprocess
# Directorios
INPUT_DIR = "batch_images"
OUTPUT_DIR = "batch_results"
RESULTS_DIR = "comparaciones"
os.makedirs(INPUT_DIR, exist_ok=True)
os.makedirs(OUTPUT_DIR, exist_ok=True)
os.makedirs(RESULTS_DIR, exist_ok=True)
def face_swap_batch(rb_img, lote_imgs):
# Limpiar directorios
shutil.rmtree(INPUT_DIR, ignore_errors=True)
shutil.rmtree(OUTPUT_DIR, ignore_errors=True)
shutil.rmtree(RESULTS_DIR, ignore_errors=True)
os.makedirs(INPUT_DIR, exist_ok=True)
os.makedirs(OUTPUT_DIR, exist_ok=True)
os.makedirs(RESULTS_DIR, exist_ok=True)
# Guardar imagen de 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, img in enumerate(lote_imgs):
name = f"img_{i}.jpg"
path = os.path.join(INPUT_DIR, name)
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"
])
# Crear comparaciones lado a lado
resultados = []
for path in img_paths:
filename = os.path.basename(path)
original = Image.open(path).resize((256, 256))
generado_path = os.path.join(OUTPUT_DIR, f"swapped_{filename}")
if not os.path.exists(generado_path):
continue
generado = Image.open(generado_path).resize((256, 256))
combinado = Image.new("RGB", (512, 256))
combinado.paste(original, (0, 0))
combinado.paste(generado, (256, 0))
out_path = os.path.join(RESULTS_DIR, f"comparado_{filename}")
combinado.save(out_path)
resultados.append(out_path)
# Descarga
if len(resultados) == 1:
return resultados[0]
else:
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
# 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")
],
outputs=gr.File(label="📦 Descargar ZIP o imagen"),
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 en lote. Resultado visual lado a lado."
).launch()
|