Python 幾行代碼即可實(shí)現(xiàn)人臉識(shí)別
摘要:一行代碼實(shí)現(xiàn)人臉識(shí)別
- 首先你需要提供一個(gè)文件夾,里面是所有你希望系統(tǒng)認(rèn)識(shí)的人的圖片。其中每個(gè)人一張圖片,圖片以人的名字命名。
- 接下來(lái),你需要準(zhǔn)備另一個(gè)文件夾,里面是你要識(shí)別的圖片。
- 然后你就可以運(yùn)行face_recognition命令了,把剛剛準(zhǔn)備的兩個(gè)文件夾作為參數(shù)傳入,命令就會(huì)返回需要識(shí)別的圖片中都出現(xiàn)了誰(shuí),一行代碼足以!??!
正文:
環(huán)境要求:
- Ubuntu17.10
- Python 2.7.14
環(huán)境搭建:
1.安裝 Ubuntu17.10 > 安裝步驟在這里
2.安裝 Python2.7.14 (Ubuntu17.10 默認(rèn)Python版本為2.7.14)
3.安裝 git 、cmake 、 python-pip
#安裝 git $ sudo apt-get install -y git # 安裝 cmake $ sudo apt-get install -y cmake # 安裝 python-pip $ sudo apt-get install -y python-pip
4.安裝編譯dlib
安裝face_recognition這個(gè)之前需要先安裝編譯dlib
# 編譯dlib前先安裝 boost $ sudo apt-get install libboost-all-dev # 開始編譯dlib # 克隆dlib源代碼 $ git clone https://github.com/davisking/dlib.git $ cd dlib $ mkdir build $ cd build $ cmake .. -DDLIB_USE_CUDA=0 -DUSE_AVX_INSTRUCTIONS=1 $ cmake --build .(注意中間有個(gè)空格) $ cd .. $ python setup.py install --yes USE_AVX_INSTRUCTIONS --no DLIB_USE_CUDA
5.安裝 face_recognition
# 安裝 face_recognition $ pip install face_recognition # 安裝face_recognition過(guò)程中會(huì)自動(dòng)安裝 numpy、scipy 等
環(huán)境搭建完成后,在終端輸入 face_recognition 命令查看是否成功

實(shí)現(xiàn)人臉識(shí)別:
示例一(1行代碼實(shí)現(xiàn)人臉識(shí)別)
1.首先你需要提供一個(gè)文件夾,里面是所有你希望系統(tǒng)認(rèn)識(shí)的人的圖片。其中每個(gè)人一張圖片,圖片以人的名字命名:
known_people文件夾下有babe、成龍、容祖兒的照片

2.接下來(lái),你需要準(zhǔn)備另一個(gè)文件夾,里面是你要識(shí)別的圖片: unknown_pic文件夾下是要識(shí)別的圖片,其中韓紅是機(jī)器不認(rèn)識(shí)的

3.然后你就可以運(yùn)行face_recognition命令了,把剛剛準(zhǔn)備的兩個(gè)文件夾作為參數(shù)傳入,命令就會(huì)返回需要識(shí)別的圖片中都出現(xiàn)了誰(shuí):

