• Dlib+Opencv库实现疲劳检测


    1.关键点检测

    https://mydreamambitious.blog.csdn.net/article/details/125542337


    2.算法实现的核心点

    请添加图片描述
    请添加图片描述
    在这里插入图片描述
    其中纵横比表示衡量是否眨眼;p1,p2,p3,p4,p5,p6为人眼的关键点坐标,||p2-p6||表示两个关键点之间的欧式距离。其实你只要看懂上面的图和公式即可。

    论文地址
    http://vision.fe.uni-lj.si/cvww2016/proceedings/papers/05.pdf
    参考理论详解
    https://blog.csdn.net/uncle_ll/article/details/117999920


    3.算法实现

    注:这个代码看起来有点多(复杂),但是读者不要“害怕”,这个思路非常的清晰,只要一步一步的来就很容易明白其中实现的过程(不难理解)。

    (1)人脸的关键点集合

    #对于68个检测点,将人脸的几个关键点排列成有序,便于后面的遍历
    shape_predictor_68_face_landmark=OrderedDict([
        ('mouth',(48,68)),
        ('right_eyebrow',(17,22)),
        ('left_eye_brow',(22,27)),
        ('right_eye',(36,42)),
        ('left_eye',(42,48)),
        ('nose',(27,36)),
        ('jaw',(0,17))
    ])
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    (2)加载人脸检测库和人脸关键点检测库

    # 加载人脸检测与关键点定位
    #http://dlib.net/python/index.html#dlib_pybind11.get_frontal_face_detector
    detector = dlib.get_frontal_face_detector()
    #http://dlib.net/python/index.html#dlib_pybind11.shape_predictor
    criticPoints = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    (3)绘制人脸检测的框

    #绘制人脸画矩形框
    def drawRectangle(detected,frame):
        margin = 0.2
        img_h,img_w,_=np.shape(frame)
        if len(detected) > 0:
            for i, locate in enumerate(detected):
                x1, y1, x2, y2, w, h = locate.left(), locate.top(), locate.right() + 1, locate.bottom() + 1, locate.width(), locate.height()
    
                xw1 = max(int(x1 - margin * w), 0)
                yw1 = max(int(y1 - margin * h), 0)
                xw2 = min(int(x2 + margin * w), img_w - 1)
                yw2 = min(int(y2 + margin * h), img_h - 1)
    
                cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
    
                face = frame[yw1:yw2 + 1, xw1:xw2 + 1, :]
                cv2.putText(frame, 'Person', (locate.left(), locate.top() - 10),
                cv2.FONT_HERSHEY_SIMPLEX, 1.2, (255, 0, 0), 3)
        return frame
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    (4)对检测之后的人脸关键点坐标进行转换

    #对检测之后获取的人脸关键点坐标进行转换
    def predict2Np(predict):
        # 创建68*2关键点的二维空数组[(x1,y1),(x2,y2)……]
        dims=np.zeros(shape=(predict.num_parts,2),dtype=np.int)
        #遍历人脸的每个关键点获取二维坐标
        length=predict.num_parts
        for i in range(0,length):
            dims[i]=(predict.part(i).x,predict.part(i).y)
        return dims
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    (5)计算欧式距离

    #计算欧式距离
    def Euclidean(PointA,PointB):
        x=math.fabs(PointA[0]-PointB[0])
        y=math.fabs(PointA[1]-PointB[1])
        Ear=math.sqrt(x*x+y*y)
        return Ear
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    (6)计算眼睛的纵横比

    #计算是否眨眼的距离
    def ComputeCloseEye(left_eye):
        #计算P2与P6,P3与P5
        P1=Euclidean(left_eye[1],left_eye[5])
        P2=Euclidean(left_eye[2],left_eye[4])
        #计算P1与P4
        P3=Euclidean(left_eye[0],left_eye[3])
        #计算P
        P=(P1+P2)/(2*P3)
        return P
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    (7)对人脸关键点绘制点

    #获取左眼和右眼的关键点坐标值
    avg_Ear=0.0
    def draw_left_and_right_eye(detected,frame):
        global avg_Ear
        for (step,locate) in enumerate(detected):
            #获取人眼的关键点
            dims=criticPoints(frame,locate)
            #将得到的坐标值转换为二维
            dims=predict2Np(dims)
            #获取左眼的关键点坐标值列表
            left_eye=dims[42:48]
            # 获取右眼的关键点坐标值列表
            right_eye=dims[36:42]
            #绘制左眼的点
            for (x, y) in left_eye:
                cv2.circle(img=frame, center=(x, y),
                           radius=2, color=(0, 255, 0), thickness=-1)
            #绘制右眼的点
            for (x, y) in right_eye:
                cv2.circle(img=frame, center=(x, y),
                           radius=2, color=(0, 255, 0), thickness=-1)
            #计算距离
            earLeft=ComputeCloseEye(left_eye)
            earRight=ComputeCloseEye(right_eye)
            #计算左眼和右眼的平均纵横比
            avg_Ear=(earRight+earLeft)/2
            cv2.putText(img=frame,text='CloseEyeDist: '+str(round(avg_Ear,2)),org=(20,50),
                        fontFace=cv2.FONT_HERSHEY_SIMPLEX,fontScale=1.0,
                        color=(0,255,0),thickness=2)
        return frame,avg_Ear
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    (8)设置相关的阈值

    #设置纵横比的阈值
    Ear_Threshod=0.2
    #眨眼动作是一个快速闭合的过程,眨眼持续差不多为100-400ms
    #设置当连续3帧的纵横比都小于阈值则表示眨眼
    Ear_frame_Threshold=3
    #一次任务中的总的眨眼次数
    ToClose_Eye=0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    (9)实时的人脸关键点检测

    #实时的人脸关键点检测
    def detect_time():
        cap=cv2.VideoCapture(0)
        #记录连续眨眼的次数
        count=0
        global ToClose_Eye
    
        while cap.isOpened():
            #记录开始时间
            statime=time.time()
            ret,frame=cap.read()
            #检测人脸位置
            detected = detector(frame)
            #利用定位到的人脸进行人脸关键点检测
            frame = drawRectangle(detected, frame)
            frame,avg_Ear=draw_left_and_right_eye(detected,frame)
            if avg_Ear<Ear_Threshod:
                count+=1
                if count>=Ear_frame_Threshold:
                    ToClose_Eye+=1
                    count=0
            cv2.putText(img=frame,text='ToClose_Eye: '+str(ToClose_Eye),org=(20,80),fontFace=cv2.FONT_HERSHEY_SIMPLEX,
                        fontScale=1.0,color=(0,255,0),thickness=2)
    
            #记录结束时间
            endtime=time.time()
            FPS=1/(endtime-statime)
            cv2.putText(img=frame, text='FPS: '+str(int(FPS)), org=(20, 110), fontFace=cv2.FONT_HERSHEY_SIMPLEX,
                        fontScale=1.0, color=(0, 255, 0), thickness=2)
            cv2.imshow('frame', frame)
            key=cv2.waitKey(1)
            if key==27:
                break
        cap.release()
        cv2.destroyAllWindows()
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    (10)整体代码

    import os
    import cv2
    import dlib
    import time
    import math
    import numpy as np
    from collections import OrderedDict
    
    #对于68个检测点,将人脸的几个关键点排列成有序,便于后面的遍历
    shape_predictor_68_face_landmark=OrderedDict([
        ('mouth',(48,68)),
        ('right_eyebrow',(17,22)),
        ('left_eye_brow',(22,27)),
        ('right_eye',(36,42)),
        ('left_eye',(42,48)),
        ('nose',(27,36)),
        ('jaw',(0,17))
    ])
    
    # 加载人脸检测与关键点定位
    #http://dlib.net/python/index.html#dlib_pybind11.get_frontal_face_detector
    detector = dlib.get_frontal_face_detector()
    #http://dlib.net/python/index.html#dlib_pybind11.shape_predictor
    criticPoints = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
    
    #绘制人脸画矩形框
    def drawRectangle(detected,frame):
        margin = 0.2
        img_h,img_w,_=np.shape(frame)
        if len(detected) > 0:
            for i, locate in enumerate(detected):
                x1, y1, x2, y2, w, h = locate.left(), locate.top(), locate.right() + 1, locate.bottom() + 1, locate.width(), locate.height()
    
                xw1 = max(int(x1 - margin * w), 0)
                yw1 = max(int(y1 - margin * h), 0)
                xw2 = min(int(x2 + margin * w), img_w - 1)
                yw2 = min(int(y2 + margin * h), img_h - 1)
    
                cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
    
                face = frame[yw1:yw2 + 1, xw1:xw2 + 1, :]
                cv2.putText(frame, 'Person', (locate.left(), locate.top() - 10),
                cv2.FONT_HERSHEY_SIMPLEX, 1.2, (255, 0, 0), 3)
        return frame
    
    #对检测之后获取的人脸关键点坐标进行转换
    def predict2Np(predict):
        # 创建68*2关键点的二维空数组[(x1,y1),(x2,y2)……]
        dims=np.zeros(shape=(predict.num_parts,2),dtype=np.int)
        #遍历人脸的每个关键点获取二维坐标
        length=predict.num_parts
        for i in range(0,length):
            dims[i]=(predict.part(i).x,predict.part(i).y)
        return dims
    
    #计算欧式距离
    def Euclidean(PointA,PointB):
        x=math.fabs(PointA[0]-PointB[0])
        y=math.fabs(PointA[1]-PointB[1])
        Ear=math.sqrt(x*x+y*y)
        return Ear
    #计算是否眨眼的距离
    def ComputeCloseEye(left_eye):
        #计算P2与P6,P3与P5
        P1=Euclidean(left_eye[1],left_eye[5])
        P2=Euclidean(left_eye[2],left_eye[4])
        #计算P1与P4
        P3=Euclidean(left_eye[0],left_eye[3])
        #计算P
        P=(P1+P2)/(2*P3)
        return P
    
    
    #获取左眼和右眼的关键点坐标值
    avg_Ear=0.0
    def draw_left_and_right_eye(detected,frame):
        global avg_Ear
        for (step,locate) in enumerate(detected):
            #获取人眼的关键点
            dims=criticPoints(frame,locate)
            #将得到的坐标值转换为二维
            dims=predict2Np(dims)
            #获取左眼的关键点坐标值列表
            left_eye=dims[42:48]
            # 获取右眼的关键点坐标值列表
            right_eye=dims[36:42]
            #绘制左眼的点
            for (x, y) in left_eye:
                cv2.circle(img=frame, center=(x, y),
                           radius=2, color=(0, 255, 0), thickness=-1)
            #绘制右眼的点
            for (x, y) in right_eye:
                cv2.circle(img=frame, center=(x, y),
                           radius=2, color=(0, 255, 0), thickness=-1)
            #计算距离
            earLeft=ComputeCloseEye(left_eye)
            earRight=ComputeCloseEye(right_eye)
            #计算左眼和右眼的平均纵横比
            avg_Ear=(earRight+earLeft)/2
            cv2.putText(img=frame,text='CloseEyeDist: '+str(round(avg_Ear,2)),org=(20,50),
                        fontFace=cv2.FONT_HERSHEY_SIMPLEX,fontScale=1.0,
                        color=(0,255,0),thickness=2)
        return frame,avg_Ear
    
    #设置纵横比的阈值
    Ear_Threshod=0.2
    #眨眼动作是一个快速闭合的过程,眨眼持续差不多为100-400ms
    #设置当连续3帧的纵横比都小于阈值则表示眨眼
    Ear_frame_Threshold=3
    #一次任务中的总的眨眼次数
    ToClose_Eye=0
    
    #实时的人脸关键点检测
    def detect_time():
        cap=cv2.VideoCapture(0)
        #记录连续眨眼的次数
        count=0
        global ToClose_Eye
    
        while cap.isOpened():
            #记录开始时间
            statime=time.time()
            ret,frame=cap.read()
            #检测人脸位置
            detected = detector(frame)
            #利用定位到的人脸进行人脸关键点检测
            frame = drawRectangle(detected, frame)
            frame,avg_Ear=draw_left_and_right_eye(detected,frame)
            if avg_Ear<Ear_Threshod:
                count+=1
                if count>=Ear_frame_Threshold:
                    ToClose_Eye+=1
                    count=0
            cv2.putText(img=frame,text='ToClose_Eye: '+str(ToClose_Eye),org=(20,80),fontFace=cv2.FONT_HERSHEY_SIMPLEX,
                        fontScale=1.0,color=(0,255,0),thickness=2)
    
            #记录结束时间
            endtime=time.time()
            FPS=1/(endtime-statime)
            cv2.putText(img=frame, text='FPS: '+str(int(FPS)), org=(20, 110), fontFace=cv2.FONT_HERSHEY_SIMPLEX,
                        fontScale=1.0, color=(0, 255, 0), thickness=2)
            cv2.imshow('frame', frame)
            key=cv2.waitKey(1)
            if key==27:
                break
        cap.release()
        cv2.destroyAllWindows()
    
    
    if __name__ == '__main__':
        print('Pycharm')
        detect_time()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152

    在这里插入图片描述

  • 相关阅读:
    EasyWindow - Android 悬浮窗框架
    消息驱动 —— SpringCloud Stream
    python之word操作
    如何快速配置NFS
    【大数据存储技术】思考题及参考答案
    python文本
    Unity中的AI算法和实现1-Waypoint
    DataFrame在指定位置插入行和列
    NX二次开发-UFUN分配内存UF_allocate_memory
    Ubuntu 安装 tbb 步骤详解
  • 原文地址:https://blog.csdn.net/Keep_Trying_Go/article/details/125559342