def getImagesWithID(path):

        imagePaths, labels, labels_dict = facenet.get_image_paths_and_labels(
            dataset)
        #print(imagePaths) 路徑
        #print(labels) 數量
        #print(labels_dict) ids

        faces = []
        Ids = []

        for imagePath in imagePaths:
            # 開啟圖像轉換成numpy
            faceImg = Image.open(imagePath).convert('L')  # 轉灰階
            # PIL圖像轉換為numpy
            faceNp = np.array(faceImg, 'uint8')

            # 從圖片名稱中獲取使用者ID
            ID = int(os.path.split(imagePath)[-1].split('.')[1])

            # iamge
            faces.append(faceNp)
            # ids
            Ids.append(ID)

            #print ID
            cv2.imshow("training", faceNp)
            cv2.waitKey(10)
        return np.array(Ids), np.array(faces)  # 回傳ids faces
def getImagesWithID(DATA_PATH):

    #-------------載入相關模型--------------#
    dataset = facenet.get_dataset(DATA_PATH)

    imagePaths, labels, labels_dict = facenet.get_image_paths_and_labels(
        dataset)
    #print(imagePaths) 路徑
    #print(labels) 數量
    #print(labels_dict) ids

    faces = []
    Ids = []

    for imagePath in imagePaths:
        # 開啟圖像轉換成numpy
        faceImg = Image.open(imagePath).convert('L')  # 轉灰階
        # PIL圖像轉換為numpy
        faceNp = np.array(faceImg, 'uint8')

        # 從圖片名稱中獲取使用者ID
        ID = int(os.path.split(imagePath)[-1].split('.')[1])

        # iamge
        faces.append(faceNp)
        # ids
        Ids.append(ID)

        #print ID
    return np.array(Ids), np.array(faces)  # 回傳ids faces
Exemple #3
0
def load_MTCNN(DATA_PATH):
    with tf.Graph().as_default():
        with tf.Session() as sess:

            #  DATA_PATH = os.path.join(BASE_DIR, "data") #data目錄

            IMG_OUT_PATH = os.path.join(DATA_PATH, "dataset")  #image目錄

            #  FACENET_DATA_PATH = os.path.join(DATA_PATH, "facenet","20180402-114759","20180402-114759.pb") #dacenet路徑

            datadir = IMG_OUT_PATH  # 經過偵測、對齊 & 裁剪後的人臉圖像目錄

            dataset = facenet.get_dataset(
                datadir)  # 取得人臉類別(ImageClass)的列表與圖像路徑

            paths, labels, labels_dict = facenet.get_image_paths_and_labels(
                dataset)  #取得每個人臉的圖像路徑跟ID標籤
            #print (paths) #test
            #print (labels) #test
            #print (labels_dict) #test
            print('Origin: Number of classes: %d' % len(labels_dict))  #人臉種類
            print('Origin: Number of images: %d' % len(paths))  #人臉總數

            #------------載入Facenet模型------------#
            modeldir = '/content/gdrive/My Drive/Colab Notebooks/facenet/20180402-114759/20180402-114759.pb'
            facenet.load_model(modeldir)

            images_placeholder = tf.get_default_graph().get_tensor_by_name(
                "input:0")
            embeddings = tf.get_default_graph().get_tensor_by_name(
                "embeddings:0")
            phase_train_placeholder = tf.get_default_graph(
            ).get_tensor_by_name("phase_train:0")
            embedding_size = embeddings.get_shape()[1]

            #------------計算人臉特徵向量------------#
            batch_size = 5  # 一次輸入的樣本數量
            image_size = 140  # 要做為Facenet的圖像輸入的大小
            times_pohto = 10.0  # 每張照片看的次數
            nrof_images = len(paths)  # 總共要處理的人臉圖像
            # 計算總共要跑的批次數
            nrof_batches_per_epoch = int(
                math.ceil(times_pohto * nrof_images / batch_size))
            # 構建一個變數來保存"人臉特徵向量"
            emb_array = np.zeros(
                (nrof_images, embedding_size))  # <-- Face Embedding

            for i in tqdm(range(nrof_batches_per_epoch)):  # 實際訓練 facenet
                start_index = i * batch_size
                end_index = min((i + 1) * batch_size, nrof_images)
                paths_batch = paths[start_index:end_index]
                images = facenet.load_data(paths_batch, False, False,
                                           image_size)
                feed_dict = {
                    images_placeholder: images,
                    phase_train_placeholder: False
                }
                emb_array[start_index:end_index, :] = sess.run(
                    embeddings, feed_dict=feed_dict)

            return emb_array, labels, labels_dict
