示例#1
0
def storeFaces():
    datafile = "faces/faces.dat"

    # Parses through file structure for faces
    known_face_encodings = []
    known_face_names = []
    train_dir = "faces/"
    for class_dir in listdir(train_dir):
        if not isdir(join(train_dir, class_dir)):
            continue
        if not listdir(join(train_dir,class_dir)):
            rmdir(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:
                remove(img_path)
                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"))
                continue
            known_face_encodings.append(face_recognition.face_encodings(image, known_face_locations=faces_bboxes)[0])
            known_face_names.append(class_dir)
        print(len(known_face_names))
        if (len(known_face_names) > 10000):
            break


    known_face_encodings = np.array(known_face_encodings)

    # Open datafile and store the two lists, seperately
    with open(datafile, "w") as f:
        pickle.dump(known_face_encodings, f,protocol=pickle.HIGHEST_PROTOCOL)
        pickle.dump(known_face_names, f,protocol=pickle.HIGHEST_PROTOCOL)
示例#2
0
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
示例#3
0
def train(train_dir,
          model_save_path="",
          n_neighbors=None,
          knn_algo='ball_tree',
          verbose=True):
    tkinter.messagebox.showinfo(title='WAIT',
                                message="Training Started, Please Wait...")
    """
    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.
    :param model_save_path: (optional) path to save model of 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 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_recognition.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"))
                continue
            X.append(
                face_recognition.face_encodings(
                    image, known_face_locations=faces_bboxes,
                    num_jitters=1)[0])
            y.append(class_dir)

    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)
    """
    tkinter.messagebox.showinfo(title='SUCCESS',
                                message="Training complete and Model Created.")
    return knn_clf
示例#4
0
def train(train_dir, model_save_path='', 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 of 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 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'))
                continue
            x.append(face_recognition.face_encodings(image, known_face_locations=faces_bboxes)[0])
            y.append(class_dir)

    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 knn_clf
def train(train_dir, model_save_path = "", 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 of 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 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"))
                continue
            X.append(face_recognition.face_encodings(image, known_face_locations=faces_bboxes)[0])
            y.append(class_dir)


    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 knn_clf
示例#6
0
def train(train_dir,
          model_save_path='trained_knn_model.clf',
          n_neighbors=3,
          knn_algo='ball_tree'):
    """
    训练一个KNN分类器.
    :param train_dir: 训练目录.其下对每个已知的人,分别以其名字,建立一个文件夹.
    :param model_save_path: (optional)
    :param n_neighbors:
    有默认值.
    :param knn_algo: (optional) 支持KNN的数据结构.
    :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 = fr.load_image_file(img_path)
            boxes = fr.face_locations(image)

            # 对于当前图片,增加编码到训练集
            X.append(fr.face_encodings(image, known_face_locations=boxes)[0])
            y.append(class_dir)

    # 决定k值for weighting in the KNN classifier
    if n_neighbors is None:
        #n_neighbors = int(round(math.sqrt(len(X))))
        n_neighbors = 3

    # 创建并训练分类器
    knn_clf = neighbors.KNeighborsClassifier(n_neighbors=n_neighbors)
    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)

    return knn_clf
示例#7
0
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
示例#8
0
def train(train_dir,
          model_save_path="",
          n_neighbors=None,
          knn_algo='ball_tree',
          verbose=False):

    X = []
    y = []
    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"))
                continue
            X.append(
                face_recognition.face_encodings(
                    image, known_face_locations=faces_bboxes)[0])
            y.append(class_dir)

    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 knn_clf
示例#9
0
def train(train_dir, n_neighbors=None, knn_algo='ball_tree', verbose=False):

    X = []
    y = []
    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"))
                continue
            X.append(
                face_recognition.face_encodings(
                    image, known_face_locations=faces_bboxes)[0])
            y.append(class_dir)

    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)

    # Dump the trained decision tree classifier with Pickle
    model_save_path = '/home/pi/Desktop/model_classifier.pkl'
    # Open the file to save as pkl file
    with open(model_save_path, 'wb') as model_pkl:
        pickle.dump(knn_clf, model_pkl)
    # Close the pickle instance
    model_pkl.close()
    print("Model created successfully.")
示例#10
0
文件: 05_fr.py 项目: hg08/ai_lecture
def train(train_dir, model_save_path='trained_knn_model.clf', n_neighbors=3,
          knn_algo='ball_tree'):
    """
    训练一个KNN分类器.
    :return: KNN分类器.
    """
    #建立数据集 X,y
    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 = fr.load_image_file(img_path)
            boxes = fr.face_locations(image)

            #对每张照片编码
            X.append(fr.face_encodings(image,known_face_locations=boxes)[0])
            y.append(class_dir)

    #决定k
    if n_neighbors is None:
        n_neighbors = 3


    #训练出分类器
    knn_clf = neighbors.KNeighborsClassifier(n_neighbors=n_neighbors)
    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)


    return knn_clf
示例#11
0
    def train(self, n_neighbors = None, knn_algo = 'ball_tree', 
            image_face_only = False, sample_size = 200, image_store = 'pickled_images.model', 
            load_saved_images = False, delete_missing_files = False, save_steps = 500, update_training_data = False):
        
        X = []
        y = []
        traversed_images = []
        train_dir = self._training_dir
        model_save_path = self._model_path
        image_data_save_steps = save_steps

        # Load pickled training data. Storing pickled training data save time by loading data instead of retraining classifier.
        if load_saved_images:
            # If pickled file exists then load training data
            if path.exists(image_store):
                with open(image_store, 'rb') as f:
                    X, y, traversed_images = pickle.load(f)
            else:
                self.exit_program("Image store not found...", 1)
        else:
            # If pickled data exists and load_saved_images = False then the pickled file will be overwitten. 
            if path.exists(image_store):
                self.exit_program("Please remove the existing image store and run the program again. Check in place to prevent accidental image store overwrites", 1)

        if update_training_data:
            for class_dir in listdir(train_dir):
                print("Training for %s" % class_dir)            
                if not isdir(join(train_dir, class_dir)):
                    continue

                image_list = image_files_in_folder(join(train_dir, class_dir))
                pre_loaded_image_data = [ [index,image] for index,image in enumerate(traversed_images) if y[index] == class_dir ]
                if len(pre_loaded_image_data) != len(image_list):
                    if load_saved_images and delete_missing_files:    
                        shifter = 0
                        image_name_list = [ path.split(name)[1] for name in image_list ]
                        for data in pre_loaded_image_data:
                            if data[1] not in image_name_list:
                                print("Image Not Found : %s at index %d" % (data[1],data[0]))
                                print("Deleting from image store...")
                                traversed_images.pop(data[0] - shifter)
                                X.pop(data[0] - shifter)
                                y.pop(data[0] - shifter)
                                shifter = shifter + 1               
                    
                        self.save_image_store(X, y, traversed_images, image_store)
                        
                    for img_path in image_list:
                        try:
                            image_name = path.split(img_path)[1]
                            if image_name in traversed_images:
                                continue
                            image = face_recognition.load_image_file(img_path)
                            if not image_face_only:
                                faces_bboxes = face_locations(image)
                                if len(faces_bboxes) != 1:
                                    remove(img_path)
                                    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"))
                                    continue

                                X.append(face_recognition.face_encodings(image, known_face_locations=faces_bboxes)[0])
                            
                            else:
                                face_box = image.shape
                                box = [( 0, face_box[0], face_box[1], 0 )]
                                X.append(face_recognition.face_encodings(image, known_face_locations = box )[0])

                            y.append(class_dir)
                            traversed_images.append(image_name)
                            if image_data_save_steps == 0:
                                self.save_image_store(X, y, traversed_images, image_store)
                                image_data_save_steps = save_steps
                            else:
                                image_data_save_steps = image_data_save_steps - 1

                        except Exception:
                            continue
                
        if n_neighbors is None:
            n_neighbors = int(round(sqrt(len(X))))
            print("Chose n_neighbors automatically as:", n_neighbors)

        knn_clf = neighbors.KNeighborsClassifier(n_neighbors=n_neighbors, algorithm=knn_algo, weights='distance')

        self.save_image_store(X, y, traversed_images, image_store)
        
        
        knn_clf.fit(X, y)
        
        if model_save_path != "":
            with open(model_save_path, 'wb') as f:
                pickle.dump(knn_clf, f)
        
        return knn_clf
示例#12
0
def train(train_dir,
          model_save_path='trained_knn_model.clf',
          n_neighbors=3,
          knn_algo='ball_tree',
          verbose=False):
    """
    训练一个用于人脸识别的k-近邻分类器.
    :参数 train_dir: 在训练文件夹中,对于每一个已知名字的人,包含一个子目录.目录名为其名字.
     (训练文件夹的结构示例:树结构)
        <训练文件夹>/
        ├── <姓名1>/
        │   ├── <照片1>.jpeg
        │   ├── <照片2>.jpeg
        │   ├── ...
        ├── <姓名2>/
        │   ├── <照片1>.jpeg
        │   └── <照片2>.jpeg
        └── ...
    :参数 model_save_path: (可选)保存的模型在硬盘上的路径
    :参数 n_neighbors: (可选)分类器中邻居的数目. 如果不制定,则自动选择
    :参数 knn_algo: (可选) 支持knn算法的底层数据结构. 默认为ball_tree
    :参数 verbose: verbosity of training
    :返回值: 根据已知数据返回一个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 是加载成numpy数组的图片,本质是一个numpy数组
            image = face_recognition.load_image_file(img_path)
            face_bounding_boxes = face_recognition.face_locations(image)

            print('face_bounding_boxes:{}'.format(face_bounding_boxes))
            if len(face_bounding_boxes) != 1:
                # 如果当前训练照片中包含有多个人脸或者没有人脸,那么程序会跳过该照片
                if verbose:
                    print("照片{} 不适合用于训练: {}".format(
                        img_path,
                        "未找到人脸" if len(face_bounding_boxes) < 1 else "发现多张人脸"))
            else:
                # 对当前照片,增加对人脸的编码to the training set
                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)

    #保存训练所得的KNN分类器
    if model_save_path is not None:
        with open(model_save_path, 'wb') as f:
            pickle.dump(knn_clf, f)

    return knn_clf
示例#13
0
def train(train_dir,
          model_save_path="",
          n_neighbors=None,
          knn_algo='ball_tree',
          verbose=False,
          load_model=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 of 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 = []
    if verbose:
        print("Training")
    for class_dir in listdir(train_dir):
        if verbose:
            print("Finding Faces in %s" % class_dir)
        if not isdir(join(train_dir, class_dir)):
            continue
        for img_path in image_files_in_folder(join(train_dir, class_dir)):
            new_img = img_path.replace(train_dir, FACE_LOC)
            print("Face Extracted")
            folder_path, renamed_file = path.split(new_img)
            full_path, folder_name = path.split(folder_path)
            renamed_file = folder_name.lower().replace(
                ' ', '_') + '_' + renamed_file
            new_img = join(folder_path, renamed_file)
            print(new_img)
            if path.exists(new_img):
                continue

            try:
                image = face_recognition.load_image_file(img_path)
                faces_bboxes = face_locations(image)
            except Exception:
                continue

            if len(faces_bboxes) != 1:
                remove(img_path)
                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"))
                continue

            folder, file_name = path.split(new_img)
            if not path.exists(folder):
                makedirs(folder)

            b = faces_bboxes[0]
            cv2.imwrite(new_img, (image[b[0]:b[2], b[3]:b[1]]))
            X.append(
                face_recognition.face_encodings(
                    image, known_face_locations=faces_bboxes)[0])
            #X.append(face_recognition.face_encodings(image, known_face_locations=[ (0, 0, image.shape[0], image.shape[1] )] )[0] )
            y.append(class_dir)

    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')

    if verbose:
        print("Fitting data...")
    knn_clf.fit(X, y)
    if verbose:
        print("Fitting completed...")

    if model_save_path != "":
        with open(model_save_path, 'wb') as f:
            pickle.dump(knn_clf, f)
    return knn_clf
示例#14
0
from face_recognition.cli import image_files_in_folder
import cv2
import sys
import time

train_data_path = sys.argv[1]
model_save_path = sys.argv[2]
knn_algo = 'ball_tree'

X = []
y = []

for class_dir in listdir(train_data_path):
    if not isdir(join(train_data_path, class_dir)):
        continue
    for img_path in image_files_in_folder(join(train_data_path, class_dir)):
        image = face_recognition.load_image_file(img_path)
        faces_bboxes = face_locations(image)
        if len(faces_bboxes) != 1:
            continue
        X.append(
            face_recognition.face_encodings(
                image, known_face_locations=faces_bboxes)[0])
        y.append(class_dir)

    n_neighbors = int(round(sqrt(len(X))))

    knn_clf = neighbors.KNeighborsClassifier(n_neighbors=n_neighbors,
                                             algorithm=knn_algo,
                                             weights='distance')
    knn_clf.fit(X, y)