0

Pythonを使って主成分分析(PCA)による顔認識をしようとしています。にあるクラスを使用pcaしていますmatplotlib。ドキュメントは次のとおりです。

class matplotlib.mlab.PCA(a) a の SVD を計算し、PCA のデータを保存します。project を使用して、縮小された一連の次元にデータを射影します

Inputs:
a: a numobservations x numdims array
Attrs:
a a 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 因子で表されます。つまり、第 1 主成分の因子負荷量は 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)
  1. 誰でもエラーを修正するのを手伝ってもらえますか?
  2. それが顔認識の正しい方法ですか?
4

1 に答える 1

0

このエラーは、2 つの配列 (ww_in) が同じサイズでnumpyはなく、値をブロードキャストして差を取る方法がわからないことを示しています。

私はこの機能に慣れていませんが、問題の原因は入力画像のサイズが異なることだと思います。

于 2013-04-16T19:08:22.567 に答える