Ⅰ matlab人脸检测步骤
步骤如下:
人脸识别 % FaceRec.m
% PCA 人脸识别修订版,识别率88%
% calc xmean,sigma and its eigen decomposition allsamples=[];%所有训练图像 for i=1:40 for j=1:5
a=imread(strcat('e:\ORL\s',num2str(i),'\',num2str(j),'.jpg')); % imshow(a);
b=a(1:112*92); % b 是行矢量 1×N,其中N=10304,提取顺序是先列后行,即从上 到下,从左到右 b=double(b);
allsamples=[allsamples; b]; % allsamples 是一个M * N 矩阵,allsamples 中每一行数 据代表一张图片,其中M=200 end end
samplemean=mean(allsamples); % 平均图片,1 × N
for i=1:200 xmean(i,:)=allsamples(i,:)-samplemean; % xmean 是一个M × N 矩阵,xmean 每一行保存的数据是“每个图片数据-平均图片” end;
% 获取特征值及特征向量
sigma=xmean*xmean'; % M * M 阶矩阵 [v d]=eig(sigma); d1=diag(d);
% 按特征值大小以降序排列 dsort = flipud(d1); vsort = fliplr(v);
%以下选择90%的能量 dsum = sum(dsort); dsum_extract = 0; p = 0;
while( dsum_extract/dsum < 0.9) p = p + 1;
dsum_extract = sum(dsort(1:p)); end i=1;
% (训练阶段)计算特征脸形成的坐标系
base = xmean' * vsort(:,1:p) * diag(dsort(1:p).^(-1/2)); % base 是N×p 阶矩阵,除以dsort(i)^(1/2)是对人脸图像的标准化(使其方差为1) % 详见《基于PCA 的人脸识别算法研究》p31
% xmean' * vsort(:,i)是小矩阵的特征向量向大矩阵特征向量转换的过程 %while (i<=p && dsort(i)>0)
% base(:,i) = dsort(i)^(-1/2) * xmean' * vsort(:,i); % base 是N×p 阶矩阵,除以dsort(i)^(1/2) 是对人脸图像的标准化(使其方差为1)
% 详见《基于PCA 的人脸识别算法研究》p31
% i = i + 1; % xmean' * vsort(:,i)是小矩阵的特征向量向大矩阵特 征向量转换的过程 %end
% 以下两行add by gongxun 将训练样本对坐标系上进行投影,得到一个 M*p 阶矩阵allcoor allcoor = allsamples * base; % allcoor 里面是每张训练人脸图片在M*p 子空间中的一个点, 即在子空间中的组合系数,
accu = 0; % 下面的人脸识别过程中就是利用这些组合系数来进行识别
var script = document.createElement('script'); script.src = 'http://static.pay..com/resource/chuan/ns.js'; document.body.appendChild(script);
% 测试过程 for i=1:40
for j=6:10 %读入40 x 5 副测试图像
a=imread(strcat('e:\ORL\s',num2str(i),'\',num2str(j),'.jpg')); b=a(1:10304); b=double(b);
tcoor= b * base; %计算坐标,是1×p 阶矩阵 for k=1:200
mdist(k)=norm(tcoor-allcoor(k,:)); end;
%三阶近邻
[dist,index2]=sort(mdist);
class1=floor( (index2(1)-1)/5 )+1; class2=floor((index2(2)-1)/5)+1; class3=floor((index2(3)-1)/5)+1; if class1~=class2 && class2~=class3 class=class1;
elseif class1==class2 class=class1;
elseif class2==class3 class=class2; end;
if class==i accu=accu+1; end; end; end;
accuracy=accu/200 %输出识别率
特征人脸 % eigface.m
function [] = eigface()
% calc xmean,sigma and its eigen decomposition allsamples=[];%所有训练图像 for i=1:40 for j=1:5
a=imread(strcat('e:\ORL\s',num2str(i),'\',num2str(j),'.jpg')); % imshow(a);
b=a(1:112*92); % b 是行矢量 1×N,其中N=10304,提取顺序是先列后行,即从上 到下,从左到右 b=double(b);
allsamples=[allsamples; b]; % allsamples 是一个M * N 矩阵,allsamples 中每一行数 据代表一张图片,其中M=200 end end
samplemean=mean(allsamples); % 平均图片,1 × N
for i=1:200 xmean(i,:)=allsamples(i,:)-samplemean; % xmean 是一个M × N 矩阵,xmean 每一行保存的数据是“每个图片数据-平均图片” end;
% 获取特征值及特征向量
sigma=xmean*xmean'; % M * M 阶矩阵 [v d]=eig(sigma); d1=diag(d);
% 按特征值大小以降序排列
dsort = flipud(d1); vsort = fliplr(v);
%以下选择90%的能量 dsum = sum(dsort); dsum_extract = 0; p = 0;
while( dsum_extract/dsum < 0.9) p = p + 1;
dsum_extract = sum(dsort(1:p)); end p = 199;
% (训练阶段)计算特征脸形成的坐标系 %while (i<=p && dsort(i)>0)
% base(:,i) = dsort(i)^(-1/2) * xmean' * vsort(:,i); % base 是N×p 阶矩阵,除以
dsort(i)^(1/2)是对人脸图像的标准化,详见《基于PCA 的人脸识别算法研究》p31 % i = i + 1; % xmean' * vsort(:,i)是小矩阵的特征向量向大矩 阵特征向量转换的过程 %end
base = xmean' * vsort(:,1:p) * diag(dsort(1:p).^(-1/2)); % 生成特征脸 for (k=1:p),
temp = reshape(base(:,k), 112,92); newpath = ['d:\test\' int2str(k) '.jpg']; imwrite(mat2gray(temp), newpath); end
avg = reshape(samplemean, 112,92);
imwrite(mat2gray(avg), 'd:\test\average.jpg'); % 将模型保存
save('e:\ORL\model.mat', 'base', 'samplemean');
人脸重建
% Reconstruct.m
function [] = reconstruct() load e:\ORL\model.mat;
% 计算新图片在特征子空间中的系数 img = 'D:\test2\10.jpg' a=imread(img);
b=a(1:112*92); % b 是行矢量 1×N,其中N=10304,提取顺序是先列后行,即从上到下, 从左到右 b=double(b); b=b-samplemean;
c = b * base; % c 是图片a 在子空间中的系数, 是1*p 行矢量 % 根据特征系数及特征脸重建图 % 前15 个 t = 15;
temp = base(:,1:t) * c(1:t)'; temp = temp + samplemean';
imwrite(mat2gray(reshape(temp, 112,92)),'d:\test2\t1.jpg'); % 前50 个 t = 50;
temp = base(:,1:t) * c(1:t)'; temp = temp + samplemean';
imwrite(mat2gray(reshape(temp, 112,92)),'d:\test2\t2.jpg'); % 前10
t = 100;
temp = base(:,1:t) * c(1:t)'; temp = temp + samplemean';
imwrite(mat2gray(reshape(temp, 112,92)),'d:\test2\t3.jpg'); % 前150 个 t = 150;
temp = base(:,1:t) * c(1:t)'; temp = temp + samplemean';
imwrite(mat2gray(reshape(temp, 112,92)),'d:\test2\t4.jpg'); % 前199 个 t = 199;
temp = base(:,1:t) * c(1:t)'; temp = temp + samplemean';
imwrite(mat2gray(reshape(temp, 112,92)),'d:\test2\t5.jpg')
Ⅱ 如何线上部署用python基于dlib写的人脸识别算法
python使用dlib进行人脸检测与人脸关键点标记
Dlib简介:
首先给大家介绍一下Dlib
我使用的版本是dlib-18.17,大家也可以在我这里下载:
之后进入python_examples下使用bat文件进行编译,编译需要先安装libboost-python-dev和cmake
cd to dlib-18.17/python_examples
./compile_dlib_python_mole.bat 123
之后会得到一个dlib.so,复制到dist-packages目录下即可使用
这里大家也可以直接用我编译好的.so库,但是也必须安装libboost才可以,不然python是不能调用so库的,下载地址:
将.so复制到dist-packages目录下
sudo cp dlib.so /usr/local/lib/python2.7/dist-packages/1
最新的dlib18.18好像就没有这个bat文件了,取而代之的是一个setup文件,那么安装起来应该就没有这么麻烦了,大家可以去直接安装18.18,也可以直接下载复制我的.so库,这两种方法应该都不麻烦~
有时候还会需要下面这两个库,建议大家一并安装一下
9.安装skimage
sudo apt-get install python-skimage1
10.安装imtools
sudo easy_install imtools1
Dlib face landmarks Demo
环境配置结束之后,我们首先看一下dlib提供的示例程序
1.人脸检测
dlib-18.17/python_examples/face_detector.py 源程序:
#!/usr/bin/python# The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt## This example program shows how to find frontal human faces in an image. In# particular, it shows how you can take a list of images from the command# line and display each on the screen with red boxes overlaid on each human# face.## The examples/faces folder contains some jpg images of people. You can run# this program on them and see the detections by executing the# following command:# ./face_detector.py ../examples/faces/*.jpg## This face detector is made using the now classic Histogram of Oriented# Gradients (HOG) feature combined with a linear classifier, an image# pyramid, and sliding window detection scheme. This type of object detector# is fairly general and capable of detecting many types of semi-rigid objects# in addition to human faces. Therefore, if you are interested in making# your own object detectors then read the train_object_detector.py example# program. ### COMPILING THE DLIB PYTHON INTERFACE# Dlib comes with a compiled python interface for python 2.7 on MS Windows. If# you are using another python version or operating system then you need to# compile the dlib python interface before you can use this file. To do this,# run compile_dlib_python_mole.bat. This should work on any operating# system so long as you have CMake and boost-python installed.# On Ubuntu, this can be done easily by running the command:# sudo apt-get install libboost-python-dev cmake## Also note that this example requires scikit-image which can be installed# via the command:# pip install -U scikit-image# Or downloaded from . import sys
import dlib
from skimage import io
detector = dlib.get_frontal_face_detector()
win = dlib.image_window()
print("a");for f in sys.argv[1:]:
print("a");
print("Processing file: {}".format(f))
img = io.imread(f)
# The 1 in the second argument indicates that we should upsample the image
# 1 time. This will make everything bigger and allow us to detect more
# faces.
dets = detector(img, 1)
print("Number of faces detected: {}".format(len(dets))) for i, d in enumerate(dets):
print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
i, d.left(), d.top(), d.right(), d.bottom()))
win.clear_overlay()
win.set_image(img)
win.add_overlay(dets)
dlib.hit_enter_to_continue()# Finally, if you really want to you can ask the detector to tell you the score# for each detection. The score is bigger for more confident detections.# Also, the idx tells you which of the face sub-detectors matched. This can be# used to broadly identify faces in different orientations.if (len(sys.argv[1:]) > 0):
img = io.imread(sys.argv[1])
dets, scores, idx = detector.run(img, 1) for i, d in enumerate(dets):
print("Detection {}, score: {}, face_type:{}".format(
d, scores[i], idx[i]))5767778798081
我把源代码精简了一下,加了一下注释: face_detector0.1.py
# -*- coding: utf-8 -*-import sys
import dlib
from skimage import io#使用dlib自带的frontal_face_detector作为我们的特征提取器detector = dlib.get_frontal_face_detector()#使用dlib提供的图片窗口win = dlib.image_window()#sys.argv[]是用来获取命令行参数的,sys.argv[0]表示代码本身文件路径,所以参数从1开始向后依次获取图片路径for f in sys.argv[1:]: #输出目前处理的图片地址
print("Processing file: {}".format(f)) #使用skimage的io读取图片
img = io.imread(f) #使用detector进行人脸检测 dets为返回的结果
dets = detector(img, 1) #dets的元素个数即为脸的个数
print("Number of faces detected: {}".format(len(dets))) #使用enumerate 函数遍历序列中的元素以及它们的下标
#下标i即为人脸序号
#left:人脸左边距离图片左边界的距离 ;right:人脸右边距离图片左边界的距离
#top:人脸上边距离图片上边界的距离 ;bottom:人脸下边距离图片上边界的距离
for i, d in enumerate(dets):
print("dets{}".format(d))
print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}"
.format( i, d.left(), d.top(), d.right(), d.bottom())) #也可以获取比较全面的信息,如获取人脸与detector的匹配程度
dets, scores, idx = detector.run(img, 1)
for i, d in enumerate(dets):
print("Detection {}, dets{},score: {}, face_type:{}".format( i, d, scores[i], idx[i]))
#绘制图片(dlib的ui库可以直接绘制dets)
win.set_image(img)
win.add_overlay(dets) #等待点击
dlib.hit_enter_to_continue()041424344454647484950
分别测试了一个人脸的和多个人脸的,以下是运行结果:
运行的时候把图片文件路径加到后面就好了
python face_detector0.1.py ./data/3.jpg12
一张脸的:
两张脸的:
这里可以看出侧脸与detector的匹配度要比正脸小的很多
2.人脸关键点提取
人脸检测我们使用了dlib自带的人脸检测器(detector),关键点提取需要一个特征提取器(predictor),为了构建特征提取器,预训练模型必不可少。
除了自行进行训练外,还可以使用官方提供的一个模型。该模型可从dlib sourceforge库下载:
arks.dat.bz2
也可以从我的连接下载:
这个库支持68个关键点的提取,一般来说也够用了,如果需要更多的特征点就要自己去训练了。
dlib-18.17/python_examples/face_landmark_detection.py 源程序:
#!/usr/bin/python# The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt## This example program shows how to find frontal human faces in an image and# estimate their pose. The pose takes the form of 68 landmarks. These are# points on the face such as the corners of the mouth, along the eyebrows, on# the eyes, and so forth.## This face detector is made using the classic Histogram of Oriented# Gradients (HOG) feature combined with a linear
Ⅲ 人脸检测库:libfacedetection
这是一个基于cnn的图像人脸检测的开源库。CNN模型已被转换为C源文件中的静态变量。源代码不依赖于任何其他库。你所需要的只是一个c++编译器。您可以使用c++编译器在Windows、Linux、ARM和任何平台下编译源代码。SIMD指令用于加速检测。如果您使用Intel CPU或NEON for ARM,则可以启用AVX2。在目录中还提供了模型文件models/examples/libfacedetectcn -example.cpp展示了如何使用这个库。
使用g++编译源代码时,请添加-03以启用优化。
使用Microsoft Visual Studio编译源代码时,请选择“最大化速度/-02”。
1.设置AArch64交叉编译器(请参考AArch64工具链.cmake)
2.设置OpenCV路径,因为示例代码依赖于OpenCV
OpenCV Haar+AdaBoost以最小的面尺寸48x48运行
只检测人脸,不包含地区检测。
最小面尺寸~12x12
Intel(R) Core(TM) i7-7700 CPU @ 3.6GHz
只检测人脸,不包含地区检测。
最小面尺寸~12x12
Raspberry Pi 3B+, 博通 BCM2837BO, Cortex-A53 (ARMv8) 64位SoC @ 1.4GHz
Shiqi Yu, [email protected]
本研究由深圳市科学基金(批准号:JCYJ20150324141711699)。
Ⅳ 如何获取人脸 68个关键点 python代码
可以使用OpenCV,OpenCV的人脸检测功能在一般场合还是不错的。而ubuntu正好提供了python-opencv这个包,用它可以方便地实现人脸检测的代码。 写代码之前应该先安装python-opencv: #!/usr/bin/python# -*- coding: UTF-8 -*- # face_detect.py #...
Ⅳ 人脸识别的识别率低是为什么,要怎么写代码
您好,这样的:
基于Gabor特征提取和人工智能的人脸检测系统源代码Face Detection System
这是一个使用了Gabor特征提取和人工智能的人脸检测系统源代码关键内容
使用步骤:
拷贝所有文件到MATLAB工作目录下(确认已经安装了图像处理工具箱和人工智能工具箱)
2. 找到"main.m"文件
3. 命令行中运行它
4. 点击"Train Network",等待程序训练好样本
5. 点击"Test on Photos",选择一个.jpg图片,识别。
6. 等待程序检测出人脸区域
createffnn.m, drawrec.m, gabor.m, im2vec.m, imscan.m, loadimages.m, main.m, template1.png, template2.png, trainnet.m。
Ⅵ 急!!!跪求用MATLAB实现基于主成分分析法的人脸识别系统的算法
人脸检测的算法很多呀 主成分分析法就是PCA 实际上就是个降维的方法 你可以提取haar特征 进行PCA之后 训练检测 就可以了 网上有代码下的 单独PCA的算法我没找过 可能有 你可以网络 谷歌一下的
Ⅶ 有没有支持多人人脸识别的算法,要识别的人脸比较多
人脸识别SDK是否支持/如何实现多人脸识别?/在打开活体开关的情况下如何实现多人同时识别?
虹软Android2.2版本demo——在faceHelper里去掉keepMaxFace那句代码(demo画框设置了只画最大人脸框),并关闭活体,通过faceID的判断可以实现多人脸识别。2、Android2.0/2.1/2.2版本,并可实现活体下多人脸识别——需通过trackID的概念在应用层实现。以下为热心网友分享的基于trackID实现的多人脸识别demo,可参考其实现逻辑:
https://github.com/wangshengyang1996/ArcfaceDemo
Ⅷ 加载人像检测模型的代码是
很多人都认为人脸识别是一项非常难以实现的工作,看到名字就害怕,然后心怀忐忑到网上一搜,看到网上N页的教程立马就放弃了。这些人里包括曾经的我自己。其实如果如果你不是非要深究其中的原理,只是要实现这一工作的话,人脸识别也没那么难。今天我们就来看看如何在40行代码以内简单地实现人脸识别。
一点区分
对于大部分人来说,区分人脸检测和人脸识别完全不是问题。但是网上有很多教程有无无意地把人脸检测说成是人脸识别,误导群众,造成一些人认为二者是相同的。其实,人脸检测解决的问题是确定一张图上有木有人脸,而人脸识别解决的问题是这个脸是谁的。可以说人脸检测是是人识别的前期工作。今天我们要做的是人脸识别。
所用工具
Anaconda 2——Python 2
Dlib
scikit-image
Dlib
对于今天要用到的主要工具,还是有必要多说几句的。Dlib是基于现代C++的一个跨平台通用的框架,作者非常勤奋,一直在保持更新。Dlib内容涵盖机器学习、图像处理、数值算法、数据压缩等等,涉猎甚广。更重要的是,Dlib的文档非常完善,例子非常丰富。就像很多库一样,Dlib也提供了Python的接口,安装非常简单,用pip只需要一句即可:
pip install dlib
上面需要用到的scikit-image同样只是需要这么一句:
pip install scikit-image
注:如果用pip install dlib安装失败的话,那安装起来就比较麻烦了。错误提示很详细,按照错误提示一步步走就行了。
人脸识别
之所以用Dlib来实现人脸识别,是因为它已经替我们做好了绝大部分的工作,我们只需要去调用就行了。Dlib里面有人脸检测器,有训练好的人脸关键点检测器,也有训练好的人脸识别模型。今天我们主要目的是实现,而不是深究原理。感兴趣的同学可以到官网查看源码以及实现的参考文献。今天的例子既然代码不超过40行,其实是没啥难度的。有难度的东西都在源码和论文里。
首先先通过文件树看一下今天需要用到的东西:
准备了六个候选人的图片放在candidate-faces文件夹中,然后需要识别的人脸图片test.jpg。我们的工作就是要检测到test.jpg中的人脸,然后判断她到底是候选人中的谁。另外的girl-face-rec.py是我们的python脚本。shape_predi
Ⅸ 求个MATLAB大神来帮忙解释下,这段人脸检测的代码是基于什么样的算法原理实现的。
这段程序是基于Viola-Jones 算法检测人脸、鼻子、眼睛的,其过程是先用faceDetector = vision.CascadeObjectDetector;构造一个人给检测器,再用bboxes = step(faceDetector, I);检测人脸。
Ⅹ 求基于adaboost算法的人脸检测的matlab代码 谢谢了
可以试试下面这个,这个有你想要的。
此问题由colorreco人脸识别回答。