【Python】OpenCV-实时眼睛疲劳检测与提醒

实时眼睛疲劳检测与提醒

1. 引言

眼睛疲劳对于长时间使用电子设备的人群来说是一个常见的问题。为了帮助用户及时发现眼睛疲劳并采取相应的措施,本文介绍了一个实时眼睛疲劳检测与提醒系统的简单实现。使用了OpenCV、MediaPipe以及Playsound库,通过摄像头捕捉实时图像,检测眼睛疲劳并在需要时播放提示音。

2. 实现

2.1 实时视频显示

首先,通过OpenCV库捕获摄像头实时视频,显示在窗口中。

python 复制代码
import cv2

cap = cv2.VideoCapture(0)
while cap.isOpened():
    ret, frame = cap.read()
    if ret is False:
        break
    cv2.imshow("frame", frame)
    cv2.waitKey(10)

2.2 眼睛疲劳检测

接下来,使用MediaPipe库中的FaceMesh模型,获取面部特征点,从中提取左右眼的特定特征点。通过计算眼睛的EAR(Eye Aspect Ratio),判断眼睛是否闭合。

python 复制代码
import cv2
import mediapipe as mp
import numpy as np

# 初始化FaceMesh
face_mesh = mp.solutions.face_mesh.FaceMesh()

cap = cv2.VideoCapture(0)
sleep_frame_count = 0
while cap.isOpened():
    ret, frame = cap.read()
    if ret is False:
        break
    rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    result = face_mesh.process(rgb_frame)
    if result.multi_face_landmarks:
        for face in result.multi_face_landmarks:
            # 获取眼睛特定特征点
            right_eye_landmark_ids = [362, 385, 387, 263, 373, 380]
            left_eye_landmark_ids = [33, 160, 158, 133, 153, 144]
            right_eye_landmarks = []
            left_eye_landmarks = []
            for id, landmark in enumerate(face.landmark):
                if id in right_eye_landmark_ids:
                    x = int(landmark.x * frame.shape[1])
                    y = int(landmark.y * frame.shape[0])
                    cv2.circle(frame, [x, y], 1, [0, 255, 0])
                    right_eye_landmarks.append(np.array([x, y]))
                if id in left_eye_landmark_ids:
                    x = int(landmark.x * frame.shape[1])
                    y = int(landmark.y * frame.shape[0])
                    cv2.circle(frame, [x, y], 1, [0, 255, 0])
                    left_eye_landmarks.append(np.array([x, y]))

            # 计算眼睛的EAR
            def EAR(landmarks):
                d1 = np.linalg.norm(landmarks[1] - landmarks[5])
                d2 = np.linalg.norm(landmarks[2] - landmarks[4])
                d3 = np.linalg.norm(landmarks[0] - landmarks[3])
                return (d1 + d2) / d3 * 0.5

            left_ear = EAR(left_eye_landmarks)
            right_ear = EAR(right_eye_landmarks)

            # 判断眼睛是否闭合
            if (left_ear + right_ear) / 2 < 0.85:
                sleep_frame_count += 1
                if sleep_frame_count >= 30:
                    sleep_frame_count = 0
                    print("不要睡觉")
                    # 在新线程中播放提示音
                    t = Thread(target=play_sound).start()
    cv2.imshow("frame", frame)
    cv2.waitKey(10)

2.3 提示音播放

当检测到眼睛疲劳时,通过Playsound库在新线程中播放提示音。

python 复制代码
from threading import Thread
from playsound import playsound

def play_sound():
    playsound("tip.mp3")

3. 结论

通过上述代码,展示了一个基于OpenCV、MediaPipe和Playsound的简单实时眼睛疲劳检测与提醒系统。通过面部特征点的获取和EAR的计算,系统能够及时识别用户的眼睛状态,并在需要时通过提示音提醒用户。这个简单而有效的系统可以用于提高长时间使用电子设备的用户对眼睛疲劳的警觉。

提示音下载:https://sc.chinaz.com/yinxiao/230223359280.htm
代码参考源自:Shady的混乱空间

相关推荐
默_笙4 天前
🍙 给每个请求过安检:FastAPI 是怎么把校验写进类型注解的
python
AI的探索之旅4 天前
97 个 OpenCV 实例(三十):双目立体,从标定到点云
人工智能·opencv·计算机视觉
小羊没烦恼!4 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
qq_426003964 天前
启动playwright录制codegen生成自动化测试脚本
python·自动化
虎头金猫4 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
长沙三为智能科技4 天前
家政小程序开发从0到上线:五阶段交付流程与验收清单
python
伞伞悦读4 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
只睡四小时4 天前
Canvas 弹道联机实战:700 行 + 固定时间步长
python·websocket·html5·游戏开发·canvas
C语言小火车4 天前
C/C++ 为什么需要编译器?
开发语言·c++
奇思妙想聪明勤奋的小羊4 天前
DeepAgents第5章:子Agent 与上下文隔离—让 Agent学会委派
人工智能·python·学习·语言模型