我正在尝试使用 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 张相似的图像。
有人可以帮忙吗?
最佳答案
原始图像位图上的 PCA 是一种较差的人脸识别算法。坦率地说,不要指望它能够真正使用人脸的真实图像来工作。它作为学习工具很有用,但仅此而已。
尝试用极其简单的图像来测试你的算法——想想不同地方有黑色形状的白色图像。 PCA 应该能够很好地做到这一点。如果它适用于这些,恭喜你,你写得正确。然后转向更复杂的算法。
或者下载标准的人脸图像学术数据集,该数据集已在研究中显示可与 PCA 配合使用。对于如此简单的算法,对齐和颜色等小问题至关重要。
关于python - 手势识别 (PCA) - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16115068/