基于YCgCr空间的肤色分割 + Dlib关键点检测 + 基于NMEAR的疲劳检测实现

发布时间:2023-02-23 12:00

前言

基于YCgCr空间的肤色分割 + Dlib关键点检测 + 基于NMEAR的疲劳检测实现

一、实现原理和流程图

基于YCgCr空间的肤色分割 + Dlib关键点检测 + 基于NMEAR的疲劳检测实现_第1张图片

二、安装

1.安装opencv:
    pip install opencv-python
2.安装dlib:
    pip install dlib-19.7.0-cp36-cp36m-win_amd64.whl
3.报缺少什么就pip安装什么库

三、基于YCgCr空间的肤色分割

@jit(nopython=True) # jit实现for循环的加速
def for_ij2(image, Cr, Cb, result):
    for j in range(image.shape[0]):
        for i in range(image.shape[1]):
            if 133 < Cr[j][i] < 173 and 77 < Cb[j][i] < 127:
                result[j][i] = 255
            # else:result[j][i] = 0
    return result

def face_detect(image, element):
    YCrCbMat = cv2.cvtColor(image, cv2.COLOR_BGR2YCrCb)
    print(YCrCbMat.shape)
    Cr = YCrCbMat[:, :, 1]
    Cb = YCrCbMat[:, :, 2]
    # print(Y.shape, Cr.shape, Cb.shape)
    result = np.zeros(image.shape[:2], dtype=np.uint8)
    t1 = time.time()
    result = for_ij2(image, Cr, Cb, result)
    temp = cv2.morphologyEx(result, cv2.MORPH_CLOSE, element)
    contours, hierarchy = cv2.findContours(temp, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    return contours

三、疲劳判断

def eye_aspect_ratio(eye):
    left = dist.euclidean(eye[1], eye[5])
    right = dist.euclidean(eye[2], eye[4])
    horizontal = dist.euclidean(eye[0], eye[3])
    ear = 2.0*horizontal/(left+right)
    return ear

if self.startCheck:
    if ear > self.eyesRatioLimit:
        self.eyesCloseCount += 1
        if self.eyesCloseCount >= 20:  # 检测到时间超过20次则发出警告
            cv2.putText(frame, "!!!!!!!!!!SLEEP!!!!!!!!!!!", (150, 70),cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
            threadNames = []
            threadingList = threading.enumerate()
            for i in range(len(threadingList)):
                threadNames.append(threadingList[i].getName())
            if "BlinkThread" not in threadNames:
                print(threadingList)
                thread = BlinkThread(1, "BlinkThread")
                thread.start()
    else:
        self.eyesCloseCount = 0

总结

简单的小demo,完整版详见本人GitHub

ItVuer - 免责声明 - 关于我们 - 联系我们

本网站信息来源于互联网,如有侵权请联系:561261067@qq.com

桂ICP备16001015号