如何解决用于 Python 人脸识别的 PCA
我从 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)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。