[英]class PCA matplotlib for face recognition
我正在尝试使用python通过主成分分析(PCA)进行人脸识别。 我正在使用在matplotlib
找到的pca
类。 这是文档:
matplotlib.mlab.PCA(a)类计算a的SVD并存储PCA的数据。 使用项目将数据投影到一组缩小的维度上
Inputs: a: a numobservations x numdims array Attrs: aa centered unit sigma version of input a numrows, numcols: the dimensions of a mu : a numdims array of means of a sigma : a numdims array of atandard deviation of a fracs : the proportion of variance of each of the principal components Wt : the weight vector for projecting a numdims point or array into PCA space Y : a projected into PCA space
因子负载在Wt因子中,即第一主成分的因子负载由Wt [0]给出
这是我的代码:
import os
from PIL import Image
import numpy as np
import glob
import numpy.linalg as linalg
from matplotlib.mlab import PCA
#Step 1: put database images into a 2D array
filenames = glob.glob('C:\\Users\\Karim\\Downloads\\att_faces\\New folder/*.pgm')
filenames.sort()
img = [Image.open(fn).convert('L').resize((90, 90)) for fn in filenames]
images = np.asarray([np.array(im).flatten() for im in img])
#Step 2: database PCA
results = PCA(images.T)
w = results.Wt
#Step 3: input image
input_image = Image.open('C:\\Users\\Karim\\Downloads\\att_faces\\1.pgm').convert('L')
input_image = np.asarray(input_image)
#Step 4: input image PCA
results_in = PCA(input_image)
w_in = results_in.Wt
#Step 5: Euclidean distance
d = np.sqrt(np.sum(np.asarray(w - w_in)**2, axis=1))
但我收到一个错误:
Traceback (most recent call last):
File "C:/Users/Karim/Desktop/Bachelor 2/New folder/matplotlib_pca.py", line 32, in <module>
d = np.sqrt(np.sum(np.asarray(w - w_in)**2, axis=1))
ValueError: operands could not be broadcast together with shapes (30,30) (92,92)
错误告诉您两个数组( w
和w_in
)的大小不相同,并且numpy
无法弄清楚如何广播值w_in
差。
我不熟悉此功能,但我想问题的根源是您输入的图像大小不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.