Dataset Viewer
Auto-converted to Parquet
repo
string
path
string
func_name
string
original_string
string
language
string
code
string
code_tokens
sequence
docstring
string
docstring_tokens
sequence
sha
string
url
string
partition
string
summary
string
input_ids
sequence
token_type_ids
sequence
attention_mask
sequence
labels
sequence
ageitgey/face_recognition
examples/face_recognition_knn.py
train
def train(train_dir, model_save_path=None, n_neighbors=None, knn_algo='ball_tree', verbose=False): """ Trains a k-nearest neighbors classifier for face recognition. :param train_dir: directory that contains a sub-directory for each known person, with its name. (View in source code to see train_dir example tree structure) Structure: <train_dir>/ β”œβ”€β”€ <person1>/ β”‚ β”œβ”€β”€ <somename1>.jpeg β”‚ β”œβ”€β”€ <somename2>.jpeg β”‚ β”œβ”€β”€ ... β”œβ”€β”€ <person2>/ β”‚ β”œβ”€β”€ <somename1>.jpeg β”‚ └── <somename2>.jpeg └── ... :param model_save_path: (optional) path to save model on disk :param n_neighbors: (optional) number of neighbors to weigh in classification. Chosen automatically if not specified :param knn_algo: (optional) underlying data structure to support knn.default is ball_tree :param verbose: verbosity of training :return: returns knn classifier that was trained on the given data. """ X = [] y = [] # Loop through each person in the training set for class_dir in os.listdir(train_dir): if not os.path.isdir(os.path.join(train_dir, class_dir)): continue # Loop through each training image for the current person for img_path in image_files_in_folder(os.path.join(train_dir, class_dir)): image = face_recognition.load_image_file(img_path) face_bounding_boxes = face_recognition.face_locations(image) if len(face_bounding_boxes) != 1: # If there are no people (or too many people) in a training image, skip the image. if verbose: print("Image {} not suitable for training: {}".format(img_path, "Didn't find a face" if len(face_bounding_boxes) < 1 else "Found more than one face")) else: # Add face encoding for current image to the training set X.append(face_recognition.face_encodings(image, known_face_locations=face_bounding_boxes)[0]) y.append(class_dir) # Determine how many neighbors to use for weighting in the KNN classifier if n_neighbors is None: n_neighbors = int(round(math.sqrt(len(X)))) if verbose: print("Chose n_neighbors automatically:", n_neighbors) # Create and train the KNN classifier knn_clf = neighbors.KNeighborsClassifier(n_neighbors=n_neighbors, algorithm=knn_algo, weights='distance') knn_clf.fit(X, y) # Save the trained KNN classifier if model_save_path is not None: with open(model_save_path, 'wb') as f: pickle.dump(knn_clf, f) return knn_clf
python
def train(train_dir, model_save_path=None, n_neighbors=None, knn_algo='ball_tree', verbose=False): """ Trains a k-nearest neighbors classifier for face recognition. :param train_dir: directory that contains a sub-directory for each known person, with its name. (View in source code to see train_dir example tree structure) Structure: <train_dir>/ β”œβ”€β”€ <person1>/ β”‚ β”œβ”€β”€ <somename1>.jpeg β”‚ β”œβ”€β”€ <somename2>.jpeg β”‚ β”œβ”€β”€ ... β”œβ”€β”€ <person2>/ β”‚ β”œβ”€β”€ <somename1>.jpeg β”‚ └── <somename2>.jpeg └── ... :param model_save_path: (optional) path to save model on disk :param n_neighbors: (optional) number of neighbors to weigh in classification. Chosen automatically if not specified :param knn_algo: (optional) underlying data structure to support knn.default is ball_tree :param verbose: verbosity of training :return: returns knn classifier that was trained on the given data. """ X = [] y = [] # Loop through each person in the training set for class_dir in os.listdir(train_dir): if not os.path.isdir(os.path.join(train_dir, class_dir)): continue # Loop through each training image for the current person for img_path in image_files_in_folder(os.path.join(train_dir, class_dir)): image = face_recognition.load_image_file(img_path) face_bounding_boxes = face_recognition.face_locations(image) if len(face_bounding_boxes) != 1: # If there are no people (or too many people) in a training image, skip the image. if verbose: print("Image {} not suitable for training: {}".format(img_path, "Didn't find a face" if len(face_bounding_boxes) < 1 else "Found more than one face")) else: # Add face encoding for current image to the training set X.append(face_recognition.face_encodings(image, known_face_locations=face_bounding_boxes)[0]) y.append(class_dir) # Determine how many neighbors to use for weighting in the KNN classifier if n_neighbors is None: n_neighbors = int(round(math.sqrt(len(X)))) if verbose: print("Chose n_neighbors automatically:", n_neighbors) # Create and train the KNN classifier knn_clf = neighbors.KNeighborsClassifier(n_neighbors=n_neighbors, algorithm=knn_algo, weights='distance') knn_clf.fit(X, y) # Save the trained KNN classifier if model_save_path is not None: with open(model_save_path, 'wb') as f: pickle.dump(knn_clf, f) return knn_clf
[ "def", "train", "(", "train_dir", ",", "model_save_path", "=", "None", ",", "n_neighbors", "=", "None", ",", "knn_algo", "=", "'ball_tree'", ",", "verbose", "=", "False", ")", ":", "X", "=", "[", "]", "y", "=", "[", "]", "# Loop through each person in the training set", "for", "class_dir", "in", "os", ".", "listdir", "(", "train_dir", ")", ":", "if", "not", "os", ".", "path", ".", "isdir", "(", "os", ".", "path", ".", "join", "(", "train_dir", ",", "class_dir", ")", ")", ":", "continue", "# Loop through each training image for the current person", "for", "img_path", "in", "image_files_in_folder", "(", "os", ".", "path", ".", "join", "(", "train_dir", ",", "class_dir", ")", ")", ":", "image", "=", "face_recognition", ".", "load_image_file", "(", "img_path", ")", "face_bounding_boxes", "=", "face_recognition", ".", "face_locations", "(", "image", ")", "if", "len", "(", "face_bounding_boxes", ")", "!=", "1", ":", "# If there are no people (or too many people) in a training image, skip the image.", "if", "verbose", ":", "print", "(", "\"Image {} not suitable for training: {}\"", ".", "format", "(", "img_path", ",", "\"Didn't find a face\"", "if", "len", "(", "face_bounding_boxes", ")", "<", "1", "else", "\"Found more than one face\"", ")", ")", "else", ":", "# Add face encoding for current image to the training set", "X", ".", "append", "(", "face_recognition", ".", "face_encodings", "(", "image", ",", "known_face_locations", "=", "face_bounding_boxes", ")", "[", "0", "]", ")", "y", ".", "append", "(", "class_dir", ")", "# Determine how many neighbors to use for weighting in the KNN classifier", "if", "n_neighbors", "is", "None", ":", "n_neighbors", "=", "int", "(", "round", "(", "math", ".", "sqrt", "(", "len", "(", "X", ")", ")", ")", ")", "if", "verbose", ":", "print", "(", "\"Chose n_neighbors automatically:\"", ",", "n_neighbors", ")", "# Create and train the KNN classifier", "knn_clf", "=", "neighbors", ".", "KNeighborsClassifier", "(", "n_neighbors", "=", "n_neighbors", ",", "algorithm", "=", "knn_algo", ",", "weights", "=", "'distance'", ")", "knn_clf", ".", "fit", "(", "X", ",", "y", ")", "# Save the trained KNN classifier", "if", "model_save_path", "is", "not", "None", ":", "with", "open", "(", "model_save_path", ",", "'wb'", ")", "as", "f", ":", "pickle", ".", "dump", "(", "knn_clf", ",", "f", ")", "return", "knn_clf" ]
Trains a k-nearest neighbors classifier for face recognition. :param train_dir: directory that contains a sub-directory for each known person, with its name. (View in source code to see train_dir example tree structure) Structure: <train_dir>/ β”œβ”€β”€ <person1>/ β”‚ β”œβ”€β”€ <somename1>.jpeg β”‚ β”œβ”€β”€ <somename2>.jpeg β”‚ β”œβ”€β”€ ... β”œβ”€β”€ <person2>/ β”‚ β”œβ”€β”€ <somename1>.jpeg β”‚ └── <somename2>.jpeg └── ... :param model_save_path: (optional) path to save model on disk :param n_neighbors: (optional) number of neighbors to weigh in classification. Chosen automatically if not specified :param knn_algo: (optional) underlying data structure to support knn.default is ball_tree :param verbose: verbosity of training :return: returns knn classifier that was trained on the given data.
[ "Trains", "a", "k", "-", "nearest", "neighbors", "classifier", "for", "face", "recognition", "." ]
c96b010c02f15e8eeb0f71308c641179ac1f19bb
https://github.com/ageitgey/face_recognition/blob/c96b010c02f15e8eeb0f71308c641179ac1f19bb/examples/face_recognition_knn.py#L46-L108
train
Train a k - nearest neighbors classifier for face recognition.
[ 30522, 13366, 3345, 1006, 3345, 1035, 16101, 1010, 2944, 1035, 3828, 1035, 4130, 1027, 3904, 1010, 1050, 1035, 10638, 1027, 3904, 1010, 14161, 2078, 1035, 2632, 3995, 1027, 1005, 3608, 1035, 3392, 1005, 1010, 12034, 9232, 1027, 6270, 1007, 1024, 1000, 1000, 1000, 4499, 1037, 1047, 1011, 7205, 10638, 2465, 18095, 2005, 2227, 5038, 1012, 1024, 11498, 2213, 3345, 1035, 16101, 1024, 14176, 2008, 3397, 1037, 4942, 1011, 14176, 2005, 2169, 2124, 2711, 1010, 2007, 2049, 2171, 1012, 1006, 3193, 1999, 3120, 3642, 2000, 2156, 3345, 1035, 16101, 2742, 3392, 3252, 1007, 3252, 1024, 1026, 3345, 1035, 16101, 1028, 1013, 100, 1026, 2711, 2487, 1028, 1013, 1616, 100, 1026, 2070, 18442, 2487, 1028, 1012, 16545, 13910, 1616, 100, 1026, 2070, 18442, 2475, 1028, 1012, 16545, 13910, 1616, 100, 1012, 30524, 2944, 2006, 9785, 1024, 11498, 2213, 1050, 1035, 10638, 1024, 1006, 11887, 1007, 2193, 1997, 10638, 2000, 17042, 1999, 5579, 1012, 4217, 8073, 2065, 2025, 9675, 1024, 11498, 2213, 14161, 2078, 1035, 2632, 3995, 1024, 1006, 11887, 1007, 10318, 2951, 3252, 2000, 2490, 14161, 2078, 1012, 12398, 2003, 3608, 1035, 3392, 1024, 11498, 2213, 12034, 9232, 1024, 12034, 25949, 1997, 2731, 1024, 2709, 1024, 5651, 14161, 2078, 2465, 18095, 2008, 2001, 4738, 2006, 1996, 2445, 2951, 1012, 1000, 1000, 1000, 1060, 1027, 1031, 1033, 1061, 1027, 1031, 1033, 1001, 7077, 2083, 2169, 2711, 1999, 1996, 2731, 2275, 2005, 2465, 1035, 16101, 1999, 9808, 1012, 2862, 4305, 2099, 1006, 3345, 1035, 16101, 1007, 1024, 2065, 2025, 9808, 1012, 4130, 1012, 2003, 4305, 2099, 1006, 9808, 1012, 4130, 1012, 3693, 1006, 3345, 1035, 16101, 1010, 2465, 1035, 16101, 1007, 1007, 1024, 3613, 1001, 7077, 2083, 2169, 2731, 3746, 2005, 1996, 2783, 2711, 2005, 10047, 2290, 1035, 4130, 1999, 3746, 1035, 6764, 1035, 1999, 1035, 19622, 1006, 9808, 1012, 4130, 1012, 3693, 1006, 3345, 1035, 16101, 1010, 2465, 1035, 16101, 1007, 1007, 1024, 3746, 1027, 2227, 1035, 5038, 1012, 7170, 1035, 3746, 1035, 5371, 1006, 10047, 2290, 1035, 4130, 1007, 2227, 1035, 5391, 2075, 1035, 8378, 1027, 2227, 1035, 5038, 1012, 2227, 1035, 5269, 1006, 3746, 1007, 2065, 18798, 1006, 2227, 1035, 5391, 2075, 1035, 8378, 1007, 999, 1027, 1015, 1024, 1001, 2065, 2045, 2024, 2053, 2111, 1006, 2030, 2205, 2116, 2111, 1007, 1999, 1037, 2731, 3746, 1010, 13558, 1996, 3746, 1012, 2065, 12034, 9232, 1024, 6140, 1006, 1000, 3746, 1063, 1065, 2025, 7218, 2005, 2731, 1024, 1063, 1065, 1000, 1012, 4289, 1006, 10047, 2290, 1035, 4130, 1010, 1000, 2134, 1005, 1056, 2424, 1037, 2227, 1000, 2065, 18798, 1006, 2227, 1035, 5391, 2075, 1035, 8378, 1007, 1026, 1015, 2842, 1000, 2179, 2062, 2084, 2028, 2227, 1000, 1007, 1007, 2842, 1024, 1001, 5587, 2227, 17181, 2005, 2783, 3746, 2000, 1996, 2731, 2275, 1060, 1012, 10439, 10497, 1006, 30523, 1012, 1012, 100, 1026, 2711, 2475, 1028, 1013, 1616, 100, 1026, 2070, 18442, 2487, 1028, 1012, 16545, 13910, 1616, 100, 1026, 2070, 18442, 2475, 1028, 1012, 16545, 13910, 100, 1012, 1012, 1012, 1024, 11498, 2213, 2944, 1035, 3828, 1035, 4130, 1024, 1006, 11887, 1007, 4130, 2000, 3828, 30526 ]
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ]
[ -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, 1012, 1012, 100, 1026, 2711, 2475, 1028, 1013, 1616, 100, 1026, 2070, 18442, 2487, 1028, 1012, 16545, 13910, 1616, 100, 1026, 2070, 18442, 2475, 1028, 1012, 16545, 13910, 100, 1012, 1012, 1012, 1024, 11498, 2213, 2944, 1035, 3828, 1035, 4130, 1024, 1006, 11887, 1007, 4130, 2000, 3828, 30526 ]
README.md exists but content is empty.
Downloads last month
2