from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtCore import QDateTime
from PyQt5 import QtWidgets, QtGui, QtCore
import sys
import cv2
import numpy as np
from ui.raspberry import Ui_MainWindow
class MyWindows(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super(MyWindows, self).__init__()
self.setupUi(self)
self.cap = cv2.VideoCapture() # 视频流
self.timer_camera = QTimer() # 定义定时器,用于控制显示视频的帧率
self.CAM_NUM = 0 # 为0时表示视频流来自笔记本内置摄像头
self.slot_init() # 初始化槽函数
self.timer1 = QTimer()
self.timer1.timeout.connect(self.showTime)
self.timer1.start(1000)
self.set_label()
self.yanmo()
def showTime(self):
# print(1)
time = QDateTime.currentDateTime()
timeDisplay = time.toString("yyyy-MM-dd hh:mm:ss")
self.label_time.setText(timeDisplay)
self.label_time.setFont(QFont("宋体", 16, QFont.Bold))
def set_label(self):
self.label_name.setText('人脸打卡终端')
self.label_name.setFont(QFont("宋体", 16, QFont.Bold))
self.label_name.setAlignment(Qt.AlignCenter)
def yanmo(self):
opencv = cv2.imread('./resource/1231.jpg')
# todo 指定黑色范围
minGreen = np.array([-1, -1, -1])
maxGreen = np.array([10, 10, 10])
# todo 确定黑色范围
mask = cv2.inRange(opencv, minGreen, maxGreen)
# todo 确定非黑色范围
self.mask_not = cv2.bitwise_not(mask)
# todo 通过掩码控制的按位与运算锁定白色区域
def slot_init(self):
self.timer_camera.timeout.connect(self.show_camera)
self.pushButton.clicked.connect(self.slotCameraButton)
def show_camera(self):
ret, self.image = self.cap.read()
green_not = cv2.bitwise_and(self.image, self.image, mask=self.mask_not)
# todo 拆分为3通道
b, g, r = cv2.split(green_not)
# todo 合成四通道
bgra = cv2.merge([b, g, r, self.mask_not])
cv2.imwrite('./resource/1.png', bgra)
pix = QPixmap('./resource/1.png')
self.label_video.setPixmap(pix)
self.label_video.setScaledContents(True)
def slotCameraButton(self):
if self.timer_camera.isActive() == False:
self.openCamera()
else:
self.closeCamera()
def openCamera(self):
ret = self.cap.open(self.CAM_NUM)
if ret == False:
msg = QMessageBox.Warning(self, u'Warning', u'请检测相机与电脑是否连接正确',
buttons=QMessageBox.Ok,
defaultButton=QMessageBox.Ok)
else:
self.timer_camera.start(10)
def closeCamera(self):
self.timer_camera.stop()
self.label_video.clear()
self.cap.release()
self.pushButton.setText('信息录入')
if __name__ == '__main__':
app = QApplication(sys.argv)
my_windows = MyWindows() # 实例化对象
# print(123123)
my_windows.show() # 显示窗口
# # my_windows.showFullScreen() # 显示窗口
# sys.exit(app.exec_())
sys.exit(app.exec_())
- 1
- 2
- 3
- 4
前往页