File size: 1,102 Bytes
57d5944
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

import os
import torch
from types import SimpleNamespace
from SimSwap.models.models import create_model
from PIL import Image
from torchvision import transforms

def run_faceswap(image_path_A, image_path_B, output_path):
    opt = SimpleNamespace()
    opt.name = "people"
    opt.checkpoints_dir = "./checkpoints"
    opt.gpu_ids = []
    opt.isTrain = False
    opt.resize_or_crop = 'none'
    opt.crop_size = 224
    opt.which_epoch = "latest"

    # Añadir atributos requeridos para netG
    opt.input_nc = 3
    opt.output_nc = 3
    opt.ngf = 64
    opt.netG = 'unet_128'

    model = create_model(opt)
    model.eval()

    transform = transforms.Compose([
        transforms.Resize((224, 224)),
        transforms.ToTensor()
    ])

    img_A = Image.open(image_path_A).convert("RGB")
    img_B = Image.open(image_path_B).convert("RGB")

    tensor_A = transform(img_A).unsqueeze(0)
    tensor_B = transform(img_B).unsqueeze(0)

    with torch.no_grad():
        output = model(tensor_A, tensor_B)

    output_img = transforms.ToPILImage()(output.squeeze(0))
    output_img.save(output_path)