传统方法眼角定位和dilb 进行疲劳检测(眨眼检测)

发布时间:2018-08-14 09:57,

浏览次数:747

, 标签:

dilb

安装第三方库文件

* opencv

* Dlib,安装方法见

https://www.learnopencv.com/install-opencv-3-and-dlib-on-windows-python-only/

* Numpy

* Imutils (一系列使得opencv 便利的功能,包括图像旋转、缩放、平移,骨架化、边缘检测、显示matplotlib

图像(imutils.opencv2matplotlib(image))

传统方法进行眼角定位

思路是:

1、利用opencv 自带训练好的 haarcascade_frontalface_default.xml 和 haarcascade_eye.xml

来检测脸部和眼睛

以脸部加载xml为例:

face=cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

face.load(脸部xml的完整路径)

以上是加载xml方法,有很多介绍直接是face=cv2.CascadeClassifier(脸部xml的完整路径),通常会报错。

2、加载完xml 文件后,下一步就是detect,opencv

函数为detectMultiScale(img,scaleFactor,minNeighbors,minsize,maxsize)

scaleFactor 是压缩率,越小金字塔数目越多,相应的检测准确相对好点

minNeighbors 是同一张脸至少被检测n次才认为是真正的

Minszie以元组方式,(检测眼睛时候会用到,避免鼻子和其他部位产生的假阳性)

3、先检测脸,然后再脸的基础上再检测眼睛,到了这一步后,就是自己设计检测的眼角方法:

1)找轮廓,取最大轮廓

2)凸缺陷检测,得到所有凸点

3)眼角如何确定?----将凸点的x,y值相加,排序,最小的为左边眼角,最大的为右边眼角(见测试图)

dlib 疲劳检测

* 下载shape_predictor_68_face_landmarks.dat 文件,这是68个眼部特征点

* 初始化检测器和预测器 detector=dlib.get_frontal_face_detector() predictor =

dlib.shape_predictor(“dat文件路径”)

3 如何确定疲劳?

1) 计算眼睛的宽高比

2)当前帧两双眼睛宽高比与前一帧的差值的绝对值大于0.2,则认为是疲劳

程序如下:

# -*- coding: utf-8 -*- import os import numpy as np import cv2 import dlib

import sys from imutils import face_utils class fatigue(object): def

__init__(self,file_dictory,landmask_path,facehaar_path,eyehaar_path):

self.file=os.path.abspath(str(file_dictory)) os.chdir(self.file)

self.roi_face=[] self.roi_eye=[] #

self.predictor_path=r'C:\Users\Y\Desktop\shape_predictor_68_face_landmarks.dat'

self.predictor_path=os.path.abspath(str(landmask_path)) #

self.face_haar_path=r'E:\opencv\opencv\sources\data\haarcascades\haarcascade_frontalface_default.xml'

self.face_haar_path=os.path.abspath(str(facehaar_path)) #

self.eye_haar_path=r'E:\opencv\opencv\sources\data\haarcascades\haarcascade_eye.xml'

self.eye_haar_path=os.path.abspath(str(eyehaar_path)) def detect_face(self):

face=cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

face.load(self.face_haar_path) i=1 for f in os.listdir(self.file):

face_image=cv2.imread(f) face_image=cv2.medianBlur(face_image,3) if

face_image.ndim==3: face_image_gray=cv2.cvtColor(face_image,cv2.COLOR_BGR2GRAY)

else: face_image_gray=face_image

faces=face.detectMultiScale(face_image_gray,1.3,5) if len(faces)!=0: for

(x,y,w,h) in faces: self.roi_face.append(face_image[y:y+h,x:x+w,:]) #

cv2.imwrite(self.file+"\\%g.jpg"%i,face_image_gray[y:y+h,x:x+w]) i+=1

print("人脸数%g"%len(self.roi_face)) def detect_eye(self):

eye=cv2.CascadeClassifier('haarcascade_eye.xml') eye.load(self.eye_haar_path)

