我写了一个代码来读取USB网络摄像头和PYQT5中的一个应用程序的条形码,但我不知道如何将一个值从读取的条形码传递到我的应用程序主面板中的序列号窗口。
谁能给点建议吗?
相机自动检测条形码并在控制台中打印:
import cv2
import numpy as np
from pyzbar.pyzbar import decode
import time
def decoder(image):
gray_img = cv2.cvtColor(image,0)
barcode = decode(gray_img)
for obj in barcode:
points = obj.polygon
(x,y,w,h) = obj.rect
pts = np.array(points, np.int32)
pts = pts.reshape((-1, 1, 2))
cv2.polylines(image, [pts], True, (0, 255, 0), 3)
barcodeData = obj.data.decode("utf-8")
barcodeType = obj.type
string = "Data " + str(barcodeData) + " | Type " + str(barcodeType)
cv2.putText(frame, string, (x,y), cv2.FONT_HERSHEY_SIMPLEX,0.8,(25,20,300), 2)
if barcodeData not in used_codes:
print("Barcode: "+barcodeData)
used_codes.append(barcodeData)
time.sleep(3)
else:
print('This code was aqlready used')
time.sleep(3)
cap = cv2.VideoCapture(0,cv2.CAP_DSHOW)
used_codes = []
img_counter = 0
while True:
ret, frame = cap.read()
decoder(frame)
cv2.imshow('Image', frame)
code = cv2.waitKey(10)
if code == ord('q'):
break
elif code == ord(' '):
# SPACE pressed
img_name = "opencv_frame_{}.png".format(img_counter)
cv2.imwrite(img_name, frame)
print("{} written!".format(img_name))
img_counter += 1
读取和稍后保存结果的简单面板:
# importing libraries
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
import cv2
import numpy as np
from pyzbar.pyzbar import decode
import time
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Python ")
self.setGeometry(100, 100, 1000, 600)
# calling method
self.UiComponents()
# showing all the widgets
self.show()
# method for widgets
def UiComponents(self):
# creating a radio button
self.radio_button_fail = QRadioButton(self)
self.radio_button_pass = QRadioButton(self)
self.text_batch = QTextEdit(self)
self.text_sn = QTextEdit(self)
self.text_problem = QTextEdit(self)
self.label1 = QLabel(self)
self.label2 = QLabel(self)
self.label3 = QLabel(self)
self.label4 = QLabel(self)
self.button_save = QPushButton(self)
self.radio_button_fail.setGeometry(450, 170, 82, 17)
self.radio_button_pass.setGeometry(450, 140, 82, 17)
self.text_batch.setGeometry(80, 140, 101, 41)
self.text_sn.setGeometry(250, 140, 131, 41)
self.text_problem.setGeometry(550, 140, 200, 41)
self.label1.setGeometry(110, 100, 101, 41)
self.label2.setGeometry(260, 100, 130, 41)
self.label3.setGeometry(450, 100, 101, 41)
self.label4.setGeometry(570, 100, 170, 41)
self.button_save.setGeometry(780, 140, 70, 41)
self.label1.setText("Batch")
self.label2.setText("Serial number")
self.label3.setText("Status")
self.label4.setText("Problem description")
self.button_save.setText("Save")
font = QtGui.QFont()
font.setPointSize(14)
self.label1.setFont(font)
self.label2.setFont(font)
self.label3.setFont(font)
self.label4.setFont(font)
self.radio_button_fail.setText("FAIL")
self.radio_button_pass.setText("PASS")
self.radio_button_pass.setChecked(True)
self.text_problem.setDisabled(True)
# setting callable method to radio button
self.radio_button_fail.clicked.connect(self.check)
self.radio_button_pass.clicked.connect(self.check)
# method called by radio button
def check(self):
# checking if it is checked
if self.radio_button_fail.isChecked():
# changing text of label
self.text_problem.setDisabled(False)
else:
# changing text of label
self.text_problem.setDisabled(True)
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app
sys.exit(App.exec())
1条答案
按热度按时间uelo1irk1#
一种方法是在你的主窗口上创建隐藏标签来保存这些值。然后只需要
self.mylabel.setText("myvalue")
。但是窗口必须已经被初始化/打开。