File size: 1,785 Bytes
3d9a256 |
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 |
# Importaciones necesarias
import numpy as np
import os
import glob
import cv2
import matplotlib.pyplot as plt
import insightface
from insightface.app import FaceAnalysis
from insightface.data import get_image as ins_get_image
# Inicializaci贸n de la aplicaci贸n
app = FaceAnalysis(name='buffalo_l')
app.prepare(ctx_id=0, det_size=(640,640))
# Modelo de intercambio de rostros
swapper = insightface.model_zoo.get_model('inswapper_128.onnx ', download=False,download_zip=False)
# Funci贸n para intercambiar rostros y mostrar im谩genes
def swap_n_show(img1_fn, img2_fn, app, swapper, plot_before=True, plot_after=True):
# Lectura de im谩genes
img1 = cv2.imread(img1_fn)
img2 = cv2.imread(img2_fn)
# Mostrar im谩genes antes del intercambio (opcional)
if plot_before:
fig, axs = plt.subplots(1, 2, figsize=(10, 5))
axs[0].imshow(img1[:, :, ::-1])
axs[0].axis('off')
axs[1].imshow(img2[:, :, ::-1])
axs[1].axis('off')
plt.show()
# Detecci贸n de rostros
face1 = app.get(img1)[0]
face2 = app.get(img2)[0]
# Copias de las im谩genes para evitar modificaciones en las originales
img1_ = img1.copy()
img2_ = img2.copy()
# Intercambio de rostros
img1 = swapper.get(img1_, face1, face2, paste_back=True)
img2 = swapper.get(img2_, face2, face1, paste_back=True)
# Mostrar im谩genes despu茅s del intercambio (opcional)
if plot_after:
fig, axs = plt.subplots(1, 2, figsize=(10, 5))
axs[0].imshow(img1[:, :, ::-1]) # Usar img1 en lugar de img1_
axs[0].axis('off')
axs[1].imshow(img2[:, :, ::-1]) # Usar img2 en lugar de img2_
axs[1].axis('off')
plt.show()
# Retorno de las im谩genes intercambiadas
return img1_, img2_
# Ejemplo de uso
_ = swap_n_show('ruta/imagen1.jpg', 'ruta/imagen2.jpg', app, swapper)
|