參考:
安裝dlib:cmd裡輸入,pip install dlib即可自動安裝
# coding=utf-8
import cv2
import dlib
detector = dlib.get_frontal_face_detector()
def discern(img):
gray = cv2.cvtcolor(img, cv2.color_bgr2gray)
dets = detector(gray, 1)
for face in dets:
left = face.left()
top = face.top()
right = face.right()
bottom = face.bottom()
cv2.rectangle(img, (left, top), (right, bottom), (0, 255, 0), 2)
cv2.imshow("image", img)
cap = cv2.videocapture(0)
while (1):
ret, img = cap.read()
discern(img)
if cv2.waitkey(1) & 0xff == ord('q'):
break
cap.release()
cv2.destroyallwindows()
人臉識別流程如下:
人臉實時識別**如下:
# coding=utf-8
import sys, os, dlib, glob, numpy
import cv2
# if len(sys.ar**) != 5:
# print("請檢查引數是否正確")
# exit()
# 1.人臉關鍵點檢測器
# predictor_path = sys.ar**[1]
predictor_path = "./dlib_data/shape_predictor_68_face_landmarks.dat"
# 2.人臉識別模型
# face_rec_model_path = sys.ar**[2]
face_rec_model_path = "./dlib_data/dlib_face_recognition_resnet_model_v1.dat"
# 3.候選人臉資料夾
# faces_folder_path = sys.ar**[3]
faces_folder_path = "./candidate-faces"
# 4.需要識別的人臉
# 1.載入正臉檢測器
detector = dlib.get_frontal_face_detector()
# 2.載入人臉關鍵點檢測器
sp = dlib.shape_predictor(predictor_path)
# 3.載入人臉識別模型
facerec = dlib.face_recognition_model_v1(face_rec_model_path)
win = dlib.image_window()
# 候選人名單
candidate = ['unknown1', 'unknown2', 'shishi', 'unknown4', 'bingbing', 'feifei','your name',]
# 候選人臉描述子list
descriptors =
# 對資料夾下的每乙個人臉進行:人臉檢測、關鍵點檢測、描述子提取
# 人臉檢測
dets = detector(img, 1)
print("number of faces detected:{}".format(len(dets)))
for k, d in enumerate(dets):
# 關鍵點檢測
shape = sp(img, d)
# 畫出人臉區域和關鍵點
win.clear_overlay()
win.add_overlay(d)
win.add_overlay(shape)
#描述運算元提取,128d向量
face_descriptor = facerec.compute_face_descriptor(img, shape)
# 轉換為numpy array
v = numpy.array(face_descriptor)
# 對需要識別人臉進行同樣處理
def discern(img):
dets = detector(img, 1)
dist =
for face in dets:
left = face.left()
top = face.top()
right = face.right()
bottom = face.bottom()
cv2.rectangle(img, (left, top), (right, bottom), (0, 0, 255), 2)
for k, d in enumerate(dets):
shape = sp(img, d)
face_descriptor = facerec.compute_face_descriptor(img, shape)
d_test = numpy.array(face_descriptor)
# 計算歐式距離
for i in descriptors:
dist_ = numpy.linalg.norm(i-d_test)
c_d = dict(zip(candidate, dist))
cd_sorted = sorted(c_d.items(), key=lambda d:d[1])
# print("\n the person is:{}".format(cd_sorted[0][0]))
# dlib.hit_enter_to_continue()
if('right' in locals()):
img = cv2.puttext(img, cd_sorted[0][0], (right, bottom), cv2.font_hershey_******x, 1.2, (0, 0, 255), 2)
cv2.imshow("image", img)
cap = cv2.videocapture(0)
while(1):
ret, img = cap.read()
discern(img)
if cv2.waitkey(1) & 0xff == ord('q'):
break
cap.release()
cv2.destroyallwindows()
存在問題:
•速度比較慢,實時性不好(可以考慮gpu加速)
•**也可以識別,可以研究下活體檢測
dlib人臉識別
的編寫在jupyter notebook中來完成 jupyter notebook是乙個工具 pip install jupyter 安裝使用 如何啟動 命令列輸入 jupyter notebook 前提,環境變數配置成功 dlib安裝 pip install dlib dlib有不同的版本,最新版...
python相簿中的人臉聚類 Dlib 人臉聚類
人臉聚類 人臉聚類屬於無監督學習.當你有許多未標記的待分類的 時,使用人臉聚類是非常有用的.其 python 指令碼位於 python examples face clustering.py.待分類目錄下有兩張神仙的肖像和兩張觀海的肖像,使用如下 可以將神仙和觀海分開.import os impor...
dlib系列 人臉檢測
dlib 是乙個機器學習庫,採用c 編寫 提供c 和python介面 裡面包含 許多常用機器學習演算法。總之就是很好的框架,是官網給的教程。coding utf 8 import sys import dlib import cv2 from skimage import io 檢測器 detect...