网站首页 > 技术文章 正文
OpenCV: OpenCV是一个用于计算机视觉的库函数,最初是用c++编写的。
Dlib: Dlib是一个Python库,包含了数据挖掘、数据结构、机器学习、图像处理等工具。
打哈欠检测与应用
打哈欠检测就是使用OpenCV和Dlib来检测打哈欠(由于疲劳或无聊而张大嘴巴深吸气)。可广泛应用于自驾车、驾驶员疲劳检测、驾驶员睡意检测、驾驶员意识检测等领域。
安装OpenCv和Dlib库
OpenCv的安装如下:
pip install opencv-python
Dlib的安装如下:
pip install cmake
pip install dlib
导入Opencv,Dlib和Numpy库
import cv2
import dlib
import numpy as np
设置dat文件
PREDICTOR_PATH = "shape_predictor_68_face_landmarks.dat"
predictor = dlib.shape_predictor(PREDICTOR_PATH)
#cascade_path='haarcascade_frontalface_default.xml
#cascade = cv2.CascadeClassifier(cascade_path)
detector = dlib.get_frontal_face_detector()
我们将.dat文件放置到工作目录中,它是一个可以识别面部特征并提供信息的模型。Dlib将从文件中提取所有信息,使用opencv查找不同的特征。
定义函数获取landmarks并使用颜色对其进行注释
def geting_landmarks(im):
rects = detector(im, 1)
if len(rects) > 1:
return "error"
if len(rects) == 0:
return "error",
return np.matrix([[p.x, p.y] for p in predictor(im, rects[0]).parts()])
def annotate_landmarks(im, landmarks):
im = im.copy()
for idx, point in enumerate(landmarks):
pos = (point[0, 0], point[0, 1])
cv2.putText(im, str(idx), pos,
fontFace=cv2.FONT_HERSHEY_SCRIPT_SIMPLEX,
fontScale=0.4,
color=(1, 2, 255))
cv2.circle(im, pos, 3, color=(0, 2, 2))
return im
形状预测器(dat文件)用于从图像中提取信息,例如眼角,鼻子周围的区域等。图像被转换为??矩阵,并且annotate_landmarks()函数标记这些特征/信息,其他函数使用这些特征/信息来提取各种像上嘴唇,上嘴等
提取有关各种特征的关键信息,以检测打哈欠
def top_lip(landmarks):
top_lip_pts = []
for i in range(50,53):
top_lip_pts.append(landmarks[i])
for i in range(61,64):
top_lip_pts.append(landmarks[i])
top_lip_all_pts = np.squeeze(np.asarray(top_lip_pts))
top_lip_mean = np.mean(top_lip_pts, axis=0)
return int(top_lip_mean[:,1])
def bottom_lip(landmarks):
bottom_lip_pts = []
for i in range(65,68):
bottom_lip_pts.append(landmarks[i])
for i in range(56,59):
bottom_lip_pts.append(landmarks[i])
bottom_lip_all_pts = np.squeeze(np.asarray(bottom_lip_pts))
bottom_lip_mean = np.mean(bottom_lip_pts, axis=0)
return int(bottom_lip_mean[:,1])
def mouth_open(image):
landmarks = geting_landmarks(image)
if landmarks == "error":
return image, 0
image_with_landmarks = annotate_landmarks(image, landmarks)
top_lip_center = top_lip(landmarks)
bottom_lip_center = bottom_lip(landmarks)
lip_distance = abs(top_lip_center - bottom_lip_center)
return image_with_landmarks, lip_distance
当yawn_status为True时,打开摄像头并定义打哈欠标准,然后相应地分配操作。Python实现如下:
cap = cv2.VideoCapture(0)
yawns = 0
yawn_status = False
while True:
ret, frame = cap.read()
image_landmarks, lip_distance = mouth_open(frame)
prev_yawn_status = yawn_status
if lip_distance > 25:
yawn_status = True
cv2.putText(frame, "Employee is Yawning", (50,450),
cv2.FONT_HERSHEY_COMPLEX, 1,(0,0,255),2)
from pygame import mixer
mixer.init()
mixer.music.load('Yawn.mp3')
mixer.music.play()
output_text = " Yawn Count: " + str(yawns + 1)
cv2.putText(frame, output_text, (50,50),
cv2.FONT_HERSHEY_COMPLEX, 1,(0,255,127),2)
else:
yawn_status = False
if prev_yawn_status == True and yawn_status == False:
yawns += 1
cv2.imshow('Live Landmarks', image_landmarks )
cv2.imshow('Yawn Detection', frame )
if cv2.waitKey(1) == 13:
break
cap.release()
cv2.destroyAllWindows()
如果lip_distance大于25,则(yawn_status)被定义为打呵欠。在这种情况下,屏幕上会出现“Employee is Yawning”的文字,然后我们播放mp3文件。如果Yawn_status为true,则“Yawn Count”将继续显示该对象打哈欠的次数。
实时显示Landmark如下所示
- 上一篇: 适合学习研究的几个音视频开源框架
- 下一篇: 一个很棒的Python CV库(python cv2)
猜你喜欢
- 2024-09-11 给3D Slicer添加Python第三方插件库
- 2024-09-11 使用Python创建自己的Instagram滤镜
- 2024-09-11 薅羊毛我愿称Python为最强,Python 自动化带你轻松赚钱
- 2024-09-11 史上最全的OpenCV入门教程!这篇够你学习半个月了!万字长文入门
- 2024-09-11 使用python实现简易的人脸检测(python人脸检测106关键点)
- 2024-09-11 机器视觉技术(一)边缘提取(机器视觉边缘检测)
- 2024-09-11 Python-OpenCV 16. 图像特征检测与描述算法
- 2024-09-11 Flask—远程查看行空板USB摄像头视频
- 2024-09-11 OpenCV+Python识别车牌和字符分割的实现
- 2024-09-11 在Fedora中安装OpenCV-Python | 二
- 02-21走进git时代, 你该怎么玩?_gits
- 02-21GitHub是什么?它可不仅仅是云中的Git版本控制器
- 02-21Git常用操作总结_git基本用法
- 02-21为什么互联网巨头使用Git而放弃SVN?(含核心命令与原理)
- 02-21Git 高级用法,喜欢就拿去用_git基本用法
- 02-21Git常用命令和Git团队使用规范指南
- 02-21总结几个常用的Git命令的使用方法
- 02-21Git工作原理和常用指令_git原理详解
- 最近发表
- 标签列表
-
- cmd/c (57)
- c++中::是什么意思 (57)
- sqlset (59)
- ps可以打开pdf格式吗 (58)
- phprequire_once (61)
- localstorage.removeitem (74)
- routermode (59)
- vector线程安全吗 (70)
- & (66)
- java (73)
- org.redisson (64)
- log.warn (60)
- cannotinstantiatethetype (62)
- js数组插入 (83)
- resttemplateokhttp (59)
- gormwherein (64)
- linux删除一个文件夹 (65)
- mac安装java (72)
- reader.onload (61)
- outofmemoryerror是什么意思 (64)
- flask文件上传 (63)
- eacces (67)
- 查看mysql是否启动 (70)
- java是值传递还是引用传递 (58)
- 无效的列索引 (74)