(X_frame, knn_clf=None, model_path=None, distance_threshold=0.5)
return [(pred, loc) if rec else ("unknown", loc) for pred, loc, rec in zip(knn_clf.predict(faces_encodings), X_face_locations, are_matches)]
Recognizes faces in given image using a trained KNN classifier

    :param X_frame: frame to do the prediction on.
    :param knn_clf: (optional) a knn classifier object. if not specified, model_save_path must be specified.
    :param model_path: (optional) path to a pickled knn classifier. if not specified, model_save_path must be knn_clf.
    :param distance_threshold: (optional) distance threshold for face classification. the larger it is, the more chance
           of mis-classifying an unknown person as a known one.
    :return: a list of names and face locations for the recognized faces in the image: [(name, bounding box), ...].
        For faces of unrecognized persons, the name 'unknown' will be returned.
Recognizes faces in given image using a trained KNN classifier
Recognizes faces in given image using a trained KNN classifier
def predict(X_frame, knn_clf=None, model_path=None, distance_threshold=0.5):
    """
    Recognizes faces in given image using a trained KNN classifier

    :param X_frame: frame to do the prediction on.
    :param knn_clf: (optional) a knn classifier object. if not specified, model_save_path must be specified.
    :param model_path: (optional) path to a pickled knn classifier. if not specified, model_save_path must be knn_clf.
    :param distance_threshold: (optional) distance threshold for face classification. the larger it is, the more chance
           of mis-classifying an unknown person as a known one.
    :return: a list of names and face locations for the recognized faces in the image: [(name, bounding box), ...].
        For faces of unrecognized persons, the name 'unknown' will be returned.
    """
    if knn_clf is None and model_path is None:
        raise Exception("Must supply knn classifier either thourgh knn_clf or model_path")

    # Load a trained KNN model (if one was passed in)
    if knn_clf is None:
        with open(model_path, 'rb') as f:
            knn_clf = pickle.load(f)

    X_face_locations = face_recognition.face_locations(X_frame)

    # If no faces are found in the image, return an empty result.
    if len(X_face_locations) == 0:
        return []

    # Find encodings for faces in the test image
    faces_encodings = face_recognition.face_encodings(X_frame, known_face_locations=X_face_locations)

    # Use the KNN model to find the best matches for the test face
    closest_distances = knn_clf.kneighbors(faces_encodings, n_neighbors=1)
    are_matches = [closest_distances[0][i][0] <= distance_threshold for i in range(len(X_face_locations))]

    # Predict classes and remove classifications that aren't within the threshold
    return [(pred, loc) if rec else ("unknown", loc) for pred, loc, rec in zip(knn_clf.predict(faces_encodings), X_face_locations, are_matches)]
def predict ( X_frame , knn_clf = None , model_path = None , distance_threshold = 0.5 ) : if knn_clf is None and model_path is None : raise Exception ( "Must supply knn classifier either thourgh knn_clf or model_path" ) # Load a trained KNN model (if one was passed in) if knn_clf is None : with open ( model_path , 'rb' ) as f : knn_clf = pickle . load ( f ) X_face_locations = face_recognition . face_locations ( X_frame ) # If no faces are found in the image, return an empty result. if len ( X_face_locations ) == 0 : return [ ] # Find encodings for faces in the test image faces_encodings = face_recognition . face_encodings ( X_frame , known_face_locations = X_face_locations ) # Use the KNN model to find the best matches for the test face closest_distances = knn_clf . kneighbors ( faces_encodings , n_neighbors = 1 ) are_matches = [ closest_distances [ 0 ] [ i ] [ 0 ] <= distance_threshold for i in range ( len ( X_face_locations ) ) ] # Predict classes and remove classifications that aren't within the threshold return [ ( pred , loc ) if rec else ( "unknown" , loc ) for pred , loc , rec in zip ( knn_clf . predict ( faces_encodings ) , X_face_locations , are_matches ) ]