識(shí)別成功?。?!
示例二(識(shí)別圖片中的所有人臉并顯示出來(lái))
# filename : find_faces_in_picture.py
# -*- coding: utf-8 -*-
# 導(dǎo)入pil模塊 ,可用命令安裝 apt-get install python-Imaging
from PIL import Image
# 導(dǎo)入face_recogntion模塊,可用命令安裝 pip install face_recognition
import face_recognition
# 將jpg文件加載到numpy 數(shù)組中
image = face_recognition.load_image_file("/opt/face/unknown_pic/all_star.jpg")
# 使用默認(rèn)的給予HOG模型查找圖像中所有人臉
# 這個(gè)方法已經(jīng)相當(dāng)準(zhǔn)確了,但還是不如CNN模型那么準(zhǔn)確,因?yàn)闆](méi)有使用GPU加速
# 另請(qǐng)參見(jiàn): find_faces_in_picture_cnn.py
face_locations = face_recognition.face_locations(image)
# 使用CNN模型
# face_locations = face_recognition.face_locations(image, number_of_times_to_upsample=0, model="cnn")
# 打印:我從圖片中找到了 多少 張人臉
print("I found {} face(s) in this photograph.".format(len(face_locations)))
# 循環(huán)找到的所有人臉
for face_location in face_locations:
# 打印每張臉的位置信息
top, right, bottom, left = face_location
print("A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom, right))
# 指定人臉的位置信息,然后顯示人臉圖片
face_image = image[top:bottom, left:right]
pil_image = Image.fromarray(face_image)
pil_image.show()
如下圖為用于識(shí)別的圖片
# 執(zhí)行python文件 $ python find_faces_in_picture.py
從圖片中識(shí)別出7張人臉,并顯示出來(lái),如下圖

示例三(自動(dòng)識(shí)別人臉特征)
# filename : find_facial_features_in_picture.py
# -*- coding: utf-8 -*-
# 導(dǎo)入pil模塊 ,可用命令安裝 apt-get install python-Imaging
from PIL import Image, ImageDraw
# 導(dǎo)入face_recogntion模塊,可用命令安裝 pip install face_recognition
import face_recognition
# 將jpg文件加載到numpy 數(shù)組中
image = face_recognition.load_image_file("biden.jpg")
#查找圖像中所有面部的所有面部特征
face_landmarks_list = face_recognition.face_landmarks(image)
print("I found {} face(s) in this photograph.".format(len(face_landmarks_list)))
for face_landmarks in face_landmarks_list:
#打印此圖像中每個(gè)面部特征的位置
facial_features = [
'chin',
'left_eyebrow',
'right_eyebrow',
'nose_bridge',
'nose_tip',
'left_eye',
'right_eye',
'top_lip',
'bottom_lip'
]
for facial_feature in facial_features:
print("The {} in this face has the following points: {}".format(facial_feature, face_landmarks[facial_feature]))
#讓我們?cè)趫D像中描繪出每個(gè)人臉特征!
pil_image = Image.fromarray(image)
d = ImageDraw.Draw(pil_image)
for facial_feature in facial_features:
d.line(face_landmarks[facial_feature], width=5)
pil_image.show()
自動(dòng)識(shí)別出人臉特征(輪廓)

示例四(識(shí)別人臉鑒定是哪個(gè)人)
# filename : recognize_faces_in_pictures.py
# -*- conding: utf-8 -*-
# 導(dǎo)入face_recogntion模塊,可用命令安裝 pip install face_recognition
import face_recognition
#將jpg文件加載到numpy數(shù)組中
babe_image = face_recognition.load_image_file("/opt/face/known_people/babe.jpeg")
Rong_zhu_er_image = face_recognition.load_image_file("/opt/face/known_people/Rong zhu er.jpg")
unknown_image = face_recognition.load_image_file("/opt/face/unknown_pic/babe2.jpg")
#獲取每個(gè)圖像文件中每個(gè)面部的面部編碼
#由于每個(gè)圖像中可能有多個(gè)面,所以返回一個(gè)編碼列表。
#但是由于我知道每個(gè)圖像只有一個(gè)臉,我只關(guān)心每個(gè)圖像中的第一個(gè)編碼,所以我取索引0。
babe_face_encoding = face_recognition.face_encodings(babe_image)[0]
Rong_zhu_er_face_encoding = face_recognition.face_encodings(Rong_zhu_er_image)[0]
unknown_face_encoding = face_recognition.face_encodings(unknown_image)[0]
known_faces = [
babe_face_encoding,
Rong_zhu_er_face_encoding
]
#結(jié)果是True/false的數(shù)組,未知面孔known_faces陣列中的任何人相匹配的結(jié)果
results = face_recognition.compare_faces(known_faces, unknown_face_encoding)
print("這個(gè)未知面孔是 Babe 嗎? {}".format(results[0]))
print("這個(gè)未知面孔是 容祖兒 嗎? {}".format(results[1]))
print("這個(gè)未知面孔是 我們從未見(jiàn)過(guò)的新面孔嗎? {}".format(not True in results))
顯示結(jié)果下如圖

示例五(識(shí)別人臉特征并美顏)
# filename : digital_makeup.py
# -*- coding: utf-8 -*-
# 導(dǎo)入pil模塊 ,可用命令安裝 apt-get install python-Imaging
from PIL import Image, ImageDraw
# 導(dǎo)入face_recogntion模塊,可用命令安裝 pip install face_recognition
import face_recognition
#將jpg文件加載到numpy數(shù)組中
image = face_recognition.load_image_file("biden.jpg")
#查找圖像中所有面部的所有面部特征
face_landmarks_list = face_recognition.face_landmarks(image)
for face_landmarks in face_landmarks_list:
pil_image = Image.fromarray(image)
d = ImageDraw.Draw(pil_image, 'RGBA')
#讓眉毛變成了一場(chǎng)噩夢(mèng)
d.polygon(face_landmarks['left_eyebrow'], fill=(68, 54, 39, 128))
d.polygon(face_landmarks['right_eyebrow'], fill=(68, 54, 39, 128))
d.line(face_landmarks['left_eyebrow'], fill=(68, 54, 39, 150), width=5)
d.line(face_landmarks['right_eyebrow'], fill=(68, 54, 39, 150), width=5)
#光澤的嘴唇
d.polygon(face_landmarks['top_lip'], fill=(150, 0, 0, 128))
d.polygon(face_landmarks['bottom_lip'], fill=(150, 0, 0, 128))
d.line(face_landmarks['top_lip'], fill=(150, 0, 0, 64), width=8)
d.line(face_landmarks['bottom_lip'], fill=(150, 0, 0, 64), width=8)
#閃耀眼睛
d.polygon(face_landmarks['left_eye'], fill=(255, 255, 255, 30))
d.polygon(face_landmarks['right_eye'], fill=(255, 255, 255, 30))
#涂一些眼線
d.line(face_landmarks['left_eye'] + [face_landmarks['left_eye'][0]], fill=(0, 0, 0, 110), width=6)
d.line(face_landmarks['right_eye'] + [face_landmarks['right_eye'][0]], fill=(0, 0, 0, 110), width=6)
pil_image.show()
美顏前后對(duì)比如下圖:

結(jié)尾:
以上就是本文的全部?jī)?nèi)容了,大家喜歡的記得點(diǎn)點(diǎn)贊!

