Spaces:
Running
Running
Upload run_faceswap.py
Browse files- run_faceswap.py +43 -0
run_faceswap.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import os
|
3 |
+
import torch
|
4 |
+
from types import SimpleNamespace
|
5 |
+
from SimSwap.models.models import create_model
|
6 |
+
from PIL import Image
|
7 |
+
from torchvision import transforms
|
8 |
+
|
9 |
+
def run_faceswap(image_path_A, image_path_B, output_path):
|
10 |
+
opt = SimpleNamespace()
|
11 |
+
opt.name = "people"
|
12 |
+
opt.checkpoints_dir = "./checkpoints"
|
13 |
+
opt.gpu_ids = []
|
14 |
+
opt.isTrain = False
|
15 |
+
opt.resize_or_crop = 'none'
|
16 |
+
opt.crop_size = 224
|
17 |
+
opt.which_epoch = "latest"
|
18 |
+
|
19 |
+
# Añadir atributos requeridos para netG
|
20 |
+
opt.input_nc = 3
|
21 |
+
opt.output_nc = 3
|
22 |
+
opt.ngf = 64
|
23 |
+
opt.netG = 'unet_128'
|
24 |
+
|
25 |
+
model = create_model(opt)
|
26 |
+
model.eval()
|
27 |
+
|
28 |
+
transform = transforms.Compose([
|
29 |
+
transforms.Resize((224, 224)),
|
30 |
+
transforms.ToTensor()
|
31 |
+
])
|
32 |
+
|
33 |
+
img_A = Image.open(image_path_A).convert("RGB")
|
34 |
+
img_B = Image.open(image_path_B).convert("RGB")
|
35 |
+
|
36 |
+
tensor_A = transform(img_A).unsqueeze(0)
|
37 |
+
tensor_B = transform(img_B).unsqueeze(0)
|
38 |
+
|
39 |
+
with torch.no_grad():
|
40 |
+
output = model(tensor_A, tensor_B)
|
41 |
+
|
42 |
+
output_img = transforms.ToPILImage()(output.squeeze(0))
|
43 |
+
output_img.save(output_path)
|