基本思想:使用pyqt 開發一個打開攝像頭和關閉攝像頭,同時加入了人眼睛眨的次數統計;
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import sys
import cv2
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import QPalette, QBrush, QPixmap
import os
import dlib
from scipy.spatial import distance
import os
from imutils import face_utils
class Ui_MainWindow(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Ui_MainWindow, self).__init__(parent)
# self.face_recong = face.Recognition()
self.timer_camera = QtCore.QTimer()
self.cap = cv2.VideoCapture()
self.CAM_NUM = 0
self.set_ui()
self.slot_init()
self.__flag_work = 0
self.x = 0
self.count = 0
self.shape_detector_path = 'shape_predictor_68_face_landmarks.dat' # 人臉特徵點檢測模型路徑
self.detector = dlib.get_frontal_face_detector() # 人臉檢測器
self.predictor = dlib.shape_predictor(self.shape_detector_path) # 人臉特徵點檢測器
self.EYE_AR_THRESH = 0.3 # EAR閾值
self.EYE_AR_CONSEC_FRAMES = 3 # 當EAR小於閾值時,接連多少幀一定發生眨眼動作
# 對應特徵點的序號
self.RIGHT_EYE_START = 37 - 1
self.RIGHT_EYE_END = 42 - 1
self.LEFT_EYE_START = 43 - 1
self.LEFT_EYE_END = 48 - 1
self.face =True
def set_ui(self):
self.__layout_main = QtWidgets.QHBoxLayout()
self.__layout_fun_button = QtWidgets.QVBoxLayout()
self.__layout_data_show = QtWidgets.QVBoxLayout()
self.button_open_camera = QtWidgets.QPushButton(u'開始播放')
self.button_close = QtWidgets.QPushButton(u'關閉播放')
# Button 的顏色修改
button_color = [self.button_open_camera, self.button_close]
for i in range(2):
button_color[i].setStyleSheet("QPushButton{color:black}"
"QPushButton:hover{color:red}"
"QPushButton{background-color:rgb(78,255,255)}"
"QPushButton{border:2px}"
"QPushButton{border-radius:10px}"
"QPushButton{padding:2px 4px}")
self.button_open_camera.setMinimumHeight(50)
self.button_close.setMinimumHeight(50)
# move()方法移動窗口在屏幕上的位置到x = 300,y = 300座標。
self.move(500, 500)
# 信息顯示
self.label_show_camera = QtWidgets.QLabel()
self.label_move = QtWidgets.QLabel()
self.label_move.setFixedSize(100, 100)
self.label_show_camera.setFixedSize(641, 481)
self.label_show_camera.setAutoFillBackground(False)
self.__layout_fun_button.addWidget(self.button_open_camera)
self.__layout_fun_button.addWidget(self.button_close)
self.__layout_fun_button.addWidget(self.label_move)
self.__layout_main.addLayout(self.__layout_fun_button)
self.__layout_main.addWidget(self.label_show_camera)
self.setLayout(self.__layout_main)
self.label_move.raise_()
self.setWindowTitle(u'攝像頭')
'''
# 設置背景圖片
palette1 = QPalette()
palette1.setBrush(self.backgroundRole(), QBrush(QPixmap('background.png')))
self.setPalette(palette1)
'''
def slot_init(self):
self.button_open_camera.clicked.connect(self.button_open_camera_click)
self.timer_camera.timeout.connect(self.show_camera)
self.button_close.clicked.connect(self.close)
def button_open_camera_click(self):
if self.timer_camera.isActive() == False:
self.frame_counter = 0 # 連續幀計數
self.blink_counter = 0 # 眨眼計數
flag = self.cap.open(self.CAM_NUM)
if flag == False:
msg = QtWidgets.QMessageBox.warning(self, u"Warning", u"請檢測相機與電腦是否連接正確",
buttons=QtWidgets.QMessageBox.Ok,
defaultButton=QtWidgets.QMessageBox.Ok)
# if msg==QtGui.QMessageBox.Cancel:
# pass
else:
self.timer_camera.start(30)
self.button_open_camera.setText(u'關閉相機')
else:
self.timer_camera.stop()
self.cap.release()
self.label_show_camera.clear()
self.button_open_camera.setText(u'打開相機')
def show_camera(self):
flag, self.image = self.cap.read()
# face = self.face_detect.align(self.image)
if self.face:
self.detect_eyes()
self.show = cv2.resize(self.image, (640, 480))
self.show = cv2.cvtColor(self.show, cv2.COLOR_BGR2RGB)
# print(show.shape[1], show.shape[0])
# show.shape[1] = 640, show.shape[0] = 480
showImage = QtGui.QImage(self.show.data, self.show.shape[1], self.show.shape[0], QtGui.QImage.Format_RGB888)
self.label_show_camera.setPixmap(QtGui.QPixmap.fromImage(showImage))
# self.x += 1
# self.label_move.move(self.x,100)
# if self.x ==320:
# self.label_show_camera.raise_()
def closeEvent(self, event):
ok = QtWidgets.QPushButton()
cacel = QtWidgets.QPushButton()
msg = QtWidgets.QMessageBox(QtWidgets.QMessageBox.Warning, u"關閉", u"是否關閉!")
msg.addButton(ok, QtWidgets.QMessageBox.ActionRole)
msg.addButton(cacel, QtWidgets.QMessageBox.RejectRole)
ok.setText(u'確定')
cacel.setText(u'取消')
# msg.setDetailedText('sdfsdff')
if msg.exec_() == QtWidgets.QMessageBox.RejectRole:
event.ignore()
else:
# self.socket_client.send_command(self.socket_client.current_user_command)
if self.cap.isOpened():
self.cap.release()
if self.timer_camera.isActive():
self.timer_camera.stop()
event.accept()
def detect_eyes(self):
gray = cv2.cvtColor(self.image, cv2.COLOR_BGR2GRAY)
rects = self.detector(gray, 0) # 人臉檢測
for rect in rects: # 遍歷每一個人臉
print('-' * 20)
shape = self.predictor(gray, rect) # 檢測特徵點
points = face_utils.shape_to_np(shape) # convert the facial landmark (x, y)-coordinates to a NumPy array
leftEye = points[self.LEFT_EYE_START:self.LEFT_EYE_END + 1] # 取出左眼對應的特徵點
rightEye = points[self.RIGHT_EYE_START:self.RIGHT_EYE_END + 1] # 取出右眼對應的特徵點
leftEAR = self.eye_aspect_ratio(leftEye) # 計算左眼EAR
rightEAR = self.eye_aspect_ratio(rightEye) # 計算右眼EAR
print('leftEAR = {0}'.format(leftEAR))
print('rightEAR = {0}'.format(rightEAR))
ear = (leftEAR + rightEAR) / 2.0 # 求左右眼EAR的均值
leftEyeHull = cv2.convexHull(leftEye) # 尋找左眼輪廓
rightEyeHull = cv2.convexHull(rightEye) # 尋找右眼輪廓
cv2.drawContours(self.image, [leftEyeHull], -1, (0, 255, 0), 1) # 繪製左眼輪廓
cv2.drawContours(self.image, [rightEyeHull], -1, (0, 255, 0), 1) # 繪製右眼輪廓
# 如果EAR小於閾值,開始計算連續幀,只有連續幀計數超過EYE_AR_CONSEC_FRAMES時,纔會計做一次眨眼
if ear < self.EYE_AR_THRESH:
self.frame_counter += 1
else:
if self.frame_counter >= self.EYE_AR_CONSEC_FRAMES:
self.blink_counter += 1
self.frame_counter = 0
# 在圖像上顯示出眨眼次數blink_counter和EAR
#cv2.putText(self.img, "Blinks:{0}".format(self.blink_counter), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255),2)
cv2.putText(self.image, "Blinks:{0}".format(self.blink_counter), (300, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
def eye_aspect_ratio(self,eye):
# print(eye)
A = distance.euclidean(eye[1], eye[5])
B = distance.euclidean(eye[2], eye[4])
C = distance.euclidean(eye[0], eye[3])
ear = (A + B) / (2.0 * C)
return ear
if __name__ == "__main__":
App = QApplication(sys.argv)
ex = Ui_MainWindow()
ex.show()
sys.exit(App.exec_())
大部分代碼是站在“巨人”的肩膀上修改的,如有冒犯,請聯繫我