i=1 for face in self.roi_face:

eyes=eye.detectMultiScale(face,1.03,20,0,(40,40))#(40,40)限制眼睛搜索最小尺寸,避免鼻子或者其他的阴影产生的假阳性

if len(eyes)!=0: for (x,y,w,h) in eyes:

self.roi_eye.append(face[y:y+h,x:x+w,:]) #

cv2.imwrite(self.file+"\\%g.jpg"%i,face[y+10:y+h,x+10:x+w,:]) i+=1

print("眼睛个数%g"%len(self.roi_eye)) #传统 def feature_eye(self): i=1 for e in

self.roi_eye: e_g=cv2.cvtColor(e,cv2.COLOR_BGR2GRAY)

_,thresh=cv2.threshold(e_g,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)

_,cnts,h=cv2.findContours(thresh,0,1) cnt_max=sorted(cnts,key=lambda

x:cv2.contourArea(x),reverse=True)[0] con_hull=cv2.convexHull(cnt_max)

hull_index=cv2.convexHull(cnt_max,returnPoints = False) defects =

cv2.convexityDefects(cnt_max,hull_index) temp=[] point=[] for j in

range(defects.shape[0]): _,_,f,d=defects[j,0]

point.append(tuple(cnt_max[f][0])) for t in point: temp.append(sum(t))

index=np.argsort(temp) close=point[index[0]]#两个眼角,colse,far

far=point[index[-1]] # np.sort() cv2.circle(e,close,5,(0,255,0),-1)

cv2.circle(e,far,5,(0,255,0),-1) cv2.drawContours(e,[con_hull],0,(0,0,255),2)

cv2.putText(e,str(cv2.contourArea(cnt_max)),(10,10),cv2.FONT_HERSHEY_SIMPLEX,0.5,(255,0,0))

cv2.imwrite(self.file+"\\%g.jpg"%i,e) i+=1 def dlib_detect(self):

detector=dlib.get_frontal_face_detector() predictor =

dlib.shape_predictor(self.predictor_path)

cap=cv2.VideoCapture(0)#也可以其它视频,打开本地摄像头 if cap.isOpened() is False: raise("IO

error") #抛出异常 cap.set(cv2.CAP_PROP_FPS,60) # cv2.namedWindow("Capture",

cv2.WINDOW_NORMAL) forward_left_eye_ratio=None forward_right_eye_ratio=None

flag=0 #根据faces个数来初始化forward_left.... while 1: ret,frame=cap.read()

frame=cv2.medianBlur(frame,3) # frame=cv2.cvtColor(frame,cv2.COLOR_BGR2RGB) if

ret==False: sys.exit() faces=detector(frame,1)#1 代表图像升采样一次,以便我们检测更多的人脸 if

len(faces)>0: if flag==0: #以第一帧检测到人脸个数为准,也就是程序每一帧检测到人脸个数相同,不然有错

temp=np.zeros((len(faces),1)) #初始化成一个数组

forward_left_eye_ratio,forward_right_eye_ratio=temp,temp else: # sys.exit() #

print("当前帧人脸消失,退出") print("当前帧人脸消失,继续下一帧") # break continue flag=1

#flag=1,标识着第二帧的时候不再预分配内存,temp if len(faces)>0: for i,d in enumerate(faces):

cv2.rectangle(frame,(d.left(),d.top()),(d.right(),d.bottom()),(0,255,0),2)

cv2.putText(frame,str(i+1),(d.left()-10,d.top()-10),cv2.FONT_HERSHEY_SIMPLEX,0.5,(0,0,255),2)

shape=predictor(frame,d)#检测68特征点 # print(shape,type(shape)) points =

face_utils.shape_to_np(shape) #将脸部特征点转为坐标(x,y)

long_left=self.distance(points[36,:],points[39,:])

short_left=self.distance((points[37,:]+points[38,:])/2,(points[41,:]+points[40,:])/2)

