파이썬을 사용하여 PCA (Principal Component Analysis)로 얼굴 인식을 구현하려고합니다. 나는이 튜토리얼의 단계를 수행하고있다 :오류 - 파이썬에서 PCA의 유클리드 거리를 계산합니다.
import os
from PIL import Image
import numpy as np
import glob
import numpy.linalg as linalg
#Step1: 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: find the mean image and the mean-shifted input images
mean_image = images.mean(axis=0)
shifted_images = images - mean_image
#Step 3: Covariance
c = np.cov(shifted_images)
#Step 4: Sorted eigenvalues and eigenvectors
eigenvalues,eigenvectors = linalg.eig(c)
idx = np.argsort(-eigenvalues)
eigenvalues = eigenvalues[idx]
eigenvectors = eigenvectors[:, idx]
#Step 5: Only keep the top 'num_eigenfaces' eigenvectors
num_components = 20
eigenvalues = eigenvalues[0:num_components].copy()
eigenvectors = eigenvectors[:, 0:num_components].copy()
#Step 6: Finding weights
w = eigenvectors.T * np.asmatrix(shifted_images)
#Step 7: Input image
input_image = Image.open('C:\\Users\\Karim\\Downloads\\att_faces\\1.pgm').convert('L').resize((90, 90))
input_image = np.asarray(input_image).flatten()
#Step 8: get the normalized image, covariance, eigenvalues and eigenvectors for input image
shifted_in = input_image - mean_image
c = np.cov(input_image)
cmat = c.reshape(1,1)
eigenvalues_in, eigenvectors_in = linalg.eig(cmat)
#Step 9: Fing weights of input image
w_in = eigenvectors_in.T * np.asmatrix(shifted_in)
print w_in
print w_in.shape
#Step 10: Euclidean distance
d = np.sqrt(np.sum((w - w_in)**2))
idx = np.argmin(d)
match = images[idx]
나는이 오류를 얻고 나는 10 단계에서 문제가 참입니다 : 이 Traceback (most recent call last): File "C:/Users/Karim/Desktop/Bachelor 2/New folder/new3.py", line 59, in <module> d = np.sqrt(np.sum((w - w_in)**2)) File "C:\Python27\lib\site-packages\numpy\matrixlib\defmatrix.py", line 343, in __pow__ return matrix_power(self, other) File "C:\Python27\lib\site-packages\numpy\matrixlib\defmatrix.py", line 160, in matrix_power raise ValueError("input must be a square array") ValueError: input must be a square array
누구든지 도움이 될 수 있습니다 여기에 http://onionesquereality.wordpress.com/2009/02/11/face-recognition-using-eigenfaces-and-distance-classifiers-a-tutorial/
은 내 코드입니다 ?? 당신이 (w - w_in)
을 갈 때
1x1 공분산 행렬에서 고유 점을 취하기로 결정한 것을 알았습니다. 아마 당신이하고 싶은 것을 확실히해야합니다. 공분산은 두 개 이상의 데이터 세트가 얼마나 상관 관계가 있는지를 찾는 것이고, 트레이닝 이미지를 실행하면 상호 연관성이 얼마나 좋은지 알게됩니다. 입력 이미지에서이를 실행할 때 자기 상관 값을 얻고 있습니다. 자기 상관 값은 원하는 것과 다를 수 있습니다. 튜토리얼을 더 자세히 보면서 그것에 대해 더 많이 생각해야하지만, 이전의 대답으로 당신을 오도하지 않도록 당신에게 경고하고 싶었습니다. – askewchan
@askewchan 귀하의 조언에 감사드립니다. 'matplotlib '에 내장 된'PCA' 클래스를 사용하여 제 직업을 얻을 수 있을지 궁금합니다. 어떻게 작동하는지 알고 있습니까? 그래서 아마 매트릭스'하지 각각의 요소 - – user2229953
아니, 불행하게도 내가 가진 경험이 없지만, 당신이 그것을 배우려고한다면, 당신은 항상 여기에 질문을 게시 할 수 있습니다 :) 내가 광장 싶은 – askewchan