Skip to content

Commit fa50680

Browse files
Add files via upload
1 parent 716b37f commit fa50680

File tree

6 files changed

+776
-0
lines changed

6 files changed

+776
-0
lines changed

main.py

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
import sys
2+
import os
3+
from glob import glob
4+
from PySide2 import QtWidgets,QtCore,QtGui
5+
from PySide2.QtWidgets import QMainWindow, QFileDialog, QMessageBox
6+
from PySide2.QtCore import QDir, QTimer,Slot
7+
from PySide2.QtGui import QPixmap,QImage
8+
from ui_mainwindow import Ui_MainWindow
9+
import cv2
10+
import myframe
11+
12+
# 定义变量
13+
# 眼睛长宽比
14+
# 闪烁阈值
15+
EYE_AR_THRESH = 0.15
16+
EYE_AR_CONSEC_FRAMES = 2
17+
# 打哈欠长宽比
18+
# 闪烁阈值
19+
MAR_THRESH = 0.65
20+
MOUTH_AR_CONSEC_FRAMES = 3
21+
# 初始化帧计数器和眨眼总数
22+
COUNTER = 0
23+
TOTAL = 0
24+
# 初始化帧计数器和打哈欠总数
25+
mCOUNTER = 0
26+
mTOTAL = 0
27+
# 行为帧数变量
28+
ActionCOUNTER = 0
29+
# 周期变量
30+
Roll = 0
31+
Rolleye = 0
32+
Rollmouth = 0
33+
34+
class MainWindow(QMainWindow, Ui_MainWindow):
35+
def __init__(self):
36+
super(MainWindow, self).__init__()
37+
self.setupUi(self)
38+
# 打开文件类型,用于类的定义
39+
self.f_type = 0
40+
41+
def window_init(self):
42+
# 设置控件属性
43+
self.label.setText("请打开摄像头")
44+
self.label_2.setText("疲劳检测:")
45+
self.label_3.setText("眨眼次数:0")
46+
self.label_4.setText("哈欠次数:0")
47+
self.label_5.setText("行为检测:")
48+
self.label_6.setText("手机")
49+
self.label_7.setText("抽烟")
50+
self.label_8.setText("喝水")
51+
self.label_9.setText("是否存在分心行为")
52+
self.label_10.setText("是否为疲劳状态")
53+
self.menu.setTitle("打开")
54+
self.actionOpen_camera.setText("打开摄像头")
55+
# 菜单按钮 槽连接 到函数
56+
self.actionOpen_camera.triggered.connect(CamConfig_init)
57+
# 自适应窗口缩放
58+
self.label.setScaledContents(True)
59+
# def printf(self, mes):
60+
# self.textBrowser.append(mes) # 在指定的区域显示提示信息
61+
# self.cursot = self.textBrowser.textCursor()
62+
# self.textBrowser.moveCursor(self.cursot.End)
63+
64+
# 定义摄像头类
65+
class CamConfig:
66+
def __init__(self):
67+
Ui_MainWindow.printf(window,"正在打开摄像头请稍后...")
68+
# 设置时钟
69+
self.v_timer = QTimer()
70+
# 打开摄像头
71+
self.cap = cv2.VideoCapture(0)
72+
if not self.cap:
73+
Ui_MainWindow.printf(window,"打开摄像头失败")
74+
return
75+
# 设置定时器周期,单位毫秒
76+
self.v_timer.start(20)
77+
# 连接定时器周期溢出的槽函数,用于显示一帧视频
78+
self.v_timer.timeout.connect(self.show_pic)
79+
Ui_MainWindow.printf(window,"载入成功,开始运行程序")
80+
Ui_MainWindow.printf(window,"")
81+
Ui_MainWindow.printf(window,"开始执行疲劳检测...")
82+
window.statusbar.showMessage("正在使用摄像头...")
83+
def show_pic(self):
84+
# 全局变量
85+
global EYE_AR_THRESH,EYE_AR_CONSEC_FRAMES,MAR_THRESH,MOUTH_AR_CONSEC_FRAMES,COUNTER,TOTAL,mCOUNTER,mTOTAL,ActionCOUNTER,Roll,Rolleye,Rollmouth
86+
# 读取一帧
87+
success, frame = self.cap.read()
88+
if success:
89+
# Mat格式图像转Qt中图像的方法
90+
#检测
91+
ret,frame = myframe.frametest(frame)
92+
lab,eye,mouth = ret
93+
#行为判断
94+
ActionCOUNTER += 1
95+
for i in lab:
96+
if(i == "phone"):
97+
window.label_6.setText("<font color=red>正在用手机</font>")
98+
window.label_9.setText("<font color=red>请不要分心</font>")
99+
if ActionCOUNTER > 0:
100+
ActionCOUNTER -= 1
101+
elif(i == "smoke"):
102+
window.label_7.setText("<font color=red>正在抽烟</font>")
103+
window.label_9.setText("<font color=red>请不要分心</font>")
104+
if ActionCOUNTER > 0:
105+
ActionCOUNTER -= 1
106+
elif(i == "drink"):
107+
window.label_7.setText("<font color=red>正在用喝水</font>")
108+
window.label_9.setText("<font color=red>请不要分心</font>")
109+
if ActionCOUNTER > 0:
110+
ActionCOUNTER -= 1
111+
#疲劳判断
112+
if eye < EYE_AR_THRESH: # 眼睛长宽比:0.2
113+
COUNTER += 1
114+
Rolleye += 1
115+
else:
116+
# 如果连续3次都小于阈值,则表示进行了一次眨眼活动
117+
if COUNTER >= EYE_AR_CONSEC_FRAMES: # 阈值:3
118+
TOTAL += 1
119+
window.label_3.setText("眨眼次数:" + str(TOTAL))
120+
# 重置眼帧计数器
121+
COUNTER = 0
122+
if mouth > MAR_THRESH: # 张嘴阈值0.5
123+
mCOUNTER += 1
124+
Rollmouth += 1
125+
else:
126+
# 如果连续3次都小于阈值,则表示打了一次哈欠
127+
if mCOUNTER >= MOUTH_AR_CONSEC_FRAMES: # 阈值:3
128+
mTOTAL += 1
129+
window.label_4.setText("哈欠次数:" + str(mTOTAL))
130+
# 重置嘴帧计数器
131+
mCOUNTER = 0
132+
if ActionCOUNTER == 15:
133+
window.label_6.setText("手机")
134+
window.label_7.setText("抽烟")
135+
window.label_8.setText("喝水")
136+
window.label_9.setText("")
137+
ActionCOUNTER = 0
138+
show = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
139+
showImage = QImage(show.data, show.shape[1], show.shape[0], QImage.Format_RGB888)
140+
window.label.setPixmap(QPixmap.fromImage(showImage))
141+
Roll += 1
142+
if Roll == 150:
143+
perclos = (Rolleye/Roll) + (Rollmouth/Roll)*0.2
144+
Ui_MainWindow.printf(window,"过去150帧中,Perclos得分为"+str(round(perclos,3)))
145+
if perclos > 0.38:
146+
Ui_MainWindow.printf(window,"当前处于疲劳状态")
147+
window.label_10.setText("<font color=red>疲劳!!!</font>")
148+
Ui_MainWindow.printf(window,"")
149+
else:
150+
Ui_MainWindow.printf(window,"当前处于清醒状态")
151+
window.label_10.setText("清醒")
152+
Ui_MainWindow.printf(window,"")
153+
#归零
154+
Roll = 0
155+
Rolleye = 0
156+
Rollmouth = 0
157+
Ui_MainWindow.printf(window,"重新开始执行疲劳检测...")
158+
def CamConfig_init():
159+
window.f_type = CamConfig()
160+
161+
162+
if __name__ == '__main__':
163+
app = QtWidgets.QApplication(sys.argv)
164+
window = MainWindow()
165+
window.window_init()
166+
window.show()
167+
sys.exit(app.exec_())

