Example #1
0
def load_dlib():
    """ Loads dlib models. 
    
        Returns
        -------
        Three dlib models: face detect, face rec, and shape predict. """

    from dlib_models import download_model, download_predictor, load_dlib_models
    download_model()
    download_predictor()
    from dlib_models import models

    load_dlib_models()
    face_detect = models["face detect"]
    face_rec_model = models["face rec"]
    shape_predictor = models["shape predict"]
    return (face_detect, face_rec_model, shape_predictor)
Example #2
0
def create_celeb(image_path):
    # with open("image_arrays.pkl", mode="rb") as opened_file:
    #     image_arrays = pickle.load(opened_file)

    download_model()
    download_predictor()
    load_dlib_models()
    img_arr = plt.imread(image_path,format='jpg').copy()
    face_detect = models["face detect"]
    face_rec_model = models["face rec"]
    shape_predictor = models["shape predict"]
    detections = list(face_detect(img_arr))
    #fig, ax = plt.subplots()
    #ax.imshow(img_arr)
    print("Number of faces detected: {}".format(len(detections)))
    for face in detections:
        # let's take a look as to what the descriptor is!!
        shape = shape_predictor(img_arr, face)
        descriptor = np.array(face_rec_model.compute_face_descriptor(img_arr, shape))
        return descriptor
    # add_profile = input("Would you like to add this picture to the database? [y/n]  ")
    # if add_profile == "y":
    #     new_name = input(f"What is the celebrity's name?   ")
        image_arrays = create_celeb(img_arr, descriptor, new_name, image_arrays)
Example #3
0
# run this cell to download the models from dlib
from dlib_models import download_model, download_predictor, load_dlib_models
download_model()
download_predictor()
from dlib_models import models
import numpy as np


def create_face(image):
    """
    Returns the descriptor vector for a face given an image
    :param image: Takes an image to turn into a descriptor
    :return: np.array that represents the descriptor vector for a face
    """

    load_dlib_models()
    face_detect = models["face detect"]
    face_rec_model = models["face rec"]
    shape_predictor = models["shape predict"]

    detections = list(face_detect(image))

    shape = shape_predictor(image, detections[0])
    descriptor = np.array(face_rec_model.compute_face_descriptor(image, shape))

    return descriptor
Example #4
0
def start_skill():
    welcome_message = 'Hello there, would you like me to take your photo?'
    download_model()
    download_predictor()
    load_dlib_models()
    return question(welcome_message)
Example #5
0
from dlib_models import download_model, download_predictor
from dlib_models import load_dlib_models
from dlib_models import models
download_model()
download_predictor()
# this loads the dlib models into memory. You should only import the models *after* loading them.
# This does lazy-loading: it doesn't do anything if the models are already loaded.

load_dlib_models()
face_detect = models["face detect"]
face_rec_model = models["face rec"]
shape_predictor = models["shape predict"]

face_rec_model.download_predictor()
face_rec_model.download_predictor()
Example #6
0
def main():
    namereturn=''
    descreturn=np.array([])
    download_model()
    download_predictor()


# take the picture
    pic = take_picture()


# first, we load the models that dlib has to detect faces.
    load_dlib_models()
    face_detect = models["face detect"]
    face_rec_model = models["face rec"]
    shape_predictor = models["shape predict"]

# detects the face through corners
    detections = list(face_detect(pic))
#print(detections)  # list of shape n for n faces

    fig, ax = plt.subplots()
    ax.imshow(pic)

    database = {}


    with open("database.pkl", mode="rb") as opened_file:
        database = pickle.load(opened_file)
        #print("DATABASE")
        #print(database)



    print("Number of faces detected: {}".format(len(detections)))
    for k, d in enumerate(detections):
        #Get the landmarks/parts for the face in box d.
        shape = shape_predictor(pic, d)
        # Draw the face landmarks on the screen.
        for i in range(68):
            ax.plot(shape.part(i).x, shape.part(i).y, '+', color="blue")

    import matplotlib.patches as patches

    for faces in detections:
        # Create a Rectangle patch
        rect = patches.Rectangle((faces.left(), faces.bottom()), faces.width(), -faces.height(), linewidth=1, edgecolor='g',
                                 facecolor='none')
        # Add the patch to the Axes
        ax.add_patch(rect)

    names = {}
    database["Unknown Counter"]=0
    unknown_counter = database["Unknown Counter"]

    for face in detections:
        # let's take a look as to what the descriptor is!!
        shape = shape_predictor(pic, face)
        descriptor = np.array(face_rec_model.compute_face_descriptor(pic, shape))
        #print(descriptor)
        # compares descriptor to database through img_in_database
        cutoff = .4
        #print("DATABASE")
        #print(database)
        name = match.img_in_database(descriptor, database, cutoff)

        if name == "not found":
            name = "Unknown" + str(unknown_counter)
            database["Unknown Counter"] += 1

        # plots name underneath square
        ax.text(face.left()+(0.25*faces.width()), face.bottom()+(0.2*faces.height()), name, bbox=dict(facecolor='green', alpha=0.5))

        # adds to names dictionary
        names[name] = descriptor
        descreturn = descriptor
        print(descreturn)
    #plt.show()
    return names, name, descreturn