界面转化命令
python -m PyQt5.uic.pyuic ./UI_1creat.ui -o ./UI_1UI.py
界面样子
調用代碼
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | from PyQt5.QtWidgets import QApplication,QMainWindow,QFileDialog from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * from PyQt5 import QtWidgets, QtCore, QtGui #导入图像库 import cv2 import numpy as np import datetime from qrcode_test import * #进程测试 #import multiprocessing import sys #线程测试 import threading from queue import Queue from multiprocessing import Process,Manager #导入自己创建的图形类 from UI_1UI import * import time import RPi.GPIO as GPIO GPIO.setwarnings( False ) GPIO.setmode(GPIO.BCM) pinA1 = 17 GPIO.setup(pinA1, GPIO.OUT) #设置脚为输出模式 GPIO.output(pinA1, GPIO.HIGH) #关闭 默认 #一个是这个类本身的,一个是这个类继承 class Example(QMainWindow,Ui_Form): def __init__( self ,ShareImages,Lock): #类的初始化 super (Example, self ).__init__() self .setupUi( self ) #界面控件初始化 #获取共享内存,外部参数数据和数据锁 self .ShareImages = ShareImages self .Lock = Lock #相机线程初始化 self .CAM_Int() def recongQR( self ): self .ShareImages[ 1 ] = 1 ''' if self.ShareImages[1]==1: self.ShareImages[1]=0 else: self.ShareImages[1]=1 ''' def opendoor( self ): GPIO.output(pinA1, GPIO.LOW) #亮LOW _translate = QtCore.QCoreApplication.translate self .label_tongxing.setText(_translate( "Form" , "手动打开闸门" )) def closedoor( self ): GPIO.output(pinA1, GPIO.HIGH) #亮 _translate = QtCore.QCoreApplication.translate self .label_tongxing.setText(_translate( "Form" , "手动关闭闸门" )) def CAM_Int( self ): #self.dateTimeEdit.setDisplayFormat("yyyy-MM-dd HH:mm:ss") #按钮绑定函数 self .pushButton.clicked.connect( self .recongQR) #开始按键 self .pushButton_2.clicked.connect( self .opendoor) #开始按键 self .pushButton_3.clicked.connect( self .closedoor) #开始按键 #定时器20毫秒更新画板 self .timer = QTimer( self ) self .timer.timeout.connect( self .CAM_SetImage) self .timer.start( 25 ) #单位为毫秒 #配合定时器刷新图像显示 def CAM_SetImage( self ): _translate = QtCore.QCoreApplication.translate with self .Lock: frame = self .ShareImages[ 0 ] #数据转化后才能用,不然只是一个numpy数组 rgbImage = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) convertToQtFormat = QtGui.QImage(rgbImage.data, rgbImage.shape[ 1 ], rgbImage.shape[ 0 ], QImage.Format_RGB888) #在这里可以对每帧图像进行处理, qtimg = convertToQtFormat.scaled( 640 , 480 , Qt.KeepAspectRatio) self .label_img.setPixmap(QPixmap.fromImage(qtimg)) #窗口刷新 if self .ShareImages[ 1 ] = = 2 : #识别成功 #print("识别结果") #print(self.ShareImages[3]) if self .ShareImages[ 3 ] = = "None" : self .label_shibie.setText(_translate( "Form" , "识别失败,请放好重新识别!" )) print ( "识别失败,请放好重新识别!" ) else : self .label_shibie.setText(_translate( "Form" , "识别成功,正在验证..." )) print ( "识别成功,正在验证..." ) #判断是否满足时间 dateTime = self .lineEditDate.text() print ( "期限时间:" ,dateTime, "识别时间:" , self .ShareImages[ 3 ]) rec = str ( self .ShareImages[ 3 ]).strip( '\n' ) #识别结果 result = self .compare_time(rec, str (dateTime)) if result: print ( "在有效期限内" ) self .label_shibie.setText(_translate( "Form" , "识别成功,在有效期限内\n" + rec)) self .label_tongxing.setText(_translate( "Form" , "自动打开闸门" )) GPIO.output(pinA1, GPIO.LOW) #亮 GPIO.output(pinA1, GPIO.HIGH) #关 else : print ( "不在有效期内" ) self .label_shibie.setText(_translate( "Form" , "识别成功,不在有效期内\n" + rec)) self .label_tongxing.setText(_translate( "Form" , "自动关闭闸门" )) GPIO.output(pinA1, GPIO.HIGH) #亮HIGH print (result) #根据结果修改状态 self .ShareImages[ 1 ] = 0 def closeEvent( self , event): self .ShareImages[ 2 ] = 1 #必须通过共享内存数据,强制关闭采集进程 print ( "QT关闭窗口" ) """ time格式 '2018-01-03 16:26:23' """ def compare_time( self ,startTime,endTime): #now = datetime.datetime.now () d_start = datetime.datetime.strptime (startTime, '%Y-%m-%d %H:%M:%S' ) d_end = datetime.datetime.strptime (endTime, '%Y-%m-%d %H:%M:%S' ) #result = (d_start<=now) and (d_end>=now) result = (d_start = = d_end) return result #result = compare_time('2018-01-03 16:26:23','2018-02-03 16:29:55') #print(result) def Getvideo(images,lock): cap = cv2.VideoCapture( 0 ) #cap.set(cv2.CAP_PROP_FRAME_WIDTH,800) #cap.set(cv2.CAP_PROP_FRAME_HEIGHT,600) #cap.set (cv2.CAP_PROP_FPS,25) while (cap.isOpened()): #print(images[1]) if images[ 2 ] = = 1 : print ( "采集进程关闭窗口" ) break ret, frame = cap.read() if ret: with lock: if images[ 1 ] = = 1 : cv2.imwrite( 'Saveimage.png' ,frame) images[ 3 ] = konw_qrcode( 'Saveimage.png' ) images[ 1 ] = 2 #识别结束 images[ 0 ] = frame else : #break continue if __name__ = = '__main__' : #---------------------开启一个单独进程,采集+识别-------------------- #共享内存数据 h = 480 #图像默认高 w = 640 #图像默认宽 img = cv2.imread( "Saveimage.png" , 1 ) #默认底板图片 img = cv2.resize(img,(w,h),) #大小调整 lock = Manager().Lock() #创建共享内存容器 ShareImages = Manager(). dict () #存str类型数据 ShareImages[ 0 ] = img #图像数据使用前必须初始化一个图像 ShareImages[ 1 ] = 0 #用于控制保存图像和开始识别 ShareImages[ 2 ] = 0 #用于QT界面关闭按键,关闭采集进程 ShareImages[ 3 ] = "None" #识别结果 p = Process(target = Getvideo, args = (ShareImages,lock)) #开启进程 p.deamon = True #伴随主进程关闭而关闭,但是有时候还是关闭不了,单独搞个标志位来控制 p.start() #开始 #----------------------QT界面主进程-------------------------------- app = QApplication(sys.argv) ui = Example(ShareImages,lock) #ShareImages共享内存数据 lock 共享数据锁 ui.show() sys.exit(app.exec_()) |
界面代碼
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | # -*- coding: utf-8 -*- # Form implementation generated from reading ui file './UI_1creat.ui' # # Created by: PyQt5 UI code generator 5.11.3 # # WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Form( object ): def setupUi( self , Form): Form.setObjectName( "Form" ) Form.resize( 1445 , 748 ) font = QtGui.QFont() font.setPointSize( 24 ) Form.setFont(font) self .label = QtWidgets.QLabel(Form) self .label.setGeometry(QtCore.QRect( 100 , 80 , 451 , 41 )) palette = QtGui.QPalette() brush = QtGui.QBrush(QtGui.QColor( 0 , 0 , 0 )) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.WindowText, brush) brush = QtGui.QBrush(QtGui.QColor( 0 , 0 , 0 )) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.WindowText, brush) brush = QtGui.QBrush(QtGui.QColor( 143 , 146 , 147 )) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.WindowText, brush) self .label.setPalette(palette) self .label.setAlignment(QtCore.Qt.AlignBottom|QtCore.Qt.AlignHCenter) self .label.setObjectName( "label" ) self .label_2 = QtWidgets.QLabel(Form) self .label_2.setGeometry(QtCore.QRect( 90 , 130 , 451 , 41 )) palette = QtGui.QPalette() brush = QtGui.QBrush(QtGui.QColor( 255 , 85 , 0 )) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.WindowText, brush) brush = QtGui.QBrush(QtGui.QColor( 255 , 85 , 0 )) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.WindowText, brush) brush = QtGui.QBrush(QtGui.QColor( 143 , 146 , 147 )) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.WindowText, brush) self .label_2.setPalette(palette) font = QtGui.QFont() font.setPointSize( 16 ) self .label_2.setFont(font) self .label_2.setAlignment(QtCore.Qt.AlignBottom|QtCore.Qt.AlignHCenter) self .label_2.setObjectName( "label_2" ) self .label_3 = QtWidgets.QLabel(Form) self .label_3.setGeometry(QtCore.QRect( 50 , 220 , 171 , 41 )) font = QtGui.QFont() font.setPointSize( 20 ) self .label_3.setFont(font) self .label_3.setObjectName( "label_3" ) self .lineEditDate = QtWidgets.QLineEdit(Form) self .lineEditDate.setGeometry(QtCore.QRect( 240 , 230 , 301 , 32 )) font = QtGui.QFont() font.setPointSize( 18 ) self .lineEditDate.setFont(font) self .lineEditDate.setObjectName( "lineEditDate" ) self .pushButton = QtWidgets.QPushButton(Form) self .pushButton.setGeometry(QtCore.QRect( 290 , 310 , 151 , 41 )) font = QtGui.QFont() font.setPointSize( 18 ) self .pushButton.setFont(font) self .pushButton.setObjectName( "pushButton" ) self .label_img = QtWidgets.QLabel(Form) self .label_img.setGeometry(QtCore.QRect( 640 , 60 , 771 , 651 )) palette = QtGui.QPalette() brush = QtGui.QBrush(QtGui.QColor( 255 , 170 , 0 )) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.WindowText, brush) brush = QtGui.QBrush(QtGui.QColor( 255 , 170 , 0 )) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.WindowText, brush) brush = QtGui.QBrush(QtGui.QColor( 143 , 146 , 147 )) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.WindowText, brush) self .label_img.setPalette(palette) self .label_img.setAlignment(QtCore.Qt.AlignCenter) self .label_img.setObjectName( "label_img" ) self .label_5 = QtWidgets.QLabel(Form) self .label_5.setGeometry(QtCore.QRect( 80 , 400 , 171 , 41 )) font = QtGui.QFont() font.setPointSize( 20 ) self .label_5.setFont(font) self .label_5.setObjectName( "label_5" ) self .label_shibie = QtWidgets.QLabel(Form) self .label_shibie.setGeometry(QtCore.QRect( 280 , 380 , 311 , 91 )) palette = QtGui.QPalette() brush = QtGui.QBrush(QtGui.QColor( 0 , 170 , 0 )) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.WindowText, brush) brush = QtGui.QBrush(QtGui.QColor( 0 , 170 , 0 )) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.WindowText, brush) brush = QtGui.QBrush(QtGui.QColor( 143 , 146 , 147 )) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.WindowText, brush) self .label_shibie.setPalette(palette) font = QtGui.QFont() font.setPointSize( 20 ) self .label_shibie.setFont(font) self .label_shibie.setObjectName( "label_shibie" ) self .label_7 = QtWidgets.QLabel(Form) self .label_7.setGeometry(QtCore.QRect( 80 , 500 , 171 , 41 )) font = QtGui.QFont() font.setPointSize( 20 ) self .label_7.setFont(font) self .label_7.setObjectName( "label_7" ) self .label_tongxing = QtWidgets.QLabel(Form) self .label_tongxing.setGeometry(QtCore.QRect( 280 , 500 , 311 , 41 )) palette = QtGui.QPalette() brush = QtGui.QBrush(QtGui.QColor( 0 , 85 , 255 )) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.WindowText, brush) brush = QtGui.QBrush(QtGui.QColor( 0 , 85 , 255 )) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.WindowText, brush) brush = QtGui.QBrush(QtGui.QColor( 143 , 146 , 147 )) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.WindowText, brush) self .label_tongxing.setPalette(palette) font = QtGui.QFont() font.setPointSize( 20 ) self .label_tongxing.setFont(font) self .label_tongxing.setObjectName( "label_tongxing" ) self .pushButton_2 = QtWidgets.QPushButton(Form) self .pushButton_2.setGeometry(QtCore.QRect( 160 , 610 , 151 , 41 )) font = QtGui.QFont() font.setPointSize( 18 ) self .pushButton_2.setFont(font) self .pushButton_2.setObjectName( "pushButton_2" ) self .pushButton_3 = QtWidgets.QPushButton(Form) self .pushButton_3.setGeometry(QtCore.QRect( 410 , 610 , 151 , 41 )) font = QtGui.QFont() font.setPointSize( 18 ) self .pushButton_3.setFont(font) self .pushButton_3.setObjectName( "pushButton_3" ) self .retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi( self , Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate( "Form" , "Form" )) self .label.setText(_translate( "Form" , "火车票二维码识别验票系统设计" )) self .label_2.setText(_translate( "Form" , "2021届毕业生 测控162 江雪婷" )) self .label_3.setText(_translate( "Form" , "请输入时间:" )) self .lineEditDate.setText(_translate( "Form" , "2021-03-03 16:26:23" )) self .pushButton.setText(_translate( "Form" , "检测识别" )) self .label_img.setText(_translate( "Form" , "检测识别窗口" )) self .label_5.setText(_translate( "Form" , "识别结果:" )) self .label_shibie.setText(_translate( "Form" , "等待识别结果......" )) self .label_7.setText(_translate( "Form" , "通行结果:" )) self .label_tongxing.setText(_translate( "Form" , "等待通行结果......" )) self .pushButton_2.setText(_translate( "Form" , "开闸门" )) self .pushButton_3.setText(_translate( "Form" , "关闸门" )) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2018-10-03 Mqtt用户认证