基于dlib库实现人脸特征值提取

一、安装Dlib库

管理员运行cmd,使用pip安装

pip install dlib

基于dlib库实现人脸特征值提取_第1张图片

二、OpenCV安装

使用pip安装

pip3 install opencv_python

三、提取人脸特征点

import dlib
import numpy as np
import cv2
predictor = dlib.shape_predictor("D:\\test\\shape_predictor_68_face_landmarks.dat")
# predictor_path = "shape_predictor_68_face_landmarks.dat"
detector = dlib.get_frontal_face_detector()
# predictor = dlib.shape_predictor(predictor_path)
points_keys = []

captureframe = cv2.VideoCapture(0, cv2.CAP_DSHOW)
while (1):
    ReturnValue, frame = captureframe.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    rects = detector(gray, 1)
    for i in range(len(rects)):
        landmarks = np.matrix([[p.x, p.y] for p in predictor(gray, rects[i]).parts()])
        for idx, point in enumerate(landmarks):
            pos = (point[0, 0], point[0, 1])
            points_keys.append([point[0, 0], point[0, 1]])
            cv2.circle(frame, pos, 2, (255, 0, 0), -1)

    cv2.imshow('frame', frame)
    cv2.waitKey(5)

运行结果:
基于dlib库实现人脸特征值提取_第2张图片

四、在眼睛处绘制黑色的实心圆

import dlib
import numpy as np
import cv2
predictor = dlib.shape_predictor("D:\\test\\shape_predictor_68_face_landmarks.dat")
# predictor_path = "shape_predictor_68_face_landmarks.dat"
detector = dlib.get_frontal_face_detector()
# predictor = dlib.shape_predictor(predictor_path)
points_keys = []

captureframe = cv2.VideoCapture(0, cv2.CAP_DSHOW)
while (1):
    ReturnValue, frame = captureframe.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    rects = detector(gray, 1)
    for i in range(len(rects)):
        landmarks = np.matrix([[p.x, p.y] for p in predictor(gray, rects[i]).parts()])
        for idx, point in enumerate(landmarks):
            pos = (point[0, 0], point[0, 1])
            points_keys.append([point[0, 0], point[0, 1]])
            cv2.circle(frame, pos, 2, (255, 0, 0), -1)

    cv2.imshow('frame', frame)
    cv2.waitKey(5)
   def painting_sunglasses(img,detector,predictor):
    #给人脸带上墨镜
    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()])
        right_eye_x=0
        right_eye_y=0
        left_eye_x=0
        left_eye_y=0
        for i in range(36,42):#右眼范围
            #将坐标相加
            right_eye_x+=landmarks[i][0,0]
            right_eye_y+=landmarks[i][0,1]
        #取眼睛的中点坐标
        pos_right=(int(right_eye_x/6),int(right_eye_y/6))
        cv2.circle(img=img, center=pos_right, radius=30, color=(0,0,0),thickness=-1)
        for i in range(42,48):#左眼范围
           #将坐标相加
            left_eye_x+=landmarks[i][0,0]
            left_eye_y+=landmarks[i][0,1]
        #取眼睛的中点坐标
        pos_left=(int(left_eye_x/6),int(left_eye_y/6))
        cv2.circle(img=img, center=pos_left, radius=30, color=(0,0,0),thickness=-1)

运行结果:
基于dlib库实现人脸特征值提取_第3张图片

五、总结

本文简略地介绍了dlib库与OpenCV,并初步尝试使用dlib库对人脸的特征值进行提取。

六、参考链接

https://blog.csdn.net/jacke121/article/details/116117697?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522163669341616780255287652%2522%252C%2522scm%2522%253A%252220140713.130102334…%2522%257D&request_id=163669341616780255287652&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2alltop_yy~default-1-116117697.first_rank_v2_pc_rank_v29&utm_term=%E4%BA%BA%E8%84%B8%E8%AF%86%E5%88%AB&spm=1018.2226.3001.4187

你可能感兴趣的:(opencv,计算机视觉,python)