pyqt5与眨眼检测结合--调用py文件以及刷新UI界面

最近做的一个东西,分享一下。
过程中,还是总结了一些经验,以及一些坑,最后还是自己慢慢填起来了。
先放UI,参考链接
原界面:
在这里插入图片描述

改进后:
在这里插入图片描述
把原来得开始按钮改为True,然后setText修改True之后得按钮文本。
增加了打开检测功能,调用人眼检测的py文件。
有个坑:因为人眼检测需要调用摄像头,点击原有的开始按钮也会调用摄像头。所以需要先release原有的,再进行调用,不然两个调用摄像头的都起不来。
所以改进如下:调用完以后,还需要重启原有调用的摄像头,不然原有程序未响应,摄像头不会有反应。

 def realtime_detection(self):
        self.MsgTE.setPlainText("detecting...")
        tag = self.ShowBt.text()
        if tag == '打开检测':
            self.camera.release()
            #调用眨眼检测py文件:
            os.system("python eye_detecting.py")
            self.camera = cv2.VideoCapture(0)

本地测试,配合原有的录像,达到"离线"检测的目的。
在这里插入图片描述

    def Onclick(self):
        msgBox = QMessageBox()
        msgBox.setWindowTitle('message')
        msgBox.setText('你是否想打开示例视频')
        msgBox.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
        buttonY = msgBox.button(QMessageBox.Yes)
        buttonY.setText('是')
        buttonN = msgBox.button(QMessageBox.No)
        buttonN.setText('选择其他文件')
        msgBox.exec_()
        if msgBox.clickedButton() == buttonY:
            os.system(cmd)
            self.MsgTE.setPlainText("video is over...")
        else:
            # dir_path = QFileDialog.getExistingDirectory(self, "choose directory", "D:/Record")
            # print(directory1)
            self.MsgTE.setPlainText("choose File.avi...")
            #可以自行百度下这个方法。
            fd=QFileDialog.getOpenFileName(self,'选择一个avi文件','D:/Record','ALL(*.*);;Images(*.png *.jpg);;avi文件(*.avi)','avi文件(*.avi)')
            fd=str(fd)
            import re
            #匹配文件名
            match_obj = re.match('\(\'(.*)\',', fd)
            if match_obj:
                res = match_obj.group(1)
                os.system('python detect_blinks.py --shape-predictor shape_predictor_68_face_landmarks.dat --video %s' % res)

又由于原有的退出,退出有点异常,会卡顿,所以调整了一下,使退出没有那么僵硬。
在这里插入图片描述

    def ExitApp(self, QCloseEvent):
        #1 中文
        msgBox = QMessageBox()
        msgBox.setWindowTitle('message')
        msgBox.setText('返回上一级点击右上角<b>X</b>')
        msgBox.setStandardButtons(QMessageBox.No | QMessageBox.Yes)
        buttonY = msgBox.button(QMessageBox.Yes)
        buttonY.setText('直接退出')
        buttonN = msgBox.button(QMessageBox.No)
        buttonN.setText('取消')
        msgBox.exec_()
        if msgBox.clickedButton() == buttonY:
            self.Timer.Stop()
            self.camera.release()
            self.MsgTE.setPlainText('Exiting the application..')
            self.close()
            # QCloseEvent.accept()
        else:
            pass

除此之外,新增了个选择界面。

在这里插入图片描述
这里放界面,简单实时传入参数即可。
这里参考链接:刷新UI界面
在这里插入图片描述
改进下:当点击中止时,然后slotAdd读取到中文字符,则break跳出循环。

  def slotAdd(self):
        self.LStopBt.setEnabled(True)
        while True:
        	#test1进行测试
        	#原有的是接受串口数据
            f=os.popen("python test1.py")
            d=f.read()
            str_n = d
            self.listFile.addItem(str_n)
            self.LMsgTE.append(str_n)
            QApplication.processEvents()
            time.sleep(0.1)
            if self.LMsgTE.toPlainText():
                str=self.LMsgTE.toPlainText().replace("\n","")
                match_obj=re.match(".*?([\u4e00-\u9fa5]+).*",str)
                if match_obj:
                    break

    #中止
    def breakloop(self):
        self.LMsgTE.append("中止")

得到如下效果:
在这里插入图片描述

以上就是一些总结。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章