dlib:68个人脸关键点检测

参考文章:图片人脸检测--Dlib版

检测效果:

dlib:68个人脸关键点检测_第1张图片

 

anaconda prompt中输入: pip install dlib安装dlib包

下载训练模型:

训练模型用于是人脸识别的关键,用于查找图片的关键点。

下载地址:http://dlib.net/files/

下载文件:shape_predictor_68_face_landmarks.dat.bz2

 

单张图片代码实现:

import cv2
import dlib
import numpy

path = "3.jpg"
img = cv2.imread(path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 
#使用默认人脸识别的模型
detector = dlib.get_frontal_face_detector()
# 获取人脸关键点预训练模型
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
 
dets = detector(gray, 1)
for face in dets:
    shape = predictor(img, face)  # 寻找人脸的68个标定点
    # 遍历所有点,打印出其坐标,并圈出来
    count = 0
    for pt in shape.parts():
        pt_pos = (pt.x, pt.y)
        count = count + 1
        if count == 38 or count == 44:
            cv2.circle(img, pt_pos, 2, (0, 255, 0), 1)
    cv2.imshow("image", img) 
    cv2.waitKey(0)
    cv2.destroyAllWindows()

视频代码实现:

import dlib
import numpy
import cv2
#打开摄像头
import cv2
cap = cv2.VideoCapture(0)
while(True):
    ret,frame = cap.read()#返回两个值,第一个为bool类型,如果读到帧返回True,如果没读到帧返回False,第二个值为帧图像
    if(ret == True):        
        gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
        dets = detector(gray, 1)
        for face in dets:
            shape = predictor(gray, face)  # 寻找人脸的68个标定点
            # 遍历所有点,打印出其坐标,并圈出来
            for pt in shape.parts():
                pt_pos = (pt.x, pt.y)
                cv2.circle(frame, pt_pos, 2, (0, 255, 0), 1)    
        cv2.imshow('frame',frame)
        if cv2.waitKey(1)==27:
            break
cap.release()
cv2.destroyAllWindows()

 

你可能感兴趣的:(tensorflow)