天天看點

人臉識别68特征點的提取,使用python+dlib

#版本python3
#建議使用anconda + pycharm 進行前期配置環境,比較簡單
#安裝教程請自行百度很簡單


import numpy as np
import cv2
import dlib
import os
detector = dlib.get_frontal_face_detector()#建立一個容器
predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')#加載一個自帶的分類器
img_path = "C:\\Users\\MSI\\Desktop\\my_py_software\\img\\" + str(1) + ".jpg"#我需要識别的圖檔位置
img = cv2.imread(img_path)#使用python-opencv讀取圖檔
img_grey = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)#使圖檔轉化為灰階圖檔
rects = detector(img_grey, 0)#傳回資訊
for i in range(len(rects)):
    landmarks = np.matrix([[p.x, p.y] for p in predictor(img_grey,rects[i]).parts()])#擷取點的坐标
    for idx, point in enumerate(landmarks):
        # 68點的坐标
        pos = (point[0, 0], point[0, 1])
        cv2.circle(img, pos, 2, (255, 0, 0), 1)#畫圈圈,(255,0,0)是圈圈的顔色
        font = cv2.FONT_HERSHEY_SIMPLEX
        cv2.putText(img, str(idx + 1), pos, font, 0.8, (0, 0, 255), 2, cv2.LINE_AA)#為圈圈标上序号
cv2.namedWindow("img", 2)
cv2.imshow("img", img)#展示
cv2.waitKey(0)
           
上一篇: 練習22