python利用dlib獲取人臉的68個landmark
更新時間:2019年11月27日 10:58:10 作者:明素07
這篇文章主要介紹了python利用dlib獲取人臉的68個landmark,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
(1) 單人臉情況
import cv2 import dlib path = "1.jpg" img = cv2.imread(path) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #人臉檢測畫框 detector = dlib.get_frontal_face_detector() # 獲取人臉關(guān)鍵點(diǎn)檢測器 predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") #獲取人臉框位置信息 dets = detector(gray, 1)#1表示采樣(upsample)次數(shù) 0識別的人臉少點(diǎn),1識別的多點(diǎn),2識別的更多,小臉也可以識別 for face in dets: shape = predictor(img, face) # 尋找人臉的68個標(biāo)定點(diǎn) # 遍歷所有點(diǎn),打印出其坐標(biāo),并圈出來 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) #人臉檢測畫框 detector = dlib.get_frontal_face_detector() # 獲取人臉關(guān)鍵點(diǎn)檢測器 predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") #獲取人臉框位置信息 dets = detector(gray, 1)#1表示采樣(upsample)次數(shù) 0識別的人臉少點(diǎn),1識別的多點(diǎn),2識別的更多,小臉也可以識別 for i in range(len(dets)): shape = predictor(img, dets[i]) # 尋找人臉的68個標(biāo)定點(diǎn) # 遍歷所有點(diǎn),打印出其坐標(biāo),并圈出來 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í)時識別標(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()方法的兩個返回值。其中ret是布爾值,如果讀取幀是正確的則返回True,如果文件讀取到結(jié)尾,它的返回值就為False。frame就是每一幀的圖像,是個三維矩陣。 # 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)閉所有圖像窗口
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- python 基于dlib庫的人臉檢測的實(shí)現(xiàn)
- python dlib人臉識別代碼實(shí)例
- Python3利用Dlib實(shí)現(xiàn)攝像頭實(shí)時人臉檢測和平鋪顯示示例
- Linux下python與C++使用dlib實(shí)現(xiàn)人臉檢測
- Python3利用Dlib19.7實(shí)現(xiàn)攝像頭人臉識別的方法
- python3+dlib實(shí)現(xiàn)人臉識別和情緒分析
- python 3利用Dlib 19.7實(shí)現(xiàn)攝像頭人臉檢測特征點(diǎn)標(biāo)定
- python3利用Dlib19.7實(shí)現(xiàn)人臉68個特征點(diǎn)標(biāo)定
- 學(xué)習(xí)Python3 Dlib19.7進(jìn)行人臉面部識別
- Python3結(jié)合Dlib實(shí)現(xiàn)人臉識別和剪切
相關(guān)文章
Python?scipy利用快速傅里葉變換實(shí)現(xiàn)濾波
這篇文章主要為大家詳細(xì)介紹了Python?scipy如何利用快速傅里葉變換實(shí)現(xiàn)濾波,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01Pandas中DataFrame.head()函數(shù)的具體使用
DataFrame.head()是Pandas庫中一個非常重要的函數(shù),用于返回DataFrame對象的前n行,本文主要介紹了Pandas中DataFrame.head()函數(shù)的具體使用,感興趣的可以了解一下2024-07-07python基于Pandas讀寫MySQL數(shù)據(jù)庫
這篇文章主要介紹了python基于Pandas讀寫MySQL數(shù)據(jù)庫,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-04-04Python實(shí)現(xiàn)考試自動答題的腳本分享
最近這段時間天氣正正好,不冷不熱,是學(xué)習(xí)考駕照的好時機(jī)。為了幫助大家能夠順利獲得駕照,小編為大家準(zhǔn)備了駕照考試的自動答題小程序,希望對大家有所幫助2023-03-03pytorch 實(shí)現(xiàn)查看網(wǎng)絡(luò)中的參數(shù)
今天小編就為大家分享一篇pytorch 實(shí)現(xiàn)查看網(wǎng)絡(luò)中的參數(shù),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01Python基于生成器迭代實(shí)現(xiàn)的八皇后問題示例
這篇文章主要介紹了Python基于生成器迭代實(shí)現(xiàn)的八皇后問題,簡單描述了八皇后問題,并結(jié)合實(shí)例形式分析了Python基于生成器迭代解決八皇后問題的相關(guān)操作技巧,需要的朋友可以參考下2018-05-05