Spaces:
Paused
Paused
Download app.py from Scrappy-Doo/Swapper_face: direct link, hf CLI and curl.
- Browser
- Download file 11.6 kB
-
https://huggingface.co/spaces/Scrappy-Doo/Swapper_face/resolve/main/app.py
- Command line
-
hf download hf://spaces/Scrappy-Doo/Swapper_face/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Scrappy-Doo/Swapper_face/resolve/main/app.py
11.6 kB
| import os | |
| import traceback | |
| import cv2 | |
| import gradio as gr | |
| import numpy as np | |
| import onnxruntime as ort | |
| import spaces | |
| from insightface.app import FaceAnalysis | |
| from PIL import Image | |
| BASE_DIR = os.path.dirname(os.path.abspath(__file__)) | |
| MAX_SIDE = 2048 # l铆mite del lado mayor de la imagen destino (evita timeouts) | |
| # --------------------------------------------------------------- | |
| # Detector / extractor de embeddings (CPU) | |
| # --------------------------------------------------------------- | |
| app = FaceAnalysis(name="buffalo_l", providers=["CPUExecutionProvider"]) | |
| app.prepare(ctx_id=-1, det_size=(640, 640)) | |
| # --------------------------------------------------------------- | |
| # Carga de modelos ONNX (CPU) | |
| # --------------------------------------------------------------- | |
| def cargar_modelo(nombre_archivo, etiqueta): | |
| ruta = os.path.join(BASE_DIR, nombre_archivo) | |
| try: | |
| s = ort.InferenceSession(ruta, providers=["CPUExecutionProvider"]) | |
| print(f"隆{etiqueta} cargado correctamente!") | |
| for inp in s.get_inputs(): | |
| print(" Input:", inp.name, inp.shape, inp.type) | |
| for out in s.get_outputs(): | |
| print(" Output:", out.name, out.shape, out.type) | |
| return s | |
| except Exception as e: | |
| print(f"Error al cargar {etiqueta} ({nombre_archivo}):", e) | |
| return None | |
| sess = cargar_modelo("hyperswap_1a_256.onnx", "HyperSwap (swap)") | |
| sess_occ = cargar_modelo("face_occluder.onnx", "Oclusor facial") | |
| sess_enh = cargar_modelo("GPEN-BFR-512.onnx", "GPEN-BFR-512 (mejorador)") | |
| # Plantilla de alineaci贸n (arcface 128 normalizada) | |
| TEMPLATE_NORM = np.array( | |
| [ | |
| [0.36167656, 0.40387734], | |
| [0.63696719, 0.40235469], | |
| [0.50019687, 0.56044219], | |
| [0.38710391, 0.72160547], | |
| [0.61507734, 0.72034453], | |
| ], | |
| dtype=np.float32, | |
| ) | |
| # --------------------------------------------------------------- | |
| # Utilidades | |
| # --------------------------------------------------------------- | |
| def cara_principal(caras): | |
| """Devuelve la cara con mayor 谩rea (la m谩s prominente).""" | |
| return max( | |
| caras, | |
| key=lambda c: (c.bbox[2] - c.bbox[0]) * (c.bbox[3] - c.bbox[1]), | |
| ) | |
| def pil_a_bgr(imagen_pil, limitar=False): | |
| img = cv2.cvtColor(np.array(imagen_pil.convert("RGB")), cv2.COLOR_RGB2BGR) | |
| if limitar: | |
| h, w = img.shape[:2] | |
| mayor = max(h, w) | |
| if mayor > MAX_SIDE: | |
| escala = MAX_SIDE / mayor | |
| img = cv2.resize( | |
| img, (int(w * escala), int(h * escala)), interpolation=cv2.INTER_AREA | |
| ) | |
| return img | |
| def matriz_alineacion(kps, size): | |
| M, _ = cv2.estimateAffinePartial2D( | |
| kps.astype(np.float32), TEMPLATE_NORM * size, method=cv2.LMEDS | |
| ) | |
| return M | |
| def mascara_caja(size, blur=0.3): | |
| """M谩scara rectangular con bordes suaves (para pegar el rostro mejorado).""" | |
| blur_amount = int(size * 0.5 * blur) | |
| blur_area = max(blur_amount // 2, 1) | |
| caja = np.ones((size, size), dtype=np.float32) | |
| caja[:blur_area, :] = 0 | |
| caja[-blur_area:, :] = 0 | |
| caja[:, :blur_area] = 0 | |
| caja[:, -blur_area:] = 0 | |
| if blur_amount > 0: | |
| caja = cv2.GaussianBlur(caja, (0, 0), blur_amount * 0.25) | |
| return caja | |
| # --------------------------------------------------------------- | |
| # M谩scara de oclusi贸n (gafas, manos, objetos delante de la cara) | |
| # --------------------------------------------------------------- | |
| def mascara_oclusion(crop_bgr): | |
| """Devuelve una m谩scara 256x256 (1 = cara visible, 0 = ocluido) o None.""" | |
| if sess_occ is None: | |
| return None | |
| try: | |
| inp = sess_occ.get_inputs()[0] | |
| x = cv2.resize(crop_bgr, (256, 256)).astype(np.float32) / 255.0 | |
| x = x[np.newaxis, ...] # NHWC | |
| forma = inp.shape | |
| if isinstance(forma, (list, tuple)) and len(forma) == 4 and forma[1] == 3: | |
| x = np.transpose(x, (0, 3, 1, 2)) # el modelo pide NCHW | |
| x = np.ascontiguousarray(x, dtype=np.float32) | |
| salida = sess_occ.run(None, {inp.name: x})[0] | |
| m = np.squeeze(salida).astype(np.float32) | |
| while m.ndim > 2: | |
| m = m[0] | |
| m = cv2.resize(m, (256, 256)) | |
| m = np.clip(m, 0, 1) | |
| m = cv2.GaussianBlur(m, (0, 0), 5) | |
| m = (np.clip(m, 0.5, 1.0) - 0.5) * 2.0 | |
| return m.astype(np.float32) | |
| except Exception: | |
| print("Aviso: fall贸 la m谩scara de oclusi贸n:", traceback.format_exc()) | |
| return None | |
| # --------------------------------------------------------------- | |
| # Mejorador de rostro GPEN-BFR-512 | |
| # --------------------------------------------------------------- | |
| def mejorar_rostro(img_bgr, kps, mezcla): | |
| """Aplica GPEN sobre el rostro ya intercambiado y lo mezcla con la imagen.""" | |
| h, w = img_bgr.shape[:2] | |
| M = matriz_alineacion(kps, 512) | |
| crop = cv2.warpAffine( | |
| img_bgr, M, (512, 512), flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_REPLICATE | |
| ) | |
| x = crop[:, :, ::-1].astype(np.float32) / 255.0 | |
| x = (x - 0.5) / 0.5 | |
| x = np.ascontiguousarray(np.transpose(x, (2, 0, 1))[np.newaxis, ...], dtype=np.float32) | |
| inp = sess_enh.get_inputs()[0] | |
| y = sess_enh.run(None, {inp.name: x})[0][0] | |
| y = (np.clip(y, -1, 1) + 1) / 2 | |
| y = np.transpose(y, (1, 2, 0)) * 255.0 | |
| y = np.clip(np.round(y), 0, 255).astype(np.uint8)[:, :, ::-1] | |
| y = np.ascontiguousarray(y) | |
| IM = cv2.invertAffineTransform(M) | |
| pegado = cv2.warpAffine(y, IM, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE) | |
| mascara = cv2.warpAffine(mascara_caja(512), IM, (w, h), flags=cv2.INTER_LINEAR, borderValue=0.0) | |
| mascara = np.clip(mascara, 0, 1)[:, :, np.newaxis] * (float(mezcla) / 100.0) | |
| salida = img_bgr.astype(np.float32) * (1 - mascara) + pegado.astype(np.float32) * mascara | |
| return np.clip(salida, 0, 255).astype(np.uint8) | |
| # --------------------------------------------------------------- | |
| # Proceso principal | |
| # --------------------------------------------------------------- | |
| def predecir( | |
| imagen_destino_pil, | |
| imagen_fuente_pil, | |
| usar_oclusion=True, | |
| usar_mejora=True, | |
| mezcla_mejora=80, | |
| ): | |
| if sess is None: | |
| return None, "Error: El modelo HyperSwap (ONNX) no se carg贸." | |
| if imagen_destino_pil is None or imagen_fuente_pil is None: | |
| return None, "Por favor, sube ambas im谩genes." | |
| try: | |
| pasos = [] | |
| img_destino = pil_a_bgr(imagen_destino_pil, limitar=True) | |
| img_fuente = pil_a_bgr(imagen_fuente_pil) | |
| caras_destino = app.get(img_destino) | |
| caras_fuente = app.get(img_fuente) | |
| if len(caras_destino) == 0: | |
| return None, "No se detect贸 rostro en la imagen de DESTINO." | |
| if len(caras_fuente) == 0: | |
| return None, "No se detect贸 rostro en la imagen de ORIGEN." | |
| cara_destino = cara_principal(caras_destino) | |
| cara_fuente = cara_principal(caras_fuente) | |
| # Embedding normalizado (L2) | |
| emb = cara_fuente.embedding.reshape(1, -1).astype(np.float32) | |
| source_embedding = emb / np.linalg.norm(emb) | |
| # Alineaci贸n del rostro destino a 256x256 | |
| M = matriz_alineacion(cara_destino.kps, 256) | |
| aligned_face = cv2.warpAffine( | |
| img_destino, | |
| M, | |
| (256, 256), | |
| flags=cv2.INTER_LINEAR, | |
| borderMode=cv2.BORDER_REPLICATE, | |
| ) | |
| # Preprocesado: BGR->RGB, normalizar a [-1, 1], NCHW | |
| target_tensor = aligned_face.astype(np.float32)[:, :, ::-1] | |
| target_tensor = (target_tensor / 255.0 - 0.5) / 0.5 | |
| target_tensor = np.transpose(target_tensor, (2, 0, 1))[np.newaxis, ...] | |
| target_tensor = np.ascontiguousarray(target_tensor, dtype=np.float32) | |
| input_names = [inp.name for inp in sess.get_inputs()] | |
| if len(input_names) >= 2: | |
| inputs = {input_names[0]: source_embedding, input_names[1]: target_tensor} | |
| else: | |
| inputs = {"source": source_embedding, "target": target_tensor} | |
| outputs = sess.run(None, inputs) | |
| swapped_face = outputs[0][0] | |
| swapped_mask = outputs[1][0][0] | |
| pasos.append("swap HyperSwap") | |
| # Posprocesado: [-1, 1] -> [0, 255], CHW->HWC, RGB->BGR | |
| swapped_face = (swapped_face * 0.5 + 0.5) * 255.0 | |
| swapped_face = np.clip(swapped_face, 0, 255).astype(np.uint8) | |
| swapped_face = np.transpose(swapped_face, (1, 2, 0))[:, :, ::-1] | |
| swapped_face = np.ascontiguousarray(swapped_face) | |
| mascara_crop = np.clip(swapped_mask, 0, 1).astype(np.float32) | |
| # Oclusiones (gafas, manos, etc.): se combinan con la m谩scara del swap | |
| if usar_oclusion and sess_occ is not None: | |
| occ = mascara_oclusion(aligned_face) | |
| if occ is not None: | |
| mascara_crop = np.minimum(mascara_crop, occ) | |
| pasos.append("m谩scara de oclusi贸n") | |
| # Suavizar el borde de la m谩scara para evitar costuras | |
| mascara_crop = cv2.GaussianBlur(mascara_crop, (0, 0), 3) | |
| mascara_crop = np.clip(mascara_crop, 0, 1) | |
| # Volver a la imagen original | |
| h, w = img_destino.shape[:2] | |
| IM = cv2.invertAffineTransform(M) | |
| img_swap = cv2.warpAffine( | |
| swapped_face, IM, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE | |
| ) | |
| mask = cv2.warpAffine( | |
| mascara_crop, IM, (w, h), flags=cv2.INTER_LINEAR, borderValue=0.0 | |
| ) | |
| mask = np.clip(mask, 0, 1)[:, :, np.newaxis] | |
| resultado = ( | |
| img_destino.astype(np.float32) * (1 - mask) | |
| + img_swap.astype(np.float32) * mask | |
| ) | |
| resultado = np.clip(resultado, 0, 255).astype(np.uint8) | |
| # Mejora de rostro con GPEN-BFR-512 | |
| if usar_mejora and sess_enh is not None and mezcla_mejora > 0: | |
| try: | |
| resultado = mejorar_rostro(resultado, cara_destino.kps, mezcla_mejora) | |
| pasos.append(f"mejora GPEN ({int(mezcla_mejora)}%)") | |
| except Exception: | |
| print("Aviso: fall贸 el mejorador GPEN:", traceback.format_exc()) | |
| pasos.append("mejora GPEN omitida por error") | |
| resultado_pil = Image.fromarray(cv2.cvtColor(resultado, cv2.COLOR_BGR2RGB)) | |
| return resultado_pil, "隆Face swap completado con 茅xito! 馃幁\nPasos: " + ", ".join(pasos) | |
| except Exception: | |
| return None, "Error: " + traceback.format_exc() | |
| with gr.Blocks(title="Swapper Face") as demo: | |
| gr.Markdown( | |
| "# 馃幁 Swapper Face Real - Sube la imagen de destino (fondo) " | |
| "y la cara que quieres ponerle." | |
| ) | |
| with gr.Row(): | |
| with gr.Column(): | |
| img_destino = gr.Image(type="pil", label="1. Imagen de Destino (Fondo)") | |
| img_fuente = gr.Image(type="pil", label="2. Imagen de Origen (Cara a extraer)") | |
| usar_oclusion = gr.Checkbox( | |
| value=True, label="Respetar oclusiones (gafas, manos, objetos)" | |
| ) | |
| usar_mejora = gr.Checkbox(value=True, label="Mejorar rostro (GPEN-BFR-512)") | |
| mezcla_mejora = gr.Slider( | |
| 0, 100, value=80, step=5, label="Intensidad de la mejora (%)" | |
| ) | |
| boton_ejecutar = gr.Button("馃攧 Ejecutar Swapper", variant="primary") | |
| with gr.Column(): | |
| salida_imagen = gr.Image(type="pil", label="Resultado Final") | |
| salida_texto = gr.Textbox(label="Estado del proceso", lines=5) | |
| boton_ejecutar.click( | |
| fn=predecir, | |
| inputs=[img_destino, img_fuente, usar_oclusion, usar_mejora, mezcla_mejora], | |
| outputs=[salida_imagen, salida_texto], | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |