def __init__(self, model_path):
        self._model_path = model_path

        self._model_files = {
            'shape_predictor':
            os.path.join(model_path, 'shape_predictor_68_face_landmarks.dat'),
            'face_template':
            os.path.join(model_path, 'face_template.npy'),
            'mean':
            os.path.join(model_path, 'mean.npy'),
            'stddev':
            os.path.join(model_path, 'stddev.npy'),
            'cnn_weights':
            os.path.join(model_path, 'weights_cnn.h5'),
            'tpe_weights':
            os.path.join(model_path, 'weights_tpe.h5'),
        }

        for model_file in self._model_files.values():
            if not os.path.exists(model_file):
                raise FileNotFoundError(model_file)

        self._mean = np.load(self._model_files['mean'])
        self._stddev = np.load(self._model_files['stddev'])
        self._fd = FaceDetector()
        self._fa = FaceAligner(self._model_files['shape_predictor'],
                               self._model_files['face_template'])
        cnn = build_cnn(227, 266)
        cnn.load_weights(self._model_files['cnn_weights'])
        self._cnn = Bottleneck(cnn, ~1)
        _, tpe = build_tpe(256, 256)
        tpe.load_weights(self._model_files['tpe_weights'])
        self._tpe = tpe
예제 #2
0
 def initialize_model(self):
     self._mean = np.load(self._model_files['mean'])
     self._stddev = np.load(self._model_files['stddev'])
     self._fd = FaceDetector()
     self._fa = FaceAligner(self._model_files['shape_predictor'],
                            self._model_files['face_template'])
     cnn = build_cnn(227, 266)
     cnn.load_weights(self._model_files['cnn_weights'])
     self._cnn = Bottleneck(cnn, ~1)
     _, tpe = build_tpe(256, 256)
     tpe.load_weights(self._model_files['tpe_weights'])
     self._tpe = tpe
예제 #3
0
class FaceVerificator:
    def __init__(self, model_dir):
        self._model_dir = model_dir

        self._model_files = {
            'shape_predictor': os.path.join(model_dir, 'shape_predictor_68_face_landmarks.dat'),
            'face_template': os.path.join(model_dir, 'face_template.npy'),
            'mean': os.path.join(model_dir, 'mean.npy'),
            'stddev': os.path.join(model_dir, 'stddev.npy'),
            'cnn_weights': os.path.join(model_dir, 'weights_cnn.h5'),
            'tpe_weights': os.path.join(model_dir, 'weights_tpe.h5'),
        }

    def initialize_model(self):
        self._mean = np.load(self._model_files['mean'])
        self._stddev = np.load(self._model_files['stddev'])
        self._fd = FaceDetector()
        self._fa = FaceAligner(self._model_files['shape_predictor'],
                               self._model_files['face_template'])
        cnn = build_my_cnn(227, 24)
        cnn.load_weights(self._model_files['cnn_weights'])
        self._cnn = Bottleneck(cnn, ~1)
        _, tpe = build_tpe(24, 24)
        tpe.load_weights(self._model_files['tpe_weights'])
        self._tpe = tpe

    def normalize(self, img):
        img = clip_to_range(img)
        return (img - self._mean) / self._stddev

    def process_image(self, img):
        face_rects = self._fd.detect_faces(img, upscale_factor=2, greater_than=GREATER_THAN)

        if not face_rects:
            return []

        faces = self._fa.align_faces(img, face_rects, dim=IMSIZE, border=IMBORDER)
        faces = list(map(self.normalize, faces))

        faces_y = self._cnn.predict(faces, batch_size=BATCH_SIZE)
        faces_y = self._tpe.predict(faces_y, batch_size=BATCH_SIZE)

        return list(zip(face_rects, faces_y))

    def compare_many(self, dist, xs, ys):
        xs = np.array(xs)
        ys = np.array(ys)
        scores = xs @ ys.T
        return scores, scores > dist
import os
import os.path

import numpy as np

from skimage import io
from preprocessing import FaceDetector, FaceAligner, clip_to_range
from tqdm import tqdm
from itertools import repeat

fd = FaceDetector()
fa = FaceAligner('../model/shape_predictor_68_face_landmarks.dat',
                 '../model/face_template.npy')

IMAGE_FORMATS = {'.jpg', '.jpeg', '.png'}


def get_images(path):
    return list(
        filter(
            lambda s: os.path.isfile(os.path.join(path, s)) and os.path.
            splitext(s)[1] in IMAGE_FORMATS, os.listdir(path)))


def get_folders(path):
    return list(
        filter(lambda s: os.path.isdir(os.path.join(path, s)),
               os.listdir(path)))


def list_data(data_path):