人脸识别 - 的Python [英] Face recognition - Python
问题描述
我想通过的进行人脸识别使用python主成分分析法的(PCA)。
I am trying to make face recognition by Principal Component Analysis (PCA) using python.
现在我能够得到训练图像图片
和输入图像 input_image
之间的最小欧氏距离。这里是我的code:
Now I am able to get the minimum euclidean distance between the training images images
and the input image input_image
. Here is my code:
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\\me\\Downloads\\/*.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.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 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)
# check eigenvectors.T/eigenvectors
#Step 7: Input image
input_image = Image.open('C:\\Users\\me\\Test\\5.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: Find weights of input image
w_in = eigenvectors_in.T * np.asmatrix(shifted_in)
# check eigenvectors/eigenvectors_in
#Step 10: Euclidean distance
d = np.sqrt(np.sum(np.asarray(w - w_in)**2, axis=1))
idx = np.argmin(d)
print idx
我现在的问题是,我要回的图像(或数组索引图片
)的最小欧几里得距离不是它指数距离 D
My problem now is that I want to return the image (or its index in the array images
) with the minimum euclidean distance not its index in the array of distances d
推荐答案
我不相信你已经相比修改的图像存储在是W
的顺序在图片
,因此, IDX
从 np.argmin(D)
应该是图像相同指数
列表,因此
I don't believe that you have modified the order that the images are stored in w
compared to in images
, therefore, the idx
from np.argmin(d)
should be the same index of the images
list, so
images[idx]
应该是你想要的图像。
should be the image you want.
当然,
images[idx].shape
会给(1800)
,因为它仍然夷为平地。如果你想unflatten它,你可以这样做:
will give (1800,)
because it's still flattened. If you want to unflatten it, you can do:
images[idx].reshape(90,90)
这篇关于人脸识别 - 的Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!