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è)畫(huà)框 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è)畫(huà)框 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)#等待鍵盤(pán)輸入 cv2.destroyAllWindows()
(3) 獲取電腦攝像頭實(shí)時(shí)識(shí)別標(biāo)定
import cv2 import dlib import numpy as np cap = cv2.VideoCapture(0)#打開(kāi)筆記本的內(nèi)置攝像頭,若參數(shù)是視頻文件路徑則打開(kāi)視頻 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?scipy利用快速傅里葉變換實(shí)現(xiàn)濾波
這篇文章主要為大家詳細(xì)介紹了Python?scipy如何利用快速傅里葉變換實(shí)現(xiàn)濾波,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01Pandas中DataFrame.head()函數(shù)的具體使用
DataFrame.head()是Pandas庫(kù)中一個(gè)非常重要的函數(shù),用于返回DataFrame對(duì)象的前n行,本文主要介紹了Pandas中DataFrame.head()函數(shù)的具體使用,感興趣的可以了解一下2024-07-07python基于Pandas讀寫(xiě)MySQL數(shù)據(jù)庫(kù)
這篇文章主要介紹了python基于Pandas讀寫(xiě)MySQL數(shù)據(jù)庫(kù),幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-04-04Python實(shí)現(xiàn)考試自動(dòng)答題的腳本分享
最近這段時(shí)間天氣正正好,不冷不熱,是學(xué)習(xí)考駕照的好時(shí)機(jī)。為了幫助大家能夠順利獲得駕照,小編為大家準(zhǔn)備了駕照考試的自動(dòng)答題小程序,希望對(duì)大家有所幫助2023-03-03pytorch 實(shí)現(xiàn)查看網(wǎng)絡(luò)中的參數(shù)
今天小編就為大家分享一篇pytorch 實(shí)現(xiàn)查看網(wǎng)絡(luò)中的參數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01Python基于生成器迭代實(shí)現(xiàn)的八皇后問(wèn)題示例
這篇文章主要介紹了Python基于生成器迭代實(shí)現(xiàn)的八皇后問(wèn)題,簡(jiǎn)單描述了八皇后問(wèn)題,并結(jié)合實(shí)例形式分析了Python基于生成器迭代解決八皇后問(wèn)題的相關(guān)操作技巧,需要的朋友可以參考下2018-05-05Python 函數(shù)基礎(chǔ)知識(shí)匯總
Python中的函數(shù),無(wú)論是命名函數(shù),還是匿名函數(shù),都是語(yǔ)句和表達(dá)式的集合。函數(shù)可以作為參數(shù)傳遞給其他函數(shù),這些以其他函數(shù)作為參數(shù)的函數(shù)通常稱(chēng)為更高階函數(shù),這就構(gòu)成了函數(shù)式編程中一個(gè)非常重要的部分。2018-03-03