我正在尝试使用 python 通过主成分分析 (PCA) 进行手势识别。我正在按照本教程中的步骤操作:http: //onionesquereality.wordpress.com/2009/02/11/face-recognition-using-eigenfaces-and-distance-classifiers-a-tutorial/
这是我的代码:
import os
from PIL import Image
import numpy as np
import glob
import numpy.linalg as linalg
#Step 1: put training images into a 2D array
filenames = glob.glob('C:\\Users\\Karim\\Desktop\\Training & Test images\\New folder\\Training/*.png')
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.asmatrix(shifted_images) * np.asmatrix(shifted_images.T)
#Step 4: Sorted eigenvalues and eigenvectors
eigenvalues,eigenvectors = linalg.eig(c)
idx = np.argsort(-eigenvalues)
eigenvalues = eigenvalues[idx]
eigenvectors = eigenvectors[:, idx]
#Step 6: Finding weights
w = eigenvectors.T * np.asmatrix(shifted_images)
w = np.asarray(w)
#Step 7: Input (Test) image
input_image = Image.open('C:\\Users\\Karim\\Desktop\\Training & Test images\\New folder\\Test\\31.png').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)
w_in = np.asarray(w_in)
#Step 10: Euclidean distance
df = np.asarray(w - w_in) # the difference between the images
dst = np.sqrt(np.sum(df**2, axis=1)) # their euclidean distances
idx = np.argmin(dst) # index of the smallest value in 'dst' which should be equal to index of the most simillar image in 'images'
print idx
检测到的图像应该是从训练图像到测试图像最近的图像,但结果是完全不同的,尽管对于每个测试图像,训练图像中有 10 个相似的图像。
任何人都可以帮忙吗?