要害词: 人脸要害点 、张嘴 、 闭眼 、 动作检测 、 人脸验证
前言
动作检测是核算机视觉领域中的一个重要任务,它旨在辨认图画或视频中的人体动作。常见的人脸辨认验证是动作检测的一个应用示例。在这里咱们以付出过程中常常会使用到的人脸验证为例子向我们讲解如何进行动作检测(张嘴 、 闭眼)
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
张嘴
在进行张嘴检测的时候,咱们挑选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 <= 8:
print("闭眼")
测验
咱们将上述的代码兼并后增加一个根据两个坐标点核算举例的代码(其实仅核算Y轴上的间隔即可):
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
经过调用摄像头对视频中人物进行检测要害点,对彻底检测到人脸的图画核算判别人物是否张嘴与闭眼:
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)
结语
周一,忙完工作后赶忙更文一篇,仅以此篇以飨读者!期望我们可以对付出验证过程中的人脸动作检测有一个开始的认识,不苍茫。