用于 Python 人脸识别的 PCA

时间:2021-06-13 10:05:29

标签: python opencv pca mlp

我从 2 个人的 2 个视频中拍摄了 130 帧。我想用 PCA 库和 MLP 测试人脸识别,但我不明白我应该如何形成数据 X 和数据 y。据我所知,X 必须是图像,y 必须是标签,所以 130 乘以 1 表示人脸 1,130 次乘以 2 表示人脸 2。

    images = [cv2.imread(file) for file in glob.glob("frame*.jpg")]
    images2 = [cv2.imread(file) for file in glob.glob("2frame*.jpg")]

    temp = asarray(images)
    temp2 = asarray(images2)
    X = [[temp], [temp2]]

    y = [[1]*130, [2]*130]
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)

    n_components = 3
    pca = PCA(n_components=n_components, whiten=True).fit(X_train)

    # apply PCA transformation
    X_train_pca = pca.transform(X_train)
    X_test_pca = pca.transform(X_test)

    # train a neural network
    print("Fitting the classifier to the training set")
    clf = MLPClassifier(hidden_layer_sizes=(1024,), batch_size=256, verbose=True, early_stopping=True).fit(X_train_pca,
                                                                                                           y_train)
    y_pred = clf.predict(X_test_pca)
    print(classification_report(y_test, y_pred))

这是我得到的错误:

ValueError: Found array with dim 6. Estimator expected <= 2.

这行发生了什么:

pca = PCA(n_components=n_components, whiten=True).fit(X_train)

0 个答案:

没有答案