NeoSerge commited on
Commit
0618434
·
verified ·
1 Parent(s): e18a616

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +26 -0
  2. run_faceswap.py +77 -0
app.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import gradio as gr
3
+ from PIL import Image
4
+ from run_faceswap import run_faceswap
5
+
6
+ def swap_faces(base_image, face_image):
7
+ try:
8
+ result = run_faceswap(base_image, face_image)
9
+ result_img = Image.fromarray(result)
10
+ return result_img
11
+ except Exception as e:
12
+ return f"Error al generar imagen: {str(e)}"
13
+
14
+ iface = gr.Interface(
15
+ fn=swap_faces,
16
+ inputs=[
17
+ gr.Image(type="pil", label="Imagen base (cuerpo en pista)"),
18
+ gr.Image(type="pil", label="Foto de la cara (webcam)")
19
+ ],
20
+ outputs=gr.Image(type="pil", label="Resultado final"),
21
+ title="SimSwap - Reemplazo realista de rostro",
22
+ description="Sube una imagen con cuerpo/fondo y una con el rostro a reemplazar. El sistema mantendrá todo igual salvo el rostro."
23
+ )
24
+
25
+ if __name__ == "__main__":
26
+ iface.launch()
run_faceswap.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import os
3
+ os.environ["CUDA_VISIBLE_DEVICES"] = ""
4
+
5
+ import cv2
6
+ import torch
7
+ import numpy as np
8
+ from PIL import Image
9
+ from torchvision import transforms
10
+ from types import SimpleNamespace
11
+
12
+ from SimSwap.models.models import create_model
13
+ from SimSwap.arcface.model import Backbone
14
+
15
+ DEVICE = "cpu"
16
+
17
+ opt_dict = {
18
+ "isTrain": False,
19
+ "Arc_path": "arcface_model.tar",
20
+ "which_epoch": "latest",
21
+ "load_pretrain": "checkpoints/simswap",
22
+ "crop_size": 224,
23
+ "resize_or_crop": "none",
24
+ "gan_mode": "hinge",
25
+ "no_ganFeat_loss": True,
26
+ "no_vgg_loss": True,
27
+ "lambda_feat": 10.0,
28
+ "lambda_rec": 10.0,
29
+ "beta1": 0.5,
30
+ "lr": 0.0002,
31
+ "continue_train": False,
32
+ "name": "simswap",
33
+ "checkpoints_dir": "./checkpoints",
34
+ "gpu_ids": [],
35
+ "use_mask": True,
36
+ "dataset_mode": "Swap",
37
+ "model": "fs_swap"
38
+ }
39
+ opt = SimpleNamespace(**opt_dict)
40
+
41
+ model = create_model(opt)
42
+ model.setup(opt)
43
+ model.device = torch.device("cpu")
44
+ if hasattr(model, 'netG'):
45
+ model.netG = model.netG.cpu()
46
+ model.eval()
47
+
48
+ arcface = Backbone(50, 0.6, 'ir_se')
49
+ arcface.load_state_dict(torch.load(opt.Arc_path, map_location=DEVICE))
50
+ arcface.eval().to(DEVICE)
51
+
52
+ transform = transforms.Compose([
53
+ transforms.Resize((224, 224)),
54
+ transforms.ToTensor()
55
+ ])
56
+
57
+ def run_faceswap(base_image_pil, face_image_pil):
58
+ base_img = cv2.cvtColor(np.array(base_image_pil), cv2.COLOR_RGB2BGR)
59
+ id_img = face_image_pil.convert("RGB").resize((112, 112))
60
+ id_tensor = transform(id_img).unsqueeze(0).to(DEVICE)
61
+
62
+ with torch.no_grad():
63
+ id_embedding = arcface(id_tensor)
64
+ id_embedding = id_embedding / id_embedding.norm(dim=-1, keepdim=True)
65
+
66
+ temp_input_path = 'temp_base.jpg'
67
+ cv2.imwrite(temp_input_path, base_img)
68
+
69
+ opt.pic_a_path = temp_input_path
70
+ opt.pic_b_path = None
71
+ model.set_input(opt, id_embedding)
72
+ model.test()
73
+ swapped = model.get_current_visuals()['synthesized_image']
74
+
75
+ result_np = swapped.squeeze().permute(1, 2, 0).cpu().numpy() * 255
76
+ result_np = result_np.astype(np.uint8)
77
+ return result_np