天天看點

python通過Dlib庫實作人臉68點特征點标記

1、使用到的庫

import cv2                    # 圖像處理庫
import dlib                   # 人臉識别庫
from skimage import io        # 圖像處理庫
           

2、代碼片段詳解

2.1 dlib.get_frontal_face_detector()

功能:人臉檢測畫框

參數:無

傳回值:預設的人臉檢測器

detector = dlib.get_frontal_face_detector()
print(detector)
# <dlib.fhog_object_detector object at 0x0000026536162F70>
           

2.2 dlib.shape_predictor()

功能:标記人臉關鍵點

參數:shape_predictor_68_face_landmarks.dat:68個關鍵點模型位址

傳回值:人臉關鍵點預測器

下載下傳連結:http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2

predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
print(predictor)
# <dlib.shape_predictor object at 0x00000265361F77B0>
           

2.3 io.imread()

skimage.io.imread: 直接傳回numpy.ndarray 對象,通道順序為RGB(注意cv2.imread()生成的是BGR),通道值預設範圍0-255。

img = cv2.imread("ma_si_ke.jpg")   # RGB
print(img)

[[[ 0  0  0]
  [ 0  0  0]
  [ 0  0  0]
  ...
  [ 0  0  0]
  [ 0  0  0]
  [ 0  0  0]]

 [[ 0  0  0]
  [ 0  0  0]
  [ 0  0  0]
  ...
  [ 0  0  0]
  [ 0  0  0]
  [ 0  0  0]]

 [[ 0  0  0]
  [ 0  0  0]
  [ 0  0  0]
  ...
  [ 0  0  0]
  [ 0  0  0]
  [ 0  0  0]]

 ...

 [[43 37 30]
  [42 36 29]
  [41 35 28]
  ...
  [ 9  4  5]
  [ 9  4  5]
  [ 9  4  5]]

 [[43 37 30]
  [42 36 29]
  [41 35 28]
  ...
  [ 9  4  5]
  [ 9  4  5]
  [ 9  4  5]]

 [[43 37 30]
  [42 36 29]
  [41 35 28]
  ...
  [ 9  4  5]
  [ 9  4  5]
  [ 9  4  5]]]
           

2.4 cv2.cvtColor()

cv2.cvtColor(p1,p2) 是顔色空間轉換函數,p1是需要轉換的圖檔,p2是轉換成何種格式。

cv2.COLOR_BGR2RGB 将BGR格式轉換成RGB格式

cv2.COLOR_BGR2GRAY 将BGR格式轉換成灰階圖檔

2.5 detector(img, 0)

dets = detector(img, 0)

功能:對圖像畫人臉框

參數:img:輸入的圖檔

傳回值:人臉檢測矩形框4點坐标

# 特征提取器的執行個體化
dets = detector(img, 0)
print(dets)
print("人臉數:", len(dets))

# rectangles[[(62, 62) (211, 211)]]
# 人臉數: 1
           

2.6 predictor(img, d)

shape = predictor(img, d)

功能:定位人臉關鍵點

參數:img:一個numpy ndarray,包含8位灰階或RGB圖像

d:開始内部形狀預測的邊界框

傳回值:68個關鍵點的位置

shape = predictor(img, d)
print(shape)
# <dlib.full_object_detection object at 0x0000025923EB1E30>
           

2.7 cv2.circle()

cv2.circle(img, center, radius, color[, thickness[, lineType[, shift]]])

參數說明

img:輸入的圖檔data
center:圓心位置
radius:圓的半徑
color:圓的顔色
thickness:圓形輪廓的粗細(如果為正)。負厚度表示要繪制實心圓。
lineType: 圓邊界的類型。
shift:中心坐标和半徑值中的小數位數。
           

2.8 cv2.putText()

putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]]) -> img

文本内容,起點坐标,字型,字型大小,顔色,線寬,線類型(cv2.LINE_AA)

2.9 cv2.imshow()

函數cv2.imshow() 顯示圖像,視窗會自動調整為圖像大小。第一個參數是視窗的名字,其次才是我們的圖像。你可以建立多個視窗,隻要你喜歡,但是必須給他們不同的名字。

2.10 cv2.waitKey(0)

關于cv2.waitKey()的詳細說明

3、實作代碼

import cv2                    # 圖像處理庫
import dlib                   # 人臉識别庫
from skimage import io        # 圖像處理庫

# 使用特征提取器get_frontal_face_detector
detector = dlib.get_frontal_face_detector()

# dlib的68點模型,使用官方訓練好的特征預測器
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")

# 圖檔所在路徑
img = cv2.imread("ma_si_ke.jpg")   # RGB
# img = cv2.imread() # BGR
# img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)

# 特征提取器的執行個體化
dets = detector(img, 0)
print("人臉數:", len(dets))

for k, d in enumerate(dets):
        print("第", k+1, "個人臉d的坐标:",
              "left:", d.left(),
              "right:", d.right(),
              "top:", d.top(),
              "bottom:", d.bottom())

        width = d.right() - d.left()
        heigth = d.bottom() - d.top()

        print('人臉面積為:',(width*heigth))

        # 利用預測器預測
        shape = predictor(img, d)
        # print(shape)
        # 标出68個點的位置
        for i in range(68):
            cv2.circle(img, (shape.part(i).x, shape.part(i).y), 2, (0, 255, 0), -1, 1)
            cv2.putText(img, str(i), (shape.part(i).x, shape.part(i).y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255))
        
        # 顯示一下處理的圖檔,然後銷毀視窗
        cv2.imshow('face_detector_68', img)
        cv2.waitKey(0)
           

4、成果展示

python通過Dlib庫實作人臉68點特征點标記

繼續閱讀