4
으로 얼굴 인식을 시도하고 있습니다. 주성분 분석 (PCA)에서 python을 사용하고 있습니다.얼굴 인식 - Python
이제 교육용 이미지 images
과 입력 이미지 input_image
사이의 최소 유클리드 거리를 얻을 수 있습니다. 여기 내 코드입니다 :
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
내 문제는 지금이 나는 최소 유클리드 거리 거리의 배열하지의 인덱스 (배열 images
또는 인덱스) 이미지를 반환 할 d
나는 그것이 사실이라고 생각하지 않습니다. 'images '는 30 개의 이미지 (3 개의 얼굴, 각각 10 개의 이미지)로 구성되기 때문에. 'd'는 20 개의 거리로 구성되어 있으므로'idx'의 최대 값은 20이므로 테스트 이미지'input_images'에 세 번째 이미지가 있으면 (출력은 21-30 사이 여야합니다) 올바른 결과를 얻지 못할 것입니다. – user2229953
제가 사용하는 구성 데이터에서 볼 수 있습니다 .-P – askewchan