Dynamsoft教程:如何在Raspberry Pi上使用PySide2构建GUI条形码阅读器

如果要使用Python和Qt构建跨平台的GUI应用程序,则可以使用PyQt或PySide。它们都是Python的Qt绑定。

Dynamsoft Barcode Reader SDK一款多功能的条码读取控件,只需要几行代码就可以将条码读取功能嵌入到Web或桌面应用程序。这可以节省数月的开发时间和成本。能支持多种图像文件格式以及从摄像机或扫描仪获取的DIB格式。使用Dynamsoft Barcode Reader SDK,你可以创建强大且实用的条形码扫描仪软件,以满足你的业务需求。

Dynamsoft Barcode Reader最新版

如果要使用Python和Qt构建跨平台的GUI应用程序,则可以使用PyQt或PySide。它们都是Python的Qt绑定。主要区别在于许可证:PyQt5是根据GPL还是商业PySide2发布,而根据则发布LGPL。由于正式建议使用PySide2,因此我将使用PySid2和Dynamsoft Python条形码SDK在Raspberry Pi上创建GUI条形码读取器应用程序。

要求

  • OpenCV Python
python3 -m pip install opencv-python
  • Dynamsoft Python条形码SDK
python3 -m pip install dbr
  • PySide2
sudo apt-get install python3-pyside2.qt3dcore python3-pyside2.qt3dinput python3-pyside2.qt3dlogic python3-pyside2.qt3drender python3-pyside2.qtcharts python3-pyside2.qtconcurrent python3-pyside2.qtcore python3-pyside2.qtgui python3-pyside2.qthelp python3-pyside2.qtlocation python3-pyside2.qtmultimedia python3-pyside2.qtmultimediawidgets python3-pyside2.qtnetwork python3-pyside2.qtopengl python3-pyside2.qtpositioning python3-pyside2.qtprintsupport python3-pyside2.qtqml python3-pyside2.qtquick python3-pyside2.qtquickwidgets python3-pyside2.qtscript python3-pyside2.qtscripttools python3-pyside2.qtsensors python3-pyside2.qtsql python3-pyside2.qtsvg python3-pyside2.qttest python3-pyside2.qttexttospeech python3-pyside2.qtuitools python3-pyside2.qtwebchannel python3-pyside2.qtwebsockets python3-pyside2.qtwidgets python3-pyside2.qtx11extras python3-pyside2.qtxml python3-pyside2.qtxmlpatterns python3-pyside2uic

Raspberry Pi OS的GUI条形码阅读器

让我们开始使用UI小部件:

  • 用于加载图像文件的按钮:
self.btn = QPushButton("Load an image")self.btn.clicked.connect(self.pickFile)def pickFile(self):    filename = QFileDialog.getOpenFileName(self, 'Open file',                                            self._path, "Barcode images (*)")    if filename is None or filename[0] == '':        self.showMessageBox("No file selected")        return
  • 用于打开实时摄像机流的按钮:
btnCamera = QPushButton("Open camera")btnCamera.clicked.connect(self.openCamera)self.timer = QTimer()self.timer.timeout.connect(self.nextFrameUpdate)def openCamera(self):    if not self._cap.isOpened():        self.showMessageBox("Failed to open camera.")        return    self.timer.start(1000./24)
  • 用于停止摄像机流的按钮:
btnCamera = QPushButton("Stop camera")btnCamera.clicked.connect(self.stopCamera)def stopCamera(self):    self.timer.stop()
  • 用于显示摄像机框架的标签和用于显示条形码解码结果的文本区域:
self.label = QLabel()self.label.setFixedSize(self.WINDOW_WIDTH - 30, self.WINDOW_HEIGHT - 160)self.results = QTextEdit()def showResults(self, frame, results):    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)    image = QImage(frame, frame.shape[1], frame.shape[0], frame.strides[0], QImage.Format_RGB888)    pixmap = QPixmap.fromImage(image)    pixmap = self.resizeImage(pixmap)    self.label.setPixmap(pixmap)    self.results.setText(results)

