首页 / PYTHON / 人脸识别 – Python
人脸识别 – Python
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了人脸识别 – Python,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2130字,纯文字阅读大概需要4分钟。
内容图文
我正在尝试使用主要组件分析(PCA)使用python进行人脸识别.
现在我能够获得训练图像图像和输入图像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
我现在的问题是我想要返回最小欧氏距离的图像(或其在数组图像中的索引)而不是距离d的索引中的索引
解决方法:
我不相信你已经修改了图像存储在w中的顺序与图像相比,因此,np.argmin(d)中的idx应该是图像列表的相同索引,所以
images[idx]
应该是你想要的形象.
当然,
images[idx].shape
会给(1800,),因为它仍然是扁平的.如果你想取消它,你可以这样做:
images[idx].reshape(90,90)
内容总结
以上是互联网集市为您收集整理的人脸识别 – Python全部内容,希望文章能够帮你解决人脸识别 – Python所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。