def train(request): if request.user.username != 'admin': return redirect('not-authorised') training_dir = 'face_recognition_data/training_dataset' count = 0 for person_name in os.listdir(training_dir): curr_directory = os.path.join(training_dir, person_name) if not os.path.isdir(curr_directory): continue for imagefile in image_files_in_folder(curr_directory): count += 1 X = [] y = [] i = 0 for person_name in os.listdir(training_dir): print(str(person_name)) curr_directory = os.path.join(training_dir, person_name) if not os.path.isdir(curr_directory): continue for imagefile in image_files_in_folder(curr_directory): print(str(imagefile)) image = cv2.imread(imagefile) try: X.append((face_recognition.face_encodings(image)[0]).tolist()) y.append(person_name) i += 1 except: print("removed") os.remove(imagefile) targets = np.array(y) encoder = LabelEncoder() encoder.fit(y) y = encoder.transform(y) X1 = np.array(X) print("shape: " + str(X1.shape)) np.save('face_recognition_data/classes.npy', encoder.classes_) svc = SVC(kernel='linear', probability=True) svc.fit(X1, y) svc_save_path = "face_recognition_data/svc.sav" with open(svc_save_path, 'wb') as f: pickle.dump(svc, f) vizualize_Data(X1, targets) messages.success(request, f'Training Complete.') return render(request, "recognition/train.html")
def save_aligned_images(inputDir): for class_dir in os.listdir(inputDir): if not os.path.isdir(os.path.join(inputDir, class_dir)): continue print(class_dir) openface.helper.mkdirP(outputDir + '/' + class_dir) i = 0 for img_path in image_files_in_folder(os.path.join(inputDir, class_dir)): print(img_path ) faces=detect(img_path,True) bgr = cv2.imread(img_path) rgb = cv2.cvtColor(bgr, cv2.COLOR_BGR2RGB) landmarks = [] for BB in faces: landmarks.append(align.findLandmarks(rgb, BB)) thumbnails = [] landmarkIndices=openface.AlignDlib.INNER_EYES_AND_BOTTOM_LIP for lm in landmarks: npLandmarks = np.float32(lm) npLandmarkIndices = np.array(landmarkIndices) H = cv2.getAffineTransform(npLandmarks[npLandmarkIndices], 96 * MINMAX_TEMPLATE[npLandmarkIndices]) thumbnail = cv2.warpAffine(rgb, H, (96, 96)) outBgr = cv2.cvtColor(thumbnail, cv2.COLOR_RGB2BGR) cv2.imwrite( outputDir + class_dir + '/' + str(i) + ".png", outBgr) print('output' , outputDir + class_dir + '/' + str(i) + ".png") i += 1 return
def train(): train_dir = "train_images/" verbose = True X = [] y = [] n_neighbors = 3 knn_algo = 'ball_tree' # 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) np.save('model/X.npy', X) np.save('model/y.npy', y)
def classification_folder(source_path, purpose_path): all_images_in_folder = image_files_in_folder(source_path) count_of_images = len(all_images_in_folder) for num in range(count_of_images): full_file_path = all_images_in_folder[num] image_file_folder, image_file = os.path.split(full_file_path) # print("Looking for faces in {}".format(image_file)) # Find all people in the image using a trained classifier model # Note: You can pass in either a classifier file name or a classifier model instance predictions = predict(full_file_path, model_path=model_save_path) # Print results on the console # for name, (top, right, bottom, left) in predictions: # print("- Found {} at ({}, {})".format(name, left, top)) if len(predictions) > 0: # Display results overlaid on an image img = show_prediction_labels_on_image(full_file_path, predictions) # img.show() save_path = '%s/%s' % (purpose_path, predictions[0][0]) f, image_file_name = os.path.split(full_file_path) if not os.path.exists(save_path): os.mkdir(save_path) img.save('%s/%s' % (save_path, image_file_name)) print('all images: %s , current: (%.2f %%)' % (count_of_images, num / count_of_images * 100)) pass
def iterate_persons(persons_source_dir): # Loop through each person in the training set for person_dir in os.listdir(persons_source_dir): if not os.path.isdir(os.path.join(persons_source_dir, person_dir)): continue print(person_dir) # TODO make request to create person. response = requests.post(urljoin(app_base_url, 'users'), json={'name': person_dir}) created_person = response.json() print(created_person) # Loop through each training image for the current person for img_path in image_files_in_folder( os.path.join(persons_dir, person_dir)): print(img_path) with open(img_path, 'rb') as image: response = requests.post( urljoin(app_base_url, 'users/{}/photos'.format(created_person['id'])), json={'data': base64.b64encode(image.read()).decode()}) print('create photo {}'.format(str(response)))
def data_default(self, train_dir): self.X = [] self.y = [] 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 self.X.append( face_recognition.face_encodings( image, known_face_locations=face_bounding_boxes)[0]) self.y.append(class_dir)
def plot_pca(train_dir, model_save_path=None): 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 print(class_dir) count = 0 # 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 = cv2.imread(img_path) image = cv2.resize(image, (48, 48)) # Add face encoding for current image to the training set encoded_face = face_recognition.face_encodings(image) if len(encoded_face) == 0: continue X.append(encoded_face[0]) y.append(class_dir) if count > 20: break count+=1 X = np.asarray(X) with open(model_save_path, 'rb') as f: clf = pickle.load(f) # Z = clf.predict(X) pca = PCA(n_components=2) X_r = pca.fit(X).transform(X) plt.figure() class_names = ['reinaldo', 'kokhui', 'emily', 'eewei', 'yanpai'] colors = ['navy', 'turquoise', 'darkorange', 'blue', 'green', 'magenta', 'yellow', 'cyan', 'red'] lw = 2 for color, i in zip(colors, class_names): ind = [] for j in range(len(y)): if y[j] == i: ind.append(j) plt.scatter(X_r[ind, 0], X_r[ind, 1], color=color, alpha=.8, lw=lw, label=i) plt.legend(loc='best', shadow=False, scatterpoints=1) plt.title('PCA of encoded FACE dataset') plt.show()
def create_encodings(): stime = time.time() X = [] y = [] companies_list, base_path = get_list_companies() for company in companies_list: ic_list = os.listdir(os.path.join(base_path, company)) for ic in ic_list: for img_path in image_files_in_folder( os.path.join(base_path, company, ic)): image = dlib.load_rgb_image(img_path) face_bbox, im, shape, _ = find_face_dlib(image) if len(face_bbox) != 1: # If there are no people (or too many people) in a training image, skip the image. print("Image {} not suitable for training: {}".format( img_path, "Didn't find a face" if len(face_bbox) < 1 else "Found more than one face")) else: # Add face encoding for current image to the training set # X.append(fr.face_encodings(image, known_face_locations=face_bounding_boxes)[0]) X.append(compute_emb(im, shape)) y.append(ic) print(img_path, ic) X = np.asarray(X) print("this", X.shape) print("this y", y) y = np.asarray(y) np.savez(os.path.join('pickle', 'X_y'), X=X, y=y) print(time.time() - stime)
def extract(images_dir, dict_file, verbose=False): temp = {} # Loop through each person in the images directory for class_dir in os.listdir(images_dir): if not os.path.isdir(os.path.join(images_dir, class_dir)): continue # Loop through each training image for the current person # Note that the name of folder is the user identity for img_path in image_files_in_folder( os.path.join(images_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 # It is also known as feature vectors v v = face_recognition.face_encodings( image, known_face_locations=face_bounding_boxes)[0] try: temp[class_dir].append(v) except KeyError: temp[class_dir] = [v] # Save all the extracted feature vectors into a file np.save(dict_file, temp)
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 prepare_to_train(train_dir): X, y = [], [] # 迴圈 by train_dir 內的每個人 for class_dir in os.listdir(train_dir): if not os.path.isdir(os.path.join(train_dir, class_dir)): continue # 迴圈 by 每個人資料夾內的所有訓練用照片 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 verbose: print("照片 {} 不適合做訓練: {}".format( img_path, "找不到臉" if len(face_bounding_boxes) < 1 else "找到超過一張臉")) else: # 在訓練資料集中加入該照片之 face encoding X.append( face_recognition.face_encodings( image, known_face_locations=face_bounding_boxes)[0]) y.append(class_dir) print("照片 {} 適合做訓練".format(img_path)) return X, y
def get_encodings(): encodings = [] persons = [] # 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) # Checking if image contains one face if len(face_bounding_boxes) == 1: encodings.append( face_recognition.face_encodings( image, known_face_locations=face_bounding_boxes)[0]) persons.append(class_dir) # encodings.append(persons) # if ENCODINGS_PATH is not None: # with open(ENCODINGS_PATH, 'wb') as f: # pickle.dump(encodings, f) return encodings, persons
def encoding_nolabels(dataset_dir, encoding_file='encodings.csv', verbose=False, model='hog'): X = [] labels = [] cache = CacheSystem(dataset_dir, cache_dir='__cache__') # Loop through each training image for the current person for img_path in image_files_in_folder(os.path.join(dataset_dir)): print('Found image: %s' % (img_path), end='') image = face_recognition.load_image_file(img_path) face_bounding_boxes = face_recognition.face_locations(image, model=model) 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: is_cached, encodings = cache.check(img_path, model=model) X.append(encodings) labels.append(img_path.split('/')[-1]) print('=> cached: %s' % (is_cached)) # save to encodings.csv save_encodings_csv(os.path.join(dataset_dir, encoding_file), X, labels=labels)
def testTrain(train_dir, model_save_path): """ 训练指定文件夹下的所有人的图片 :param train_dir: :param model_save_path: :return: """ # 遍历训练集中的子文件夹 labels = [] datas = [] i = 0 for user_dir in os.listdir(train_dir): if not os.path.isdir(os.path.join(train_dir, user_dir)): continue # 遍历这个人的每一张照片 print(i) for image_path in image_files_in_folder( os.path.join(train_dir, user_dir)): # 加载图片 image = fr.load_image_file(image_path) # 定位人脸位置 boxes = fr.face_locations(image) # 获取人脸特征 encodings = fr.face_encodings(image, known_face_locations=boxes) print(image_path) if len(encodings) == 0: print(image_path + "have no face") continue # 判断是否是已存在用户 labels.append(i) datas.append(encodings[0].tolist()) i = i + 1 train_model(labels, datas, model_save_path)
def train(train_dir, model_save_path=None, n_neighbors=None, knn_algo='ball_tree', verbose=False): x = [] y = [] for class_dir in os.listdir(train_dir): if not os.path.isdir(os.path.join(train_dir, class_dir)): continue for img_path in image_files_in_folder(os.path.join(train_dir, class_dir)): exif_deleter(img_path) image = face_recognition.load_image_file(img_path) face_bounding_boxes = face_recognition.face_locations(image) if len(face_bounding_boxes) != 1: 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: x.append(face_recognition.face_encodings(image, known_face_locations=face_bounding_boxes)[0]) y.append(class_dir) if n_neighbors is None: n_neighbors = int(round(math.sqrt(len(x)))) if verbose: print("Chose n_neighbors automatically:", n_neighbors) knn_clf = neighbors.KNeighborsClassifier(n_neighbors=n_neighbors, algorithm=knn_algo, weights='distance') knn_clf.fit(x, y) if model_save_path is not None : with open(model_save_path, 'wb') as f: pickle.dump(knn_clf, f)
def mp_process(person_list): for class_dir in person_list: # 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, model='hog') if len(face_bounding_boxes) != 1: # If there are no people (or too many people) in a training image, skip the image. 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 known_face_encodings.append( face_recognition.face_encodings( image, known_face_locations=face_bounding_boxes, num_jitters=2)[0]) known_face_names.append(class_dir) #print(known_face_names) return known_face_encodings, known_face_names
def train(self, train_dir, model_save_path=model_path, n_neighbors=None, knn_algo='ball_tree', verbose=False): train = [] test = [] # 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 face_count = 0 # 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) print("[Training]: checking ", img_path) 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 train.append( face_recognition.face_encodings( image, known_face_locations=face_bounding_boxes)[0]) test.append(class_dir) face_count += 1 if face_count == 25: break # Determine how many neighbors to use for weighting in the KNN classifier if n_neighbors is None: n_neighbors = int(round(math.sqrt(len(train)))) print("Chose n_neighbors automatically:", n_neighbors) if verbose: print("Chose n_neighbors automatically:", n_neighbors) # Create and train the KNN classifier classifier = neighbors.KNeighborsClassifier(n_neighbors=n_neighbors, algorithm=knn_algo, weights='distance') classifier.fit(train, test) # Save the trained KNN classifier if model_save_path is not None: with open(model_save_path, 'wb') as f: pickle.dump(classifier, f) return classifier
def train(train_dir, model_save_path=None, num_threads=4, verbose=False): """ Trains a knn classifier for face recognition. :param train_dir: directory that contains a sub-directory for each known person, with its name coded by DIGITS. 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 num_threads: Set number of threads used during batch search/construction :param verbose: verbose mode for 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) # Create and train the KNN classifier model = Index(space='l2', dim=128) model.init_index(len(X)) model.set_ef(10) model.set_num_threads(num_threads) model.add_items(X, y) # Save the trained KNN classifier if model_save_path is not None: model.save_index(model_save_path) return model
def train(train_dir, model_save_path=None, n_neighbors=None, knn_algo='ball_tree', verbose=False): """ Обрабатывает классификатор k-ближайших соседей для распознавания лиц. : param train_dir: каталог, который содержит подкаталог для каждого известного человека с его именем. (Посмотрите в исходном коде, чтобы увидеть пример структуры дерева train_dir) Структура: <Train_dir> / Person── <человек1> / So ├── <somename1> .jpeg So ├── <somename2> .jpeg │ ├── ... Person── <человек2> / So ├── <somename1> .jpeg So └── <somename2> .jpeg └── ... : param model_save_path: (необязательно) путь для сохранения модели на диске : param n_neighbors: (необязательно) количество соседей, которые будут взвешиваться в классификации. Выбирается автоматически, если не указано : param knn_algo: (необязательно) базовая структура данных для поддержки knn.default - ball_tree : param verbose: многословие обучения : return: возвращает классификатор knn, который был обучен по заданным данным. """ X = [] y = [] # Проходить через каждого человека в тренировочном наборе for class_dir in os.listdir(train_dir): if not os.path.isdir(os.path.join(train_dir, class_dir)): continue # Цикл каждого учебного изображения для текущего человека 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 verbose: print("Изображение {} не подходит для обучения: {}".format(img_path, "Не найдено лицо" if len(face_bounding_boxes) < 1 else "Найдено более одного лица")) else: # Добавить кодировку лица для текущего изображения в тренировочный набор X.append(face_recognition.face_encodings(image, known_face_locations=face_bounding_boxes)[0]) y.append(class_dir) # Определить, сколько соседей использовать для взвешивания в классификаторе KNN if n_neighbors is None: n_neighbors = int(round(math.sqrt(len(X)))) if verbose: print("ВыберАНО n_neighbors автоматически:", n_neighbors) # Создать и обучить классификатор KNN knn_clf = neighbors.KNeighborsClassifier(n_neighbors=n_neighbors, algorithm=knn_algo, weights='distance') knn_clf.fit(X, y) # Сохранить обученный классификатор KNN 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='auto', verbose=False): X = [] y = [] # Loop through each person in the training set examples = 0 wrong_examples = 0 for class_dir in os.listdir(train_dir): examples = examples + 1 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: wrong_examples = wrong_examples + 1 shutil.rmtree(os.path.join(train_dir, class_dir)) 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) print(str(examples) + " - " + 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) print("Total processed images:" + str(examples)) print("Total wrong images:" + str(wrong_examples)) return knn_clf
def images(self): from face_recognition.face_recognition_cli import image_files_in_folder images = list(filter( lambda path: path.startswith('train') and path.endswith('.jpg'), image_files_in_folder('/'.join([self.DATA_DIR, self.id])) )) images.sort() return images
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 = [] # 循环遍历训练集中的每一个人 for class_dir in os.listdir(train_dir): if not os.path.isdir(os.path.join(train_dir, class_dir)): continue # 循环遍历当前训练集中的每个人 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 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: # 将图片中的人脸的编码加入到训练集中 X.append(face_recognition.face_encodings(image, known_face_locations=face_bounding_boxes)[0]) y.append(class_dir) # 确定KNN分类器中的权重 if n_neighbors is None: n_neighbors = int(round(math.sqrt(len(X)))) if verbose: print("Chose n_neighbors automatically:", n_neighbors) # 建立并训练KNN训练集 knn_clf = neighbors.KNeighborsClassifier(n_neighbors=n_neighbors, algorithm=knn_algo, weights='distance') knn_clf.fit(X, y) # Save the trained KNN classifier # 保存KNN分类器 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 = [] print(os.listdir(train_dir)) # Loop through each person in the training set for class_dir in os.listdir(train_dir)[1:]: print(class_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)): print(img_path) image = face_recognition.load_image_file(img_path) face_bounding_boxes = face_recognition.face_locations(image) print(face_bounding_boxes) 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 scale x.append( face_recognition.face_encodings( image, known_face_locations=face_bounding_boxes)[0]) y.append(class_dir) print(x) print(y) # 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 CascadeClassifier 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="", n_neighbors=None, knn_algo='ball_tree', verbose=True): id_folder = str(session.get('id_folder')) X = [] y = [] z = 0 for class_dir in listdir(train_dir): if not isdir(join(train_dir, class_dir)): continue for img_path in image_files_in_folder(join(train_dir, class_dir)): image = face_recognition.load_image_file(img_path) faces_bboxes = face_locations(image) if len(faces_bboxes) != 1: if verbose: print("image {} not fit for training: {}".format( img_path, "didn't find a face" if len(faces_bboxes) < 1 else "found more than one face")) os.remove(img_path) z = z + 1 continue X.append( face_recognition.face_encodings( image, known_face_locations=faces_bboxes)[0]) y.append(class_dir) print(listdir(train_dir + "/" + id_folder)) train_dir_f = listdir(train_dir + "/" + id_folder) for i in range(len(train_dir_f)): if (train_dir_f[i].startswith('.')): os.remove(train_dir + "/" + id_folder + "/" + train_dir_f[i]) print(listdir(train_dir + "/" + id_folder)) if (listdir(train_dir + "/" + id_folder) == []): return render_template("upload.html", msg1="training data empty, upload again") elif (z >= 1): return render_template("upload.html", msg1="Data trained for " + id_folder + ", But one of the image not fit for trainning") if n_neighbors is None: n_neighbors = int(round(sqrt(len(X)))) if verbose: print("Chose n_neighbors automatically as:", n_neighbors) knn_clf = neighbors.KNeighborsClassifier(n_neighbors=n_neighbors, algorithm=knn_algo, weights='distance') knn_clf.fit(X, y) if model_save_path != "": with open(model_save_path, 'wb') as f: pickle.dump(knn_clf, f) return render_template("upload.html", msg1="Data trained for " + id_folder)
def train(train_dir, model_save_path=None, n_neighbors=None, knn_algo='ball_tree', verbose=False): X = [] y = [] # Recorre a través de cada persona en el conjunto de entrenamiento for class_dir in os.listdir(train_dir): if not os.path.isdir(os.path.join(train_dir, class_dir)): continue # Recorre cada imagen de entrenamiento para la persona actual 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: # Filtro de muchas caras if verbose: print( "Imagen {} no es recomendable para el entrenamiento: {}" .format( img_path, "No se encontraron caras" if len(face_bounding_boxes) < 1 else "Muchas caras encontradas")) else: # Codifica imagen actual del conjunto de entrenamiento X.append( face_recognition.face_encodings( image, known_face_locations=face_bounding_boxes)[0]) y.append(class_dir) # Calcula numero de vecinos if n_neighbors is None: n_neighbors = int(round(math.sqrt(len(X)))) if verbose: print("Numero de vecinos encontrados:", n_neighbors) # Crea y entrena el KNN knn_clf = neighbors.KNeighborsClassifier(n_neighbors=n_neighbors, algorithm=knn_algo, weights='distance') knn_clf.fit(X, y) # Guarda el kernel generado 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(tr_dir, mod_sp=None, nn_int= None, k_build='ball_tree', flag=True): X = [] y = [] # Loop through each entry in the training set for class_dir in os.listdir(tr_dir): entry = class_dir if not os.path.isdir(os.path.join(tr_dir, class_dir)): continue # init_db_entry(person) # Loop through each training image for the current person for img_path in image_files_in_folder(os.path.join(tr_dir, class_dir)): image = face_recognition.load_image_file(img_path) obj_face_bound_out = face_recognition.face_locations(image) if len(obj_face_bound_out) != 1: if len(obj_face_bound_out)>1: bound_next = obj_face_bound_out[0] # must select the larger bounding box area = 0 for size in obj_face_bound_out: new_area = abs(size[2]-size[0])*abs(size[1]-size[3]) if new_area >= area: area = new_area bound_next = [size] X.append(face_recognition.face_encodings(image, known_face_locations=bound_next)[0]) y.append(class_dir) print("Image {} has more than one face for training: {} - {}".format(img_path, "selecting face with area one face", area)) # if flag: # print("Image {} not suitable for training: {}".format(img_path, "Didn't find a face" if len(obj_face_bound_out) < 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=obj_face_bound_out)[0]) y.append(class_dir) # Determine how many neighbors to use for weighting in the KNN classifier if nn_int is None: nn_int = int(round(math.sqrt(len(X)))) if flag: print("Chose n automatically:", nn_int) # part for adressing the classifier knn_clf = neighbors.KNeighborsClassifier(n_neighbors=nn_int, algorithm=k_build, weights='distance') knn_clf.fit(X, y) # Save the trained KNN classifier if mod_sp is not None: with open(mod_sp, 'wb') as f: pickle.dump(knn_clf, f) return knn_clf
def prepare_data(self, path): time_start = time.time() if "train" in path: mode = "training" elif "test" in path: mode = "testing" print("[INFO] Start to prepare data for {}...".format(mode)) X = [] y = [] # Loop through each person in the training set for class_dir in os.listdir(path): if not os.path.isdir(os.path.join(path, class_dir)): continue # Count how many different persons/faces in training or testing if mode == "training": self.total_persons_train += 1 elif mode == "testing": self.total_persons_test += 1 # Loop through each training image for the current person for image_path in image_files_in_folder(os.path.join(path, class_dir)): image = fr.load_image_file(image_path) faces_boxes = fr.face_locations( image, number_of_times_to_upsample=1) if len(faces_boxes) != 1: # If there are no people (or too many people) in a training image, skip the image. print("[WARNING] Image {} not suitable for {}: {}".format( image_path, mode, "Didn't find a face" if len(faces_boxes) < 1 else "Found more than one face")) else: file_name = image_path.strip(path+'/'+class_dir+'/') # Count how many images in training or testing if mode == "training": self.total_images_train += 1 self.file_name_train.append(file_name) elif mode == "testing": self.total_images_test += 1 self.file_name_test.append(file_name) # Add face encoding for current image to the training set X.append(fr.face_encodings( image, known_face_locations=faces_boxes)[0]) y.append(class_dir) time_end = time.time() time_spent = time_end - time_start print("[INFO] Data has been prepared well for {}. Time: {:.3f}s".format( mode, time_spent)) return X, y
def train(model_save_path=None, n_neighbors=None, knn_algo='ball_tree', verbose=False): users = get_all_user() if len(users) == 0: return None X = [] y = [] # Loop through each person in the training set for user in users: if not os.path.isdir(os.path.join(DATASET_PATH, user.face_id)): continue # Loop through each training image for the current person for img_path in image_files_in_folder( os.path.join(DATASET_PATH, user.face_id)): image = face_recognition.load_image_file(img_path) # print(type(image)) # print(image) face_bounding_boxes = face_recognition.face_locations(image) if len(face_bounding_boxes) != 1: 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, num_jitters=10)[0]) y.append(user.face_id) # 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, n_neighbors=1, knn_algo='ball_tree', verbose=False): X = loadtxt("/home/ubuntu/s03p31b107/face_classifier/output2.csv", delimiter=',').tolist() y = loadtxt("/home/ubuntu/s03p31b107/face_classifier/output3.csv", dtype=str).tolist() # 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: pass 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)))) pointX = asarray(X) d = np.array(y) pointY = d.reshape(d.shape[0], -1) savetxt("/home/ubuntu/s03p31b107/face_classifier/output2.csv", pointX, delimiter=',') savetxt("/home/ubuntu/s03p31b107/face_classifier/output3.csv", pointY, fmt='%s') knn_clf = neighbors.KNeighborsClassifier(n_neighbors=n_neighbors, algorithm=knn_algo, weights='distance') knn_clf.fit(X, y) # Save the trained KNN classifier model_save_path = "/home/ubuntu/s03p31b107/face_classifier/trained_knn_model.csv" 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(self): """Train DeepStack to recognize faces.""" train_dir = os.path.join(self._processor_config.face_recognition_path, "faces") try: faces_dirs = os.listdir(train_dir) except FileNotFoundError: LOGGER.error( f"{train_dir} does not exist. " "Make sure its created properly. " "See the documentation for the proper folder structure" ) return for face_dir in faces_dirs: if face_dir == "unknown": continue LOGGER.debug(f"Training face {face_dir}") # Loop through each training image for the current person try: img_paths = image_files_in_folder(os.path.join(train_dir, face_dir)) except NotADirectoryError as error: LOGGER.error( f"{train_dir} can only contain directories. " "Please remove any other files" ) LOGGER.error(error) return if not img_paths: LOGGER.warning( f"No images were found for face {face_dir} " f"in folder {os.path.join(train_dir, face_dir)}. Please provide " f"some images of this person." ) continue self._ds.delete_face(face_dir) for img_path in img_paths: face_image = cv2.imencode(".jpg", cv2.imread(img_path))[1].tobytes() detections = self._ds.detect(face_image) if len(detections) != 1: # Skip image if amount of people !=1 LOGGER.warning( "Image {} not suitable for training: {}".format( img_path, "Didn't find a face" if len(detections) < 1 else "Found more than one face", ) ) else: self._ds.register(face_dir, face_image)
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