PYTHON | 基于dlib的68点面部特征点人脸检测

环境:


pyhton == 3.6
dlib == 1.19.10
scipy
scikit-image
numpy
imutils
opencv


环境安装:


create - n face python=3.6
conda activate face
conda install -c conda-forge dlib numpy=1.18.5 numpy-base
pip install opencv-python3.4.5.20 opencv-contrib-python3.4.5.20 scipy imutils scikit-image --timeout=3000


提前下载好检测模型:
shape_predictor_68_face_landmarks.dat.bz2下载地址
解压在你的项目文件夹中,文件名是:
shape_predictor_68_face_landmarks.dat

说明:
EAR:眼睛纵横比
每只眼睛由6个(x,y)坐标表示,从眼睛的左角开始,然后围绕该区域的其余部分顺时针显示:
EAR
EAR=P2P6+P3P52P4P1 EAR=\frac{ \left \| P_{2} - P_{6} \right \| + \left \| P_{3} - P_{5} \right \| }{2\left \| P_{4} - P_{1} \right \| }

def eye_aspect_ratio(eye):
    A = dist.euclidean(eye[1], eye[5])
    B = dist.euclidean(eye[2], eye[4])
    C = dist.euclidean(eye[0], eye[3])
    ear = (A + B) / (2.0 * C)
    return ear

完整代码:

import sys
import os
import dlib
import glob
from skimage import io
import numpy as np
import cv2
import time
import imutils
from imutils import face_utils
from scipy.spatial import distance as dist

# 睁眼率
def eye_aspect_ratio(eye):
    A = dist.euclidean(eye[1], eye[5])
    B = dist.euclidean(eye[2], eye[4])
    C = dist.euclidean(eye[0], eye[3])
    ear = (A + B) / (2.0 * C)
    return ear


WINDOWS_WIDTH = 640
WINDOWS_HEIGHT = 480
EYE_AR_THRESH = 0.2
EYE_AR_CONSEC_FRAMES = 2
COUNTER = 0
TOTAL = 0
FPS = 0

predictor_path = 'shape_predictor_68_face_landmarks.dat'
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(predictor_path)

(lStart, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"]
(rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"]

cap = cv2.VideoCapture(0)
while (True):
    start_time = time.time()
    ret, frame = cap.read()
    frame = cv2.flip(frame, 1)
    img = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
    dets = detector(img, 1)
    for index, det in enumerate(dets):
        right_top = (det.right(), det.top())
        left_bottom = (det.left(), det.bottom())
        # 画面部检测框
        cv2.rectangle(frame, right_top, left_bottom, (255, 0, 0), 2)

        # 面部特征点检测
        shape = predictor(img, det)
        landmarks = np.matrix([[p.x, p.y] for p in shape.parts()])
        for idx, point in enumerate(landmarks):
            # 68点的坐标
            pos = (point[0, 0], point[0, 1])
            cv2.circle(frame, pos, 2, (0, 255, 0), -1)

        shape = face_utils.shape_to_np(shape)
        leftEye = shape[lStart:lEnd]
        rightEye = shape[rStart:rEnd]
        leftEAR = eye_aspect_ratio(leftEye)
        rightEAR = eye_aspect_ratio(rightEye)
        ear = (leftEAR + rightEAR) / 2.0

        leftEyeHull = cv2.convexHull(leftEye)
        rightEyeHull = cv2.convexHull(rightEye)
        cv2.drawContours(frame, [leftEyeHull], -1, (0, 255, 0), 1)
        cv2.drawContours(frame, [rightEyeHull], -1, (0, 255, 0), 1)

        if ear < EYE_AR_THRESH:
            COUNTER += 1
        else:
            if COUNTER >= EYE_AR_CONSEC_FRAMES:
                TOTAL += 1
            COUNTER = 0

        cv2.putText(frame, 'EAR: {:.2f}'.format(ear), (WINDOWS_WIDTH - 120,
                                                       20 + 30), cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 255), 1, cv2.LINE_AA)
        cv2.putText(frame, 'State: {}'.format('OPEN' if ear > 0.2 else 'CLOSE'), (WINDOWS_WIDTH -
                                                                                  120, 20 + 30 + 30), cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 255), 1, cv2.LINE_AA)
        cv2.putText(frame, 'Blind: {}'.format(TOTAL), (20, 20+30),
                    cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 255), 1, cv2.LINE_AA)

    end_time = time.time()
    FPS = 1 / (end_time - start_time)
    cv2.putText(frame, 'There are {} faces in the picture.'.format(
        len(dets)), (20, 20), cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 255), 1, cv2.LINE_AA)
    cv2.putText(frame, 'FPS: {:.4f}'.format(FPS), (WINDOWS_WIDTH-120, 20), cv2.FONT_HERSHEY_COMPLEX,
                0.5, (0, 0, 255), 1, cv2.LINE_AA)

    cv2.namedWindow("Cap", 0)
    cv2.resizeWindow("Cap", WINDOWS_WIDTH, WINDOWS_HEIGHT)
    cv2.imshow('Cap', frame)
    # cv2.imshow('Gray', img)
    key = cv2.waitKey(5)
    if key == 27:
        break

cap.release()
cv2.destroyAllWindows()

效果图:
效果图

©️2020 CSDN 皮肤主题: 深蓝海洋 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值