import numpy as np from our_profile import Profile from sys import exit from dlib_models import download_model, download_predictor download_model() download_predictor() from dlib_models import load_dlib_models # 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() from dlib_models import models face_detect = models["face detect"] face_rec_model = models["face rec"] shape_predictor = models["shape predict"] def match(descriptor, database, threshold): """ Takes a descriptor and tries to find a match to a person. Parameters ---------- descriptor: [np.array()] A descriptor for an image. database: Dictionary {String name : Profile profile}
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
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()
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)