Ⅰ 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人臉識別回答。