python - Python中用于人脸识别的PCA
问题描述
我从 2 个人的 2 个视频中拍摄了 130 帧。我想用 PCA 库和 MLP 测试人脸识别,但我不明白我应该如何形成数据 X 和数据 y。据我所知,X 必须是图像,y 必须是标签,因此第 1 面的 130 乘以 1,第 2 面的 130 乘以 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)
解决方案
推荐阅读
- azure - Azure 的认知服务文本分析 API 中的事务是什么
- rest - 功能请求 ActiveCollab - 实时(可过滤)项目时间日志
- java - 如何在Java中从Excel工作表(XSSF)中删除图像
- python - python爬虫ieee论文关键字
- laravel - 将数据从刀片传递到 vue 组件对黑客来说是安全的吗?
- vb.net - 将值保存到字符串集合中的设置
- c# - c#实体框架生成类排序子列表
- r - R中对数正态函数的二阶导数?
- jupyter-notebook - 为什么 Binder 无法识别 requirements.txt 文件?
- docker - 带有 docker 端口映射的 iptables