Python基于Dlib的人臉識(shí)別系統(tǒng)的實(shí)現(xiàn)
之前已經(jīng)介紹過(guò)人臉識(shí)別的基礎(chǔ)概念,以及基于opencv的實(shí)現(xiàn)方式,今天,我們使用dlib來(lái)提取128維的人臉嵌入,并使用k臨近值方法來(lái)實(shí)現(xiàn)人臉識(shí)別。
人臉識(shí)別系統(tǒng)的實(shí)現(xiàn)流程與之前是一樣的,只是這里我們借助了dlib和face_recognition這兩個(gè)庫(kù)來(lái)實(shí)現(xiàn)。face_recognition是對(duì)dlib庫(kù)的包裝,使對(duì)dlib的使用更方便。所以首先要安裝這2個(gè)庫(kù)。
pip3 install dlib pip3 install face_recognition
然后,還要安裝imutils庫(kù)
pip3 install imutils
我們看一下項(xiàng)目的目錄結(jié)構(gòu):
. ├── dataset │ ├── alan_grant [22 entries exceeds filelimit, not opening dir] │ ├── claire_dearing [53 entries exceeds filelimit, not opening dir] │ ├── ellie_sattler [31 entries exceeds filelimit, not opening dir] │ ├── ian_malcolm [41 entries exceeds filelimit, not opening dir] │ ├── john_hammond [36 entries exceeds filelimit, not opening dir] │ └── owen_grady [35 entries exceeds filelimit, not opening dir] ├── examples │ ├── example_01.png │ ├── example_02.png │ └── example_03.png ├── output │ ├── lunch_scene_output.avi │ └── webcam_face_recognition_output.avi ├── videos │ └── lunch_scene.mp4 ├── encode_faces.py ├── encodings.pickle ├── recognize_faces_image.py ├── recognize_faces_video_file.py ├── recognize_faces_video.py └── search_bing_api.py 10 directories, 12 files
首先,提取128維的人臉嵌入:
命令如下:
python3 encode_faces.py --dataset dataset --encodings encodings.pickle -d hog
記住:如果你的電腦內(nèi)存不夠大,請(qǐng)使用hog模型進(jìn)行人臉檢測(cè),如果內(nèi)存夠大,可以使用cnn神經(jīng)網(wǎng)絡(luò)進(jìn)行人臉檢測(cè)。
看代碼:
# USAGE
# python encode_faces.py --dataset dataset --encodings encodings.pickle
# import the necessary packages
from imutils import paths
import face_recognition
import argparse
import pickle
import cv2
import os
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--dataset", required=True,
help="path to input directory of faces + images")
ap.add_argument("-e", "--encodings", required=True,
help="path to serialized db of facial encodings")
ap.add_argument("-d", "--detection-method", type=str, default="hog",
help="face detection model to use: either `hog` or `cnn`")
args = vars(ap.parse_args())
# grab the paths to the input images in our dataset
print("[INFO] quantifying faces...")
imagePaths = list(paths.list_images(args["dataset"]))
# initialize the list of known encodings and known names
knownEncodings = []
knownNames = []
# loop over the image paths
for (i, imagePath) in enumerate(imagePaths):
# extract the person name from the image path
print("[INFO] processing image {}/{}".format(i + 1,
len(imagePaths)))
name = imagePath.split(os.path.sep)[-2]
# load the input image and convert it from RGB (OpenCV ordering)
# to dlib ordering (RGB)
image = cv2.imread(imagePath)
rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# detect the (x, y)-coordinates of the bounding boxes
# corresponding to each face in the input image
boxes = face_recognition.face_locations(rgb,
model=args["detection_method"])
# compute the facial embedding for the face
encodings = face_recognition.face_encodings(rgb, boxes)
# loop over the encodings
for encoding in encodings:
# add each encoding + name to our set of known names and
# encodings
knownEncodings.append(encoding)
knownNames.append(name)
# dump the facial encodings + names to disk
print("[INFO] serializing encodings...")
data = {"encodings": knownEncodings, "names": knownNames}
f = open(args["encodings"], "wb")
f.write(pickle.dumps(data))
f.close()
輸出結(jié)果是每張圖片輸出一個(gè)人臉的128維的向量和對(duì)于的名字,并序列化到硬盤(pán),供后續(xù)人臉識(shí)別使用。
識(shí)別圖像中的人臉:
這里使用KNN方法實(shí)現(xiàn)最終的人臉識(shí)別,而不是使用SVM進(jìn)行訓(xùn)練。
命令如下:
python3 recognize_faces_image.py --encodings encodings.pickle --image examples/example_01.png
看代碼:
# USAGE
# python recognize_faces_image.py --encodings encodings.pickle --image examples/example_01.png
# import the necessary packages
import face_recognition
import argparse
import pickle
import cv2
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-e", "--encodings", required=True,
help="path to serialized db of facial encodings")
ap.add_argument("-i", "--image", required=True,
help="path to input image")
ap.add_argument("-d", "--detection-method", type=str, default="cnn",
help="face detection model to use: either `hog` or `cnn`")
args = vars(ap.parse_args())
# load the known faces and embeddings
print("[INFO] loading encodings...")
data = pickle.loads(open(args["encodings"], "rb").read())
# load the input image and convert it from BGR to RGB
image = cv2.imread(args["image"])
rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# detect the (x, y)-coordinates of the bounding boxes corresponding
# to each face in the input image, then compute the facial embeddings
# for each face
print("[INFO] recognizing faces...")
boxes = face_recognition.face_locations(rgb,
model=args["detection_method"])
encodings = face_recognition.face_encodings(rgb, boxes)
# initialize the list of names for each face detected
names = []
# loop over the facial embeddings
for encoding in encodings:
# attempt to match each face in the input image to our known
# encodings
matches = face_recognition.compare_faces(data["encodings"],
encoding)
name = "Unknown"
# check to see if we have found a match
if True in matches:
# find the indexes of all matched faces then initialize a
# dictionary to count the total number of times each face
# was matched
matchedIdxs = [i for (i, b) in enumerate(matches) if b]
counts = {}
# loop over the matched indexes and maintain a count for
# each recognized face face
for i in matchedIdxs:
name = data["names"][i]
counts[name] = counts.get(name, 0) + 1
# determine the recognized face with the largest number of
# votes (note: in the event of an unlikely tie Python will
# select first entry in the dictionary)
name = max(counts, key=counts.get)
# update the list of names
names.append(name)
# loop over the recognized faces
for ((top, right, bottom, left), name) in zip(boxes, names):
# draw the predicted face name on the image
cv2.rectangle(image, (left, top), (right, bottom), (0, 255, 0), 2)
y = top - 15 if top - 15 > 15 else top + 15
cv2.putText(image, name, (left, y), cv2.FONT_HERSHEY_SIMPLEX,
0.75, (0, 255, 0), 2)
# show the output image
cv2.imshow("Image", image)
cv2.waitKey(0)
實(shí)際效果如下:

