【Dlib动作检测:以常见的人脸识别验证为例讲解张嘴与闭眼

2023年 7月 25日 50.9k 0

关键词: 人脸关键点 、张嘴 、 闭眼 、 动作检测 、 人脸验证

前言

  动作检测是计算机视觉领域中的一个重要任务,它旨在识别图像或视频中的人体动作。常见的人脸识别验证是动作检测的一个应用示例。在这里我们以支付过程中经常会使用到的人脸验证为例子向大家讲解如何进行动作检测(张嘴 、 闭眼)

Dlib简介

  Dlib提供了先进的人脸检测和关键点定位功能,可以快速准确地识别图像中的人脸,并定位人脸的关键点,例如眼睛、鼻子、嘴巴等。另外Dilb库是一个优秀的开源算法库,支持在多个操作系统上进行,包括WIN 、LINUX 、 MAXOS。

import math
import dlib
import cv2


detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")


def draw_landmarks(src_img):
    gray = cv2.cvtColor(src_img, cv2.COLOR_BGR2GRAY)
    faces = detector(gray)
    landmarks_part = []
    for face in faces:
        landmarks = predictor(gray, face)
        # 遍历关键点,并在图像上绘制出来
        for n in range(0, 68):
            x = landmarks.part(n).x
            y = landmarks.part(n).y
            landmarks_part.append([x, y])

            # cv2.circle(src_img, (x, y), 2, (0, 255, 0), -1)
    return landmarks_part

坐标区域.jpg
张嘴

  在进行张嘴检测的时候,我们选择63与67这两个关键点进行检测计算人物是否张嘴,通过63与67这两个关键点之间的距离进行判定

def Mouth_open(landmarks_part):
    [up_min_mouth_x, up_min_mouth_y] = landmarks_part[62]
    cv2.circle(src_img, (up_min_mouth_x, up_min_mouth_y), 2, (0, 0, 255), -1)
    [down_min_mouth_x, down_min_mouth_y] = landmarks_part[66]
    cv2.circle(src_img, (down_min_mouth_x, down_min_mouth_y), 2, (255, 0, 0), -1)
    mouth_min = calculate_distance(up_min_mouth_x, up_min_mouth_y, down_min_mouth_x, down_min_mouth_y)

    if mouth_min > 30:
        print("张嘴了")

闭眼

  在进行闭眼检测的时候,我们选择38-39-42-41与44-45-48-47这两组关键点进行检测计算人物是否闭眼,通过计算这两组关键点距离的均值判定人物是否闭眼。

def eyes_close(landmarks_part):
[x38, y38] = landmarks_part[37]
cv2.circle(src_img, (x38, y38), 2, (0, 0, 255), -1)

[x39, y39] = landmarks_part[38]
cv2.circle(src_img, (x39, y39), 2, (0, 0, 255), -1)

[x42, y42] = landmarks_part[41]
cv2.circle(src_img, (x42, y42), 2, (255, 0, 0), -1)

[x41, y41] = landmarks_part[40]
cv2.circle(src_img, (x41, y41), 2, (255, 0, 0), -1)

[x44, y44] = landmarks_part[43]
cv2.circle(src_img, (x44, y44), 2, (0, 0, 255), -1)

[x45, y45] = landmarks_part[44]
cv2.circle(src_img, (x45, y45), 2, (0, 0, 255), -1)

[x48, y48] = landmarks_part[47]
cv2.circle(src_img, (x48, y48), 2, (255, 0, 0), -1)

[x47, y47] = landmarks_part[46]
cv2.circle(src_img, (x47, y47), 2, (255, 0, 0), -1)

d3842 = calculate_distance(x38, y38, x42, y42)
d3941 = calculate_distance(x39, y39, x41, y41)
d4448 = calculate_distance(x44, y44, x48, y48)
d4547 = calculate_distance(x45, y45, x47, y47)
d = (d3842 + d3941 + d4448 + d4547) / 4

if d >= 12:
print("睁眼")
if d

相关文章

服务器端口转发,带你了解服务器端口转发
服务器开放端口,服务器开放端口的步骤
产品推荐:7月受欢迎AI容器镜像来了,有Qwen系列大模型镜像
如何使用 WinGet 下载 Microsoft Store 应用
百度搜索:蓝易云 – 熟悉ubuntu apt-get命令详解
百度搜索:蓝易云 – 域名解析成功但ping不通解决方案

发布评论