data info
Browse files- app.py +59 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Importaciones necesarias
|
| 2 |
+
import numpy as np
|
| 3 |
+
import os
|
| 4 |
+
import glob
|
| 5 |
+
import cv2
|
| 6 |
+
import matplotlib.pyplot as plt
|
| 7 |
+
import insightface
|
| 8 |
+
from insightface.app import FaceAnalysis
|
| 9 |
+
from insightface.data import get_image as ins_get_image
|
| 10 |
+
|
| 11 |
+
# Inicializaci贸n de la aplicaci贸n
|
| 12 |
+
app = FaceAnalysis(name='buffalo_l')
|
| 13 |
+
app.prepare(ctx_id=0, det_size=(640,640))
|
| 14 |
+
|
| 15 |
+
# Modelo de intercambio de rostros
|
| 16 |
+
swapper = insightface.model_zoo.get_model('inswapper_128.onnx ', download=False,download_zip=False)
|
| 17 |
+
|
| 18 |
+
# Funci贸n para intercambiar rostros y mostrar im谩genes
|
| 19 |
+
def swap_n_show(img1_fn, img2_fn, app, swapper, plot_before=True, plot_after=True):
|
| 20 |
+
|
| 21 |
+
# Lectura de im谩genes
|
| 22 |
+
img1 = cv2.imread(img1_fn)
|
| 23 |
+
img2 = cv2.imread(img2_fn)
|
| 24 |
+
|
| 25 |
+
# Mostrar im谩genes antes del intercambio (opcional)
|
| 26 |
+
if plot_before:
|
| 27 |
+
fig, axs = plt.subplots(1, 2, figsize=(10, 5))
|
| 28 |
+
axs[0].imshow(img1[:, :, ::-1])
|
| 29 |
+
axs[0].axis('off')
|
| 30 |
+
axs[1].imshow(img2[:, :, ::-1])
|
| 31 |
+
axs[1].axis('off')
|
| 32 |
+
plt.show()
|
| 33 |
+
|
| 34 |
+
# Detecci贸n de rostros
|
| 35 |
+
face1 = app.get(img1)[0]
|
| 36 |
+
face2 = app.get(img2)[0]
|
| 37 |
+
|
| 38 |
+
# Copias de las im谩genes para evitar modificaciones en las originales
|
| 39 |
+
img1_ = img1.copy()
|
| 40 |
+
img2_ = img2.copy()
|
| 41 |
+
|
| 42 |
+
# Intercambio de rostros
|
| 43 |
+
img1 = swapper.get(img1_, face1, face2, paste_back=True)
|
| 44 |
+
img2 = swapper.get(img2_, face2, face1, paste_back=True)
|
| 45 |
+
|
| 46 |
+
# Mostrar im谩genes despu茅s del intercambio (opcional)
|
| 47 |
+
if plot_after:
|
| 48 |
+
fig, axs = plt.subplots(1, 2, figsize=(10, 5))
|
| 49 |
+
axs[0].imshow(img1[:, :, ::-1]) # Usar img1 en lugar de img1_
|
| 50 |
+
axs[0].axis('off')
|
| 51 |
+
axs[1].imshow(img2[:, :, ::-1]) # Usar img2 en lugar de img2_
|
| 52 |
+
axs[1].axis('off')
|
| 53 |
+
plt.show()
|
| 54 |
+
|
| 55 |
+
# Retorno de las im谩genes intercambiadas
|
| 56 |
+
return img1_, img2_
|
| 57 |
+
|
| 58 |
+
# Ejemplo de uso
|
| 59 |
+
_ = swap_n_show('ruta/imagen1.jpg', 'ruta/imagen2.jpg', app, swapper)
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
numpy
|
| 2 |
+
opencv-python
|
| 3 |
+
matplotlib
|
| 4 |
+
insightface
|