如果要詳細(xì)了解細(xì)節(jié),請(qǐng)參考:https://www.pyimagesearch.com/2018/06/18/face-recognition-with-opencv-python-and-deep-learning/
到此這篇關(guān)于Python基于Dlib的人臉識(shí)別系統(tǒng)的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Python Dlib人臉識(shí)別內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 用Python實(shí)現(xiàn)簡(jiǎn)單的人臉識(shí)別功能步驟詳解
- python基于opencv實(shí)現(xiàn)人臉識(shí)別
- python實(shí)現(xiàn)圖片,視頻人臉識(shí)別(dlib版)
- python實(shí)現(xiàn)圖片,視頻人臉識(shí)別(opencv版)
- python調(diào)用百度API實(shí)現(xiàn)人臉識(shí)別
- 使用python-cv2實(shí)現(xiàn)Harr+Adaboost人臉識(shí)別的示例
- python3.8動(dòng)態(tài)人臉識(shí)別的實(shí)現(xiàn)示例
- 簡(jiǎn)單的Python人臉識(shí)別系統(tǒng)
- Python3 利用face_recognition實(shí)現(xiàn)人臉識(shí)別的方法
- 如何通過(guò)python實(shí)現(xiàn)人臉識(shí)別驗(yàn)證
- face++與python實(shí)現(xiàn)人臉識(shí)別簽到(考勤)功能
- 10分鐘學(xué)會(huì)使用python實(shí)現(xiàn)人臉識(shí)別(附源碼)
相關(guān)文章
pandas調(diào)整列的順序以及添加列的實(shí)現(xiàn)
這篇文章主要介紹了pandas調(diào)整列的順序以及添加列的實(shí)現(xiàn)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03
Python 25行代碼實(shí)現(xiàn)的RSA算法詳解
這篇文章主要介紹了Python 25行代碼實(shí)現(xiàn)的RSA算法,結(jié)合實(shí)例形式詳細(xì)分析了rsa加密算法的概念、原理、相關(guān)實(shí)現(xiàn)技巧與注意事項(xiàng),需要的朋友可以參考下2018-04-04
Python通過(guò)Manager方式實(shí)現(xiàn)多個(gè)無(wú)關(guān)聯(lián)進(jìn)程共享數(shù)據(jù)的實(shí)現(xiàn)
這篇文章主要介紹了Python通過(guò)Manager方式實(shí)現(xiàn)多個(gè)無(wú)關(guān)聯(lián)進(jìn)程共享數(shù)據(jù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
python 判斷文件還是文件夾的簡(jiǎn)單實(shí)例
今天小編就為大家分享一篇python 判斷文件還是文件夾的簡(jiǎn)單實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06
pyqt5+opencv?實(shí)現(xiàn)讀取視頻數(shù)據(jù)的方法
這篇文章主要介紹了pyqt5+opencv?實(shí)現(xiàn)讀取視頻數(shù)據(jù)的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01
OpenCV學(xué)習(xí)方框?yàn)V波實(shí)現(xiàn)圖像處理代碼示例
這篇文章主要為大家介紹了OpenCV學(xué)習(xí)如何使用方框?yàn)V波實(shí)現(xiàn)對(duì)圖像處理代碼示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-10-10
Python加密方法小結(jié)【md5,base64,sha1】
這篇文章主要介紹了Python加密方法,結(jié)合實(shí)例形式總結(jié)分析了md5,base64,sha1的簡(jiǎn)單加密方法,需要的朋友可以參考下2017-07-07