我们使用OpenCV读取图像文件并捕获 络摄像头帧:

frame = cv2.imread(filename)self._cap = cv2.VideoCapture(0)self._cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)self._cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)ret, frame = self._cap.read()

下一步很重要。我们需要考虑如何集成条形码解码API。

我们立即想到的最简单的方法是,一旦获得帧,便调用解码方法:

def nextFrameSlot(self):      ret, frame = self._cap.read()      if not ret:          self.showMessageBox('Failed to get camera frame!')          return      frame, results = self._manager.decode_frame(frame)      self.showResults(frame, results)

但是,我们无法在Raspberry Pi上执行此操作!条形码扫描是一项占用大量CPU的任务,如果花费太多时间,它将阻塞UI线程。因此,我们必须在不同的线程中分别执行UI代码和条形码识别代码。由于Python GIL的性能限制,Python线程也不可行。那QThread呢 QThread与Python线程的实现方式相同。为了验证QThread的性能,我们可以使用以下代码进行测试:

class WorkerSignals(QObject):    result = Signal(object)class BarcodeManager(QThread):    def __init__(self, license):        super(BarcodeManager, self).__init__()        self.signals = WorkerSignals()        self._reader = BarcodeReader()        self._reader.init_license(license)        settings = self._reader.get_runtime_settings()        settings.max_algorithm_thread_count = 1        self._reader.update_runtime_settings(settings)        self.frameQueue = Queue(1)    def register_callback(self, fn):        self.signals.result.connect(fn)    def run(self):        while True:            try:                frame = self.frameQueue.get(False, 10)                if type(frame) is str:                    break            except:                time.sleep(0.01)                continue            try:                results = self._reader.decode_buffer(frame)                self.signals.result.emit(results)            except BarcodeReaderError as error:                print(error)

事实证明,性能没有任何提高。因此,最可行的方法是在另一种方法中执行条形码解码任务Python Process:

from multiprocessing import Process, Queueimport timeimport numpy as npdef process_barcode_frame(license, frameQueue, resultQueue):    # Create Dynamsoft Barcode Reader    reader = BarcodeReader()    # Apply for a trial license: https://www.dynamsoft.com/customer/license/trialLicense    reader.init_license(license)    settings = reader.get_runtime_settings()    settings.max_algorithm_thread_count = 1    reader.update_runtime_settings(settings)    while True:        results = None        try:            frame = frameQueue.get(False, 10)            if type(frame) is str:                break        except:            time.sleep(0.01)            continue        try:            frameHeight, frameWidth, channel = frame.shape[:3]            results = reader.decode_buffer_manually(np.array(frame).tobytes(), frameWidth, frameHeight, frame.strides[0], EnumImagePixelFormat.IPF_RGB_888)        except BarcodeReaderError as error:            print(error)        try:            resultQueue.put(results, False, 10)        except:            passdef create_decoding_process(license):        size = 1        frameQueue = Queue(size)        resultQueue = Queue(size)        barcodeScanning = Process(target=process_barcode_frame, args=(license, frameQueue, resultQueue))        barcodeScanning.start()        return frameQueue, resultQueue, barcodeScanning

我们创建两个队列作为两个进程之间的数据隧道。

到目前为止,该应用程序已完成。另一件事是申请免费试用许可证并将其保存到本地磁盘。

将USB 络摄像头连接到Raspberry Pi,然后运行该应用程序:

python3 app.py license.txt

Raspberry Pi GUInbsp;èˉGUI条码阅读器应用程序是跨平台的。它也可以工作Windows,Linux和macOS。

想要购买Dynamsoft Barcode Reader正版授权,或了解更多产品信息请点击【咨询在线客服】

Dynamsoft教程:如何在Raspberry Pi上使用PySide2构建GUI条形码阅读器

标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2020年11月26日
下一篇 2020年11月26日

相关推荐

发表回复

登录后才能评论