Esempio n. 1
0
    def agregar_a_imagen_kp(self, img, keypoints):
        """agregar el filtro a la imagen img con respecto a coordenadas del
        keypoint (CENTRO DE LOS OJOS)
        """
        ojo_izq = keypoints[0]
        ojo_der = keypoints[1]

        pendiente = (np.abs(ojo_izq[1] - ojo_der[1]) /
                     np.abs(ojo_izq[0] - ojo_der[0]))
        angulo_fondo = np.arctan(pendiente)
        delta_angulos = np.degrees(self.angulo - angulo_fondo)

        imagen = self.imagen.copy()
        imagen = cv.circle(imagen, tuple(self.ojo_izq), 4, (255, 0, 0), -1)
        imagen = cv.circle(imagen, tuple(self.ojo_der), 4, (0, 255, 0), -1)
        # rotacion
        img_rotada, M = rotate_bound(imagen, delta_angulos)
        ojo_ir = np.squeeze(cv.transform(self.ojo_izq.reshape(1, 1, -1), M))
        ojo_dr = np.squeeze(cv.transform(self.ojo_der.reshape(1, 1, -1), M))
        # escalamiento
        dis_fondo = np.linalg.norm(ojo_izq - ojo_der)
        ratio = dis_fondo / self.distancia_ojos
        img_escalada = cv.resize(img_rotada,
                                 None,
                                 fx=ratio,
                                 fy=ratio,
                                 interpolation=cv.INTER_CUBIC)
        # translación
        posicion_filtro = (ojo_izq - ojo_ir * ratio).astype(np.int32)
        agregar_imagen(img, img_escalada, posicion_filtro[0],
                       posicion_filtro[1])
Esempio n. 2
0
    def agregar_a_imagen(self, img, x, y, w, h):
        """agregar el filtro a la imagen img con respecto a un bounding box
        definido por x, y, w, h
        """

        # ajustar el alto
        w_h = int(self.aspect_ratio * h)
        a_h = w_h * h
        # ajustar el ancho
        h_w = int(w / self.aspect_ratio)
        a_w = w * h_w
        # tomar el ajuste que genere la mayor area
        if a_h > a_w:
            rw, rh = (w_h, h)
            rx, ry = x - np.abs(int((w - rw) / 2)), y
        else:
            rw, rh = (w, h_w)
            rx, ry = x, y - np.abs(int((h - rh) / 2))
        imagen_chica = cv.resize(self.imagen, (rw, rh))
        agregar_imagen(img, imagen_chica, rx, ry)
Esempio n. 3
0
import imageio
import os
from utils import agregar_imagen

script_dir = os.path.dirname(os.path.realpath(__file__))
image_dir = os.path.join(script_dir, 'imagenes')

pa = {'x': 20, 'y': 20, 'w': 250, 'h': 250}

cerveza = imageio.imread(os.path.join(image_dir, "cerveza.jpg"))
mascara = imageio.imread(
    os.path.join(image_dir, "stormtrooper-ojos-transparentes.png"))
mascara_chico = cv.resize(mascara, (pa['w'], pa['h']))
print(mascara.shape)

agregar_imagen(cerveza, mascara, 200, 200)
cv.imshow('Ttulo de la ventana', cerveza)

# capturar imagen desde la webcam
cap = cv.VideoCapture(0)

while True:
    ret, img = cap.read()  # leer la webcam
    img = cv.flip(img, 1)  # flip horizontal para que sea un espejo

    agregar_imagen(img, mascara_chico, 200, 200)

    cv.imshow('Ttulo de la ventana', img)

    k = cv.waitKey(30)
    if k == 27:  # ESC (ASCII)
Esempio n. 4
0
                x, y, w, h, KEYPOINT_X_PORCENTAJE, KEYPOINT_Y_PORCENTAJE)
            cara = img[y_cara:y_cara + h_cara, x_cara:x_cara + w_cara]
            # convertir a escala de grises
            cara = cv.cvtColor(cara, cv.COLOR_RGB2GRAY)
            # hacer un resize a 96, 96
            cara_chica = cv.resize(cara, (96, 96))
            # transformar el array a una dimensionalidad compatible con el modelo
            cara_input = np.array([cara_chica.reshape(96, 96, 1)]) / 255
            # predecir
            keypoints = detector_keypoints.predict(cara_input) * 96
            # traducir los keypoints a el tamaño y posición original

            factor_cara_x = w_cara / 96
            factor_cara_y = h_cara / 96
            if PRINT_DEBUG:
                agregar_imagen(img, cara_chica, 20, 150)
            keypoints = keypoints.astype(np.int32).reshape(15, 2)

            keypoints_original = keypoints.copy()
            keypoints_original[:,
                               0] = keypoints_original[:,
                                                       0] * factor_cara_x + x_cara
            keypoints_original[:,
                               1] = keypoints_original[:,
                                                       1] * factor_cara_y + y_cara
            keypoints_original = keypoints_original.astype(np.int32)
            if PRINT_DEBUG:
                for j in range(2):  # lo equivalente a los centros de los ojos
                    c = tuple(keypoints[j] + np.array([20, 150]))
                    c_original = tuple(keypoints_original[j])
                    cv.circle(img, c, 2, (0, 255, 0))
Esempio n. 5
0
import cv2 as cv
import imageio
import os
from utils import agregar_imagen

script_dir = os.path.dirname(os.path.realpath(__file__))
image_dir = os.path.join(script_dir, 'imagenes')

mascara = imageio.imread(os.path.join(image_dir,"stormtrooper-ojos-transparentes.png"))

# capturar imagen desde la webcam
cap = cv.VideoCapture(0)

while True:
    ret, img = cap.read() # leer la webcam
    img = cv.flip( img, 1 ) # flip horizontal para que sea un espejo
    
    agregar_imagen(img, mascara, 200, 200)

    cv.imshow('Ttulo de la ventana', img)

    k = cv.waitKey(30)
    if k == 27: # ESC (ASCII)
        break
cap.release()
cv.destroyAllWindows()