新聞中心
如何線上部署用python基于dlib寫的人臉識別算法
python使用dlib進(jìn)行人臉檢測與人臉關(guān)鍵點標(biāo)記
創(chuàng)新互聯(lián)是一家專業(yè)提供鐵山企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站設(shè)計、成都網(wǎng)站設(shè)計、H5頁面制作、小程序制作等業(yè)務(wù)。10年已為鐵山眾多企業(yè)、政府機構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站制作公司優(yōu)惠進(jìn)行中。
Dlib簡介:
首先給大家介紹一下Dlib
Dlib是一個跨平臺的C++公共庫,除了線程支持,網(wǎng)絡(luò)支持,提供測試以及大量工具等等優(yōu)點,Dlib還是一個強大的機器學(xué)習(xí)的C++庫,包含了許多機器學(xué)習(xí)常用的算法。同時支持大量的數(shù)值算法如矩陣、大整數(shù)、隨機數(shù)運算等等。
Dlib同時還包含了大量的圖形模型算法。
最重要的是Dlib的文檔和例子都非常詳細(xì)。
Dlib主頁:
這篇博客所述的人臉標(biāo)記的算法也是來自Dlib庫,Dlib實現(xiàn)了One Millisecond Face Alignment with an Ensemble of Regression Trees中的算法
這篇論文非常出名,在谷歌上打上One Millisecond就會自動補全,是CVPR 2014(國際計算機視覺與模式識別會議)上的一篇國際頂級水平的論文。毫秒級別就可以實現(xiàn)相當(dāng)準(zhǔn)確的人臉標(biāo)記,包括一些半側(cè)臉,臉很不清楚的情況,論文本身的算法十分復(fù)雜,感興趣的同學(xué)可以下載看看。
Dlib實現(xiàn)了這篇最新論文的算法,所以Dlib的人臉標(biāo)記算法是十分先進(jìn)的,而且Dlib自帶的人臉檢測庫也很準(zhǔn)確,我們項目受到硬件所限,攝像頭拍攝到的畫面比較模糊,而在這種情況下之前嘗試了幾個人臉庫,識別率都非常的低,而Dlib的效果簡直出乎意料。
相對于C++我還是比較喜歡使用python,同時Dlib也是支持python的,只是在配置的時候碰了不少釘子,網(wǎng)上大部分的Dlib資料都是針對于C++的,我好不容易才配置好了python的dlib,這里分享給大家:
Dlib for python 配置:
因為是用python去開發(fā)計算機視覺方面的東西,python的這些科學(xué)計算庫是必不可少的,這里我把常用的科學(xué)計算庫的安裝也涵蓋在內(nèi)了,已經(jīng)安裝過這些庫的同學(xué)就可以忽略了。
我的環(huán)境是Ubuntu14.04:
大家都知道Ubuntu是自帶python2.7的,而且很多Ubuntu系統(tǒng)軟件都是基于python2.7的,有一次我系統(tǒng)的python版本亂了,我腦殘的想把python2.7卸載了重裝,然后……好像是提醒我要卸載幾千個軟件來著,沒看好直接回車了,等我反應(yīng)過來Ctrl + C 的時候系統(tǒng)已經(jīng)沒了一半了…
所以我發(fā)現(xiàn)想要搞崩系統(tǒng),這句話比rm -rf 還給力…
sudo apt-get remove python2.71
首先安裝兩個python第三方庫的下載安裝工具,ubuntu14.04好像是預(yù)裝了easy_install
以下過程都是在終端中進(jìn)行:
1.安裝pip
sudo apt-get install python-pip1
2.安裝easy-install
sudo apt-get install python-setuptools1
3.測試一下easy_install
有時候系統(tǒng)環(huán)境復(fù)雜了,安裝的時候會安裝到別的python版本上,這就麻煩了,所以還是謹(jǐn)慎一點測試一下,這里安裝一個我之前在博客中提到的可以模擬瀏覽器的第三方python庫測試一下。
sudo easy_install Mechanize1
4.測試安裝是否成功
在終端輸入python進(jìn)入python shell
python1
進(jìn)入python shell后import一下剛安裝的mechanize
import mechanize1
沒有報錯,就是安裝成功了,如果說沒有找到,那可能就是安裝到別的python版本的路徑了。
同時也測試一下PIL這個基礎(chǔ)庫
import PIL1
沒有報錯的話,說明PIL已經(jīng)被預(yù)裝過了
5.安裝numpy
接下來安裝numpy
首先需要安裝python-dev才可以編譯之后的擴展庫
sudo apt-get install python-dev1
之后就可以用easy-install 安裝numpy了
sudo easy_install numpy1
這里有時候用easy-install 安裝numpy下載的時候會卡住,那就只能用 apt-get 來安裝了:
sudo apt-get install numpy1
不推薦這樣安裝的原因就是系統(tǒng)環(huán)境或者說python版本多了之后,直接apt-get安裝numpy很有可能不知道裝到哪個版本去了,然后就很麻煩了,我有好幾次遇到這個問題,不知道是運氣問題還是什么,所以風(fēng)險還是很大的,所以還是盡量用easy-install來安裝。
同樣import numpy 進(jìn)行測試
python
import numpy1234
沒有報錯的話就是成功了
下面的安裝過程同理,我就從簡寫了,大家自己每步別忘了測試一下
6.安裝scipy
sudo apt-get install python-scipy1
7.安裝matplotlib
sudo apt-get install python-matplotlib1
8.安裝dlib
我當(dāng)時安裝dlib的過程簡直太艱辛,網(wǎng)上各種說不知道怎么配,配不好,我基本把stackoverflow上的方法試了個遍,才最終成功編譯出來并且導(dǎo)入,不過聽說18.18更新之后有了setup.py,那真是極好的,18.18我沒有親自配過也不能亂說,這里給大家分享我配置18.17的過程吧:
1.首先必須安裝libboost,不然是不能使用.so庫的
sudo apt-get install libboost-python-dev cmake1
2.到Dlib的官網(wǎng)上下載dlib,會下載下來一個壓縮包,里面有C++版的dlib庫以及例子文檔,Python dlib庫的代碼例子等等
我使用的版本是dlib-18.17,大家也可以在我這里下載:
之后進(jìn)入python_examples下使用bat文件進(jìn)行編譯,編譯需要先安裝libboost-python-dev和cmake
cd to dlib-18.17/python_examples
./compile_dlib_python_module.bat 123
之后會得到一個dlib.so,復(fù)制到dist-packages目錄下即可使用
這里大家也可以直接用我編譯好的.so庫,但是也必須安裝libboost才可以,不然python是不能調(diào)用so庫的,下載地址:
將.so復(fù)制到dist-packages目錄下
sudo cp dlib.so /usr/local/lib/python2.7/dist-packages/1
最新的dlib18.18好像就沒有這個bat文件了,取而代之的是一個setup文件,那么安裝起來應(yīng)該就沒有這么麻煩了,大家可以去直接安裝18.18,也可以直接下載復(fù)制我的.so庫,這兩種方法應(yīng)該都不麻煩~
有時候還會需要下面這兩個庫,建議大家一并安裝一下
9.安裝skimage
sudo apt-get install python-skimage1
10.安裝imtools
sudo easy_install imtools1
Dlib face landmarks Demo
環(huán)境配置結(jié)束之后,我們首先看一下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_module.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]))123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
我把源代碼精簡了一下,加了一下注釋: 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[]是用來獲取命令行參數(shù)的,sys.argv[0]表示代碼本身文件路徑,所以參數(shù)從1開始向后依次獲取圖片路徑for f in sys.argv[1:]: ? ?#輸出目前處理的圖片地址
print("Processing file: {}".format(f)) ? ?#使用skimage的io讀取圖片
img = io.imread(f) ? ?#使用detector進(jìn)行人臉檢測 dets為返回的結(jié)果
dets = detector(img, 1) ? ?#dets的元素個數(shù)即為臉的個數(shù)
print("Number of faces detected: {}".format(len(dets))) ? ?#使用enumerate 函數(shù)遍歷序列中的元素以及它們的下標(biāo)
#下標(biāo)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()1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
分別測試了一個人臉的和多個人臉的,以下是運行結(jié)果:
運行的時候把圖片文件路徑加到后面就好了
python face_detector0.1.py ./data/3.jpg12
一張臉的:
兩張臉的:
這里可以看出側(cè)臉與detector的匹配度要比正臉小的很多
2.人臉關(guān)鍵點提取
人臉檢測我們使用了dlib自帶的人臉檢測器(detector),關(guān)鍵點提取需要一個特征提取器(predictor),為了構(gòu)建特征提取器,預(yù)訓(xùn)練模型必不可少。
除了自行進(jìn)行訓(xùn)練外,還可以使用官方提供的一個模型。該模型可從dlib sourceforge庫下載:
arks.dat.bz2
也可以從我的連接下載:
這個庫支持68個關(guān)鍵點的提取,一般來說也夠用了,如果需要更多的特征點就要自己去訓(xùn)練了。
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
html如何調(diào)用python的opencv人臉識別
html嵌入python代碼(python做人臉識別)
2022-07-31 14:19:00閱讀 2300
大家好,又見面了,我是你們的朋友全棧君。
最近閑來無事,研究研究在安卓上跑Python。
想起以前玩過的kivy技術(shù),kivy[1]是一個跨平臺的UI框架。當(dāng)然對我們最有用的是,kivy可以把python代碼打包成安卓App。但是由于安卓打包的工具鏈很長,包括android sdk打包java代碼、ndk編譯python、 編譯各種python依賴包,經(jīng)?;ㄒ徽鞆娜腴T到放棄。這次使出認(rèn)真研究的心態(tài),終于找到一個解決方案,于是有了這篇文章:
?只要會python就能寫安卓App,無需安卓開發(fā)基礎(chǔ),無需編譯?手機上也有交互式python解釋器,直接調(diào)試python代碼?可以使用各種python庫,包括numpy/opencv等機器學(xué)習(xí)包?可以與安卓接口交互,使用手機硬件,比如攝像頭
那么我們就以人臉識別App為例,看看如何簡單幾步搞定。先看看成品的效果:
?
第一步:安裝airport.apk
AirPort是我編譯好的一個安卓App,里面包含了python解釋器和一些常用的python庫。
第二步:連接手機的python解釋器
啟動手機上的AirPort應(yīng)用,就會運行python解釋器。我內(nèi)置了一個ssh服務(wù)器,用于調(diào)試代碼非常方便。應(yīng)用啟動時會顯示手機的ip地址。
?
在電腦上使用ssh命令,就可以連接到手機。
注意:確保你的手機和電腦在同一局域網(wǎng)中。
#在電腦上連接手機,注意這里ip需要替換成AirPort顯示的ipssh -p 8000 [email protected]#輸入密碼meteorix
然后你就可以在手機上盡情使用python了,比如試試
import osos.getcwd()’/data/data/org.airtest.airport/files/app’import requestsr = requests.get(“”)r.status_code200
第三步: 一個攝像頭的App
在kivy的官方文檔中,我們可以找到這樣一個攝像頭的example[2]
代碼非常簡單,Builder.load_string函數(shù)加載了一段配
誰用過python中的第三方庫face recognition
簡介
該庫可以通過python或者命令行即可實現(xiàn)人臉識別的功能。使用dlib深度學(xué)習(xí)人臉識別技術(shù)構(gòu)建,在戶外臉部檢測數(shù)據(jù)庫基準(zhǔn)(Labeled Faces in the Wild)上的準(zhǔn)確率為99.38%。
在github上有相關(guān)的鏈接和API文檔。
在下方為提供的一些相關(guān)源碼或是文檔。當(dāng)前庫的版本是v0.2.0,點擊docs可以查看API文檔,我們可以查看一些函數(shù)相關(guān)的說明等。
安裝配置
安裝配置很簡單,按照github上的說明一步一步來就可以了。
根據(jù)你的python版本輸入指令:
pip install face_recognition11
或者
pip3 install face_recognition11
正常來說,安裝過程中會出錯,會在安裝dlib時出錯,可能報錯也可能會卡在那不動。因為pip在編譯dlib時會出錯,所以我們需要手動編譯dlib再進(jìn)行安裝。
按照它給出的解決辦法:
1、先下載下來dlib的源碼。
git clone
2、編譯dlib。
cd dlib
mkdir build
cd build
cmake .. -DDLIB_USE_CUDA=0 -DUSE_AVX_INSTRUCTIONS=1
cmake --build1234512345
3、編譯并安裝python的拓展包。
cd ..
python3 setup.py install --yes USE_AVX_INSTRUCTIONS --no DLIB_USE_CUDA1212
注意:這個安裝步驟是默認(rèn)認(rèn)為沒有GPU的,所以不支持cuda。
在自己手動編譯了dlib后,我們可以在python中import dlib了。
之后再重新安裝,就可以配置成功了。
根據(jù)你的python版本輸入指令:
pip install face_recognition11
或者
pip3 install face_recognition11
安裝成功之后,我們可以在python中正常import face_recognition了。
編寫人臉識別程序
編寫py文件:
# -*- coding: utf-8 -*-
#
# 檢測人臉
import face_recognition
import cv2
# 讀取圖片并識別人臉
img = face_recognition.load_image_file("silicon_valley.jpg")
face_locations = face_recognition.face_locations(img)
print face_locations
# 調(diào)用opencv函數(shù)顯示圖片
img = cv2.imread("silicon_valley.jpg")
cv2.namedWindow("原圖")
cv2.imshow("原圖", img)
# 遍歷每個人臉,并標(biāo)注
faceNum = len(face_locations)
for i in range(0, faceNum):
top = face_locations[i][0]
right = face_locations[i][1]
bottom = face_locations[i][2]
left = face_locations[i][3]
start = (left, top)
end = (right, bottom)
color = (55,255,155)
thickness = 3
cv2.rectangle(img, start, end, color, thickness)
# 顯示識別結(jié)果
cv2.namedWindow("識別")
cv2.imshow("識別", img)
cv2.waitKey(0)
cv2.destroyAllWindows()12345678910111213141516171819202122232425262728293031323334353637381234567891011121314151617181920212223242526272829303132333435363738
注意:這里使用了python-OpenCV,一定要配置好了opencv才能運行成功。
運行結(jié)果:
程序會讀取當(dāng)前目錄下指定的圖片,然后識別其中的人臉,并標(biāo)注每個人臉。
(使用圖片來自美劇硅谷)
編寫人臉比對程序
首先,我在目錄下放了幾張圖片:
這里用到的是一張喬布斯的照片和一張奧巴馬的照片,和一張未知的照片。
編寫程序:
# 識別圖片中的人臉
import face_recognition
jobs_image = face_recognition.load_image_file("jobs.jpg");
obama_image = face_recognition.load_image_file("obama.jpg");
unknown_image = face_recognition.load_image_file("unknown.jpg");
jobs_encoding = face_recognition.face_encodings(jobs_image)[0]
obama_encoding = face_recognition.face_encodings(obama_image)[0]
unknown_encoding = face_recognition.face_encodings(unknown_image)[0]
results = face_recognition.compare_faces([jobs_encoding, obama_encoding], unknown_encoding )
labels = ['jobs', 'obama']
print('results:'+str(results))
for i in range(0, len(results)):
if results[i] == True:
print('The person is:'+labels[i])123456789101112131415161718123456789101112131415161718
運行結(jié)果:
識別出未知的那張照片是喬布斯的。
攝像頭實時識別
代碼:
# -*- coding: utf-8 -*-
import face_recognition
import cv2
video_capture = cv2.VideoCapture(1)
obama_img = face_recognition.load_image_file("obama.jpg")
obama_face_encoding = face_recognition.face_encodings(obama_img)[0]
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True
while True:
ret, frame = video_capture.read()
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
if process_this_frame:
face_locations = face_recognition.face_locations(small_frame)
face_encodings = face_recognition.face_encodings(small_frame, face_locations)
face_names = []
for face_encoding in face_encodings:
match = face_recognition.compare_faces([obama_face_encoding], face_encoding)
if match[0]:
name = "Barack"
else:
name = "unknown"
face_names.append(name)
process_this_frame = not process_this_frame
for (top, right, bottom, left), name in zip(face_locations, face_names):
top *= 4
right *= 4
bottom *= 4
left *= 4
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), 2)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left+6, bottom-6), font, 1.0, (255, 255, 255), 1)
cv2.imshow('Video', frame)
if cv2.waitKey(1) 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545512345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
識別結(jié)果:
我直接在手機上百度了幾張圖試試,程序識別出了奧巴馬。
這個庫很cool?。?/p>
怎么用python調(diào)取一個人臉識別 api
必備知識
Haar-like
通俗的來講,就是作為人臉特征即可。
Haar特征值反映了圖像的灰度變化情況。例如:臉部的一些特征能由矩形特征簡單的描述,如:眼睛要比臉頰顏色要深,鼻梁兩側(cè)比鼻梁顏色要深,嘴巴比周圍顏色要深等。
opencv api
要想使用opencv,就必須先知道其能干什么,怎么做。于是API的重要性便體現(xiàn)出來了。就本例而言,使用到的函數(shù)很少,也就普通的讀取圖片,灰度轉(zhuǎn)換,顯示圖像,簡單的編輯圖像罷了。
如下:
讀取圖片
只需要給出待操作的圖片的路徑即可。
import cv2
image = cv2.imread(imagepath)
灰度轉(zhuǎn)換
灰度轉(zhuǎn)換的作用就是:轉(zhuǎn)換成灰度的圖片的計算強度得以降低。
import cv2
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
畫圖
opencv 的強大之處的一個體現(xiàn)就是其可以對圖片進(jìn)行任意編輯,處理。
下面的這個函數(shù)最后一個參數(shù)指定的就是畫筆的大小。
import cv2
cv2.rectangle(image,(x,y),(x+w,y+w),(0,255,0),2)
顯示圖像
編輯完的圖像要么直接的被顯示出來,要么就保存到物理的存儲介質(zhì)。
import cv2
cv2.imshow("Image Title",image)
獲取人臉識別訓(xùn)練數(shù)據(jù)
看似復(fù)雜,其實就是對于人臉特征的一些描述,這樣opencv在讀取完數(shù)據(jù)后很據(jù)訓(xùn)練中的樣品數(shù)據(jù),就可以感知讀取到的圖片上的特征,進(jìn)而對圖片進(jìn)行人臉識別。
import cv2
face_cascade = cv2.CascadeClassifier(r'./haarcascade_frontalface_default.xml')
里賣弄的這個xml文件,就是opencv在GitHub上共享出來的具有普適的訓(xùn)練好的數(shù)據(jù)。我們可以直接的拿來使用。
訓(xùn)練數(shù)據(jù)參考地址:
探測人臉
說白了,就是根據(jù)訓(xùn)練的數(shù)據(jù)來對新圖片進(jìn)行識別的過程。
import cv2
# 探測圖片中的人臉
faces = face_cascade.detectMultiScale(
gray,
scaleFactor = 1.15,
minNeighbors = 5,
minSize = (5,5),
flags = cv2.cv.CV_HAAR_SCALE_IMAGE
)
我們可以隨意的指定里面參數(shù)的值,來達(dá)到不同精度下的識別。返回值就是opencv對圖片的探測結(jié)果的體現(xiàn)。
處理人臉探測的結(jié)果
結(jié)束了剛才的人臉探測,我們就可以拿到返回值來做進(jìn)一步的處理了。但這也不是說會多么的復(fù)雜,無非添加點特征值罷了。
import cv2
print "發(fā)現(xiàn){0}個人臉!".format(len(faces))
for(x,y,w,h) in faces:
cv2.rectangle(image,(x,y),(x+w,y+w),(0,255,0),2)
實例
有了剛才的基礎(chǔ),我們就可以完成一個簡單的人臉識別的小例子了。
圖片素材
下面的這張圖片將作為我們的檢測依據(jù)。
人臉檢測代碼
# coding:utf-8
import sys
reload(sys)
sys.setdefaultencoding('utf8')
# __author__ = '郭 璞'
# __date__ = '2016/9/5'
# __Desc__ = 人臉檢測小例子,以圓圈圈出人臉
import cv2
# 待檢測的圖片路徑
imagepath = r'./heat.jpg'
# 獲取訓(xùn)練好的人臉的參數(shù)數(shù)據(jù),這里直接從GitHub上使用默認(rèn)值
face_cascade = cv2.CascadeClassifier(r'./haarcascade_frontalface_default.xml')
# 讀取圖片
image = cv2.imread(imagepath)
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
# 探測圖片中的人臉
faces = face_cascade.detectMultiScale(
gray,
scaleFactor = 1.15,
minNeighbors = 5,
minSize = (5,5),
flags = cv2.cv.CV_HAAR_SCALE_IMAGE
)
print "發(fā)現(xiàn){0}個人臉!".format(len(faces))
for(x,y,w,h) in faces:
# cv2.rectangle(image,(x,y),(x+w,y+w),(0,255,0),2)
cv2.circle(image,((x+x+w)/2,(y+y+h)/2),w/2,(0,255,0),2)
cv2.imshow("Find Faces!",image)
cv2.waitKey(0)
人臉檢測結(jié)果
輸出圖片:
輸出結(jié)果:
D:\Software\Python2\python.exe E:/Code/Python/DataStructor/opencv/Demo.py
發(fā)現(xiàn)3個人臉!
python人臉識別所用的優(yōu)化算法有什么
python三步實現(xiàn)人臉識別
Face Recognition軟件包
這是世界上最簡單的人臉識別庫了。你可以通過Python引用或者命令行的形式使用它,來管理和識別人臉。
該軟件包使用dlib中最先進(jìn)的人臉識別深度學(xué)習(xí)算法,使得識別準(zhǔn)確率在《Labled Faces in the world》測試基準(zhǔn)下達(dá)到了99.38%。
它同時提供了一個叫face_recognition的命令行工具,以便你可以用命令行對一個文件夾中的圖片進(jìn)行識別操作。
特性
在圖片中識別人臉
找到圖片中所有的人臉
找到并操作圖片中的臉部特征
獲得圖片中人類眼睛、鼻子、嘴、下巴的位置和輪廓
找到臉部特征有很多超級有用的應(yīng)用場景,當(dāng)然你也可以把它用在最顯而易見的功能上:美顏功能(就像美圖秀秀那樣)。
鑒定圖片中的臉
識別圖片中的人是誰。
你甚至可以用這個軟件包做人臉的實時識別。
這里有一個實時識別的例子:
1
安裝
環(huán)境要求
Python3.3+或者Python2.7
MacOS或者Linux(Windows不做支持,但是你可以試試,也許也能運行)
安裝步驟
在MacOS或者Linux上安裝
首先,確保你安裝了dlib,以及該軟件的Python綁定接口。如果沒有的話,看這篇安裝說明:
1? ?? ?
然后,用pip安裝這個軟件包:
如果你安裝遇到問題,可以試試這個安裝好了的虛擬機:
1? ?? ?
在樹莓派2+上安裝
看這篇說明:
1? ?? ?
在Windows上安裝
雖然Windows不是官方支持的,但是有熱心網(wǎng)友寫出了一個Windows上的使用指南,請看這里:
1? ?? ?
使用已經(jīng)配置好的虛擬機(支持VMWare和VirtualBox)
看這篇說明:
1? ?? ?
使用方法
命令行接口
如果你已經(jīng)安裝了face_recognition,那么你的系統(tǒng)中已經(jīng)有了一個名為face_recognition的命令,你可以使用它對圖片進(jìn)行識別,或者對一個文件夾中的所有圖片進(jìn)行識別。
首先你需要提供一個文件夾,里面是所有你希望系統(tǒng)認(rèn)識的人的圖片。其中每個人一張圖片,圖片以人的名字命名。
然后你需要準(zhǔn)備另一個文件夾,里面是你要識別的圖片。
然后你就可以運行face_recognition命令了,把剛剛準(zhǔn)備的兩個文件夾作為參數(shù)傳入,命令就會返回需要識別的圖片中都出現(xiàn)了誰。
輸出中,識別到的每張臉都單獨占一行,輸出格式為
通過Python模塊使用
你可以通過導(dǎo)入face_recognition模塊來使用它,使用方式超級簡單,文檔在這里:
自動找到圖片中所有的臉
看看這個例子自己實踐一下:
1? ?? ?
你還可以自定義替換人類識別的深度學(xué)習(xí)模型。
注意:想獲得比較好的性能的話,你可能需要GPU加速(使用英偉達(dá)的CUDA庫)。所以編譯的時候你也需要開啟dlib的GPU加速選項。
你也可以通過這個例子實踐一下:
1? ?? ?
如果你有很多圖片和GPU,你也可以并行快速識別,看這篇文章:
1? ?? ?
自動識別人臉特征
試試這個例子:
1? ?? ?
識別人臉鑒定是哪個人
這里是一個例子:
1? ?? ?
當(dāng)前名稱:python人臉識別函數(shù)的簡單介紹
文章起源:http://fisionsoft.com.cn/article/docieeh.html