def trainer_photo(request):
    DATA_PATH = os.path.join(BASE_DIR, "data")  #data目錄

    FACENET_DATA_PATH = os.path.join(DATA_PATH, "facenet", "20180402-114759",
                                     "20180402-114759.pb")  #dacenet路徑

    SVM_DATA_PATH = os.path.join(DATA_PATH, "serializer",
                                 "lfw_svm_classifier.pkl")  #svm路徑

    IMG_OUT_PATH = os.path.join(DATA_PATH, "dataset")  #裁切後人臉目錄

    #----------載入MTCNN-----------#
    with tf.Graph().as_default():
        with tf.Session() as sess:
            datadir = IMG_OUT_PATH  # 經過偵測、對齊 & 裁剪後的人臉圖像目錄

            dataset = facenet.get_dataset(
                datadir)  # 取得人臉類別(ImageClass)的列表與圖像路徑

            paths, labels, labels_dict = facenet.get_image_paths_and_labels(
                dataset)  #取得每個人臉的圖像路徑跟ID標籤
            #print (paths) #test
            #print (labels) #test
            #print (labels_dict) #test
            print('Origin: Number of classes: %d' % len(labels_dict))  #人臉種類
            print('Origin: Number of images: %d' % len(paths))  #人臉總數

            #------------載入Facenet模型------------#
            modeldir = FACENET_DATA_PATH
            facenet.load_model(modeldir)

            images_placeholder = tf.get_default_graph().get_tensor_by_name(
                "input:0")
            embeddings = tf.get_default_graph().get_tensor_by_name(
                "embeddings:0")
            phase_train_placeholder = tf.get_default_graph(
            ).get_tensor_by_name("phase_train:0")
            embedding_size = embeddings.get_shape()[1]

            #------------計算人臉特徵向量------------#
            batch_size = 5  # 一次輸入的樣本數量
            image_size = 140  # 要做為Facenet的圖像輸入的大小
            times_pohto = 10.0  # 每張照片看的次數
            nrof_images = len(paths)  # 總共要處理的人臉圖像
            # 計算總共要跑的批次數
            nrof_batches_per_epoch = int(
                math.ceil(times_pohto * nrof_images / batch_size))
            # 構建一個變數來保存"人臉特徵向量"
            emb_array = np.zeros(
                (nrof_images, embedding_size))  # <-- Face Embedding

            for i in tqdm(range(nrof_batches_per_epoch)):  # 實際訓練 facenet
                start_index = i * batch_size
                end_index = min((i + 1) * batch_size, nrof_images)
                paths_batch = paths[start_index:end_index]
                images = facenet.load_data(paths_batch, False, False,
                                           image_size)
                feed_dict = {
                    images_placeholder: images,
                    phase_train_placeholder: False
                }
                emb_array[start_index:end_index, :] = sess.run(
                    embeddings, feed_dict=feed_dict)

    #--------------保存facenet.pkl-------------#
    # 人臉特徵
    emb_features_file = open(
        os.path.join(DATA_PATH + '/recognizer', 'lfw_emb_features.pkl'), 'wb')
    pickle.dump(emb_array, emb_features_file)
    emb_features_file.close()

    # 矩陣
    emb_lables_file = open(
        os.path.join(DATA_PATH + '/recognizer', 'lfw_emb_labels.pkl'), 'wb')
    pickle.dump(labels, emb_lables_file)
    emb_lables_file.close()

    # user_ids
    emb_lables_dict_file = open(
        os.path.join(DATA_PATH + '/recognizer', 'lfw_emb_labels_dict.pkl'),
        'wb')
    pickle.dump(labels_dict, emb_lables_dict_file)
    emb_lables_dict_file.close()

    #------------------載入pkl------------------#

    # 人臉特徵
    with open(os.path.join(DATA_PATH + '/recognizer', 'lfw_emb_features.pkl'),
              'rb') as emb_features_file:
        emb_features = pickle.load(emb_features_file)

    # 每一張的人臉標籤
    with open(os.path.join(DATA_PATH + '/recognizer', 'lfw_emb_labels.pkl'),
              'rb') as emb_lables_file:
        emb_labels = pickle.load(emb_lables_file)

    # 總共的人臉標籤種類
    with open(
            os.path.join(DATA_PATH + '/recognizer', 'lfw_emb_labels_dict.pkl'),
            'rb') as emb_lables_dict_file:
        emb_labels_dict = pickle.load(emb_lables_dict_file)

    #-------------------測試--------------------#

    print("人臉特徵數量: {}, shape: {}, type: {}".format(len(emb_features),
                                                   emb_features.shape,
                                                   type(emb_features)))
    print("人臉標籤數量: {}, type: {}".format(len(emb_labels), type(emb_labels)))
    print("人臉標籤種類: {}, type: {}", len(emb_labels_dict), type(emb_labels_dict))

    #-------------------準備相關變數-----------------#

    # 訓練/測試變數
    X_train = []
    y_train = []
    X_test = []
    y_test = []

    # 保存己經有處理過的人臉label
    processed = set()

    # 分割訓練資料集與驗證資料集
    for (emb_feature, emb_label) in zip(emb_features, emb_labels):
        if emb_label in processed:
            X_train.append(emb_feature)
            y_train.append(emb_label)
        else:
            X_test.append(emb_feature)
            y_test.append(emb_label)
            processed.add(emb_label)

    # 結果
    print('X_train: {}, y_train: {}'.format(len(X_train), len(y_train)))
    print('X_test: {}, y_test: {}'.format(len(X_test), len(y_test)))

    #----------------訓練人臉分類器(SVM Classifier)-----------------#
    #使用scikit-learn的SVM分類器來進行訓練。
    #使用linearSvc來訓練

    # 訓練分類器
    print('Training classifier')
    linearsvc_classifier = LinearSVC(C=1, multi_class='crammer_singer')

    # 進行訓練
    linearsvc_classifier.fit(X_train, y_train)

    classifier_filename = SVM_DATA_PATH

    class_names = []
    for key in sorted(emb_labels_dict.keys()):
        class_names.append(emb_labels_dict[key].replace('_', ' '))

    with open(classifier_filename, 'wb') as outfile:
        pickle.dump((linearsvc_classifier, class_names), outfile)

    print('Saved classifier model to file "%s"' % classifier_filename)

    return redirect('/')