long_right=self.distance(points[42,:],points[45,:])

short_right=self.distance((points[43,:]+points[44,:])/2,(points[46,:]+points[47,:])/2)

if forward_left_eye_ratio[i]==0 and

forward_right_eye_ratio[i]==0:#取第一帧的人脸眼睛宽高比,取完后,进行下一帧,continue 后的语句不再执行

forward_left_eye_ratio[i]=short_left/long_left

forward_right_eye_ratio[i]=short_right/long_right continue #跳转下一个人脸眼睛的宽高比 ##下一帧

left_eye_ratio_now=np.zeros((forward_left_eye_ratio.shape))

right_eye_ratio_now=np.zeros((forward_right_eye_ratio.shape))

left_eye_ratio_now[i]=short_left/long_left

right_eye_ratio_now[i]=short_right/long_right

print("第%g个人脸当前左眼宽高比与前一帧的差值:%g"%(i+1,abs(left_eye_ratio_now[i]-forward_left_eye_ratio[i])))

if abs(left_eye_ratio_now[i]-forward_left_eye_ratio[i])>0.2:

print("第%g个人左眼变化%g"%(i+1,abs(left_eye_ratio_now-forward_left_eye_ratio))) if

abs(right_eye_ratio_now[i]-forward_right_eye_ratio[i])>0.2:

print("第%g个人左眼变化%g"%(i+1,abs(right_eye_ratio_now-forward_right_eye_ratio))) if

abs(left_eye_ratio_now[i]-forward_left_eye_ratio[i])>0.2 and

abs(right_eye_ratio_now[i]-forward_right_eye_ratio[i])>0.2:

print("%g号先生您很疲劳了,请注意休息"%(i+1)) forward_left_eye_ratio[i]=left_eye_ratio_now[i]

forward_right_eye_ratio[i]=right_eye_ratio_now[i] cv2.imshow("Capture",frame)

k=cv2.waitKey(10) if k==27: break cap.release() cv2.destroyAllWindows() def

distance(self,p1,p2): return np.sqrt(np.sum((p1-p2)*(p1-p2))) if

__name__=="__main__": # param=sys.argv[1] print("cmd---运行格式--python '****.py'

图像demo1文件夹路径 68_face_landmarks.dat路径 haarcascade_frontalface_default.xml路径

haarcascade_eye.xml路径") if len(sys.argv)!=5: print("参数不够") #

fold_param=r'C:\Users\Y\Desktop\demo1' # fatigue_drive=fatigue(fold_param)

fatigue_drive=fatigue(sys.argv[1],sys.argv[2],sys.argv[3],sys.argv[4])

print("**********传统方法人眼角定位*************\n") fatigue_drive.detect_face()

fatigue_drive.detect_eye() fatigue_drive.feature_eye()

print("*************疲劳测试************\n") fatigue_drive.dlib_detect()

测试图:

