【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的混乱空间

相关推荐
小杍随笔1 分钟前
【Rust `lib.rs` 使用方法:模块组织、API导出与最佳实践】
服务器·开发语言·rust
lly2024063 分钟前
SQLite Truncate Table: 深入理解与最佳实践
开发语言
iFeng的小屋5 分钟前
【2026最新携程酒店爬虫分享】用Python批量爬取酒店评论,含回复内容一键保存Excel!
开发语言·爬虫·python
独自破碎E9 分钟前
手撕真题-计算二叉树中两个节点之间的距离
java·开发语言
为美好的生活献上中指13 分钟前
*Java 沉淀重走长征路*之——《Java Web 应用开发完全指南:从零到企业实战(两万字深度解析)》
java·开发语言·前端·html·javaweb·js
赵丙双13 分钟前
python-docx 报错 KeyError: “There is no item named ‘NULL‘ in the archive“
python·word·docx·python-docx
不光头强15 分钟前
抽象类和接口的区别
java·开发语言·python
ShoreKiten16 分钟前
Flask/ssti --by vulhub
后端·python·flask
llxxyy卢16 分钟前
polar-web题目
开发语言·前端·javascript
悦心无谓21 分钟前
C++负载均衡式在线OJ测试报告
开发语言·c++·selenium·测试工具·负载均衡·编程语言·后端开发