使用 OpenCV 和 Dlib 进行打哈欠检测
在本文中,我们将介绍使用 OpenCV 和dlib包构建打哈欠检测程序所需的所有步骤。但是在做这个项目之前应该熟悉 OpenCV 的基础知识,你还应该知道如何使用dlib模块进行人脸检测和地标检测。
要求:
- 已安装 Dlib 库
- Dlib 面对地标“.dat”文件。可选: harcasscade分类器的 XML 文件(如果要使用 harcasscade 分类器链接:-https://github.com/opencv/opencv/tree/master/data/haarcascades)
- OpenCV 包应该安装在你的环境中
脚步:
- 在 OpenCV 中使用VideoCapture方法初始化视频渲染对象
- 创建灰度图像
- 为人脸和地标检测实例化模型对象
- 检测人脸,然后将人脸作为输入传递给地标检测模型
- 计算上唇和下唇距离(或任何你想用于打哈欠检测的指标)
- 创建唇边距离的If条件
- 显示帧/图像
执行:
Python3
import numpy as np
import cv2
import dlib
import time
from scipy.spatial import distance as dist
from imutils import face_utils
def cal_yawn(shape):
top_lip = shape[50:53]
top_lip = np.concatenate((top_lip, shape[61:64]))
low_lip = shape[56:59]
low_lip = np.concatenate((low_lip, shape[65:68]))
top_mean = np.mean(top_lip, axis=0)
low_mean = np.mean(low_lip, axis=0)
distance = dist.euclidean(top_mean,low_mean)
return distance
cam = cv2.VideoCapture('http://192.168.1.50:4747/video')
#-------Models---------#
face_model = dlib.get_frontal_face_detector()
landmark_model = dlib.shape_predictor('Model\shape_predictor_68_face_landmarks.dat')
#--------Variables-------#
yawn_thresh = 35
ptime = 0
while True :
suc,frame = cam.read()
if not suc :
break
#---------FPS------------#
ctime = time.time()
fps= int(1/(ctime-ptime))
ptime = ctime
cv2.putText(frame,f'FPS:{fps}',(frame.shape[1]-120,frame.shape[0]-20),cv2.FONT_HERSHEY_PLAIN,2,(0,200,0),3)
#------Detecting face------#
img_gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
faces = face_model(img_gray)
for face in faces:
# #------Uncomment the following lines if you also want to detect the face ----------#
# x1 = face.left()
# y1 = face.top()
# x2 = face.right()
# y2 = face.bottom()
# # print(face.top())
# cv2.rectangle(frame,(x1,y1),(x2,y2),(200,0,00),2)
#----------Detect Landmarks-----------#
shapes = landmark_model(img_gray,face)
shape = face_utils.shape_to_np(shapes)
#-------Detecting/Marking the lower and upper lip--------#
lip = shape[48:60]
cv2.drawContours(frame,[lip],-1,(0, 165, 255),thickness=3)
#-------Calculating the lip distance-----#
lip_dist = cal_yawn(shape)
# print(lip_dist)
if lip_dist > yawn_thresh :
cv2.putText(frame, f'User Yawning!',(frame.shape[1]//2 - 170 ,frame.shape[0]//2),cv2.FONT_HERSHEY_SIMPLEX,2,(0,0,200),2)
cv2.imshow('Webcam' , frame)
if cv2.waitKey(1) & 0xFF == ord('q') :
break
cam.release()
cv2.destroyAllWindows()
输出:
接下来是什么?
您可以尝试将此程序与用于预测用户状态的眨眼检测/活跃度检测程序结合使用,这可以作为实际应用程序检测用户状态并相应地设置警报或提醒的基础。