我正在尝试使用python(matplotlib)通过主成分分析(PCA)进行人脸识别。我正在尝试按照此图片中的说明进行操作:
这是我的代码:
import os
from PIL import Image
import numpy as np
import glob
from matplotlib.mlab import PCA
#Step1: put database images into a 3D array
filenames = glob.glob('C:\\Users\\Karim\\Downloads\\att_faces\\New folder/*.pgm')
filenames.sort()
img = [Image.open(fn).convert('L') for fn in filenames]
images = np.dstack([np.array(im) for im in img])
# Step2: create 2D flattened version of 3D input array
d1,d2,d3 = images.shape
b = np.zeros([d1,d2*d3])
for i in range(len(images)):
b[i] = images[i].flatten()
#Step 3: database PCA
results = PCA(b.T)
x = results.Wt
#Step 4: input image
input_image = Image.open('C:\\Users\\Karim\\Downloads\\att_faces\\1.pgm').convert('L')
input_image = np.array(input_image)
input_image = input_image.flatten()
#Step 5: input PCA
in_results = PCA(input_image.T)
y = in_results.Wt
#Step 6: get shortest distance
但我说错了in_results = PCA(input_image.T)
:
Traceback (most recent call last):
File "C:\Users\Karim\Desktop\Bachelor 2\New folder\new2.py", line 29, in <module>
in_results = PCA(input_image.T)
File "C:\Python27\lib\site-packages\matplotlib\mlab.py", line 846, in __init__
n, m = a.shape
ValueError: need more than 1 value to unpack
有人可以帮忙吗??