基於Dlib庫的人臉68個特徵點檢測

2021-09-29 04:36:29 字數 1640 閱讀 1967

利用dlib.get_frontal_face_detector()獲取人臉框

利用dlib.shape_predictor(『shape_predictor_68_face_landmarks.dat』)獲取人臉68個特徵點

import numpy as np

import cv2

import dlib

detector = dlib.get_frontal_face_detector(

)#獲取人臉分類器

predictor = dlib.shape_predictor(

'shape_predictor_68_face_landmarks.dat'

)#獲取人臉檢測器

# shape_predictor_68_face_landmarks.dat是進行人臉標定的模型,它是基於hog特徵的

# cv2讀取影象

)# 取灰度

img_gray = cv2.cvtcolor(img, cv2.color_rgb2gray)

# 人臉數rects

rects = detector(img_gray,0)

for i in

range

(len

(rects)):

landmarks = np.matrix(

[[p.x, p.y]

for p in predictor(img,rects[i]

).parts()]

)# 這個函式predictor(img, rect[i]).parts()尋找人臉的68個標定點

# print(landmarks)#landmarks 是乙個二維陣列

for idx, point in

enumerate

(landmarks)

:# 68點的座標

# print (point)#point 是乙個二維陣列

pos =

(point[0,

0], point[0,

1])print

(idx,pos)

# 利用cv2.circle給每個特徵點畫乙個圈,共68個

cv2.circle(img, pos,

3, color=(0

,255,0

),thickness=-1

)# 利用cv2.puttext輸出1-68

font = cv2.font_hershey_******x

cv2.puttext(img,

str(idx+1)

, pos, font,

0.4,(0

,0,255),

1,cv2.line_aa)

# 影象,文字內容, 座標 ,字型,大小,顏色,字型厚度

人臉識別Dlib 68個特徵點

前面說了怎麼開啟攝像頭框出人臉,接下來學習了一下人臉的68個特徵點 直接 import dlib import cv2 與人臉檢測相同,使用dlib自帶的frontal face detector作為人臉檢測器 detector dlib.get frontal face detector 使用官方...

dlib讀取68個人臉特徵點

可以讀取到人臉上的68個特徵點,可以利用這些特徵點做點有意思的事 coding utf 8 created on thu jan 14 16 49 37 2021 author yuyanchuan import dlib import cv2 import os predictor path sh...

基於dlib和opencv庫的人臉識別

檔名為 shape predictor 68 face landmarks.dat img cv2.imread image img cv2.cvtcolor img,cv2.color bgr2rgb cv2.circle img,center,radius,color,thickness cam...