mainwindow.ui

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ui version="4.0">
3+
<class>MainWindow</class>
4+
<widget class="QMainWindow" name="MainWindow">
5+
<property name="geometry">
6+
<rect>
7+
<x>0</x>
8+
<y>0</y>
9+
<width>1060</width>
10+
<height>578</height>
11+
</rect>
12+
</property>
13+
<property name="windowTitle">
14+
<string>MainWindow</string>
15+
</property>
16+
<widget class="QWidget" name="centralwidget">
17+
<layout class="QVBoxLayout" name="verticalLayout_2">
18+
<item>
19+
<layout class="QHBoxLayout" name="horizontalLayout">
20+
<item>
21+
<widget class="QLabel" name="label">
22+
<property name="minimumSize">
23+
<size>
24+
<width>720</width>
25+
<height>480</height>
26+
</size>
27+
</property>
28+
<property name="maximumSize">
29+
<size>
30+
<width>720</width>
31+
<height>480</height>
32+
</size>
33+
</property>
34+
<property name="text">
35+
<string>TextLabel</string>
36+
</property>
37+
</widget>
38+
</item>
39+
<item>
40+
<layout class="QVBoxLayout" name="verticalLayout">
41+
<item>
42+
<layout class="QHBoxLayout" name="horizontalLayout_5">
43+
<item>
44+
<widget class="QLabel" name="label_2">
45+
<property name="maximumSize">
46+
<size>
47+
<width>120</width>
48+
<height>30</height>
49+
</size>
50+
</property>
51+
<property name="text">
52+
<string>TextLabel</string>
53+
</property>
54+
</widget>
55+
</item>
56+
<item>
57+
<widget class="QLabel" name="label_10">
58+
<property name="maximumSize">
59+
<size>
60+
<width>180</width>
61+
<height>30</height>
62+
</size>
63+
</property>
64+
<property name="text">
65+
<string>TextLabel</string>
66+
</property>
67+
</widget>
68+
</item>
69+
</layout>
70+
</item>
71+
<item>
72+
<layout class="QHBoxLayout" name="horizontalLayout_2">
73+
<item>
74+
<widget class="QLabel" name="label_3">
75+
<property name="maximumSize">
76+
<size>
77+
<width>150</width>
78+
<height>30</height>
79+
</size>
80+
</property>
81+
<property name="text">
82+
<string>TextLabel</string>
83+
</property>
84+
</widget>
85+
</item>
86+
<item>
87+
<widget class="QLabel" name="label_4">
88+
<property name="maximumSize">
89+
<size>
90+
<width>150</width>
91+
<height>30</height>
92+
</size>
93+
</property>
94+
<property name="text">
95+
<string>TextLabel</string>
96+
</property>
97+
</widget>
98+
</item>
99+
</layout>
100+
</item>
101+
<item>
102+
<layout class="QHBoxLayout" name="horizontalLayout_4">
103+
<item>
104+
<widget class="QLabel" name="label_5">
105+
<property name="maximumSize">
106+
<size>
107+
<width>120</width>
108+
<height>30</height>
109+
</size>
110+
</property>
111+
<property name="text">
112+
<string>TextLabel</string>
113+
</property>
114+
</widget>
115+
</item>
116+
<item>
117+
<widget class="QLabel" name="label_9">
118+
<property name="maximumSize">
119+
<size>
120+
<width>180</width>
121+
<height>30</height>
122+
</size>
123+
</property>
124+
<property name="text">
125+
<string>TextLabel</string>
126+
</property>
127+
</widget>
128+
</item>
129+
</layout>
130+
</item>
131+
<item>
132+
<layout class="QHBoxLayout" name="horizontalLayout_3">
133+
<item>
134+
<widget class="QLabel" name="label_6">
135+
<property name="maximumSize">
136+
<size>
137+
<width>100</width>
138+
<height>30</height>
139+
</size>
140+
</property>
141+
<property name="text">
142+
<string>TextLabel</string>
143+
</property>
144+
</widget>
145+
</item>
146+
<item>
147+
<widget class="QLabel" name="label_7">
148+
<property name="maximumSize">
149+
<size>
150+
<width>100</width>
151+
<height>30</height>
152+
</size>
153+
</property>
154+
<property name="text">
155+
<string>TextLabel</string>
156+
</property>
157+
</widget>
158+
</item>
159+
<item>
160+
<widget class="QLabel" name="label_8">
161+
<property name="maximumSize">
162+
<size>
163+
<width>100</width>
164+
<height>30</height>
165+
</size>
166+
</property>
167+
<property name="text">
168+
<string>TextLabel</string>
169+
</property>
170+
</widget>
171+
</item>
172+
</layout>
173+
</item>
174+
<item>
175+
<widget class="QTextBrowser" name="textBrowser">
176+
<property name="maximumSize">
177+
<size>
178+
<width>300</width>
179+
<height>360</height>
180+
</size>
181+
</property>
182+
</widget>
183+
</item>
184+
</layout>
185+
</item>
186+
</layout>
187+
</item>
188+
</layout>
189+
</widget>
190+
<widget class="QMenuBar" name="menubar">
191+
<property name="geometry">
192+
<rect>
193+
<x>0</x>
194+
<y>0</y>
195+
<width>1060</width>
196+
<height>26</height>
197+
</rect>
198+
</property>
199+
<widget class="QMenu" name="menu">
200+
<property name="title">
201+
<string>Open</string>
202+
</property>
203+
<addaction name="actionOpen_camera"/>
204+
</widget>
205+
<addaction name="menu"/>
206+
</widget>
207+
<widget class="QStatusBar" name="statusbar"/>
208+
<action name="actionOpen_camera">
209+
<property name="text">
210+
<string>Open camera</string>
211+
</property>
212+
</action>
213+
</widget>
214+
<resources/>
215+
<connections/>
216+
</ui>

0 commit comments

Comments
 (0)