class MainWindow( QtWidgets.QMainWindow, Ui_kitchenWindow ): # 继承qtdesiner的MinWindow 确保window为qtwidgets 不然massagebox无法启用 def __init__(self, *args, **kwargs): QtWidgets.QMainWindow.__init__(self, *args, **kwargs) self.code_path = os.path.dirname(os.path.abspath(__file__)) # 获取代码路径 self.project_path = os.path.dirname(self.code_path) # 获取识别项目路径 self.photo_path = r'\\192.168.137.69\pi\share\otherImage' self.source_path = os.path.join(self.project_path, "Resources") # 获取依赖数据路径 self.errors_path = os.path.join(self.source_path, "Errors") # 获取依赖数据路径 self.imagePath = os.path.join(self.photo_path, r'1.jpg') self.thread = QThread() self.myThread = MyThread() self.myThread.moveToThread(self.thread) self.myThread.startsig.connect(self.myThread.work) self.myThread.endsig.connect(self.update) self.thread.start() self.ssh = SSH() self.log = fileAndLog() self.timer = QTimer() self.timer.timeout.connect(self.run) def run(self): if len(os.listdir(self.photo_path)) == 1: global img1 img1 = cv2.imread(self.imagePath) self.NormalImageLable.setPixmap( QPixmap(self.imagePath).scaled(493, 572)) QApplication.processEvents() self.myThread.startsig.emit(1) os.remove(self.imagePath) def start(self): self.startButton.setEnabled(False) # 禁止重复开始 self.stopButton.setEnabled(True) self.ssh.startOther() time.sleep(2) self.timer.start(100) def update(self, flag): if flag: height, width, bytesPerComponent = img2.shape bytesPerLine = bytesPerComponent * width show = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB) # 视频色彩转换回RGB,这样才是现实的颜色 showImage = QImage(show.data, width, height, bytesPerLine, QImage.Format_RGB888) # 把读取到的视频数据变成QImage形式 self.ErrorImageLable.setPixmap( QPixmap.fromImage(showImage).scaled(466, 298)) QApplication.processEvents() logtext = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) logtext += " 图像检测发现有火焰" # self.log.save(img1, 2) # 保存图片,写入日志 self.ErrorText.appendPlainText(logtext) # 插入窗口日志 def stop(self): replay = QMessageBox.question(self, "", "确认停止嘛", QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if replay == QMessageBox.Yes: self.ssh.stopOther() # 树莓派结束拍摄 self.timer.stop() # 结束识别 self.NormalImageLable.clear() self.ErrorImageLable.clear() self.ssh.clear() # 清空文件夹 self.startButton.setEnabled(True) self.stopButton.setEnabled(False) def closeEvent(self, event): self.ssh.stopOther() self.ssh.clear() # 清空文件夹 event.accept() def open(self): os.system("explorer.exe %s" % self.errors_path)
class Ui_mainWindow(object): def __init__(self): self.doorWindow = dw.doorWindow() self.kitchenWindow = kw.kitchenWindow() self.ssh = SSH() self.ssh.stopFace() self.ssh.stopOther() time.sleep(0.2) self.ssh.clear() def setupUi(self, mainWindow): mainWindow.setObjectName("mainWindow") mainWindow.resize(954, 682) mainWindow.setStyleSheet( "#mainWindow{border-image: url(:/Resources/background.png);\n" "}\n" "") self.centralwidget = QtWidgets.QWidget(mainWindow) self.centralwidget.setObjectName("centralwidget") self.horizontalLayout = QtWidgets.QHBoxLayout(self.centralwidget) self.horizontalLayout.setObjectName("horizontalLayout") spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) self.horizontalLayout.addItem(spacerItem) self.verticalLayout = QtWidgets.QVBoxLayout() self.verticalLayout.setObjectName("verticalLayout") spacerItem1 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) self.verticalLayout.addItem(spacerItem1) spacerItem2 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) self.verticalLayout.addItem(spacerItem2) spacerItem3 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) self.verticalLayout.addItem(spacerItem3) spacerItem4 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) self.verticalLayout.addItem(spacerItem4) spacerItem5 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) self.verticalLayout.addItem(spacerItem5) spacerItem6 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) self.verticalLayout.addItem(spacerItem6) self.doorButton = QtWidgets.QPushButton(self.centralwidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth( self.doorButton.sizePolicy().hasHeightForWidth()) self.doorButton.setSizePolicy(sizePolicy) self.doorButton.setObjectName("doorButton") self.verticalLayout.addWidget(self.doorButton) spacerItem7 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) self.verticalLayout.addItem(spacerItem7) self.kitchenButton = QtWidgets.QPushButton(self.centralwidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth( self.kitchenButton.sizePolicy().hasHeightForWidth()) self.kitchenButton.setSizePolicy(sizePolicy) self.kitchenButton.setObjectName("kitchenButton") self.verticalLayout.addWidget(self.kitchenButton) spacerItem8 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) self.verticalLayout.addItem(spacerItem8) self.waterButton = QtWidgets.QPushButton(self.centralwidget) self.waterButton.setEnabled(False) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth( self.waterButton.sizePolicy().hasHeightForWidth()) self.waterButton.setSizePolicy(sizePolicy) self.waterButton.setObjectName("waterButton") self.verticalLayout.addWidget(self.waterButton) spacerItem9 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) self.verticalLayout.addItem(spacerItem9) spacerItem10 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) self.verticalLayout.addItem(spacerItem10) spacerItem11 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) self.verticalLayout.addItem(spacerItem11) spacerItem12 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) self.verticalLayout.addItem(spacerItem12) self.horizontalLayout.addLayout(self.verticalLayout) spacerItem13 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) self.horizontalLayout.addItem(spacerItem13) spacerItem14 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) self.horizontalLayout.addItem(spacerItem14) spacerItem15 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) self.horizontalLayout.addItem(spacerItem15) spacerItem16 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) self.horizontalLayout.addItem(spacerItem16) mainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(mainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 954, 26)) self.menubar.setObjectName("menubar") mainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(mainWindow) self.statusbar.setObjectName("statusbar") mainWindow.setStatusBar(self.statusbar) self.retranslateUi(mainWindow) QtCore.QMetaObject.connectSlotsByName(mainWindow) # 设置按钮响应 self.doorButton.clicked.connect(self.Button_doorWindow) self.kitchenButton.clicked.connect(self.Button_kitchenWindow) def retranslateUi(self, mainWindow): _translate = QtCore.QCoreApplication.translate mainWindow.setWindowTitle(_translate("mainWindow", "主菜单")) self.doorButton.setText(_translate("mainWindow", "智能门禁")) self.kitchenButton.setText(_translate("mainWindow", "智能厨房")) self.waterButton.setText(_translate("mainWindow", "智能水浸")) # 智能门禁按钮响应 def Button_doorWindow(self): self.doorWindow.show() # 智能厨房按钮响应 def Button_kitchenWindow(self): self.kitchenWindow.show()
class MainWindow(QtWidgets.QMainWindow, Ui_doorWindow): # 确保window为qtwidgets 不然massagebox无法启用 def __init__(self, *args, **kwargs): QtWidgets.QMainWindow.__init__(self, *args, **kwargs) self.code_path = os.path.dirname(os.path.abspath(__file__)) # 获取代码路径 self.project_path = os.path.dirname(self.code_path) # 获取识别项目路径 self.source_path = os.path.join(self.project_path, "resources") # 获取依赖数据路径 self.photo_path = r'\\192.168.137.69\pi\share\faceImage' self.facerec = Face_Rec() self.ssh = SSH() self.runflag = False # 循环起始控制变量 self.timer = QTimer() def start(self): self.runflag = True # 开始循环 self.count = 0 # 人脸检测数清0 self.detecttime = 0 # 人脸检测起始时间清0 self.startButton.setEnabled(False) # 禁止重复开始 self.stopButton.setEnabled(True) self.ssh.startFace() time.sleep(0.5) print(1) self.timer.timeout.connect(self.detect) self.timer.start(300) def detect(self): print(2) if self.runflag and len(os.listdir(self.photo_path)) == 1: imagePath = os.path.join(self.photo_path, r'1.jpg') print(imagePath) img1 = cv2.imread(imagePath) self.NormalFace.setPixmap( QPixmap(imagePath).scaled(self.NormalFace.width(), self.NormalFace.height())) QtWidgets.QApplication.processEvents() isperson = self.facerec.isPerson(img1) print(isperson) if isperson: flag, img2 = self.facerec.isHost(img1) # flag为真表示是主人 print(flag) height, width, bytesPerComponent = img2.shape bytesPerLine = bytesPerComponent * width show = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB) # 视频色彩转换回RGB,这样才是现实的颜色 showImage = QImage(show.data, width, height, bytesPerLine, QImage.Format_RGB888) # 把读取到的视频数据变成QImage形式 self.ErrorFace.setPixmap( QPixmap.fromImage(showImage).scaled( self.ErrorFace.width(), self.ErrorFace.height())) QtWidgets.QApplication.processEvents() if not flag: if not self.detecttime == 0: # 起始时间未记录则记录开始检测到人脸的时间 self.detecttime = time.time() self.count += 1 if time.time( ) - self.detecttime > 20 and self.count > 20: # 停留时间超过2分钟 识别次数超过20次 记录为异常 # 调用文件处理 存储异常图片 返回文件路径 # 调用文件处理 返回要记录的日志文本 同时在外部log文件追加内容 self.count = 0 self.detecttime = 0 wrongImagePath = 1 # 储存的异常图片路径 待完善 logtext = '1' # 要插入的日志文本内容 待完善 self.ErrorText.appendPlainText(logtext) # 插入日志 elif time.time( ) - self.detecttime > 180 and self.count < 5: # 无人或正常人经过 记录清空 self.count = 0 self.detecttime = 0 os.remove(imagePath) time.sleep(0.1) def stop(self): replay = QMessageBox.question(self, "", "确认停止嘛", QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if replay == QMessageBox.Yes: self.timer.stop() self.runflag = False self.ssh.stopFace() self.NormalFace.clear() time.sleep(0.1) self.ssh.clear() # 清空文件夹 self.startButton.setEnabled(True) self.stopButton.setEnabled(False) def collect(self): self.imageget = imageCollect() self.imageget.show() def closeEvent(self, event): self.ssh.stopFace() event.accept()
class MainWindow( QtWidgets.QMainWindow, Ui_doorWindow ): # 继承qtdesiner的MinWindow 确保window为qtwidgets 不然massagebox无法启用 def __init__(self, *args, **kwargs): QtWidgets.QMainWindow.__init__(self, *args, **kwargs) self.code_path = os.path.dirname(os.path.abspath(__file__)) # 获取代码路径 self.project_path = os.path.dirname(self.code_path) # 获取识别项目路径 self.photo_path = r'\\192.168.137.69\pi\share\faceImage' self.source_path = os.path.join(self.project_path, "Resources") # 获取依赖数据路径 self.errors_path = os.path.join(self.source_path, "Errors") # 获取依赖数据路径 self.imagePath = os.path.join(self.photo_path, r'1.jpg') self.thread = QThread() self.myThread = MyThread() self.myThread.moveToThread(self.thread) self.myThread.startsig.connect(self.myThread.work) self.myThread.endsig.connect(self.update) self.thread.start() self.ssh = SSH() self.log = fileAndLog() self.timer = QTimer() self.timer.timeout.connect(self.run) def run(self): if len(os.listdir(self.photo_path)) == 1: global img1 img1 = cv2.imread(self.imagePath) self.NormalFace.setPixmap(QPixmap(self.imagePath).scaled(493, 572)) QApplication.processEvents() self.myThread.startsig.emit(1) os.remove(self.imagePath) def start(self): self.startButton.setEnabled(False) # 禁止重复开始 self.stopButton.setEnabled(True) self.ssh.startFace() time.sleep(2) self.timer.start(400) def update(self, flag, ishost): global detecttime, count if flag: height, width, bytesPerComponent = img2.shape bytesPerLine = bytesPerComponent * width show = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB) # 视频色彩转换回RGB,这样才是现实的颜色 showImage = QImage(show.data, width, height, bytesPerLine, QImage.Format_RGB888) # 把读取到的视频数据变成QImage形式 self.ErrorFace.setPixmap( QPixmap.fromImage(showImage).scaled(466, 298)) QApplication.processEvents() else: self.ErrorFace.clear() QApplication.processEvents() if flag and not ishost: # print(time.time() - detecttime, count) if detecttime == 0: # 起始时间未记录则记录开始检测到人脸的时间 detecttime = time.time() count = count + 1 if time.time( ) - detecttime > 30 and count > 30: # 停留时间超过2分钟 识别次数超过20次 记录为异常 count = 0 detecttime = 0 logtext = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) logtext += " 图像检测发现有陌生人长期停留" self.log.save(img1, 1) # 保存图片,写入日志 self.ErrorText.appendPlainText(logtext) # 插入窗口日志 elif time.time() - detecttime > 180 and count < 20: # 无人或正常人经过 记录清空 count = 0 detecttime = 0 def stop(self): replay = QMessageBox.question(self, "", "确认停止嘛", QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if replay == QMessageBox.Yes: self.ssh.stopFace() # 树莓派结束拍摄 self.timer.stop() # 结束识别 self.NormalFace.clear() self.ErrorFace.clear() self.ssh.clear() # 清空文件夹 self.startButton.setEnabled(True) self.stopButton.setEnabled(False) def collect(self): self.imageget = imageCollect() self.imageget.show() def closeEvent(self, event): self.ssh.stopFace() self.ssh.clear() # 清空文件夹 event.accept() def open(self): os.system("explorer.exe %s" % self.errors_path)