dlib疲劳检测_传统方法眼角定位和dilb 进行疲劳检测(眨眼检测)相关推荐

  1. 传统方法眼角定位和dilb 进行疲劳检测(眨眼检测)

    安装第三方库文件 opencv Dlib,安装方法见https://www.learnopencv.com/install-opencv-3-and-dlib-on-windows-python-on ...

  2. 肺结节目标检测_基于改进Faster R-CNN的肺结节检测

    基于改进 Faster R-CNN 的肺结节检测 肺癌在我国乃至全球范围内 , 都是发病率及死亡率最高的恶性肿瘤.其早期 的表现形式是直径不超过 30mm 的肺内圆形或不规则形结节. 肺癌的早期诊断与 ...

  3. 检测多边形是否重叠_只要保留定位感知通道,目标检测模型也能剪枝70%参数

    作者 | Bbuf 编辑 | 杨晓凡 下面要介绍的论文发于2019,题为「Localization-aware Channel Pruning for Object Detection」 axriv地 ...

  4. 基于python的移动物体检测_树莓派+摄像头实现对移动物体的检测

    在上一篇文章中实现了树莓派下对摄像头的调用,有兴趣的可以看一下:python+opencv实现摄像头调用的方法 接下来,我们将使用python+opencv实现对移动物体的检测 一.环境变量的配置 我 ...

  5. python实现yolo目标检测_从零开始PyTorch项目:YOLO v3目标检测实现

    在过去几个月中,我一直在实验室中研究提升目标检测的方法.在这之中我获得的最大启发就是意识到:学习目标检测的最佳方法就是自己动手实现这些算法,而这正是本教程引导你去做的. 在本教程中,我们将使用 PyT ...

  6. python 人脸检测_借助摄像头在Python中实现人脸检测

    Python部落(www.freelycode.com)组织翻译, 禁止转载 本文作者是Shantnu Tiwari--曾多年在C/C++的魔爪中饱受折磨,直到他发现了Python--使用起来感觉如呼 ...

  7. python裂缝检测_基于卷积神经网络的高楼外墙裂缝检测系统

    fy the severity, general or slight damage degree of external wall cracks, and the effective identifi ...

  8. python图像检测_用Python实现通过哈希算法检测图片重复的教程

    Iconfinder 是一个图标搜索引擎,为设计师.开发者和其他创意工作者提供精美图标,目前托管超过 34 万枚图标,是全球最大的付费图标库.用户也可以在 Iconfinder 的交易板块上传出售原创 ...

  9. python文章抄袭检测_翟天临的噩梦:怎样用Python检测抄袭行为?

    全文共3168字,预计学习时长8分钟 图源:Google 本教程将介绍如何使用机器学习技术(如word2vec和余弦相似度等),在Python中用几行代码制作抄袭检测器.搭建完成后,抄袭检测器将会从文 ...

最新文章

  1. 解决Mask RCNN自己航拍数据集训练的问题
  2. oracle 无效索引
  3. layer.js弹窗组件layer.prompt无法调用解决
  4. Windows7配置GPU和Theano编程环境
  5. 如何使用原生的 JavaScript 代码,触发 SAP UI5 按钮控件的点击事件处理函数
  6. python io多路复用_Python之IO多路复用
  7. LINUX REDHAT第九单元文档
  8. wince flash Android,关于wince下用C#实现flash播放器
  9. vue-cli 基本原理
  10. 游戏开发之C++对C的扩展(C++基础)
  11. 土木工程与计算机专业考研学校排名,2017年土木工程专业考研大学排名
  12. day24-XSS过滤及单实例
  13. RTCM—CRC校验
  14. 简单个人网页设计作业 静态HTML个人博客主页 DW个人网站模板下载 大学生简单个人网页作品代码 个人网页制作 学生个人网页设计作业
  15. python字典输出键值对_Python:遍历字典 键值对
  16. 720s ideapad 黑苹果_Hackintosh 黑苹果长期维护机型 EFI 及安装教程整理
  17. Tracert命令原理
  18. MCS-51单片机指令系统总结(自学笔记)
  19. win10设置计算机关机时间,Win10怎么设置自动关机时间_Win10设置自动关机教程-192路由网...
  20. 计算机通信普遍采用报文交换,报文交换

热门文章

  1. shell脚本应用——正则表达式
  2. cookie清空Java_java清除及设置cookie代码
  3. 总结 | Revit安装失败的常见问题及解决办法
  4. CSV格式的文件与EXCEL文件的区别
  5. 大数据Hadoop视频教程
  6. python实现工资管理系统的源代码_Python3实现的简单工资管理系统实例
  7. 如何找计算机配置文件,怎么查看电脑系统配置
  8. 一个不是很了解CS的人,该从哪里开始自学CS?
  9. 系统集成项目管理工程师+android,系统集成项目管理工程师证书样本
  10. 微光图像增强的零参考深度曲线估计