到此這篇關(guān)于Python 幾行代碼即可實(shí)現(xiàn)人臉識(shí)別 的文章就介紹到這了,更多相關(guān)Python 人臉識(shí)別內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 基于Python實(shí)現(xiàn)簡(jiǎn)單的人臉識(shí)別系統(tǒng)
- Python實(shí)現(xiàn)人臉識(shí)別
- 基于Python搭建人臉識(shí)別考勤系統(tǒng)
- 使用Python實(shí)現(xiàn)簡(jiǎn)單的人臉識(shí)別功能(附源碼)
- Python基于pyopencv人臉識(shí)別并繪制GUI界面
- Python人臉識(shí)別之微笑檢測(cè)
- 基于Python實(shí)現(xiàn)人臉識(shí)別和焦點(diǎn)人物檢測(cè)功能
- Python摸魚神器之利用樹莓派opencv人臉識(shí)別自動(dòng)控制電腦顯示桌面
- 教你如何用Python實(shí)現(xiàn)人臉識(shí)別(含源代碼)
相關(guān)文章
python中time tzset()函數(shù)實(shí)例用法
在本篇文章里小編給大家整理的是一篇關(guān)于python中time tzset()函數(shù)實(shí)例用法內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-02-02
Python requests及aiohttp速度對(duì)比代碼實(shí)例
這篇文章主要介紹了Python requests及aiohttp速度對(duì)比代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
Python實(shí)現(xiàn)亂序文件重新命名編號(hào)
這篇文章主要為大家詳細(xì)介紹一下Python的一個(gè)神操作,那就是實(shí)現(xiàn)亂序文件重新命名編號(hào)功能,文中的示例代碼講解詳細(xì),感興趣的可以嘗試一下2022-08-08
pytorch .detach() .detach_() 和 .data用于切斷反向傳播的實(shí)現(xiàn)
這篇文章主要介紹了pytorch .detach() .detach_() 和 .data用于切斷反向傳播的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Python詳解如何動(dòng)態(tài)給對(duì)象增加屬性和方法
python是動(dòng)態(tài)語(yǔ)?,動(dòng)態(tài)編程語(yǔ)?是?級(jí)程序設(shè)計(jì)語(yǔ)?的?個(gè)類別,在計(jì)算機(jī)科學(xué)領(lǐng)域已被?泛應(yīng)?。它是?類在?運(yùn)?時(shí)可以改變其結(jié)構(gòu)?的語(yǔ)??:例如新的函數(shù)、對(duì)象、甚?代碼可以被引進(jìn),已有的函數(shù)可以被刪除或是其他結(jié)構(gòu)上的變化2022-07-07
使用Python腳本來(lái)控制Windows Azure的簡(jiǎn)單教程
這篇文章主要介紹了使用Python腳本來(lái)控制Windows Azure的簡(jiǎn)單教程,由于微軟官方提供了Python SDK,使得用戶自己用Python控制Azure成為了可能,需要的朋友可以參考下2015-04-04
一個(gè)計(jì)算身份證號(hào)碼校驗(yàn)位的Python小程序
閑著無(wú)事,便想寫個(gè)實(shí)用點(diǎn)的Python小程序,如何計(jì)算機(jī)身份證號(hào)碼的校驗(yàn)位,這樣的文章在網(wǎng)上一抓一大把,這里僅簡(jiǎn)單介紹下吧2014-08-08
Python?colorama?彩色打印實(shí)現(xiàn)代碼
這篇文章主要介紹了Python?colorama?彩色打印實(shí)現(xiàn)代碼,將介紹的類為Back,?它實(shí)現(xiàn)了與?Fore?類相同的九個(gè)關(guān)鍵字:BLACK、RED、GREEN、YELLOW、BLUE、MAGENTA、CYAN、WHITE、RESET,感興趣的朋友一起看看吧2022-04-04

