python利用dlib獲取人臉的68個(gè)landmark
(1) 單人臉情況
import cv2
import dlib
path = "1.jpg"
img = cv2.imread(path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#人臉檢測(cè)畫框
detector = dlib.get_frontal_face_detector()
# 獲取人臉關(guān)鍵點(diǎn)檢測(cè)器
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
#獲取人臉框位置信息
dets = detector(gray, 1)#1表示采樣(upsample)次數(shù) 0識(shí)別的人臉少點(diǎn),1識(shí)別的多點(diǎn),2識(shí)別的更多,小臉也可以識(shí)別
for face in dets:
shape = predictor(img, face) # 尋找人臉的68個(gè)標(biāo)定點(diǎn)
# 遍歷所有點(diǎn),打印出其坐標(biāo),并圈出來(lái)
for pt in shape.parts():
pt_pos = (pt.x, pt.y)
cv2.circle(img, pt_pos, 2, (0, 0, 255), 1)#img, center, radius, color, thickness
cv2.imshow("image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
(2) 多人臉情況
import cv2
import dlib
path1 = "zxc.jpg"
img = cv2.imread(path1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#人臉檢測(cè)畫框
detector = dlib.get_frontal_face_detector()
# 獲取人臉關(guān)鍵點(diǎn)檢測(cè)器
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
#獲取人臉框位置信息
dets = detector(gray, 1)#1表示采樣(upsample)次數(shù) 0識(shí)別的人臉少點(diǎn),1識(shí)別的多點(diǎn),2識(shí)別的更多,小臉也可以識(shí)別
for i in range(len(dets)):
shape = predictor(img, dets[i]) # 尋找人臉的68個(gè)標(biāo)定點(diǎn)
# 遍歷所有點(diǎn),打印出其坐標(biāo),并圈出來(lái)
for pt in shape.parts():
pt_pos = (pt.x, pt.y)
cv2.circle(img, pt_pos, 2, (0, 0, 255), 1)#img, center, radius, color, thickness
cv2.imshow("image", img)
cv2.waitKey(0)#等待鍵盤輸入
cv2.destroyAllWindows()
(3) 獲取電腦攝像頭實(shí)時(shí)識(shí)別標(biāo)定
import cv2
import dlib
import numpy as np
cap = cv2.VideoCapture(0)#打開筆記本的內(nèi)置攝像頭,若參數(shù)是視頻文件路徑則打開視頻
cap.isOpened()
def key_points(img):
points_keys = []
PREDICTOR_PATH = "shape_predictor_68_face_landmarks.dat"
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(PREDICTOR_PATH)
rects = detector(img,1)
for i in range(len(rects)):
landmarks = np.matrix([[p.x,p.y] for p in predictor(img,rects[i]).parts()])
for point in landmarks:
pos = (point[0,0],point[0,1])
points_keys.append(pos)
cv2.circle(img,pos,2,(255,0,0),-1)
return img
while(True):
ret, frame = cap.read()#按幀讀取視頻,ret,frame是cap.read()方法的兩個(gè)返回值。其中ret是布爾值,如果讀取幀是正確的則返回True,如果文件讀取到結(jié)尾,它的返回值就為False。frame就是每一幀的圖像,是個(gè)三維矩陣。
# gray = cv2.cvtColor(frame)
face_key = key_points(frame)
cv2.imshow('frame',face_key)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()#釋放攝像頭
cv2.destroyAllWindows()#關(guān)閉所有圖像窗口
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python 基于dlib庫(kù)的人臉檢測(cè)的實(shí)現(xiàn)
- python dlib人臉識(shí)別代碼實(shí)例
- Python3利用Dlib實(shí)現(xiàn)攝像頭實(shí)時(shí)人臉檢測(cè)和平鋪顯示示例
- Linux下python與C++使用dlib實(shí)現(xiàn)人臉檢測(cè)
- Python3利用Dlib19.7實(shí)現(xiàn)攝像頭人臉識(shí)別的方法
- python3+dlib實(shí)現(xiàn)人臉識(shí)別和情緒分析
- python 3利用Dlib 19.7實(shí)現(xiàn)攝像頭人臉檢測(cè)特征點(diǎn)標(biāo)定
- python3利用Dlib19.7實(shí)現(xiàn)人臉68個(gè)特征點(diǎn)標(biāo)定
- 學(xué)習(xí)Python3 Dlib19.7進(jìn)行人臉面部識(shí)別
- Python3結(jié)合Dlib實(shí)現(xiàn)人臉識(shí)別和剪切
相關(guān)文章
python解決Missing 1 required positional ar
這篇文章主要介紹了python解決Missing 1 required positional argument報(bào)錯(cuò)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
用python實(shí)現(xiàn)一個(gè)簡(jiǎn)單計(jì)算器(完整DEMO)
這篇文章主要介紹了用python實(shí)現(xiàn)一個(gè)簡(jiǎn)單計(jì)算器(完整DEMO),需要的朋友可以參考下2020-10-10
python統(tǒng)計(jì)字符的個(gè)數(shù)代碼實(shí)例
在本篇文章里小編給大家整理了關(guān)于python統(tǒng)計(jì)字符的個(gè)數(shù)代碼實(shí)例內(nèi)容,需要的朋友們可以參考下。2020-02-02
python爬蟲入門教程之點(diǎn)點(diǎn)美女圖片爬蟲代碼分享
這篇文章主要介紹了python爬蟲入門教程之點(diǎn)點(diǎn)美女圖片爬蟲代碼分享,本文以采集抓取點(diǎn)點(diǎn)網(wǎng)美女圖片為例,需要的朋友可以參考下2014-09-09
Python3通過(guò)chmod修改目錄或文件權(quán)限的方法示例
這篇文章主要介紹了Python3通過(guò)chmod修改目錄或文件權(quán)限的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
基于Python3 逗號(hào)代碼 和 字符圖網(wǎng)格(詳談)
下面小編就為大家?guī)?lái)一篇基于Python3 逗號(hào)代碼 和 字符圖網(wǎng)格(詳談)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06
Python中turtle.write方法使用說(shuō)明
turtle模塊以面向?qū)ο蠛兔嫦蜻^(guò)程的方式提供turtle圖形基元,由于它使用Tkinter作為基礎(chǔ)圖形,因此需要安裝有Tk支持的Python版本,下面這篇文章主要給大家介紹了關(guān)于Python中turtle.write方法使用說(shuō)明的相關(guān)資料,需要的朋友可以參考下2022-02-02
使用Python構(gòu)建一個(gè)Hexo博客發(fā)布工具
雖然Hexo的命令行工具非常強(qiáng)大,但對(duì)于日常的博客撰寫和發(fā)布過(guò)程,我總覺(jué)得缺少一個(gè)直觀的圖形界面來(lái)簡(jiǎn)化操作,下面我們就來(lái)看看如何使用Python構(gòu)建一個(gè)Hexo博客發(fā)布工具吧2025-04-04
windows下添加Python環(huán)境變量的方法匯總
默認(rèn)情況下,在windows下安裝python之后,系統(tǒng)并不會(huì)自動(dòng)添加相應(yīng)的環(huán)境變量。此時(shí)不能在命令行直接使用python命令。今天我們就來(lái)看下,如何簡(jiǎn)單快捷的在windows下添加Python環(huán)境變量2018-05-05

