人脸识别之特征脸方法
本文采用的数据集为“Labeled Faces in the Wild”, aka LFW: http://vis-www.cs.umass.edu/lfw/lfw-funneled.tgz (233MB)
下面结合Eigenfaces和SVM进行人脸识别:
Python源码:
from __future__ import print_functionfrom time import time
import logging
import matplotlib.pyplot as pltfrom sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
from sklearn.datasets import fetch_lfw_people
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn.decomposition import PCA
from sklearn.svm import SVCprint(__doc__)# Display progress logs on stdout
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(message)s')# #############################################################################
# Download the data, if not already on disk and load it as numpy arrayslfw_people = fetch_lfw_people(min_faces_per_person=70, resize=0.4)# introspect the images arrays to find the shapes (for plotting)
n_samples, h, w = lfw_people.images.shape# for machine learning we use the 2 data directly (as relative pixel
# positions info is ignored by this model)
X = lfw_people.data
n_features = X.shape[1]# the label to predict is the id of the person
y = lfw_people.target
target_names = lfw_people.target_names
n_classes = target_names.shape[0]print("Total dataset size:")
print("n_samples: %d" % n_samples)
print("n_features: %d" % n_features)
print("n_classes: %d" % n_classes)# #############################################################################
# Split into a training set and a test set using a stratified k fold# split into a training and testing set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)# #############################################################################
# Compute a PCA (eigenfaces) on the face dataset (treated as unlabeled
# dataset): unsupervised feature extraction / dimensionality reduction
n_components = 150print("Extracting the top %d eigenfaces from %d faces"% (n_components, X_train.shape[0]))
t0 = time()
pca = PCA(n_components=n_components, svd_solver='randomized',whiten=True).fit(X_train)
print("done in %0.3fs" % (time() - t0))eigenfaces = pca.components_.reshape((n_components, h, w))print("Projecting the input data on the eigenfaces orthonormal basis")
t0 = time()
X_train_pca = pca.transform(X_train)
X_test_pca = pca.transform(X_test)
print("done in %0.3fs" % (time() - t0))# #############################################################################
# Train a SVM classification modelprint("Fitting the classifier to the training set")
t0 = time()
param_grid = {'C': [1e3, 5e3, 1e4, 5e4, 1e5],'gamma': [0.0001, 0.0005, 0.001, 0.005, 0.01, 0.1], }
clf = GridSearchCV(SVC(kernel='rbf', class_weight='balanced'), param_grid)
clf = clf.fit(X_train_pca, y_train)
print("done in %0.3fs" % (time() - t0))
print("Best estimator found by grid search:")
print(clf.best_estimator_)# #############################################################################
# Quantitative evaluation of the model quality on the test setprint("Predicting people's names on the test set")
t0 = time()
y_pred = clf.predict(X_test_pca)
print("done in %0.3fs" % (time() - t0))print(classification_report(y_test, y_pred, target_names=target_names))
print(confusion_matrix(y_test, y_pred, labels=range(n_classes)))# #############################################################################
# Qualitative evaluation of the predictions using matplotlibdef plot_gallery(images, titles, h, w, n_row=3, n_col=4):"""Helper function to plot a gallery of portraits"""plt.figure(figsize=(1.8 * n_col, 2.4 * n_row))plt.subplots_adjust(bottom=0, left=.01, right=.99, top=.90, hspace=.35)for i in range(n_row * n_col):plt.subplot(n_row, n_col, i + 1)plt.imshow(images[i].reshape((h, w)), cmap=plt.cm.gray)plt.title(titles[i], size=12)plt.xticks(())plt.yticks(())# plot the result of the prediction on a portion of the test setdef title(y_pred, y_test, target_names, i):pred_name = target_names[y_pred[i]].rsplit(' ', 1)[-1]true_name = target_names[y_test[i]].rsplit(' ', 1)[-1]return 'predicted: %s\ntrue: %s' % (pred_name, true_name)prediction_titles = [title(y_pred, y_test, target_names, i)for i in range(y_pred.shape[0])]plot_gallery(X_test, prediction_titles, h, w)# plot the gallery of the most significative eigenfaceseigenface_titles = ["eigenface %d" % i for i in range(eigenfaces.shape[0])]
plot_gallery(eigenfaces, eigenface_titles, h, w)plt.show()
Result:
人脸识别之特征脸方法相关推荐
- 人脸识别之特征脸方法(Eigenface)
人脸识别之特征脸方法(Eigenface) zouxy09@qq.com http://blog.csdn.net/zouxy09 因为需要,花了一点时间写了下经典的基于特征脸(EigenFace)的 ...
- Python机器学习:PCA与梯度上升:009人脸识别与特征脸(lfw_people数据集)
将w的每一行想成一个样本,则第一行是最重要的样本..第二行次重要..(Wk特征engen face) CODE 我们使用lfw_people数据集 #人脸识别与特征脸 import numpy as ...
- 人脸识别—特征脸方法
人脸识别之特征脸方法(Eigenface) zouxy09@qq.com http://blog.csdn.net/zouxy09 因为需要,花了一点时间写了下经典的基于特征脸(EigenFace)的 ...
- 人脸识别经典算法一:特征脸方法(Eigenface)
这篇文章是撸主要介绍人脸识别经典方法的第一篇,后续会有其他方法更新.特征脸方法基本是将人脸识别推向真正可用的第一种方法,了解一下还是很有必要的.特征脸用到的理论基础PCA在另一篇博客里:特征脸(Eig ...
- OpenCV人脸识别之Eigenface算法(PCA特征脸方法)
Eigenface Eigenfaces就是特征脸的意思,是一种从主成分分析(Principal Component Analysis,PCA)中导出的人脸识别和描述技术.特征脸方法的主要思路就是将输 ...
- 人脸识别算法之特征脸方法(Eigenface)
一.人脸识别算法之特征脸方法(Eigenface) 1.原理介绍及数据收集 特征脸方法主要是基于PCA降维实现. 详细介绍和主要思想可以参考 https://www.cnblogs.com/littl ...
- python人脸识别特征脸法_人脸识别经典算法:特征脸方法(Eigenface)
特征脸方法基本是将人脸识别推向真正可用的第一种方法,了解一下还是很有必要的.特征脸用到的理论基础PCA在之前的文章中已经讲过了.直接上特征脸方法的步骤: 步骤一:获取包含M张人脸图像的集合S.在我们的 ...
- python人脸识别特征脸法_人脸识别经典算法一 特征脸方法(Eigenface)
这篇文章是撸主要介绍人脸识别经典方法的第一篇,后续会有其他方法更新.特征脸方法基本是将人脸识别推向真正可用的第一种方法,了解一下还是很有必要的.特征脸用到的理论基础PCA在另一篇博客里:特征脸(Eig ...
- matlab怎么显示特征脸,matlab表情识别 Matlab表情识别,特征脸[1 ]作为面部表情分类的方法 联合开发网 - pudn.com...
matlab表情识别 所属分类:其他 开发工具:matlab 文件大小:4575KB 下载次数:23 上传日期:2019-03-11 20:20:45 上 传 者:bbqQq 说明: Matlab表 ...
最新文章
- ps grep java_ps -ef | grep java 查看所有关于java的进程
- CentOS7内核升级
- API接口通讯参数规范
- Effective C++ --4 设计与声明
- 在PyPI上发布自己的python包
- bashrc, bash_profile etc;
- v-if 表单验证_避免许多if块进行验证检查
- RocketMQ 端云一体化设计与实践
- uv转化率多少正常_宣城UV光解设备价格多少-低价供应
- HTML的基本知识(五)——无序列表、有序列表、自定义列表
- 国外游戏开发商吐槽:开发VR游戏付账单的钱都赚不到
- linux之ssh-keygen命令
- Atitit SpringCloud 使用总结 目录 1.1. 启动一个服务注册中心EurekaServer	1 1.2. 三、创建一个服务提供者 (eureka client)	2 1.3. 创建
- java 代码走查_代码走查如何保证软件质量
- 工程实践线切割3B代码参考
- PIC单片机开发环境的搭建总结及新上手单片机平台如何实现快速开发的几点经验分享
- 【AI视野·今日NLP 自然语言处理论文速览 第三十三期】Thu, 21 Apr 2022
- 《随笔二十二》—— C++中的“ 函数模板 和 类模板 ”
- 女生学前端适合么?新人应该怎么学习?
- 作为程序员的硬实力是什么 ?