人脸识别-Python

2024-01-07

我正在尝试通过以下方式进行人脸识别主成分分析(PCA)使用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


我不相信您已经修改了图像存储的顺序w与在images,因此,idx from np.argmin(d)应该是相同的索引images列出,所以

images[idx]

应该是你想要的图像。

当然,

images[idx].shape

会给(1800,)因为它仍然是扁平的。如果你想把它展平,你可以这样做:

images[idx].reshape(90,90)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

人脸识别-Python 的相关文章

随机推荐

  • 使用 LINQ 从数据集中选择行,其中 RowsID 列表位于 List

    首先我必须说 我是一个使用 LINQ 的新手 实际上我以前从未使用过 但我有一个任务需要使用来自列表的值来过滤数据表 所以我想知道是否可以在 LINQ 中使用列表中的值作为过滤器值来查询数据表 有人可以给我一些提示 谢谢 执行此操作的最佳方
  • Flutter 图像选择器无法从相册/图库中选择图像

    我正在尝试在我的应用程序中实现 imagepicker 库 我的应用程序运行没有任何错误 但我无法从相册 图库中选取 选择图像 选择图像后 应用程序应在正文中显示图像 但没有任何反应 我必须点击 取消 才能返回主屏幕 我还应该提到控制台返回
  • JavaScript 图像调整大小

    有谁知道如何使用 JavaScript 按比例调整图像大小 我尝试通过添加属性来修改 DOMheight and width即时运行 但似乎在 IE6 上不起作用 要按比例修改图像 只需更改宽度 高度 CSS 属性之一 将另一个设置为自动
  • 从 Windows 命令行获取文件夹大小

    在 Windows 中 是否可以在不使用任何第三方工具的情况下从命令行获取文件夹的大小 我想要与右键单击 Windows 资源管理器 属性中的文件夹时获得的结果相同的结果 有一个内置 Windows 工具为了那个原因 dir s Folde
  • 如何避免锯齿并提高性能?

    In this 堆栈溢出答案 https stackoverflow com a 9709724 2345484事实证明 C 中的别名会降低代码速度 C 中的别名不仅适用于指针 还适用于引用 更普遍地适用于标准指定的这些类型 https s
  • ajax POST 后无法访问 JQuery $(this)?

    假设我有一堆共享点击事件的链接 a href class do stuff Click me a a href class do stuff Click me a a href class do stuff Click me a a hre
  • 使用 Utf8Json 库序列化时排除空字段?

    使用以下方法将 POCO 序列化为 JSON 字符串时 有什么方法可以忽略空字段Utf8Json图书馆 我有一个ToString我在外部使用的下面的类中的方法 所以我想看看是否有任何方法可以在进行序列化时排除空字段 基本上我不希望序列化后
  • 禁用 gcc 对不兼容选项的警告

    我很好奇是否有一个选项可以禁用 gcc 关于参数对正在编译的语言无效的警告 Ex cc1 warning command line option Wno deprecated is valid for C Java ObjC but not
  • 如何在显示 jQuery ui 选项卡时插入 JavaScript?

    我想在我的页面上运行 JavaScript 函数 但仅限于显示选项卡时 其实有两个问题 我没有使用 ajax 选项卡 那么如果我只想在显示选项卡后运行代码 我会将 JavaScript 放入 加载 或 显示 回调中吗 根据上面的答案 我的代
  • IE 和 Firefox 中的文本大小不同

    为什么 IE 中的文本比 Firefox 中的文本稍大 宽 示例 顶部是 IE 渲染文本的方式 底部是 FF Image http www dreamscape hu text jpg 在 CSS 中声明为 Arial 16px 的文本 我
  • 如何将 Xamarin Forms Shell 集成到 MvvmCross 设置中

    我打算将 Xamarin Forms 与 MvvmCross 一起使用 根据他们的tutorial https www mvvmcross com documentation tutorials tipcalc a xamarin form
  • 如何接入Kubernetes部署

    我已经创建了 Docker 映像 并部署在具有最少数量机器的 k8s 集群中 设置了一台主机和一台工作机 两台机器都已启动并运行 并使用相同的 VLAN 网络相互通信 请查找以下 Pod 和部署服务以及所描述的状态 root jenkins
  • 如何配置 protobuf-net 的 RuntimeModel.Default 以支持序列化/反序列化 SessionSecurityToken?

    BinaryFormatter 能够简单地处理序列化 private byte TokenToBytes SessionSecurityToken token if token null return null using var memo
  • XML 格式中的换行符?

    在 XML 中编辑字符串时 我需要添加换行符 我想问一下 为 android 编程时 正确的形式是什么 因为 br 有效 但 ECLIPSE 将该区域标记为有问题 如果我查看建议 Eclipse 会告诉我我应该添加一个结束标签 如果我添加换
  • Blazor 作用域 CSS 未在 Azure Pipelines 上构建

    我正在开发一个使用作用域 CSS 的 Blazor 应用程序 但是当我尝试使用 Azure Pipelines 构建它时 不会生成作用域 CSS 文件 我在一台可以通过 RDP 访问的本地计算机上构建 我看到wwwroot assembly
  • 从 mach_timebase_info() 创建结构体

    在 C 中创建时基信息结构很容易 但在 Swift 中 以下内容在 Playground 中不起作用 let timebaseInfo mach timebase info data t mach timebase info timebas
  • 使用 pandas 和 scipy 绘制树状图

    我希望使用相关性生成树状图pandas and scipy 我使用数据集 作为DataFrame 由返回组成 其大小为n x m where n是日期的数量 m公司数量 然后我只需运行脚本 import pandas as pd impor
  • 如何让 ediff + hg 在 emacs 23.2 for mac 中工作?

    我使用 emacs 二进制文件http emacsformacosx com http emacsformacosx com 当我在 Mercurial 源目录中编辑文件并运行 ediff revision 时 我得到 File the f
  • 错误:预期未定义为 GraphQL 模式

    我收到一条错误 内容为 错误 预期未定义为 GraphQL 架构 请检查一下这是什么问题 当我移动到 localhost 3000 graphiql 时 它显示上述错误 也许我犯了一些错误 请任何人检查并帮助我 如果可能的话 我的服务器 j
  • 人脸识别-Python

    我正在尝试通过以下方式进行人脸识别主成分分析 PCA 使用Python 现在我能够获得训练图像之间的最小欧几里德距离images和输入图像input image 这是我的代码 import os from PIL import Image