前言
动作检测是计算机视觉领域中的一个重要任务,它旨在识别图像或视频中的人体动作。常见的人脸识别验证是动作检测的一个应用示例。在这里我们以支付过程中经常会使用到的人脸验证为例子向大家讲解如何进行动作检测(张嘴 、 闭眼)
Dlib简介
Dlib提供了先进的人脸检测和关键点定位功能,可以快速准确地识别图像中的人脸,并定位人脸的关键点,例如眼睛、鼻子、嘴巴等。另外Dilb库是一个优秀的开源算法库,支持在多个操作系统上进行,包括WIN 、LINUX 、 MAXOS。
shell
复制代码
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
张嘴
在进行张嘴检测的时候,我们选择63与67这两个关键点进行检测计算人物是否张嘴,通过63与67这两个关键点之间的距离进行判定
shell
复制代码
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这两组关键点进行检测计算人物是否闭眼,通过计算这两组关键点距离的均值判定人物是否闭眼。
shell
复制代码
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 <= 8: print("闭眼")
测试
我们将上述的代码合并后添加一个根据两个坐标点计算举例的代码(其实仅计算Y轴上的距离即可):
shell
复制代码
def calculate_distance(p0_x, p0_y, p1_x, p1_y): d_x = p1_x - p0_x d_y = p1_y - p0_y # 计算两点之间的距离 distance = math.sqrt(d_x ** 2 + d_y ** 2) return distance
通过调用摄像头对视频中人物进行检测关键点,对完全检测到人脸的图像计算判断人物是否张嘴与闭眼:
shell
复制代码
if __name__ == "__main__": cap = cv2.VideoCapture(0) while cap.isOpened(): success, src_img = cap.read() if not success: print("Ignoring empty camera frame.") continue else: landmarks_part = draw_landmarks(src_img) if len(landmarks_part) == 68: eyes_close(landmarks_part) Mouth_open(landmarks_part) cv2.imshow("src_img", src_img) cv2.waitKey(1)
结语
周一,忙完工作后赶紧更文一篇,仅以此篇以飨读者!希望大家能够对支付验证过程中的人脸动作检测有一个初步的认识,不迷茫。